Skip to content

Commit

Permalink
Adds analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Oct 21, 2023
1 parent 6c9d303 commit c385f50
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .devnet/analytics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Get the directory of the bash script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Navigate to the directory containing the JavaScript program
cd "$SCRIPT_DIR/analytics"

# Check if the 'node_modules' directory exists
if [ ! -d "node_modules" ]; then
echo "Node.js dependencies not found. Running 'npm install'..."
npm install
else
echo "Node.js dependencies already installed."
fi

# Call the JavaScript program using Node.js
npm start
75 changes: 75 additions & 0 deletions .devnet/analytics/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');
const axios = require('axios');

// Read the ~/.ssh/config file
const sshConfigFile = fs.readFileSync(`${process.env.HOME}/.ssh/config`, 'utf8');

// Define the AWS node name to search for (e.g., aws-n0)
const awsNodeName = 'aws-n0';

// Use regular expressions to extract the IP address associated with aws-n0
const regex = new RegExp(`Host\\s+${awsNodeName}[\\s\\S]*?HostName\\s+(\\S+)`);
const match = sshConfigFile.match(regex);

if (match && match[1]) {
const ipAddress = match[1];
const baseUrl = `http://${ipAddress}:3033/testnet3/block`;

console.log(`IP Address: ${ipAddress}`);
console.log(`Base URL: ${baseUrl}`);

// Function to get the latest block height
async function getLatestBlockHeight() {
try {
const response = await axios.get(`${baseUrl}/height/latest`);
const latestHeight = response.data;
console.log(`Latest Block Height: ${latestHeight}`);
return latestHeight;
} catch (error) {
console.error('Error fetching latest block height:', error.message);
return null;
}
}

// Function to calculate the average block time
async function calculateAverageBlockTime() {
const latestHeight = await getLatestBlockHeight();
if (latestHeight === null) {
return;
}

let totalBlockTime = 0;
let previousTimestamp = 0;

for (let height = 1; height <= latestHeight; height++) {
// Print the current height every 10 blocks
if (height % 10 === 0) {
console.log(`Processed ${height} blocks...`);
}

try {
const response = await axios.get(`${baseUrl}/${height}`);
const timestamp = response.data.header.metadata.timestamp;

if (timestamp && timestamp > 0) {
if (previousTimestamp > 0) {
const deltaTimestamp = timestamp - previousTimestamp;
console.log(`Block ${height} Delta Timestamp: ${deltaTimestamp}`);
totalBlockTime += deltaTimestamp;
}
previousTimestamp = timestamp;
}
} catch (error) {
console.error(`Error fetching block at height ${height}:`, error.message);
}
}

const averageBlockTime = totalBlockTime / (latestHeight - 1); // Subtract 1 for the first block
console.log(`Average Block Time: ${averageBlockTime} seconds`);
}

// Calculate and output the average block time
calculateAverageBlockTime();
} else {
console.error(`No IP address found for ${awsNodeName} in ~/.ssh/config`);
}
15 changes: 15 additions & 0 deletions .devnet/analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "analytics",
"version": "1.0.0",
"description": "",
"main": "analytics.js",
"scripts": {
"start": "node analytics.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "The Aleo Team",
"license": "Apache 2.0",
"dependencies": {
"axios": "^1.5.1"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ wasm/Cargo.lock
validator-*
**.bft-storage-*/
**proptest-regressions/
**package-lock.json
**node_modules/

0 comments on commit c385f50

Please sign in to comment.