Skip to content

Commit

Permalink
Merge pull request #1080 from openmeterio/feat/automatic-grant-issuing
Browse files Browse the repository at this point in the history
Feat/automatic grant issuing
  • Loading branch information
turip authored Jun 27, 2024
2 parents 3df746b + cd8e3c3 commit 2e2787c
Show file tree
Hide file tree
Showing 24 changed files with 713 additions and 435 deletions.
366 changes: 186 additions & 180 deletions api/api.gen.go

Large diffs are not rendered by default.

366 changes: 186 additions & 180 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1837,13 +1837,20 @@ components:
- $ref: "#/components/schemas/ExpirationPeriod"
maxRolloverAmount:
description: |
The maximum amount of the grant that can be rolled over. Defaults to 0.
Grants are rolled over at reset, after which they can have a different balance compared to what they had before the reset.
- maxAmount = {original_amount} -> rollover original amount
- maxAmount = 0 -> no rollover
- maxAmount = 90 -> rollover 90 max
Balance after the reset is calculated as:
Balance_After_Reset = MIN(MaxRolloverAmount, MAX(Balance_Before_Reset, MinRolloverAmount))
type: number
format: double
default: 0
example: 100
minRolloverAmount:
description: |
Grants are rolled over at reset, after which they can have a different balance compared to what they had before the reset.
If it's larger than 0 then the grant's balance will be the MAX(maxRollover, balance) + amount.
Balance after the reset is calculated as:
Balance_After_Reset = MIN(MaxRolloverAmount, MAX(Balance_Before_Reset, MinRolloverAmount))
type: number
format: double
default: 0
Expand Down
3 changes: 3 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ func TestCredit(t *testing.T) {

priority := 1
maxRolloverAmount := 100.0
minRolloverAmount := 0.0

// Create grant
resp, err := client.CreateGrantWithResponse(context.Background(), subject, *entitlementId, api.EntitlementGrantCreateInput{
Expand All @@ -571,6 +572,7 @@ func TestCredit(t *testing.T) {
},
Priority: &priority,
MaxRolloverAmount: &maxRolloverAmount,
MinRolloverAmount: &minRolloverAmount,
Recurrence: &api.RecurringPeriodCreateInput{
Anchor: convert.ToPointer(time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)),
Interval: "YEAR",
Expand All @@ -591,6 +593,7 @@ func TestCredit(t *testing.T) {
Priority: &priority,
EffectiveAt: effectiveAt.UTC(),
MaxRolloverAmount: &maxRolloverAmount,
MinRolloverAmount: &minRolloverAmount,
Expiration: api.ExpirationPeriod{
Duration: "MONTH",
Count: 1,
Expand Down
14 changes: 12 additions & 2 deletions internal/credit/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (n NamespacedGrantOwner) NamespacedID() models.NamespacedID {
}
}

const (
GrantPriorityDefault uint8 = 1
)

// Grant is an immutable definition used to increase balance.
type Grant struct {
models.ManagedModel
Expand Down Expand Up @@ -57,8 +61,13 @@ type Grant struct {
VoidedAt *time.Time `json:"voidedAt,omitempty"`

// How much of the grant can be rolled over after a reset operation.
// Balance after a reset will be between ResetMinRollover and ResetMaxRollover.
ResetMaxRollover float64 `json:"resetMaxRollover"`

// How much balance the grant must have after a reset.
// Balance after a reset will be between ResetMinRollover and ResetMaxRollover.
ResetMinRollover float64 `json:"resetMinRollover"`

// Recurrence config for the grant. If nil the grant doesn't recur.
Recurrence *recurrence.Recurrence `json:"recurrence,omitempty"`
}
Expand Down Expand Up @@ -95,8 +104,9 @@ func (g Grant) RecurrenceBalance(currentBalance float64) float64 {

// Calculates the new balance after a rollover from the current balance
func (g Grant) RolloverBalance(currentBalance float64) float64 {
// At a rollover the maximum balance that can remain is the ResetMaxRollover
return math.Min(g.ResetMaxRollover, currentBalance)
// At a rollover the maximum balance that can remain is the ResetMaxRollover,
// while the minimum that has to be granted is ResetMinRollover.
return math.Min(g.ResetMaxRollover, math.Max(g.ResetMinRollover, currentBalance))
}

func (g Grant) GetNamespacedID() models.NamespacedID {
Expand Down
3 changes: 3 additions & 0 deletions internal/credit/grant_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CreateGrantInput struct {
Expiration ExpirationPeriod
Metadata map[string]string
ResetMaxRollover float64
ResetMinRollover float64
Recurrence *recurrence.Recurrence
}

Expand Down Expand Up @@ -57,6 +58,7 @@ type GrantRepoCreateGrantInput struct {
ExpiresAt time.Time
Metadata map[string]string
ResetMaxRollover float64
ResetMinRollover float64
Recurrence *recurrence.Recurrence
}

Expand Down Expand Up @@ -133,6 +135,7 @@ func (m *grantConnector) CreateGrant(ctx context.Context, owner NamespacedGrantO
ExpiresAt: input.Expiration.GetExpiration(input.EffectiveAt),
Metadata: input.Metadata,
ResetMaxRollover: input.ResetMaxRollover,
ResetMinRollover: input.ResetMinRollover,
Recurrence: input.Recurrence,
})

Expand Down
13 changes: 12 additions & 1 deletion internal/credit/postgresadapter/ent/db/grant.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/credit/postgresadapter/ent/db/grant/grant.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/credit/postgresadapter/ent/db/grant/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/credit/postgresadapter/ent/db/grant_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/credit/postgresadapter/ent/db/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e2787c

Please sign in to comment.