Skip to content

Commit

Permalink
Add users count in leaderboard response with query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Jul 24, 2024
1 parent 77b805e commit 2d3a354
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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 Down
2 changes: 2 additions & 0 deletions internal/data/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type BalancesQ interface {
WithoutPassportEvent() ([]WithoutPassportEventBalance, error)
WithoutReferralEvent() ([]ReferredReferrer, error)

Count() (int64, error)

FilterByNullifier(...string) BalancesQ
FilterDisabled() BalancesQ
FilterByAnonymousID(id string) BalancesQ
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 @@ -16,6 +16,7 @@ type balances struct {
db *pgdb.DB
selector squirrel.SelectBuilder
updater squirrel.UpdateBuilder
counter squirrel.SelectBuilder
rank squirrel.SelectBuilder
}

Expand All @@ -24,6 +25,7 @@ func NewBalances(db *pgdb.DB) data.BalancesQ {
db: db,
selector: squirrel.Select("*").From(balancesTable),
updater: squirrel.Update(balancesTable),
counter: squirrel.Select("COUNT(*) as count").From(balancesTable),
rank: squirrel.Select("*, ROW_NUMBER() OVER (ORDER BY amount DESC, updated_at ASC) AS rank").From(balancesTable),
}
}
Expand Down Expand Up @@ -55,6 +57,18 @@ func (q *balances) Update(fields map[string]any) error {
return 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
}

// applyRankedPage is similar to the pgdb.OffsetParams.ApplyTo method,
// but the sorting values are hardcoded because the fields must
// be sorted in opposite directions
Expand Down Expand Up @@ -204,5 +218,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

0 comments on commit 2d3a354

Please sign in to comment.