Skip to content

Commit

Permalink
feat: return history from last reset (#1079)
Browse files Browse the repository at this point in the history
* feat: return history from last reset

* fix: regenerate clients

* feat: add tests for auto from

* feat: move defaults handling to connector
  • Loading branch information
turip authored Jun 27, 2024
1 parent 2e2787c commit eaa76d6
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 81 deletions.
38 changes: 16 additions & 22 deletions api/api.gen.go

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

47 changes: 26 additions & 21 deletions api/client/go/client.gen.go

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

5 changes: 3 additions & 2 deletions api/client/node/schemas/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1989,10 +1989,11 @@ export interface operations {
parameters: {
query: {
/**
* @description Start of time range to query entitlement: date-time in RFC 3339 format.
* @description Start of time range to query entitlement: date-time in RFC 3339 format. Defaults to
* the last reset.
* Gets truncated to the granularity of the underlying meter.
*/
from: string
from?: string
/**
* @description End of time range to query entitlement: date-time in RFC 3339 format. Defaults to now.
* If not now then gets truncated to the granularity of the underlying meter.
Expand Down
18 changes: 10 additions & 8 deletions api/client/python/src/openmeter/_operations/_operations.py

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

13 changes: 7 additions & 6 deletions api/client/python/src/openmeter/aio/_operations/_operations.py

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

5 changes: 3 additions & 2 deletions api/client/web/src/client/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1989,10 +1989,11 @@ export interface operations {
parameters: {
query: {
/**
* @description Start of time range to query entitlement: date-time in RFC 3339 format.
* @description Start of time range to query entitlement: date-time in RFC 3339 format. Defaults to
* the last reset.
* Gets truncated to the granularity of the underlying meter.
*/
from: string
from?: string
/**
* @description End of time range to query entitlement: date-time in RFC 3339 format. Defaults to now.
* If not now then gets truncated to the granularity of the underlying meter.
Expand Down
5 changes: 3 additions & 2 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1067,9 +1067,10 @@ paths:
- $ref: "#/components/parameters/entitlementId"
- name: from
in: query
required: true
required: false
description: |
Start of time range to query entitlement: date-time in RFC 3339 format.
Start of time range to query entitlement: date-time in RFC 3339 format. Defaults to
the last reset.
Gets truncated to the granularity of the underlying meter.
schema:
type: string
Expand Down
2 changes: 1 addition & 1 deletion internal/entitlement/httpdriver/metered.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (h *meteredEntitlementHandler) GetEntitlementBalanceHistory() GetEntitlemen
},
params: meteredentitlement.BalanceHistoryParams{
From: params.Params.From,
To: defaultx.WithDefault(params.Params.To, time.Now()),
To: params.Params.To,
WindowSize: meteredentitlement.WindowSize(params.Params.WindowSize),
WindowTimeZone: *tLocation,
},
Expand Down
35 changes: 20 additions & 15 deletions internal/entitlement/metered/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const (
)

type BalanceHistoryParams struct {
From time.Time
To time.Time
From *time.Time
To *time.Time
WindowSize WindowSize
WindowTimeZone time.Location
}
Expand Down Expand Up @@ -95,26 +95,31 @@ func (e *connector) GetEntitlementBalanceHistory(ctx context.Context, entitlemen
// TODO: we should guard against abuse, getting history is expensive

// validate that we're working with a metered entitlement
ent, err := e.entitlementRepo.GetEntitlement(ctx, entitlementID)
entRepoEntity, err := e.entitlementRepo.GetEntitlement(ctx, entitlementID)
if err != nil {
return nil, credit.GrantBurnDownHistory{}, err
}
_, err = ParseFromGenericEntitlement(ent)
if err != nil {
return nil, credit.GrantBurnDownHistory{}, err

if entRepoEntity == nil {
return nil, credit.GrantBurnDownHistory{}, &entitlement.NotFoundError{EntitlementID: entitlementID}
}

// query period cannot be before start of measuring usage
start, err := e.ownerConnector.GetStartOfMeasurement(ctx, credit.NamespacedGrantOwner{
Namespace: entitlementID.Namespace,
ID: credit.GrantOwner(entitlementID.ID),
})
ent, err := ParseFromGenericEntitlement(entRepoEntity)
if err != nil {
return nil, credit.GrantBurnDownHistory{}, err
}

if params.From.Before(start) {
return nil, credit.GrantBurnDownHistory{}, &models.GenericUserError{Message: fmt.Sprintf("from cannot be before %s", start.UTC().Format(time.RFC3339))}
if params.From == nil {
params.From = &ent.LastReset
}

if params.To == nil {
params.To = convert.ToPointer(time.Now())
}

// query period cannot be before start of measuring usage
if params.From.Before(ent.MeasureUsageFrom) {
return nil, credit.GrantBurnDownHistory{}, &models.GenericUserError{Message: fmt.Sprintf("from cannot be before %s", ent.MeasureUsageFrom.UTC().Format(time.RFC3339))}
}

owner := credit.NamespacedGrantOwner{
Expand All @@ -135,8 +140,8 @@ func (e *connector) GetEntitlementBalanceHistory(ctx context.Context, entitlemen
if err != nil {
return nil, credit.GrantBurnDownHistory{}, fmt.Errorf("failed to get owner query params: %w", err)
}
meterParams.From = &params.From
meterParams.To = &params.To
meterParams.From = params.From
meterParams.To = params.To
meterParams.WindowSize = convert.ToPointer(models.WindowSize(params.WindowSize))
meterParams.WindowTimeZone = &params.WindowTimeZone

Expand Down
Loading

0 comments on commit eaa76d6

Please sign in to comment.