Skip to content

Commit

Permalink
chore: change grant ordering default to be EffectiveAt descending OM-…
Browse files Browse the repository at this point in the history
…840 (#1253)

* chore: change grant ordering default to be EffectiveAt descending

* feat: add option to change order direction

* style: fix lint

* chore: use null value for sortx.Order
  • Loading branch information
GAlexIHU authored Jul 26, 2024
1 parent a8e5665 commit b262805
Show file tree
Hide file tree
Showing 12 changed files with 732 additions and 544 deletions.
569 changes: 300 additions & 269 deletions api/api.gen.go

Large diffs are not rendered by default.

577 changes: 308 additions & 269 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions api/client/node/schemas/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,12 @@ export interface components {
eventId: string
/** @description Include disabled entries. */
queryIncludeDisabled?: boolean
/**
* @description Order by field.
*
* Usage: `?order=ASC`
*/
order?: 'ASC' | 'DESC'
}
requestBodies: never
headers: never
Expand Down Expand Up @@ -2331,6 +2337,7 @@ export interface operations {
pageSize?: components['parameters']['queryPageSize']
limit?: components['parameters']['queryLimit']
offset?: components['parameters']['queryOffset']
order?: components['parameters']['order']
/** @description Order by field */
orderBy?: 'id' | 'createdAt' | 'updatedAt'
includeDeleted?: components['parameters']['includeDeleted']
Expand Down
9 changes: 9 additions & 0 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.

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

7 changes: 7 additions & 0 deletions api/client/web/src/client/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,12 @@ export interface components {
eventId: string
/** @description Include disabled entries. */
queryIncludeDisabled?: boolean
/**
* @description Order by field.
*
* Usage: `?order=ASC`
*/
order?: 'ASC' | 'DESC'
}
requestBodies: never
headers: never
Expand Down Expand Up @@ -2331,6 +2337,7 @@ export interface operations {
pageSize?: components['parameters']['queryPageSize']
limit?: components['parameters']['queryLimit']
offset?: components['parameters']['queryOffset']
order?: components['parameters']['order']
/** @description Order by field */
orderBy?: 'id' | 'createdAt' | 'updatedAt'
includeDeleted?: components['parameters']['includeDeleted']
Expand Down
15 changes: 15 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ paths:
- $ref: "#/components/parameters/queryPageSize"
- $ref: "#/components/parameters/queryLimit"
- $ref: "#/components/parameters/queryOffset"
- $ref: "#/components/parameters/order"
- name: orderBy
in: query
required: false
Expand Down Expand Up @@ -3478,6 +3479,20 @@ components:
type: boolean
default: false
example: true
order:
name: order
in: query
required: false
description: |
Order by field.
Usage: `?order=ASC`
schema:
type: string
enum:
- ASC
- DESC
default: ASC

responses:
ConflictProblemResponse:
Expand Down
2 changes: 2 additions & 0 deletions internal/credit/grant_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/recurrence"
"github.com/openmeterio/openmeter/pkg/sortx"
)

type CreateGrantInput struct {
Expand Down Expand Up @@ -47,6 +48,7 @@ type ListGrantsParams struct {
IncludeDeleted bool
Page pagination.Page
OrderBy GrantOrderBy
Order sortx.Order
// will be deprecated
Limit int
// will be deprecated
Expand Down
16 changes: 15 additions & 1 deletion internal/credit/httpdriver/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
entitlement_httpdriver "github.com/openmeterio/openmeter/internal/entitlement/httpdriver"
meteredentitlement "github.com/openmeterio/openmeter/internal/entitlement/metered"
"github.com/openmeterio/openmeter/internal/namespace/namespacedriver"
"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/sortx"
)

type GrantHandler interface {
Expand Down Expand Up @@ -69,7 +71,19 @@ func (h *grantHandler) ListGrants() ListGrantsHandler {
},
Limit: defaultx.WithDefault(params.Params.Limit, commonhttp.DefaultPageSize),
Offset: defaultx.WithDefault(params.Params.Offset, 0),
OrderBy: credit.GrantOrderBy(defaultx.WithDefault((*string)(params.Params.OrderBy), string(credit.GrantOrderByCreatedAt))),
OrderBy: credit.GrantOrderBy(defaultx.WithDefault((*string)(params.Params.OrderBy), string(credit.GrantOrderByEffectiveAt))),
Order: defaultx.WithDefault(
convert.SafeDeRef[api.ListGrantsParamsOrder, sortx.Order](
params.Params.Order,
func(o api.ListGrantsParamsOrder) *sortx.Order {
if o == api.ListGrantsParamsOrderASC {
return convert.ToPointer(sortx.OrderAsc)
}
return convert.ToPointer(sortx.OrderDesc)
},
),
sortx.OrderNone,
),
},
}, nil
},
Expand Down
17 changes: 12 additions & 5 deletions internal/credit/postgresadapter/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"time"

"entgo.io/ent/dialect/sql"

"github.com/openmeterio/openmeter/internal/credit"
"github.com/openmeterio/openmeter/internal/ent/db"
db_grant "github.com/openmeterio/openmeter/internal/ent/db/grant"
"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/framework/entutils"
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/recurrence"
Expand Down Expand Up @@ -74,17 +77,21 @@ func (g *grantDBADapter) ListGrants(ctx context.Context, params credit.ListGrant
}

if params.OrderBy != "" {
order := []sql.OrderTermOption{}
if !params.Order.IsDefaultValue() {
order = entutils.GetOrdering(params.Order)
}
switch params.OrderBy {
case credit.GrantOrderByCreatedAt:
query = query.Order(db_grant.ByCreatedAt())
query = query.Order(db_grant.ByCreatedAt(order...))
case credit.GrantOrderByUpdatedAt:
query = query.Order(db_grant.ByUpdatedAt())
query = query.Order(db_grant.ByUpdatedAt(order...))
case credit.GrantOrderByExpiresAt:
query = query.Order(db_grant.ByExpiresAt())
query = query.Order(db_grant.ByExpiresAt(order...))
case credit.GrantOrderByEffectiveAt:
query = query.Order(db_grant.ByEffectiveAt())
query = query.Order(db_grant.ByEffectiveAt(order...))
case credit.GrantOrderByOwner:
query = query.Order(db_grant.ByOwnerID())
query = query.Order(db_grant.ByOwnerID(order...))
}
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/framework/entutils/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package entutils

import (
"entgo.io/ent/dialect/sql"

"github.com/openmeterio/openmeter/pkg/sortx"
)

func GetOrdering(order sortx.Order) []sql.OrderTermOption {
type o = sql.OrderTermOption

switch order {
case sortx.OrderAsc:
return []o{sql.OrderAsc()}
case sortx.OrderDesc:
return []o{sql.OrderDesc()}
default:
return getStrOrdering(string(order))
}
}

func getStrOrdering(order string) []sql.OrderTermOption {
type o = sql.OrderTermOption

switch order {
case string(sortx.OrderAsc):
return []o{sql.OrderAsc()}
case string(sortx.OrderDesc):
return []o{sql.OrderDesc()}
default:
return []o{}
}
}
18 changes: 18 additions & 0 deletions pkg/sortx/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sortx

type Order string

const (
OrderAsc Order = "ASC"
OrderDesc Order = "DESC"
OrderDefault Order = "ASC"
OrderNone Order = ""
)

func (s Order) String() string {
return string(s)
}

func (s Order) IsDefaultValue() bool {
return s == ""
}

0 comments on commit b262805

Please sign in to comment.