From 295a83ea7049ab8b039f6f6f7c59f4e3e156dcfc Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Tue, 30 Apr 2024 10:07:59 -0400 Subject: [PATCH] Add new endpoint `/head` --- src/fetch/GET.ts | 2 ++ src/fetch/head.ts | 10 ++++++++++ src/fetch/openapi.ts | 22 ++++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/fetch/head.ts diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index 26dbc10..40f78dd 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -1,6 +1,7 @@ import { registry } from "../prometheus.js"; import openapi from "./openapi.js"; import health from "./health.js"; +import head from "./head.js"; import balance from "./balance.js"; import supply from "./supply.js"; import * as prometheus from "../prometheus.js"; @@ -26,6 +27,7 @@ export default async function (req: Request) { if (pathname === "/version") return toJSON({ version: APP_VERSION.split('+')[0], commit: APP_VERSION.split('+')[1] }); // Token endpoints + if (pathname === "/head") return head(req); if (pathname === "/supply") return supply(req); if (pathname === "/balance") return balance(req); if (pathname === "/transfers") return transfers(req); diff --git a/src/fetch/head.ts b/src/fetch/head.ts new file mode 100644 index 0000000..0a56bac --- /dev/null +++ b/src/fetch/head.ts @@ -0,0 +1,10 @@ +import { addMetadata, toJSON } from "./utils.js"; +import { makeQuery } from "../clickhouse/makeQuery.js"; + +export default async function (req: Request) { + return toJSON( + addMetadata( + await makeQuery("SELECT block_num FROM cursors ORDER BY block_num DESC LIMIT 1") + ) + ); +} \ No newline at end of file diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index a3623d6..2881d36 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -17,6 +17,17 @@ const timestampExamplesArrayFilter = ["greater_or_equals_by_timestamp", "greater const blockExamplesArrayFilter = ["greater_or_equals_by_block", "greater_by_block", "less_or_equals_by_block", "less_by_block"]; const amountExamplesArrayFilter = ["amount_greater_or_equals", "amount_greater", "amount_less_or_equals", "amount_less"]; +const head_example = addMetadata({ + meta: [], + data: [{ block_num: "107439534" }], + rows: 0, + rows_before_limit_at_least: 0, + statistics: { + elapsed: 0.00132, + rows_read: 4, + bytes_read: 32 + } +}); const supply_example = await makeQuery(getTotalSupply(new URLSearchParams({ limit: "1" }), true)).then(res => addMetadata(res, 1, 1)); const balance_example = await makeQuery(getBalanceChanges(new URLSearchParams({ limit: "2" }), true)).then(res => addMetadata(res, 2, 1)); const transfers_example = await makeQuery(getTransfers(new URLSearchParams({ limit: "5" }), true)).then(res => addMetadata(res, 5, 1)); @@ -164,11 +175,18 @@ export default new OpenApiBuilder() responses: { 200: { description: "OK", content: { "text/plain": { example: "OK" } } } }, }, }) + .addPath("/head", { + get: { + tags: [TAGS.MONITORING], + summary: "Information about the current head block in the database", + responses: { 200: { description: "Information about the current head block in the database", content: { "application/json": { example: head_example } } } }, + }, + }) .addPath("/metrics", { get: { tags: [TAGS.MONITORING], - summary: "Prometheus metrics", - responses: { 200: { description: "Prometheus metrics", content: { "text/plain": { example: await registry.metrics(), schema: { type: "string" } } } } }, + summary: "Prometheus metrics for the API", + responses: { 200: { description: "Prometheus metrics for the API", content: { "text/plain": { example: await registry.metrics(), schema: { type: "string" } } } } }, }, }) .addPath("/openapi", {