Skip to content

Commit

Permalink
Include statistics from ClickHouse in meta response field
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 24, 2024
1 parent 2d8e096 commit 5a55ab3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/fetch/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default async function (req: Request) {

return toJSON(
addMetadata(
response.data,
response.rows_before_limit_at_least,
response,
parseLimit(searchParams.get("limit")),
parsePage(searchParams.get("page"))
)
Expand Down
3 changes: 1 addition & 2 deletions src/fetch/supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default async function (req: Request) {

return toJSON(
addMetadata(
response.data,
response.rows_before_limit_at_least,
response,
parseLimit(searchParams.get("limit")),
parsePage(searchParams.get("page"))
)
Expand Down
3 changes: 1 addition & 2 deletions src/fetch/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export default async function (req: Request) {

return toJSON(
addMetadata(
response.data,
response.rows_before_limit_at_least,
response,
parseLimit(searchParams.get("limit")),
parsePage(searchParams.get("page"))
)
Expand Down
15 changes: 9 additions & 6 deletions src/fetch/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Query } from "../clickhouse/makeQuery.js";

export function toJSON(data: any, status: number = 200) {
return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json" } });
}

export function addMetadata(data: any[], total_before_limit: number, limit: number, page: number) {
export function addMetadata(response: Query<any>, req_limit: number, req_page: number) {
// TODO: Catch page number greater than total_pages and return error
return {
data,
data: response.data,
meta: {
"next_page": (page * limit >= total_before_limit) ? page : page + 1,
"previous_page": (page <= 1) ? page : page - 1,
"total_pages": Math.ceil(total_before_limit / limit),
"total_results": total_before_limit
statistics: response.statistics,
"next_page": (req_page * req_limit >= response.rows_before_limit_at_least) ? req_page : req_page + 1,
"previous_page": (req_page <= 1) ? req_page : req_page - 1,
"total_pages": Math.ceil( response.rows_before_limit_at_least / req_limit),
"total_results": response.rows_before_limit_at_least
}
}
}

0 comments on commit 5a55ab3

Please sign in to comment.