Skip to content

Commit

Permalink
Support snapshot pagination (#547)
Browse files Browse the repository at this point in the history
* Support snapshot pagination

* Update yaml
  • Loading branch information
jamessalvatore authored Oct 5, 2023
1 parent ec08f1c commit 1153f03
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,16 @@ paths:
type: integer
format: int64
minimum: 1
- in: query
name: limit
type: integer
format: int64
description: the number of snapshots to return
- in: query
name: offset
type: integer
format: int64
description: return snapshots given the offset, it should usually set together with limit
responses:
'200':
description: returns the flag snapshots
Expand Down Expand Up @@ -1063,6 +1073,7 @@ definitions:
type: integer
format: int64
minimum: 1
readOnly: true
updatedBy:
type: string
flag:
Expand Down
14 changes: 11 additions & 3 deletions pkg/handler/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,20 @@ func (c *crud) GetFlag(params flag.GetFlagParams) middleware.Responder {
}

func (c *crud) GetFlagSnapshots(params flag.GetFlagSnapshotsParams) middleware.Responder {
tx := getDB()
fs := []entity.FlagSnapshot{}
err := getDB().

if params.Limit != nil {
tx = tx.Limit(int(*params.Limit))
}
if params.Offset != nil {
tx = tx.Offset(int(*params.Offset))
}

if err := tx.
Order("created_at desc").
Where(entity.FlagSnapshot{FlagID: util.SafeUint(params.FlagID)}).
Find(&fs).Error
if err != nil {
Find(&fs).Error; err != nil {
return flag.NewGetFlagSnapshotsDefault(500).WithPayload(
ErrorMessage("cannot find flag snapshots for %v. %s", params.FlagID, err))
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/handler/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,53 @@ func TestFindFlags(t *testing.T) {
})
}

func TestGetFlagSnapshots(t *testing.T) {
var res middleware.Responder
db := entity.NewTestDB()
c := &crud{}
numOfSnapshots := 10

tmpDB, dbErr := db.DB()
if dbErr != nil {
t.Errorf("Failed to get database")
}

defer tmpDB.Close()
defer gostub.StubFunc(&getDB, db).Reset()

c.CreateFlag(flag.CreateFlagParams{
Body: &models.CreateFlagRequest{
Description: util.StringPtr("funny flag"),
},
})

for i := 0; i < numOfSnapshots; i++ {
entity.SaveFlagSnapshot(db, 1, "[email protected]")
}

t.Run("GetFlagSnapshots - got all the results", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, numOfSnapshots+1)
})

t.Run("GetFlagSnapshots (with limit)", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{
Limit: util.Int64Ptr(int64(2)),
})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, 2)
})

t.Run("GetFlagSnapshots (with limit and offset)", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{
Limit: util.Int64Ptr(int64(2)),
Offset: util.Int64Ptr(int64(2)),
})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, 2)
assert.Equal(t, int64(9), res.(*flag.GetFlagSnapshotsOK).Payload[0].ID)
assert.Equal(t, int64(8), res.(*flag.GetFlagSnapshotsOK).Payload[1].ID)
})
}

func TestCrudSegments(t *testing.T) {
var res middleware.Responder
db := entity.NewTestDB()
Expand Down
2 changes: 1 addition & 1 deletion pkg/mapper/entity_restapi/e2r/e2r.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func MapFlagSnapshot(e *entity.FlagSnapshot) (*models.FlagSnapshot, error) {
}
r := &models.FlagSnapshot{
Flag: f,
ID: util.Int64Ptr(int64(e.ID)),
ID: int64(e.ID),
UpdatedBy: e.UpdatedBy,
UpdatedAt: util.StringPtr(e.UpdatedAt.UTC().Format(time.RFC3339)),
}
Expand Down
10 changes: 10 additions & 0 deletions swagger/flag_snapshots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ get:
type: integer
format: int64
minimum: 1
- in: query
name: limit
type: integer
format: int64
description: the number of snapshots to return
- in: query
name: offset
type: integer
format: int64
description: return snapshots given the offset, it should usually set together with limit
responses:
200:
description: returns the flag snapshots
Expand Down
1 change: 1 addition & 0 deletions swagger/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ definitions:
type: integer
format: int64
minimum: 1
readOnly: true
updatedBy:
type: string
flag:
Expand Down
20 changes: 17 additions & 3 deletions swagger_gen/models/flag_snapshot.go

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

34 changes: 32 additions & 2 deletions swagger_gen/restapi/embedded_spec.go

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.

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

0 comments on commit 1153f03

Please sign in to comment.