From e852aa48f3d82887fe0992ca24149ed4e5b5ffed Mon Sep 17 00:00:00 2001 From: rrozek Date: Thu, 18 Jul 2024 01:27:07 +0200 Subject: [PATCH 1/2] improve indexer reporting visibility --- package.json | 2 ++ .../dapp-analytics.controller.ts | 34 ++++++++++++++----- .../dapp-analytics/helpers/formatting.ts | 21 ++++++++++++ src/components/node-api/chainstate.ts | 20 +++++++++++ tsconfig.json | 3 +- 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/components/dapp-analytics/helpers/formatting.ts create mode 100644 src/components/node-api/chainstate.ts diff --git a/package.json b/package.json index bab864a..c20bd53 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/components/dapp-analytics/dapp-analytics.controller.ts b/src/components/dapp-analytics/dapp-analytics.controller.ts index e64cba3..12a1689 100644 --- a/src/components/dapp-analytics/dapp-analytics.controller.ts +++ b/src/components/dapp-analytics/dapp-analytics.controller.ts @@ -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'; @@ -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); @@ -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); @@ -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}:`, @@ -528,7 +540,10 @@ export const getAllDapps = async (req, res) => { } return { ...dApp, - indexingStatus, + indexingStatus: convertAndFormatNumbers( + indexingProgress, + indexingMaxBlock, + ), containerStatus, }; }), @@ -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); diff --git a/src/components/dapp-analytics/helpers/formatting.ts b/src/components/dapp-analytics/helpers/formatting.ts new file mode 100644 index 0000000..59560d5 --- /dev/null +++ b/src/components/dapp-analytics/helpers/formatting.ts @@ -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}%)`; +} + diff --git a/src/components/node-api/chainstate.ts b/src/components/node-api/chainstate.ts new file mode 100644 index 0000000..72d07f5 --- /dev/null +++ b/src/components/node-api/chainstate.ts @@ -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(); +} diff --git a/tsconfig.json b/tsconfig.json index 3eee32e..0aa0f00 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"], @@ -28,7 +27,7 @@ "@core/*": ["core/*"], "@components/*": ["components/*"], "@config/*": ["config/*"], - "@preload-data/*": ["preload-data/*"] + "@preload-data/*": ["preload-data/*"], }, "plugins": [{ "transform": "ts-optchain/transform" }] }, From f7816c3043fb3a619643856064f7a9450a94e9c5 Mon Sep 17 00:00:00 2001 From: rrozek Date: Thu, 18 Jul 2024 01:28:27 +0200 Subject: [PATCH 2/2] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c20bd53..73c1709 100644 --- a/package.json +++ b/package.json @@ -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": {