Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total_count field in meta for leaderboard endpoint #42

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ get:
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/pageNumber'
- $ref: '#/components/parameters/pageOrder'
- in: query
name: count
description: Count total number of users.
required: false
schema:
type: boolean
example: true
responses:
200:
description: Success
Expand All @@ -71,6 +78,15 @@ get:
type: array
items:
$ref: '#/components/schemas/Balance'
meta:
type: object
required:
- total_count
properties:
total_count:
type: integer
description: Appears when `count=true` is specified
example: 18
400:
$ref: '#/components/responses/invalidParameter'
409:
Expand Down
2 changes: 2 additions & 0 deletions internal/data/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type BalancesQ interface {
GetWithRank(nullifier string) (*Balance, error)
SelectWithRank() ([]Balance, error)

Count() (int64, error)

// WithoutPassportEvent returns balances which already
// have scanned passport, but there no claimed events
// for this. Filters are not applied.
Expand Down
15 changes: 15 additions & 0 deletions internal/data/pg/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type balances struct {
selector squirrel.SelectBuilder
updater squirrel.UpdateBuilder
rank squirrel.SelectBuilder
counter squirrel.SelectBuilder
}

func NewBalances(db *pgdb.DB) data.BalancesQ {
Expand All @@ -25,6 +26,7 @@ func NewBalances(db *pgdb.DB) data.BalancesQ {
selector: squirrel.Select("*").From(balancesTable),
updater: squirrel.Update(balancesTable),
rank: squirrel.Select("*, ROW_NUMBER() OVER (ORDER BY amount DESC, updated_at ASC) AS rank").From(balancesTable),
counter: squirrel.Select("COUNT(*) as count").From(balancesTable),
}
}

Expand Down Expand Up @@ -124,6 +126,18 @@ func (q *balances) Get() (*data.Balance, error) {
return &res, nil
}

func (q *balances) Count() (int64, error) {
res := struct {
Count int64 `db:"count"`
}{}

if err := q.db.Get(&res, q.counter); err != nil {
return 0, fmt.Errorf("get balance: %w", err)
}

return res.Count, nil
}

func (q *balances) GetWithRank(nullifier string) (*data.Balance, error) {
var res data.Balance
stmt := fmt.Sprintf(`
Expand Down Expand Up @@ -205,5 +219,6 @@ func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ {
q.selector = q.selector.Where(cond)
q.updater = q.updater.Where(cond)
q.rank = q.rank.Where(cond)
q.counter = q.counter.Where(cond)
return q
}
12 changes: 12 additions & 0 deletions internal/service/handlers/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ func Leaderboard(w http.ResponseWriter, r *http.Request) {

resp := newLeaderboardResponse(leaders)
resp.Links = req.GetLinks(r)
if req.Count {
leadersCount, err := BalancesQ(r).FilterDisabled().Count()
if err != nil {
Log(r).WithError(err).Error("Failed to count balances")
ape.RenderErr(w, problems.InternalError())
return
}

_ = resp.PutMeta(struct {
EventsCount int64 `json:"events_count"`
}{leadersCount})
}
ape.Render(w, resp)
}

Expand Down
1 change: 1 addition & 0 deletions internal/service/requests/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Leaderboard struct {
page.OffsetParams
Count bool `url:"count"`
}

func NewLeaderboard(r *http.Request) (req Leaderboard, err error) {
Expand Down
Loading