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

Include statistics from ClickHouse in meta response field #14

Merged
merged 2 commits into from
Apr 24, 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
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
17 changes: 12 additions & 5 deletions src/fetch/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
import { expect, test } from "bun:test";
import { addMetadata } from "./utils.js";
import { Query } from "../clickhouse/makeQuery.js";

test("addMetadata pagination", () => {
const limit = 5;
const mock_query_reponse = {
const mock_query_reponse: Query<any> = {
meta: [],
data: Array(limit),
rows: limit,
rows_before_limit_at_least: 5*limit, // Simulate query with more total results than the query limit making pagination relevant
statistics: {
elapsed: 0,
rows_read: 0,
bytes_read: 0,
}
};

const first_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 1);
const first_page = addMetadata(mock_query_reponse, limit, 1);
expect(first_page.meta.next_page).toBe(2);
expect(first_page.meta.previous_page).toBe(1); // Previous page should be set to 1 on first page
expect(first_page.meta.total_pages).toBe(5);
expect(first_page.meta.total_results).toBe(5*limit);

const odd_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 3);
const odd_page = addMetadata(mock_query_reponse, limit, 3);
expect(odd_page.meta.next_page).toBe(4);
expect(odd_page.meta.previous_page).toBe(2);
expect(odd_page.meta.total_pages).toBe(5);
expect(odd_page.meta.total_results).toBe(5*limit);

const even_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 4);
const even_page = addMetadata(mock_query_reponse, limit, 4);
expect(even_page.meta.next_page).toBe(5);
expect(even_page.meta.previous_page).toBe(3);
expect(even_page.meta.total_pages).toBe(5);
expect(even_page.meta.total_results).toBe(5*limit);

const last_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 5);
const last_page = addMetadata(mock_query_reponse, limit, 5);
expect(last_page.meta.next_page).toBe(last_page.meta.total_pages); // Next page should be capped to total_pages on last page
expect(last_page.meta.previous_page).toBe(4);
expect(last_page.meta.total_pages).toBe(5);
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
}
}
}
Loading