Skip to content

Commit

Permalink
Merge pull request #29 from tokenguardio/dapp-analytics-dev
Browse files Browse the repository at this point in the history
Release 3.1.2
  • Loading branch information
rrozek authored Jul 17, 2024
2 parents 6c34c60 + f7816c3 commit 8bab5e9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dashboard-creator-server",
"private": false,
"version": "3.1.1",
"version": "3.1.2",
"license": "MIT",
"main": "src/server.ts",
"engines": {
Expand All @@ -25,6 +25,7 @@
},
"dependencies": {
"@kubernetes/client-node": "^0.21.0",
"@polkadot/api": "^10.11.2",
"@polkadot/keyring": "^12.6.2",
"@polkadot/util": "^12.6.2",
"@subsquid/ink-abi": "^3.0.3",
Expand All @@ -46,6 +47,7 @@
"http-status": "^1.5.0",
"jest-mock-axios": "^4.7.3",
"joi": "^17.5.0",
"lodash": "^4.17.21",
"lusca": "^1.7.0",
"md5": "^2.3.0",
"mongodb-memory-server": "^9.1.6",
Expand Down
34 changes: 26 additions & 8 deletions src/components/dapp-analytics/dapp-analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from './abi.interface';
import { resolveType } from './substrate-types.mapping';
import { docker, k8sApi } from 'server';
import { getCurrentBlock } from '@components/node-api/chainstate';
import { convertAndFormatNumbers } from './helpers/formatting';
import DAPP_ANALYTICS_NETWORKS from './../../config/dapp-analytics-networks';
import config from '@config/config';

Expand Down Expand Up @@ -393,13 +395,18 @@ export const getDapp = async (
if (dappError) {
return res.status(dappError.status).json({ message: dappError.message });
}
let indexingStatus = 0;
let indexingProgress = 0;
let indexingMaxBlock = 0;
try {
const response = await axios.get(
`${API_BASE_URL}/dapp-analytics/dapp/${id}/status`,
);
if (response.status === 200) {
indexingStatus = response.data.output.status.height;
indexingProgress = response.data.output.status.height;
}
const nodeResponse = await getCurrentBlock(dappData.blockchain);
if (nodeResponse) {
indexingMaxBlock = nodeResponse;
}
} catch (error) {
logger.error(`Error retrieving DApp status with id ${id}:`, error);
Expand All @@ -415,7 +422,7 @@ export const getDapp = async (
const responseData = {
...dappData,
containerStatus: containerStatus || 'not found',
indexingStatus,
indexingStatus: convertAndFormatNumbers(indexingProgress, indexingMaxBlock),
};

return res.status(200).json(responseData);
Expand Down Expand Up @@ -510,16 +517,21 @@ export const getAllDapps = async (req, res) => {

const dAppsWithIndexingStatus = await Promise.all(
dApps.map(async (dApp) => {
let indexingStatus = 0,
containerStatus = 'not found';
let indexingProgress = 0;
let indexingMaxBlock = 0;
let containerStatus = 'not found';
try {
const statusResponse = await axios.get(
`${API_BASE_URL}/dapp-analytics/dapp/${dApp.id}/status`,
);
indexingStatus =
indexingProgress =
statusResponse.status === 200
? statusResponse.data.output.status.height
: 0;
const nodeResponse = await getCurrentBlock(dApp.blockchain);
if (nodeResponse) {
indexingMaxBlock = nodeResponse;
}
} catch (error) {
logger.error(
`Error retrieving DApp status with id ${dApp.id}:`,
Expand All @@ -528,7 +540,10 @@ export const getAllDapps = async (req, res) => {
}
return {
...dApp,
indexingStatus,
indexingStatus: convertAndFormatNumbers(
indexingProgress,
indexingMaxBlock,
),
containerStatus,
};
}),
Expand All @@ -546,7 +561,10 @@ export const getAllDapps = async (req, res) => {
);
}
} catch (error) {
logger.error(`in config: ${config.deploymentMode} error checking container status:`, error);
logger.error(
`in config: ${config.deploymentMode} error checking container status:`,
error,
);
}

return res.status(200).json(dAppsWithStatus);
Expand Down
21 changes: 21 additions & 0 deletions src/components/dapp-analytics/helpers/formatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import _ from 'lodash';

export function formatNumber(num: number): string {
if (num >= 1e9) {
return (num / 1e9).toFixed(1) + 'B';
} else if (num >= 1e6) {
return (num / 1e6).toFixed(1) + 'M';
} else if (num >= 1e3) {
return (num / 1e3).toFixed(1) + 'k';
}
return num.toString();
}

export function convertAndFormatNumbers(num1: number, num2: number): string {
const formattedNum1 = formatNumber(num1);
const formattedNum2 = formatNumber(num2);
const percentage = ((num1 / num2) * 100).toFixed(2);

return `${formattedNum1} / ${formattedNum2} (${percentage}%)`;
}

20 changes: 20 additions & 0 deletions src/components/node-api/chainstate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiPromise, WsProvider } from '@polkadot/api';

const rpcEndpoints: { [key: string]: string } = {
polkadot: 'wss://rpc.polkadot.io',
kusama: 'wss://kusama-rpc.polkadot.io',
'aleph-zero': 'wss://rpc.azero.dev',
};

export async function getCurrentBlock(chain: string) {
const endpoint = rpcEndpoints[chain];
if (!endpoint) {
throw new Error(`RPC endpoint for chain ${chain} not found`);
}

const wsProvider = new WsProvider(endpoint);
const api = await ApiPromise.create({ provider: wsProvider });

const currentBlock = await api.rpc.chain.getHeader();
return currentBlock.number.toNumber();
}
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"charset": "utf-8",
"types": ["@types/jest", "node"],
"typeRoots": ["node_modules/@types", "typings"],
"lib": ["es2015", "es2016", "es2017", "es2018", "dom"],
Expand All @@ -28,7 +27,7 @@
"@core/*": ["core/*"],
"@components/*": ["components/*"],
"@config/*": ["config/*"],
"@preload-data/*": ["preload-data/*"]
"@preload-data/*": ["preload-data/*"],
},
"plugins": [{ "transform": "ts-optchain/transform" }]
},
Expand Down

0 comments on commit 8bab5e9

Please sign in to comment.