From 5a55ab335845638ce6392feecc0ab03c71e1d358 Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Wed, 24 Apr 2024 12:24:21 -0400 Subject: [PATCH] Include statistics from ClickHouse in `meta` response field --- src/fetch/balance.ts | 3 +-- src/fetch/supply.ts | 3 +-- src/fetch/transfers.ts | 3 +-- src/fetch/utils.ts | 15 +++++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/fetch/balance.ts b/src/fetch/balance.ts index d470c97..56172d9 100644 --- a/src/fetch/balance.ts +++ b/src/fetch/balance.ts @@ -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")) ) diff --git a/src/fetch/supply.ts b/src/fetch/supply.ts index 280ac50..2838e78 100644 --- a/src/fetch/supply.ts +++ b/src/fetch/supply.ts @@ -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")) ) diff --git a/src/fetch/transfers.ts b/src/fetch/transfers.ts index 5ec0b8d..26aaeb5 100644 --- a/src/fetch/transfers.ts +++ b/src/fetch/transfers.ts @@ -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")) ) diff --git a/src/fetch/utils.ts b/src/fetch/utils.ts index 2601853..be97426 100644 --- a/src/fetch/utils.ts +++ b/src/fetch/utils.ts @@ -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, 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 } } } \ No newline at end of file