From 212c6be41aff0c17e5988434130f5dc7e53e2d2c Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Thu, 22 Aug 2024 11:44:01 +0200 Subject: [PATCH 1/3] fix: expose measure usage from in event payloads --- internal/entitlement/entitlement.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/entitlement/entitlement.go b/internal/entitlement/entitlement.go index 72977e414..8c4fed330 100644 --- a/internal/entitlement/entitlement.go +++ b/internal/entitlement/entitlement.go @@ -96,7 +96,7 @@ type Entitlement struct { // All none-core fields are optional // metered - MeasureUsageFrom *time.Time `json:"-"` + MeasureUsageFrom *time.Time `json:"measureUsageFrom,omitempty"` IssueAfterReset *float64 `json:"issueAfterReset,omitempty"` IssueAfterResetPriority *uint8 `json:"issueAfterResetPriority,omitempty"` IsSoftLimit *bool `json:"isSoftLimit,omitempty"` From 68d295dc135bf30fd21484234ac0b34f21004ffd Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Thu, 22 Aug 2024 11:44:47 +0200 Subject: [PATCH 2/3] feat: expose handler for testing purposes --- internal/notification/consumer/consumer.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/notification/consumer/consumer.go b/internal/notification/consumer/consumer.go index cd27dff51..5006fa123 100644 --- a/internal/notification/consumer/consumer.go +++ b/internal/notification/consumer/consumer.go @@ -64,10 +64,11 @@ func New(opts Options) (*Consumer, error) { ), ) - return &Consumer{ - opts: opts, - router: router, - }, nil + return consumer, nil +} + +func (c *Consumer) Handle(ctx context.Context, event snapshot.SnapshotEvent) error { + return c.balanceThresholdHandler.Handle(ctx, event) } func (c *Consumer) Run(ctx context.Context) error { From 3317bf57a912f426ce865e42c04b1903dd98b529 Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Thu, 22 Aug 2024 11:46:14 +0200 Subject: [PATCH 3/3] fix: use the payloads namespace info As the entitlement's namespace is not part of the event. --- internal/notification/consumer/balancetreshold.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/notification/consumer/balancetreshold.go b/internal/notification/consumer/balancetreshold.go index 34cd036e9..7aed24e66 100644 --- a/internal/notification/consumer/balancetreshold.go +++ b/internal/notification/consumer/balancetreshold.go @@ -44,7 +44,7 @@ func (b *BalanceThresholdEventHandler) Handle(ctx context.Context, event snapsho // TODO[issue-1364]: this must be cached to prevent going to the DB for each balance.snapshot event affectedRulesPaged, err := b.Notification.ListRules(ctx, notification.ListRulesInput{ - Namespaces: []string{event.Entitlement.Namespace}, + Namespaces: []string{event.Namespace.ID}, Types: []notification.RuleType{notification.RuleTypeBalanceThreshold}, }) if err != nil { @@ -101,7 +101,7 @@ func (b *BalanceThresholdEventHandler) handleRule(ctx context.Context, balSnapsh PageSize: 1, PageNumber: 1, }, - Namespaces: []string{balSnapshot.Entitlement.Namespace}, + Namespaces: []string{balSnapshot.Namespace.ID}, From: balSnapshot.Entitlement.CurrentUsagePeriod.From, To: balSnapshot.Entitlement.CurrentUsagePeriod.To, @@ -185,7 +185,7 @@ func (b *BalanceThresholdEventHandler) createEvent(ctx context.Context, in creat _, err = b.Notification.CreateEvent(ctx, notification.CreateEventInput{ NamespacedModel: models.NamespacedModel{ - Namespace: in.Snapshot.Entitlement.Namespace, + Namespace: in.Snapshot.Namespace.ID, }, Annotations: annotations, Type: notification.EventTypeBalanceThreshold,