Skip to content

Commit

Permalink
fix: add runOrLog function
Browse files Browse the repository at this point in the history
  • Loading branch information
forbesus committed Oct 1, 2024
1 parent 2f9b8de commit 936b712
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 59 deletions.
86 changes: 28 additions & 58 deletions apps/api/src/routes/v1/dashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,66 +149,26 @@ const route = createRoute({
});

export default new OpenAPIHono().openapi(route, async c => {

let chainStats;
let now;
let compare;
let networkCapacity;
let networkCapacityStats;
let latestBlocks;
let latestTransactions;

try {
const chainStatsQuery = await getChainStats();
chainStats = { ...chainStatsQuery };
} catch (error) {
chainStats = {}
logger.error({ event: "Failed to fetch getChainStats", error });
getSentry().captureException(error);
}

try {
const dashboardData = await getDashboardData();
now = dashboardData.now;
compare = dashboardData.compare;
} catch (error) {
logger.error({ event: "Failed to fetch dashboardData", error });
getSentry().captureException(error);
}


try {
networkCapacity = await getNetworkCapacity();
} catch (error) {
logger.error({ event: "Failed to fetch networkCapacity", error });
getSentry().captureException(error);
}

try {
networkCapacityStats = await getProviderGraphData("count");
} catch (error) {
logger.error({ event: "Failed to fetch networkCapacityStats", error });
getSentry().captureException(error);
}

try {
latestBlocks = await getBlocks(5);
chainStats = {
...chainStats,
height: latestBlocks[0]?.height ?? null,
transactionCount: latestBlocks[0]?.totalTransactionCount ?? null,
const chainStatsQuery = await runOrLog(
getChainStats,
{
bondedTokens: undefined,
totalSupply: undefined,
communityPool: undefined,
inflation: undefined,
stakingAPR: undefined
}
} catch (error) {
logger.error({ event: "Failed to fetch latestBlocks", error });
getSentry().captureException(error);
}

try {
latestTransactions = await getTransactions(5);
} catch (error) {
logger.error({ event: "Failed to fetch latestTransactions", error });
getSentry().captureException(error);
);
const { now, compare } = await runOrLog(getDashboardData)
const networkCapacity = await runOrLog(getNetworkCapacity);
const networkCapacityStats = await runOrLog(() => getProviderGraphData("count"));
const latestBlocks = await runOrLog(() => getBlocks(5));
const chainStats = {
...chainStatsQuery,
height: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].height : undefined,
transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined,
}
const latestTransactions = await runOrLog(() => getTransactions(5));

return c.json({
chainStats,
Expand All @@ -220,3 +180,13 @@ export default new OpenAPIHono().openapi(route, async c => {
latestTransactions
});
});

async function runOrLog<T>(cb: () => Promise<T>, defaultValue?: T): Promise<T> {
try {
return await cb();
} catch (error) {
logger.error({ event: `Failed to fetch ${cb.name}`, error });
getSentry().captureException(error);
return defaultValue;
}
}
1 change: 0 additions & 1 deletion apps/api/src/services/external/apiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export async function getChainStats() {
60 * 5, // 5 minutes
cacheKeys.getChainStats,
async () => {
// Define default values in case requests fail
let bondedTokens;
let totalSupply;
let communityPool;
Expand Down

0 comments on commit 936b712

Please sign in to comment.