From 1ca2e884f75031958ff939ab840af3910678c91f Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Mon, 25 Nov 2024 19:06:56 -0500 Subject: [PATCH] Add count to pagination for talkgroups --- pkg/database/mocks/Store.go | 113 ++++++++++++++++++++++++++++ pkg/database/querier.go | 2 + pkg/database/talkgroups.sql.go | 24 ++++++ pkg/rest/talkgroups.go | 9 ++- pkg/talkgroups/tgstore/store.go | 25 +++++- sql/postgres/queries/talkgroups.sql | 9 ++- 6 files changed, 176 insertions(+), 6 deletions(-) diff --git a/pkg/database/mocks/Store.go b/pkg/database/mocks/Store.go index 291e475..9630a74 100644 --- a/pkg/database/mocks/Store.go +++ b/pkg/database/mocks/Store.go @@ -1244,6 +1244,63 @@ func (_c *Store_GetTalkgroupsWithLearnedBySystem_Call) RunAndReturn(run func(con return _c } +// GetTalkgroupsWithLearnedBySystemCount provides a mock function with given fields: ctx, system +func (_m *Store) GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error) { + ret := _m.Called(ctx, system) + + if len(ret) == 0 { + panic("no return value specified for GetTalkgroupsWithLearnedBySystemCount") + } + + var r0 int64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, int32) (int64, error)); ok { + return rf(ctx, system) + } + if rf, ok := ret.Get(0).(func(context.Context, int32) int64); ok { + r0 = rf(ctx, system) + } else { + r0 = ret.Get(0).(int64) + } + + if rf, ok := ret.Get(1).(func(context.Context, int32) error); ok { + r1 = rf(ctx, system) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Store_GetTalkgroupsWithLearnedBySystemCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTalkgroupsWithLearnedBySystemCount' +type Store_GetTalkgroupsWithLearnedBySystemCount_Call struct { + *mock.Call +} + +// GetTalkgroupsWithLearnedBySystemCount is a helper method to define mock.On call +// - ctx context.Context +// - system int32 +func (_e *Store_Expecter) GetTalkgroupsWithLearnedBySystemCount(ctx interface{}, system interface{}) *Store_GetTalkgroupsWithLearnedBySystemCount_Call { + return &Store_GetTalkgroupsWithLearnedBySystemCount_Call{Call: _e.mock.On("GetTalkgroupsWithLearnedBySystemCount", ctx, system)} +} + +func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) Run(run func(ctx context.Context, system int32)) *Store_GetTalkgroupsWithLearnedBySystemCount_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(int32)) + }) + return _c +} + +func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) Return(_a0 int64, _a1 error) *Store_GetTalkgroupsWithLearnedBySystemCount_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Store_GetTalkgroupsWithLearnedBySystemCount_Call) RunAndReturn(run func(context.Context, int32) (int64, error)) *Store_GetTalkgroupsWithLearnedBySystemCount_Call { + _c.Call.Return(run) + return _c +} + // GetTalkgroupsWithLearnedBySystemP provides a mock function with given fields: ctx, system, offset, perPage func (_m *Store) GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]database.GetTalkgroupsWithLearnedBySystemPRow, error) { ret := _m.Called(ctx, system, offset, perPage) @@ -1365,6 +1422,62 @@ func (_c *Store_GetTalkgroupsWithLearnedP_Call) RunAndReturn(run func(context.Co return _c } +// GetTalkgroupsWithLearnedPCount provides a mock function with given fields: ctx +func (_m *Store) GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for GetTalkgroupsWithLearnedPCount") + } + + var r0 int64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (int64, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) int64); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(int64) + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Store_GetTalkgroupsWithLearnedPCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTalkgroupsWithLearnedPCount' +type Store_GetTalkgroupsWithLearnedPCount_Call struct { + *mock.Call +} + +// GetTalkgroupsWithLearnedPCount is a helper method to define mock.On call +// - ctx context.Context +func (_e *Store_Expecter) GetTalkgroupsWithLearnedPCount(ctx interface{}) *Store_GetTalkgroupsWithLearnedPCount_Call { + return &Store_GetTalkgroupsWithLearnedPCount_Call{Call: _e.mock.On("GetTalkgroupsWithLearnedPCount", ctx)} +} + +func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) Run(run func(ctx context.Context)) *Store_GetTalkgroupsWithLearnedPCount_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) Return(_a0 int64, _a1 error) *Store_GetTalkgroupsWithLearnedPCount_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Store_GetTalkgroupsWithLearnedPCount_Call) RunAndReturn(run func(context.Context) (int64, error)) *Store_GetTalkgroupsWithLearnedPCount_Call { + _c.Call.Return(run) + return _c +} + // GetUserByID provides a mock function with given fields: ctx, id func (_m *Store) GetUserByID(ctx context.Context, id int) (database.User, error) { ret := _m.Called(ctx, id) diff --git a/pkg/database/querier.go b/pkg/database/querier.go index b9ede53..167df43 100644 --- a/pkg/database/querier.go +++ b/pkg/database/querier.go @@ -30,8 +30,10 @@ type Querier interface { GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) GetTalkgroupsWithLearned(ctx context.Context) ([]GetTalkgroupsWithLearnedRow, error) GetTalkgroupsWithLearnedBySystem(ctx context.Context, system int32) ([]GetTalkgroupsWithLearnedBySystemRow, error) + GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error) GetTalkgroupsWithLearnedBySystemP(ctx context.Context, system int32, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedBySystemPRow, error) GetTalkgroupsWithLearnedP(ctx context.Context, offset int32, perPage int32) ([]GetTalkgroupsWithLearnedPRow, error) + GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error) GetUserByID(ctx context.Context, id int) (User, error) GetUserByUID(ctx context.Context, id int) (User, error) GetUserByUsername(ctx context.Context, username string) (User, error) diff --git a/pkg/database/talkgroups.sql.go b/pkg/database/talkgroups.sql.go index 19603d4..b5731d2 100644 --- a/pkg/database/talkgroups.sql.go +++ b/pkg/database/talkgroups.sql.go @@ -377,6 +377,18 @@ func (q *Queries) GetTalkgroupsWithLearnedBySystem(ctx context.Context, system i return items, nil } +const getTalkgroupsWithLearnedBySystemCount = `-- name: GetTalkgroupsWithLearnedBySystemCount :one +SELECT COUNT(*) FROM talkgroups tg +WHERE tg.system_id = $1 +` + +func (q *Queries) GetTalkgroupsWithLearnedBySystemCount(ctx context.Context, system int32) (int64, error) { + row := q.db.QueryRow(ctx, getTalkgroupsWithLearnedBySystemCount, system) + var count int64 + err := row.Scan(&count) + return count, err +} + const getTalkgroupsWithLearnedBySystemP = `-- name: GetTalkgroupsWithLearnedBySystemP :many SELECT tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, tg.learned, tg.ignored, sys.id, sys.name @@ -483,6 +495,18 @@ func (q *Queries) GetTalkgroupsWithLearnedP(ctx context.Context, offset int32, p return items, nil } +const getTalkgroupsWithLearnedPCount = `-- name: GetTalkgroupsWithLearnedPCount :one +SELECT COUNT(*) FROM talkgroups tg +WHERE ignored IS NOT TRUE +` + +func (q *Queries) GetTalkgroupsWithLearnedPCount(ctx context.Context) (int64, error) { + row := q.db.QueryRow(ctx, getTalkgroupsWithLearnedPCount) + var count int64 + err := row.Scan(&count) + return count, err +} + const restoreTalkgroupVersion = `-- name: RestoreTalkgroupVersion :one INSERT INTO talkgroups( system_id, diff --git a/pkg/rest/talkgroups.go b/pkg/rest/talkgroups.go index 21b443e..ef57595 100644 --- a/pkg/rest/talkgroups.go +++ b/pkg/rest/talkgroups.go @@ -112,13 +112,16 @@ func (tga *talkgroupAPI) postPaginated(w http.ResponseWriter, r *http.Request) { return } - var res interface{} + res := struct { + Talkgroups []*talkgroups.Talkgroup `json:"talkgroups"` + Count int `json:"count"` + }{} switch { case p.System != nil: - res, err = tgs.SystemTGs(ctx, int32(*p.System), tgstore.WithPagination(input, DefaultPerPage)) + res.Talkgroups, err = tgs.SystemTGs(ctx, int32(*p.System), tgstore.WithPagination(input, DefaultPerPage, &res.Count)) default: // get all talkgroups - res, err = tgs.TGs(ctx, nil, tgstore.WithPagination(input, DefaultPerPage)) + res.Talkgroups, err = tgs.TGs(ctx, nil, tgstore.WithPagination(input, DefaultPerPage, &res.Count)) } if err != nil { diff --git a/pkg/talkgroups/tgstore/store.go b/pkg/talkgroups/tgstore/store.go index 4a5837a..81ef67c 100644 --- a/pkg/talkgroups/tgstore/store.go +++ b/pkg/talkgroups/tgstore/store.go @@ -66,6 +66,7 @@ type Store interface { type options struct { pagination *Pagination + totalDest *int perPageDefault int } @@ -78,10 +79,11 @@ func sOpt(opts []option) (o options) { type option func(*options) -func WithPagination(p *Pagination, defPerPage int) option { +func WithPagination(p *Pagination, defPerPage int, totalDest *int) option { return func(o *options) { o.pagination = p o.perPageDefault = defPerPage + o.totalDest = totalDest } } @@ -284,11 +286,30 @@ func (t *cache) TGs(ctx context.Context, tgs tgsp.IDs, opts ...option) ([]*tgsp. if opt.pagination != nil { offset, perPage := opt.pagination.OffsetPerPage(opt.perPageDefault) - tgRecords, err := db.GetTalkgroupsWithLearnedP(ctx, offset, perPage) + var tgRecords []database.GetTalkgroupsWithLearnedPRow + var err error + err = db.InTx(ctx, func(db database.Store) error { + tgRecords, err = db.GetTalkgroupsWithLearnedP(ctx, offset, perPage) + if err != nil { + return err + } + + if opt.totalDest != nil { + count, err := db.GetTalkgroupsWithLearnedPCount(ctx) + if err != nil { + return err + } + + *opt.totalDest = int(count) + } + + return nil + }, pgx.TxOptions{}) if err != nil { return nil, err } + return addToRowListS(t, r, tgRecords), nil } diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql index a2be432..d3310e0 100644 --- a/sql/postgres/queries/talkgroups.sql +++ b/sql/postgres/queries/talkgroups.sql @@ -40,7 +40,10 @@ WHERE tg.system_id = @system ORDER BY tg.system_id ASC, tg.tgid ASC OFFSET sqlc.arg('offset') ROWS FETCH NEXT sqlc.arg('per_page') ROWS ONLY; -; + +-- name: GetTalkgroupsWithLearnedBySystemCount :one +SELECT COUNT(*) FROM talkgroups tg +WHERE tg.system_id = @system; -- name: GetTalkgroupsWithLearnedBySystem :many SELECT @@ -66,6 +69,10 @@ ORDER BY tg.system_id ASC, tg.tgid ASC OFFSET sqlc.arg('offset') ROWS FETCH NEXT sqlc.arg('per_page') ROWS ONLY; +-- name: GetTalkgroupsWithLearnedPCount :one +SELECT COUNT(*) FROM talkgroups tg +WHERE ignored IS NOT TRUE; + -- name: GetSystemName :one SELECT name FROM systems WHERE id = @system_id;