Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { getSelected_heartbeat, get_NetworkByCycleOrCloset } from '../storage/getters';
- import { insertReplace_network } from '../storage/setters';
- export class Renderer {
- nodesDetails: { [key: string]: any };
- lastObservedTimestamp: number;
- constructor() {
- this.nodesDetails = {};
- this.lastObservedTimestamp = 0;
- }
- }
- interface NetworkData {
- joining: number;
- joined: number;
- active: number;
- total: number;
- desired: number;
- cycleMarker: number;
- cycleCounter: number;
- cycleDuration: number;
- txsProcessed: number;
- txsRejected: number;
- txsExpired: number;
- maxTps: number;
- avgTps: number;
- rejTps: number;
- netLoad: number;
- intReq: number;
- extReq: number;
- Qlen: number;
- Qtime: number;
- }
- interface ChartData {
- joined: number;
- active: number;
- avgTps: number;
- rejTps: number;
- load: number;
- intLoad: number;
- cycleDuration: number;
- }
- // function used for processing heartbeats, populating an array to hold details of all the nodes
- export async function processRawHeartbeatAndStore(renderer: Renderer) {
- // Get all heartbeats with a timestamp greater than or equal to the last observed timestamp
- const res = await getSelected_heartbeat(renderer.lastObservedTimestamp);
- if (res.rowCount <= 0) {
- return;
- }
- // Loop through the selected heartbeats and store them in the nodesDetails array
- for (let i = 0; i < res.rows.length; i++) {
- try {
- const heartbeat = res.rows[i];
- const status = heartbeat.endpoint;
- heartbeat.data.push(status);
- renderer.nodesDetails[heartbeat.data.ipport] = { ...heartbeat.data };
- } catch (e) {
- console.log('nodesDetails array population error', e);
- }
- }
- // Process network statistics using the updated nodesDetails array
- await processForNetworkStats(renderer.nodesDetails);
- // Update the last observed timestamp to the current time
- renderer.lastObservedTimestamp = Date.now();
- }
- // function used for calculating netowrk statistics and populating the network table in the dB
- async function processForNetworkStats(nodesDetails: { [key: string]: any }) {
- // Filter the nodesDetails array to get info on joiningNodes, joinedNodes and activeNodes
- const joiningNodesCount = nodesDetails.filter(node => node.status === "/joining").length;
- const joinedNodesCount = nodesDetails.filter(node => node.status === "/joined").length;
- const activeNodesData = nodesDetails.filter(node => node.status === "/active" || node.status === "/heartbeat");
- const activeNodesCount = activeNodesData.length
- // If there are no active nodes, return
- if (activeNodesCount <= 0) {
- return;
- }
- // Initialize variables for computing and storing data for the network and charts table
- let lastCycleTxsProcessed = 0;
- let lastCycleTxsRejected = 0;
- let lastCycleTxsExpired = 0;
- let modeDesiredNodes = 0;
- let totalQlen = 0;
- let totalQtime = 0;
- let networkLoadTotal = 0;
- let intLoadTotal = 0;
- let extLoadTotal = 0;
- let cycleDurationAcquired = Number(activeNodesData[0].data.duration);
- let reportInterval = Number(activeNodesData[0].data.reportInterval);
- // Get the response for this cycle or closest from the network table
- const response_thisCycleOrCloset = await get_NetworkByCycleOrCloset(activeNodesData[0].data.cycleCounter);
- // Initialize variables for storing last known values
- let lastTotalTxProcessed = 0;
- let lastTotalTxRejected = 0;
- let lastTotalTxExpired = 0;
- let lastAvgTps = 0;
- let lastMaxTps = 0;
- let lastRejTps = 0;
- // If rowCount > 0, assign values from response to get the last known values for the network to maintain continuity in the charts and graphs
- if (response_thisCycleOrCloset.rowCount > 0) {
- const { txsProcessed, txsRejected, txsExpired, avgTps, maxTps, rejTps } = response_thisCycleOrCloset.rows[0];
- lastTotalTxProcessed = Number(txsProcessed);
- lastTotalTxRejected = Number(txsRejected);
- lastTotalTxExpired = Number(txsExpired);
- lastAvgTps = Number(avgTps);
- lastMaxTps = Number(maxTps);
- lastRejTps = Number(rejTps);
- }
- // json object that contains the network details
- const network_data: NetworkData = {
- joining: joiningNodesCount,
- joined: joinedNodesCount,
- active: activeNodesCount,
- total: joiningNodesCount + joinedNodesCount + activeNodesCount,
- desired: modeDesiredNodes,
- cycleMarker: activeNodesData[0].cycleMarker,
- cycleCounter: activeNodesData[0].cycleCounter,
- cycleDuration: cycleDurationAcquired,
- txsProcessed: lastCycleTxsProcessed + lastTotalTxProcessed,
- txsRejected: lastCycleTxsRejected + lastTotalTxRejected,
- txsExpired: lastCycleTxsExpired + lastTotalTxExpired,
- maxTps: lastMaxTps,
- avgTps: lastAvgTps,
- rejTps: lastRejTps,
- netLoad: parseFloat((networkLoadTotal / activeNodesCount).toFixed(3)),
- intReq: parseFloat((intLoadTotal / activeNodesCount).toFixed(3)),
- extReq: parseFloat((extLoadTotal / activeNodesCount).toFixed(3)),
- Qlen: parseFloat((totalQlen / activeNodesCount).toFixed(3)),
- Qtime: parseFloat((totalQtime / activeNodesCount).toFixed(3)),
- };
- // json object that contains the chart details
- const chart_data: ChartData = {
- joined: joinedNodesCount,
- active: activeNodesCount,
- avgTps: network_data.avgTps,
- rejTps: network_data.rejTps,
- load: network_data.netLoad,
- intLoad: network_data.intReq,
- cycleDuration: network_data.cycleDuration,
- };
- // updating avgTps and regTps
- if (network_data.active != 0) {
- let diffRatio = 0; // initialize the difference ratio variable
- // calculate the new average TPS based on the number of transactions processed during the last reporting interval
- const newAvgTps = Math.round(lastCycleTxsProcessed / (reportInterval / 1000));
- // calculate the difference ratio between the new and previous average TPS
- if (lastAvgTps > 0) {
- diffRatio = (newAvgTps - lastAvgTps) / lastAvgTps;
- }
- // check if the new average TPS is significantly higher or lower than the previous average TPS
- if (diffRatio < 1.5 || diffRatio > 0.5) {
- // check if the new average TPS is higher than the previous maximum TPS
- if (newAvgTps > network_data.maxTps) {
- network_data.maxTps = newAvgTps; // update the maximum TPS value
- }
- }
- // update the average TPS value in the network data object
- network_data.avgTps = newAvgTps;
- // calculate the new rejected TPS based on the number of transactions rejected during the last reporting interval
- const rejectedTps = Math.round(lastCycleTxsRejected / (reportInterval / 1000));
- // update the rejected TPS value in the network data object
- network_data.rejTps = rejectedTps;
- }
- // inserting the network data into the network table along with a column for chart data
- await insertReplace_network(
- network_data.cycleCounter,
- network_data,
- chart_data,
- Date.now()
- );
- }
- Please look at this
- // example implementation in server.ts
- // const renderer = new Renderer();
- // setInterval(processRawHeartbeatAndStore(renderer), CONFIG.renderer.interval * 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement