Advertisement
tk4vr

Renderer Code Final

Mar 3rd, 2023 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { getSelected_heartbeat, get_NetworkByCycleOrCloset } from '../storage/getters';
  2. import { insertReplace_network } from '../storage/setters';
  3.  
  4. export class Renderer {
  5.     nodesDetails: { [key: string]: any };
  6.     lastObservedTimestamp: number;
  7.  
  8.     constructor() {
  9.         this.nodesDetails = {};
  10.         this.lastObservedTimestamp = 0;
  11.     }
  12. }
  13. interface NetworkData {
  14.     joining: number;
  15.     joined: number;
  16.     active: number;
  17.     total: number;
  18.     desired: number;
  19.     cycleMarker: number;
  20.     cycleCounter: number;
  21.     cycleDuration: number;
  22.     txsProcessed: number;
  23.     txsRejected: number;
  24.     txsExpired: number;
  25.     maxTps: number;
  26.     avgTps: number;
  27.     rejTps: number;
  28.     netLoad: number;
  29.     intReq: number;
  30.     extReq: number;
  31.     Qlen: number;
  32.     Qtime: number;
  33. }
  34. interface ChartData {
  35.     joined: number;
  36.     active: number;
  37.     avgTps: number;
  38.     rejTps: number;
  39.     load: number;
  40.     intLoad: number;
  41.     cycleDuration: number;
  42. }
  43. // function used for processing heartbeats, populating an array to hold details of all the nodes
  44. export async function processRawHeartbeatAndStore(renderer: Renderer) {
  45.  
  46.     // Get all heartbeats with a timestamp greater than or equal to the last observed timestamp
  47.     const res = await getSelected_heartbeat(renderer.lastObservedTimestamp);
  48.     if (res.rowCount <= 0) {
  49.         return;
  50.     }
  51.  
  52.     // Loop through the selected heartbeats and store them in the nodesDetails array
  53.     for (let i = 0; i < res.rows.length; i++) {
  54.         try {
  55.             const heartbeat = res.rows[i];
  56.             const status = heartbeat.endpoint;
  57.             heartbeat.data.push(status);
  58.  
  59.             renderer.nodesDetails[heartbeat.data.ipport] = { ...heartbeat.data };
  60.         } catch (e) {
  61.             console.log('nodesDetails array population error', e);
  62.         }
  63.     }
  64.  
  65.     // Process network statistics using the updated nodesDetails array
  66.     await processForNetworkStats(renderer.nodesDetails);
  67.     // Update the last observed timestamp to the current time
  68.     renderer.lastObservedTimestamp = Date.now();
  69. }
  70. // function used for calculating netowrk statistics and populating the network table in the dB
  71. async function processForNetworkStats(nodesDetails: { [key: string]: any }) {
  72.     // Filter the nodesDetails array to get info on joiningNodes, joinedNodes and activeNodes
  73.     const joiningNodesCount = nodesDetails.filter(node => node.status === "/joining").length;
  74.     const joinedNodesCount = nodesDetails.filter(node => node.status === "/joined").length;
  75.     const activeNodesData = nodesDetails.filter(node => node.status === "/active" || node.status === "/heartbeat");
  76.     const activeNodesCount = activeNodesData.length
  77.  
  78.     // If there are no active nodes, return
  79.     if (activeNodesCount <= 0) {
  80.         return;
  81.     }
  82.  
  83.     // Initialize variables for computing and storing data for the network and charts table
  84.     let lastCycleTxsProcessed = 0;
  85.     let lastCycleTxsRejected = 0;
  86.     let lastCycleTxsExpired = 0;
  87.     let modeDesiredNodes = 0;
  88.     let totalQlen = 0;
  89.     let totalQtime = 0;
  90.     let networkLoadTotal = 0;
  91.     let intLoadTotal = 0;
  92.     let extLoadTotal = 0;
  93.     let cycleDurationAcquired = Number(activeNodesData[0].data.duration);
  94.     let reportInterval = Number(activeNodesData[0].data.reportInterval);
  95.  
  96.     // Get the response for this cycle or closest from the network table
  97.     const response_thisCycleOrCloset = await get_NetworkByCycleOrCloset(activeNodesData[0].data.cycleCounter);
  98.  
  99.     // Initialize variables for storing last known values
  100.     let lastTotalTxProcessed = 0;
  101.     let lastTotalTxRejected = 0;
  102.     let lastTotalTxExpired = 0;
  103.     let lastAvgTps = 0;
  104.     let lastMaxTps = 0;
  105.     let lastRejTps = 0;
  106.  
  107.     // If rowCount > 0, assign values from response to get the last known values for the network to maintain continuity in the charts and graphs
  108.     if (response_thisCycleOrCloset.rowCount > 0) {
  109.         const { txsProcessed, txsRejected, txsExpired, avgTps, maxTps, rejTps } = response_thisCycleOrCloset.rows[0];
  110.         lastTotalTxProcessed = Number(txsProcessed);
  111.         lastTotalTxRejected = Number(txsRejected);
  112.         lastTotalTxExpired = Number(txsExpired);
  113.         lastAvgTps = Number(avgTps);
  114.         lastMaxTps = Number(maxTps);
  115.         lastRejTps = Number(rejTps);
  116.     }
  117.  
  118.     // json object that contains the network details      
  119.     const network_data: NetworkData = {
  120.         joining: joiningNodesCount,
  121.         joined: joinedNodesCount,
  122.         active: activeNodesCount,
  123.         total: joiningNodesCount + joinedNodesCount + activeNodesCount,
  124.         desired: modeDesiredNodes,
  125.         cycleMarker: activeNodesData[0].cycleMarker,
  126.         cycleCounter: activeNodesData[0].cycleCounter,
  127.         cycleDuration: cycleDurationAcquired,
  128.         txsProcessed: lastCycleTxsProcessed + lastTotalTxProcessed,
  129.         txsRejected: lastCycleTxsRejected + lastTotalTxRejected,
  130.         txsExpired: lastCycleTxsExpired + lastTotalTxExpired,
  131.         maxTps: lastMaxTps,
  132.         avgTps: lastAvgTps,
  133.         rejTps: lastRejTps,
  134.         netLoad: parseFloat((networkLoadTotal / activeNodesCount).toFixed(3)),
  135.         intReq: parseFloat((intLoadTotal / activeNodesCount).toFixed(3)),
  136.         extReq: parseFloat((extLoadTotal / activeNodesCount).toFixed(3)),
  137.         Qlen: parseFloat((totalQlen / activeNodesCount).toFixed(3)),
  138.         Qtime: parseFloat((totalQtime / activeNodesCount).toFixed(3)),
  139.     };
  140.     // json object that contains the chart details
  141.     const chart_data: ChartData = {
  142.         joined: joinedNodesCount,
  143.         active: activeNodesCount,
  144.         avgTps: network_data.avgTps,
  145.         rejTps: network_data.rejTps,
  146.         load: network_data.netLoad,
  147.         intLoad: network_data.intReq,
  148.         cycleDuration: network_data.cycleDuration,
  149.     };
  150.  
  151.     // updating avgTps and regTps
  152.     if (network_data.active != 0) {
  153.         let diffRatio = 0; // initialize the difference ratio variable
  154.  
  155.         // calculate the new average TPS based on the number of transactions processed during the last reporting interval
  156.         const newAvgTps = Math.round(lastCycleTxsProcessed / (reportInterval / 1000));
  157.  
  158.         // calculate the difference ratio between the new and previous average TPS
  159.         if (lastAvgTps > 0) {
  160.             diffRatio = (newAvgTps - lastAvgTps) / lastAvgTps;
  161.         }
  162.  
  163.         // check if the new average TPS is significantly higher or lower than the previous average TPS
  164.         if (diffRatio < 1.5 || diffRatio > 0.5) {
  165.             // check if the new average TPS is higher than the previous maximum TPS
  166.             if (newAvgTps > network_data.maxTps) {
  167.                 network_data.maxTps = newAvgTps; // update the maximum TPS value
  168.             }
  169.         }
  170.  
  171.         // update the average TPS value in the network data object
  172.         network_data.avgTps = newAvgTps;
  173.  
  174.         // calculate the new rejected TPS based on the number of transactions rejected during the last reporting interval
  175.         const rejectedTps = Math.round(lastCycleTxsRejected / (reportInterval / 1000));
  176.  
  177.         // update the rejected TPS value in the network data object
  178.         network_data.rejTps = rejectedTps;
  179.     }
  180.  
  181.     // inserting the network data into the network table along with a column for chart data
  182.     await insertReplace_network(
  183.         network_data.cycleCounter,
  184.         network_data,
  185.         chart_data,
  186.         Date.now()
  187.     );
  188.  
  189. }
  190. Please look at this
  191. //  example implementation in server.ts
  192. //  const renderer = new Renderer();
  193. //  setInterval(processRawHeartbeatAndStore(renderer), CONFIG.renderer.interval * 1000);
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement