From 1f69ce22b5160a64c1d398b0459f8e41fffb8dfe Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Thu, 18 Apr 2024 10:27:16 -0400 Subject: [PATCH] Add comments and replace `console` log --- src/clickhouse/createClient.ts | 1 + src/clickhouse/makeQuery.ts | 2 +- src/fetch/GET.ts | 5 +++++ src/fetch/health.ts | 1 + src/prometheus.ts | 5 +++-- src/queries.ts | 20 ++++++++++++++++++++ 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/clickhouse/createClient.ts b/src/clickhouse/createClient.ts index e566dfe..79ae600 100644 --- a/src/clickhouse/createClient.ts +++ b/src/clickhouse/createClient.ts @@ -2,6 +2,7 @@ import { createClient } from "@clickhouse/client-web"; import { ping } from "./ping.js"; import { APP_NAME, config } from "../config.js"; +// TODO: Make client connect to all DB instances const client = createClient({ ...config, clickhouse_settings: { diff --git a/src/clickhouse/makeQuery.ts b/src/clickhouse/makeQuery.ts index 1de3e1f..fbfe929 100644 --- a/src/clickhouse/makeQuery.ts +++ b/src/clickhouse/makeQuery.ts @@ -30,7 +30,7 @@ export async function makeQuery(query: string) { return data; } catch (e: any) { - console.error(e.message) + logger.error(e.message) return { data: [] } } diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index 5ff8ae7..e6340c3 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -13,11 +13,16 @@ export default async function (req: Request) { const { pathname } = new URL(req.url); prometheus.request.inc({ pathname }); + // Landing page if (pathname === "/") return new Response(Bun.file(swaggerHtml)); if (pathname === "/favicon.png") return new Response(Bun.file(swaggerFavicon)); + + // Utils if (pathname === "/health") return health(req); if (pathname === "/metrics") return new Response(await registry.metrics(), { headers: { "Content-Type": registry.contentType } }); if (pathname === "/openapi") return new Response(openapi, { headers: { "Content-Type": "application/json" } }); + + // Token endpoints if (pathname === "/supply") return supply(req); if (pathname === "/balance") return balance(req); if (pathname === "/transfers") return transfers(req); diff --git a/src/fetch/health.ts b/src/fetch/health.ts index 97c7c30..24719a5 100644 --- a/src/fetch/health.ts +++ b/src/fetch/health.ts @@ -2,6 +2,7 @@ import client from "../clickhouse/createClient.js"; import { logger } from "../logger.js"; import * as prometheus from "../prometheus.js"; +// TODO: Add log entry export default async function (_req: Request) { try { const response = await client.ping(); diff --git a/src/prometheus.ts b/src/prometheus.ts index 32fb386..a9c0b98 100644 --- a/src/prometheus.ts +++ b/src/prometheus.ts @@ -1,5 +1,6 @@ // From https://github.com/pinax-network/substreams-sink-websockets/blob/main/src/prometheus.ts import client, { Counter, CounterConfiguration, Gauge, GaugeConfiguration } from 'prom-client'; +import { logger } from "./logger.js"; export const registry = new client.Registry(); @@ -9,7 +10,7 @@ export function registerCounter(name: string, help = "help", labelNames: string[ registry.registerMetric(new Counter({ name, help, labelNames, ...config })); return registry.getSingleMetric(name) as Counter; } catch (e) { - console.error({ name, e }); + logger.error({ name, e }); throw new Error(`${e}`); } } @@ -19,7 +20,7 @@ export function registerGauge(name: string, help = "help", labelNames: string[] registry.registerMetric(new Gauge({ name, help, labelNames, ...config })); return registry.getSingleMetric(name) as Gauge; } catch (e) { - console.error({ name, e }); + logger.error({ name, e }); throw new Error(`${e}`); } } diff --git a/src/queries.ts b/src/queries.ts index 991453f..86d79c3 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -4,6 +4,26 @@ import { parseLimit, parseTimestamp } from "./utils.js"; // For reference on Clickhouse Database tables: // https://raw.githubusercontent.com/pinax-network/substreams-antelope-tokens/main/schema.sql +// Query for count of unique token holders grouped by token (contract, symcode) pairs +/* +SELECT + Count(*), + contract, + symcode +FROM + ( + SELECT + DISTINCT account, + contract, + symcode + FROM + eos_tokens_v1.account_balances FINAL + ) +GROUP BY + (contract, symcode) +order BY + (contract, symcode) ASC +*/ export function addTimestampBlockFilter(searchParams: URLSearchParams, where: any[]) { const operators = [ ["greater_or_equals", ">="],