Skip to content

Commit

Permalink
Merge pull request #1441 from openmeterio/refactor/allow-recalculator…
Browse files Browse the repository at this point in the history
…-reuse

refactor: allow recalculator reuse
  • Loading branch information
turip authored Aug 28, 2024
2 parents 77c44ab + 3b77ab8 commit 985dc4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions cmd/jobs/entitlement/recalculatesnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ func NewRecalculateBalanceSnapshotsCommand() *cobra.Command {

recalculator, err := balanceworker.NewRecalculator(balanceworker.RecalculatorOptions{
Entitlement: entitlementConnectors.Registry,
Namespace: "default",
EventBus: entitlementConnectors.EventBus,
MetricMeter: metricMeter,
})
if err != nil {
return err
}

return recalculator.Recalculate(cmd.Context())
return recalculator.Recalculate(cmd.Context(), "default")
},
}
}
17 changes: 8 additions & 9 deletions openmeter/entitlement/balanceworker/recalculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var (

type RecalculatorOptions struct {
Entitlement *registry.Entitlement
Namespace string
SubjectResolver SubjectResolver
EventBus eventbus.Publisher
MetricMeter metric.Meter
Expand All @@ -54,10 +53,6 @@ func (o RecalculatorOptions) Validate() error {
return errors.New("missing entitlement registry")
}

if o.Namespace == "" {
return errors.New("missing namespace")
}

if o.EventBus == nil {
return errors.New("missing event bus")
}
Expand Down Expand Up @@ -110,11 +105,15 @@ func NewRecalculator(opts RecalculatorOptions) (*Recalculator, error) {
}, nil
}

func (r *Recalculator) Recalculate(ctx context.Context) error {
func (r *Recalculator) Recalculate(ctx context.Context, ns string) error {
if ns == "" {
return errors.New("namespace is required")
}

affectedEntitlements, err := r.opts.Entitlement.EntitlementRepo.ListEntitlements(
ctx,
entitlement.ListEntitlementsParams{
Namespaces: []string{r.opts.Namespace},
Namespaces: []string{ns},
IncludeDeleted: true,
IncludeDeletedAfter: time.Now().Add(-DefaultIncludeDeletedDuration),
})
Expand Down Expand Up @@ -154,7 +153,7 @@ func (r *Recalculator) processEntitlements(ctx context.Context, entitlements []e
}

func (r *Recalculator) sendEntitlementDeletedEvent(ctx context.Context, ent entitlement.Entitlement) error {
subject, err := r.getSubjectByKey(ctx, r.opts.Namespace, ent.SubjectKey)
subject, err := r.getSubjectByKey(ctx, ent.Namespace, ent.SubjectKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -185,7 +184,7 @@ func (r *Recalculator) sendEntitlementDeletedEvent(ctx context.Context, ent enti
}

func (r *Recalculator) sendEntitlementUpdatedEvent(ctx context.Context, ent entitlement.Entitlement) error {
subject, err := r.getSubjectByKey(ctx, r.opts.Namespace, ent.SubjectKey)
subject, err := r.getSubjectByKey(ctx, ent.Namespace, ent.SubjectKey)
if err != nil {
return err
}
Expand Down

0 comments on commit 985dc4a

Please sign in to comment.