Skip to content

Commit

Permalink
fix: add logging util
Browse files Browse the repository at this point in the history
  • Loading branch information
forbesus committed Oct 2, 2024
1 parent 38299ed commit 8928a68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
15 changes: 3 additions & 12 deletions apps/api/src/routes/v1/dashboardData.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import { getSentry } from "@src/core/providers/sentry.provider";
import { LoggerService } from "@src/core/services/logger/logger.service";
import { getBlocks } from "@src/services/db/blocksService";
import { getNetworkCapacity } from "@src/services/db/providerStatusService";
import { getDashboardData, getProviderGraphData } from "@src/services/db/statsService";
import { getTransactions } from "@src/services/db/transactionsService";
import { getChainStats } from "@src/services/external/apiNodeService";
import { createLoggingExecutor } from "@src/utils/logging";


const logger = new LoggerService({ context: "Dashboard" });
const runOrLog = createLoggingExecutor(logger)

const route = createRoute({
method: "get",
Expand Down Expand Up @@ -179,14 +180,4 @@ export default new OpenAPIHono().openapi(route, async c => {
latestBlocks,
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;
}
}
});
24 changes: 6 additions & 18 deletions apps/api/src/services/external/apiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import fetch from "node-fetch";
import { Op } from "sequelize";

import { cacheKeys, cacheResponse } from "@src/caching/helpers";
import { getSentry } from "@src/core/providers/sentry.provider";
import { LoggerService } from "@src/core/services/logger/logger.service";
import { getTransactionByAddress } from "@src/services/db/transactionsService";
import {
Expand All @@ -30,21 +29,12 @@ import { CosmosMintInflationResponse } from "@src/types/rest/cosmosMintInflation
import { CosmosStakingPoolResponse } from "@src/types/rest/cosmosStakingPoolResponse";
import { coinToAsset } from "@src/utils/coin";
import { apiNodeUrl, averageBlockCountInAMonth, betaTypeVersion, betaTypeVersionMarket } from "@src/utils/constants";
import { createLoggingExecutor } from "@src/utils/logging";
import { getDeploymentRelatedMessages } from "../db/deploymentService";
import { getProviderList } from "../db/providerStatusService";

export async function getChainStats() {
const createLoggingExecutor = (logger: LoggerService) => async <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;
}
}

const logger = new LoggerService()
const logger = new LoggerService({ context: "ApiNode" })
const runOrLog = createLoggingExecutor(logger)

const result = await cacheResponse(
Expand Down Expand Up @@ -101,19 +91,17 @@ export async function getChainStats() {
true
);

let stakingAPR;
if (result.bondedTokens && result.inflation && result.communityTax && result.totalSupply) {
if (result.bondedTokens > 0) {
stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens
}
let stakingAPR: number | undefined;
if (result.bondedTokens && result.bondedTokens > 0 && result.inflation && result.communityTax && result.totalSupply) {
stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens
}

return {
bondedTokens: result.bondedTokens,
totalSupply: result.totalSupply,
communityPool: result.communityPool,
inflation: result.inflation,
stakingAPR: stakingAPR,
stakingAPR,
};
}

Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getSentry } from "@src/core/providers/sentry.provider";
import { LoggerService } from "@src/core/services/logger/logger.service";

export const createLoggingExecutor = (logger: LoggerService) => async <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;
}
}

0 comments on commit 8928a68

Please sign in to comment.