Skip to content

Commit

Permalink
Add comments and replace console log
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 18, 2024
1 parent 143cb70 commit 1f69ce2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/clickhouse/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/clickhouse/makeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function makeQuery<T = unknown>(query: string) {

return data;
} catch (e: any) {
console.error(e.message)
logger.error(e.message)

return { data: [] }
}
Expand Down
5 changes: 5 additions & 0 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/fetch/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/prometheus.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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}`);
}
}
Expand All @@ -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}`);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", ">="],
Expand Down

0 comments on commit 1f69ce2

Please sign in to comment.