Skip to content

Commit

Permalink
Add new endpoint /head
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 30, 2024
1 parent e7d712c commit 295a83e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/fetch/head.ts
Original file line number Diff line number Diff line change
@@ -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")
)
);
}
22 changes: 20 additions & 2 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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", {
Expand Down

0 comments on commit 295a83e

Please sign in to comment.