From f8fef0f4a7effffdfaf2778c336e3dfb5b5f3f87 Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Tue, 3 Sep 2024 17:49:44 +0200 Subject: [PATCH] feat: fetch expired entitlements for multiple namespaces This patch allows getting the expired entitlements for all/multiple namespaces. The added index helps with fetching expired entitlements for all namespaces. --- openmeter/ent/db/migrate/schema.go | 5 +++++ openmeter/ent/schema/entitlement.go | 2 ++ openmeter/entitlement/adapter/entitlement.go | 14 +++++++++----- openmeter/entitlement/metered/reset.go | 2 +- openmeter/entitlement/repository.go | 2 +- ...240903155435_entitlement-expired-index.down.sql | 2 ++ ...20240903155435_entitlement-expired-index.up.sql | 2 ++ tools/migrate/migrations/atlas.sum | 4 +++- 8 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 tools/migrate/migrations/20240903155435_entitlement-expired-index.down.sql create mode 100644 tools/migrate/migrations/20240903155435_entitlement-expired-index.up.sql diff --git a/openmeter/ent/db/migrate/schema.go b/openmeter/ent/db/migrate/schema.go index ccd97ad56..59728a435 100644 --- a/openmeter/ent/db/migrate/schema.go +++ b/openmeter/ent/db/migrate/schema.go @@ -120,6 +120,11 @@ var ( Unique: false, Columns: []*schema.Column{EntitlementsColumns[1], EntitlementsColumns[18]}, }, + { + Name: "entitlement_current_usage_period_end_deleted_at", + Unique: false, + Columns: []*schema.Column{EntitlementsColumns[18], EntitlementsColumns[5]}, + }, }, } // FeaturesColumns holds the columns for the "features" table. diff --git a/openmeter/ent/schema/entitlement.go b/openmeter/ent/schema/entitlement.go index 3e980baa1..87b7d9dbe 100644 --- a/openmeter/ent/schema/entitlement.go +++ b/openmeter/ent/schema/entitlement.go @@ -55,6 +55,8 @@ func (Entitlement) Indexes() []ent.Index { index.Fields("namespace", "id", "subject_key"), index.Fields("namespace", "feature_id", "id"), index.Fields("namespace", "current_usage_period_end"), + // index for collecting all the entitlements with due resets + index.Fields("current_usage_period_end", "deleted_at"), } } diff --git a/openmeter/entitlement/adapter/entitlement.go b/openmeter/entitlement/adapter/entitlement.go index 78c019acc..f88ea852d 100644 --- a/openmeter/entitlement/adapter/entitlement.go +++ b/openmeter/entitlement/adapter/entitlement.go @@ -409,15 +409,19 @@ func (a *entitlementDBAdapter) UpdateEntitlementUsagePeriod(ctx context.Context, return err } -func (a *entitlementDBAdapter) ListEntitlementsWithExpiredUsagePeriod(ctx context.Context, namespace string, expiredBefore time.Time) ([]entitlement.Entitlement, error) { - res, err := withLatestUsageReset(a.db.Entitlement.Query(), []string{namespace}). +func (a *entitlementDBAdapter) ListEntitlementsWithExpiredUsagePeriod(ctx context.Context, namespaces []string, expiredBefore time.Time) ([]entitlement.Entitlement, error) { + query := withLatestUsageReset(a.db.Entitlement.Query(), namespaces). Where( - db_entitlement.Namespace(namespace), db_entitlement.CurrentUsagePeriodEndNotNil(), db_entitlement.CurrentUsagePeriodEndLTE(expiredBefore), db_entitlement.Or(db_entitlement.DeletedAtIsNil(), db_entitlement.DeletedAtGT(clock.Now())), - ). - All(ctx) + ) + + if len(namespaces) > 0 { + query = query.Where(db_entitlement.NamespaceIn(namespaces...)) + } + + res, err := query.All(ctx) if err != nil { return nil, err } diff --git a/openmeter/entitlement/metered/reset.go b/openmeter/entitlement/metered/reset.go index d046b24bb..e77872125 100644 --- a/openmeter/entitlement/metered/reset.go +++ b/openmeter/entitlement/metered/reset.go @@ -73,7 +73,7 @@ func (e *connector) ResetEntitlementUsage(ctx context.Context, entitlementID mod } func (c *connector) ResetEntitlementsWithExpiredUsagePeriod(ctx context.Context, namespace string, highwatermark time.Time) ([]models.NamespacedID, error) { - entitlements, err := c.entitlementRepo.ListEntitlementsWithExpiredUsagePeriod(ctx, namespace, highwatermark) + entitlements, err := c.entitlementRepo.ListEntitlementsWithExpiredUsagePeriod(ctx, []string{namespace}, highwatermark) if err != nil { return nil, fmt.Errorf("failed to list entitlements with due reset: %w", err) } diff --git a/openmeter/entitlement/repository.go b/openmeter/entitlement/repository.go index 6954e7f83..6d13de519 100644 --- a/openmeter/entitlement/repository.go +++ b/openmeter/entitlement/repository.go @@ -32,7 +32,7 @@ type EntitlementRepo interface { LockEntitlementForTx(ctx context.Context, entitlementID models.NamespacedID) error UpdateEntitlementUsagePeriod(ctx context.Context, entitlementID models.NamespacedID, params UpdateEntitlementUsagePeriodParams) error - ListEntitlementsWithExpiredUsagePeriod(ctx context.Context, namespace string, highwatermark time.Time) ([]Entitlement, error) + ListEntitlementsWithExpiredUsagePeriod(ctx context.Context, namespaces []string, highwatermark time.Time) ([]Entitlement, error) entutils.TxCreator entutils.TxUser[EntitlementRepo] diff --git a/tools/migrate/migrations/20240903155435_entitlement-expired-index.down.sql b/tools/migrate/migrations/20240903155435_entitlement-expired-index.down.sql new file mode 100644 index 000000000..ff8f3a8c1 --- /dev/null +++ b/tools/migrate/migrations/20240903155435_entitlement-expired-index.down.sql @@ -0,0 +1,2 @@ +-- reverse: create index "entitlement_current_usage_period_end_deleted_at" to table: "entitlements" +DROP INDEX "entitlement_current_usage_period_end_deleted_at"; diff --git a/tools/migrate/migrations/20240903155435_entitlement-expired-index.up.sql b/tools/migrate/migrations/20240903155435_entitlement-expired-index.up.sql new file mode 100644 index 000000000..5b9ffde72 --- /dev/null +++ b/tools/migrate/migrations/20240903155435_entitlement-expired-index.up.sql @@ -0,0 +1,2 @@ +-- create index "entitlement_current_usage_period_end_deleted_at" to table: "entitlements" +CREATE INDEX "entitlement_current_usage_period_end_deleted_at" ON "entitlements" ("current_usage_period_end", "deleted_at"); diff --git a/tools/migrate/migrations/atlas.sum b/tools/migrate/migrations/atlas.sum index bc4494a7a..080a54221 100644 --- a/tools/migrate/migrations/atlas.sum +++ b/tools/migrate/migrations/atlas.sum @@ -1,3 +1,5 @@ -h1:EPap+DaNoXsLQC61DQvaBbCLBcYaGP1DHSglnzexChY= +h1:jmrq8Fzq+/Ds0gmHmWUNruJ3jzKFhVVBYSDlbtordSA= 20240826120919_init.down.sql h1:AIbgwwngjkJEYa3yRZsIXQyBa2+qoZttwMXHxXEbHLI= 20240826120919_init.up.sql h1:/hYHWF3Z3dab8SMKnw99ixVktCuJe2bAw5wstCZIEN8= +20240903155435_entitlement-expired-index.down.sql h1:np2xgYs3KQ2z7qPBcobtGNhqWQ3V8NwEP9E5U3TmpSA= +20240903155435_entitlement-expired-index.up.sql h1:HegNgovI3MQxQMd/072kOchfm5B37SXzODZIteOwX78=