From f2443cc973188ca91f434d2fed5db57ba1f18c90 Mon Sep 17 00:00:00 2001 From: Pelotfr Date: Wed, 22 Nov 2023 19:19:06 +0100 Subject: [PATCH] moved uaw request handling into aggregate, added some comments --- src/fetch/GET.ts | 3 +-- src/fetch/aggregate.ts | 4 ++++ src/fetch/uaw.ts | 25 ------------------------- src/queries.spec.ts | 2 +- src/utils.ts | 3 +++ 5 files changed, 9 insertions(+), 28 deletions(-) delete mode 100644 src/fetch/uaw.ts diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index e93317d..1766363 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -4,7 +4,6 @@ import health from "./health.js"; import chains from "./chains.js"; import block from "./block.js"; import aggregate from "./aggregate.js"; -import uaw from "./uaw.js"; import * as prometheus from "../prometheus.js"; import { logger } from "../logger.js"; import swaggerHtml from "../../swagger/index.html" @@ -23,7 +22,7 @@ export default async function (req: Request) { if ( pathname === "/block" ) return block(req); if ( pathname === "/trace_calls" ) return aggregate(req, pathname); if ( pathname === "/transaction_traces" ) return aggregate(req, pathname); - if ( pathname === "/uaw" ) return uaw(req); + if ( pathname === "/uaw" ) return aggregate(req, pathname); logger.warn(`Not found: ${pathname}`); prometheus.request_error.inc({pathname, status: 404}); return NotFound; diff --git a/src/fetch/aggregate.ts b/src/fetch/aggregate.ts index 79eca96..af1902e 100644 --- a/src/fetch/aggregate.ts +++ b/src/fetch/aggregate.ts @@ -5,7 +5,9 @@ import * as prometheus from "../prometheus.js"; import { BadRequest, toJSON } from "./cors.js"; import { parseNormalized, verifyParameters } from "../utils.js"; +// endpoint for aggregates (trace_calls, transaction_traces, uaw) export default async function (req: Request, pathname: string) { + // verify some crucial parameters beforehand const parametersResult = await verifyParameters(req); if(parametersResult instanceof Response) { return parametersResult; @@ -13,8 +15,10 @@ export default async function (req: Request, pathname: string) { try { const { searchParams } = new URL(req.url); logger.info({searchParams: Object.fromEntries(Array.from(searchParams))}); + // creates the query for requested aggregate column based on pathname const query = getAggregate(searchParams, pathname.replace("/", "")); const response = await makeQuery(query) + // formats the response into daily intervals const formatted = parseNormalized(response.data, 86400); return toJSON(formatted); } catch (e: any) { diff --git a/src/fetch/uaw.ts b/src/fetch/uaw.ts deleted file mode 100644 index 2aa2812..0000000 --- a/src/fetch/uaw.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { makeQuery } from "../clickhouse/makeQuery.js"; -import { logger } from "../logger.js"; -import { NormalizedHistoryData, getAggregate } from "../queries.js"; -import * as prometheus from "../prometheus.js"; -import { BadRequest, toJSON } from "./cors.js"; -import { parseNormalized, verifyParameters } from "../utils.js"; - -export default async function (req: Request) { - const parametersResult = await verifyParameters(req); - if(parametersResult instanceof Response) { - return parametersResult; - } - try { - const { searchParams } = new URL(req.url); - logger.info({searchParams: Object.fromEntries(Array.from(searchParams))}); - const query = getAggregate(searchParams, "uaw"); - const response = await makeQuery(query); - const formatted = parseNormalized(response.data, 86400); - return toJSON(formatted); - } catch (e: any) { - logger.error(e); - prometheus.request_error.inc({pathname: "/uaw", status: 400}); - return BadRequest - } -} \ No newline at end of file diff --git a/src/queries.spec.ts b/src/queries.spec.ts index f64731c..784fdf2 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -1,5 +1,5 @@ import { expect, jest, mock, test } from "bun:test"; -import { createBlockQuery, getBlock, getAggregate, /*getUAWHistory*/ } from "./queries.js"; +import { createBlockQuery, getBlock, getAggregate } from "./queries.js"; import { store } from "./clickhouse/stores.js"; // Mock supported chains data to prevent DB query diff --git a/src/utils.ts b/src/utils.ts index 6f1d182..454d708 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -89,6 +89,7 @@ export function parseAggregateFunction(aggregate_function?: string|null) { return aggregate_function; } +// same logic from above export function parseHistoryRange(range?: string|null) { if (!range) return "24h"; if (!z.enum(["24h", "7d", "30d", "90d", "1y", "all"]).safeParse(range).success) { @@ -98,6 +99,7 @@ export function parseHistoryRange(range?: string|null) { return range; } +// used before running the query to gain time export async function verifyParameters(req: Request) { const url = new URL(req.url); // chain @@ -120,6 +122,7 @@ export async function verifyParameters(req: Request) { } } +// parses the db response into normalized format for easier further handling export function parseNormalized(data: NormalizedHistoryData[], interval: number): NormalizedHistoryFormat[] { const parsedData: Record = {};