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 1 commit
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
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
}
21 changes: 18 additions & 3 deletions internal/service/handlers/leaderboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/rarimo/rarime-points-svc/internal/data"
Expand All @@ -24,16 +25,30 @@ func Leaderboard(w http.ResponseWriter, r *http.Request) {
return
}

resp := newLeaderboardResponse(leaders)
leadersCount, err := BalancesQ(r).FilterDisabled().Count()
if err != nil {
Log(r).WithError(err).Error("Failed to leaders count")
Zaptoss marked this conversation as resolved.
Show resolved Hide resolved
ape.RenderErr(w, problems.InternalError())
return
}

resp := newLeaderboardResponse(leaders, leadersCount)
resp.Links = req.GetLinks(r)
ape.Render(w, resp)
}

func newLeaderboardResponse(balances []data.Balance) resources.BalanceListResponse {
func newLeaderboardResponse(balances []data.Balance, totalLeaders int64) resources.BalanceListResponse {
list := make([]resources.Balance, len(balances))
for i, balance := range balances {
list[i] = newBalanceModel(balance)
}

return resources.BalanceListResponse{Data: list}
// must never panic
serMeta, _ := json.Marshal(struct {
TotalCount int64 `json:"total_count"`
}{
TotalCount: totalLeaders,
})

return resources.BalanceListResponse{Data: list, Meta: serMeta}
}
Loading