Skip to content

Commit

Permalink
feat: endpoints for UI main page stats (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipHarald authored Sep 30, 2024
1 parent 0f1ff0a commit 9749df1
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./get-tx-effect.js";
export * from "./stats.js";
13 changes: 13 additions & 0 deletions services/explorer-api/src/database/controllers/l2TxEffect/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { count } from "drizzle-orm";
import { getDb as db } from "../../../database/index.js";
import { txEffect } from "../../../database/schema/index.js";

export const getTotalTxEffects = async (): Promise<number> => {
const dbRes = await db().select({ count: count() }).from(txEffect).execute();
return dbRes[0].count;
};

export const getTotalTxEffectsLast24h = (): number => {
// TODO: we need l2Block.header.globalVariables.timestamp as number to sum this
return -1;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./get-latest.js";
export * from "./get-block.js";
export * from "./store.js";
export * from "./stats.js";
14 changes: 14 additions & 0 deletions services/explorer-api/src/database/controllers/l2block/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import { getDb as db } from "../../../database/index.js";
// import { l2Block } from "../../../database/schema/index.js";

// eslint-disable-next-line @typescript-eslint/require-await
export const getAverageFees = async (): Promise<number> => {
// TODO: we need l2Block.header.totalFees as number to average this
return -1;
};

// eslint-disable-next-line @typescript-eslint/require-await
export const getAverageBlockTime = async (): Promise<number> => {
// TODO: we need l2Block.header.globalVariables.timestamp as number to average this
return -1;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./get-contract-instance.js";
export * from "./get-contract-instances.js";
export * from "./store.js";
export * from "./stats.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { count } from "drizzle-orm";
import { getDb as db } from "../../../database/index.js";
import { l2ContractClassRegistered } from "../../../database/schema/index.js";

export const getTotalContracts = async (): Promise<number> => {
const dbRes = await db().select({ count: count() }).from(l2ContractClassRegistered).execute();
return dbRes[0].count;
};
41 changes: 41 additions & 0 deletions services/explorer-api/src/http-server/routes/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export const GET_ROUTES = asyncHandler(async (_req, res) => {
r.push(routes.contractInstance + "NOT FOUND");
}

const statsRoutes = [
routes.statsTotalTxEffects,
routes.statsTotalTxEffectsLast24h,
routes.statsTotalContracts,
routes.statsAverageFees,
routes.statsAverageBlockTime,
];

const html = `
<html>
<head>
Expand All @@ -87,6 +95,9 @@ export const GET_ROUTES = asyncHandler(async (_req, res) => {
<ul>
${r.map((route) => `<li><a href=${SUB_PATH + route}>${route}</a></li>`).join("")}
</ul>
<br>
<ul>
${statsRoutes.map((route) => `<li><a href=${SUB_PATH + route}>${route}</a></li>`).join("")}
</body>
</html>
`;
Expand Down Expand Up @@ -175,3 +186,33 @@ export const GET_L2_CONTRACT_INSTANCES_BY_BLOCK_HASH = asyncHandler(
res.status(200).send(JSON.stringify(instances));
}
);

export const GET_STATS_TOTAL_TX_EFFECTS = asyncHandler(async (_req, res) => {
const total = await db.l2TxEffect.getTotalTxEffects();
if (!total) throw new Error("Total tx effects not found");
res.status(200).send(JSON.stringify(total));
});

export const GET_STATS_TOTAL_TX_EFFECTS_LAST_24H = asyncHandler((_req, res) => {
const txEffects = db.l2TxEffect.getTotalTxEffectsLast24h();
if (!txEffects) throw new Error("Tx effects not found");
res.status(200).send(JSON.stringify(txEffects));
});

export const GET_STATS_TOTAL_CONTRACTS = asyncHandler(async (_req, res) => {
const total = await db.l2Contract.getTotalContracts();
if (!total) throw new Error("Total contracts not found");
res.status(200).send(JSON.stringify(total));
});

export const GET_STATS_AVERAGE_FEES = asyncHandler(async (_req, res) => {
const average = await db.l2Block.getAverageFees();
if (!average) throw new Error("Average fees not found");
res.status(200).send(JSON.stringify(average));
});

export const GET_STATS_AVERAGE_BLOCK_TIME = asyncHandler(async (_req, res) => {
const average = await db.l2Block.getAverageBlockTime();
if (!average) throw new Error("Average block time not found");
res.status(200).send(JSON.stringify(average));
});
6 changes: 6 additions & 0 deletions services/explorer-api/src/http-server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ export const init = ({
router.get(routes.contractInstancesByBlockHash, controller.GET_L2_CONTRACT_INSTANCES_BY_BLOCK_HASH);
router.get(routes.contractInstance, controller.GET_L2_CONTRACT_INSTANCE);

router.get(routes.statsTotalTxEffects, controller.GET_STATS_TOTAL_TX_EFFECTS);
router.get(routes.statsTotalTxEffectsLast24h, controller.GET_STATS_TOTAL_TX_EFFECTS_LAST_24H);
router.get(routes.statsTotalContracts, controller.GET_STATS_TOTAL_CONTRACTS);
router.get(routes.statsAverageFees, controller.GET_STATS_AVERAGE_FEES);
router.get(routes.statsAverageBlockTime, controller.GET_STATS_AVERAGE_BLOCK_TIME);

return router;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export const routes = {
// contractInstancesByContractClassId: `/l2/contract-classes/:${contractClassId}/contract-instances`,
contractInstancesByBlockHash: `/l2/blocks/:${blockHash}/contract-instances`,
contractInstance: `/l2/contract-instances/:${address}`,

statsTotalTxEffects: '/l2/stats/total-tx-effects',
statsTotalTxEffectsLast24h: '/l2/stats/tx-effects-last-24h',
statsTotalContracts: '/l2/stats/total-contracts',
statsAverageFees: '/l2/stats/average-fees',
statsAverageBlockTime: '/l2/stats/average-block-time',
};

export const getBlockByHeightOrHashSchema = z.object({
Expand Down

0 comments on commit 9749df1

Please sign in to comment.