Skip to content

Commit

Permalink
moved uaw request handling into aggregate, added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Nov 22, 2023
1 parent a3277f2 commit f2443cc
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/fetch/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ 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;
}
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<NormalizedHistoryData>(query)
// formats the response into daily intervals
const formatted = parseNormalized(response.data, 86400);
return toJSON(formatted);
} catch (e: any) {
Expand Down
25 changes: 0 additions & 25 deletions src/fetch/uaw.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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<string, NormalizedHistoryFormat> = {};

Expand Down

0 comments on commit f2443cc

Please sign in to comment.