/ .devnet / analytics.sh
analytics.sh
 1  #!/bin/bash
 2  
 3  # Get the directory of the bash script
 4  SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 5  
 6  # Navigate to the directory containing the JavaScript program
 7  cd "$SCRIPT_DIR/.analytics"
 8  
 9  # Check if the 'node_modules' directory exists
10  if [ ! -d "node_modules" ]; then
11    echo "Node.js dependencies not found. Running 'npm install'..."
12    npm install
13  else
14    echo "Node.js dependencies already installed."
15  fi
16  
17  # Prompt the user to specify a network ID
18  while true; do
19    echo "Please specify a network ID (0 for mainnet, 1 for testnet, 2 for canary):"
20    read networkID
21    if [[ $networkID == 0 || $networkID == 1 || $networkID == 2 ]]; then
22      break
23    else
24      echo "Invalid network ID. Please enter 0 or 1."
25    fi
26  done
27  
28  # Prompt the user to select a metric type
29  PS3="Select a metric type: "
30  options=("Average Block Time" "Rounds in Blocks" "Check Block Hash" "Quit")
31  select opt in "${options[@]}"
32  do
33    case $opt in
34      "Average Block Time")
35        echo ""
36        node analytics.js --metric-type averageBlockTime --network-id $networkID
37        break
38        ;;
39      "Rounds in Blocks")
40        echo ""
41        node analytics.js --metric-type roundsInBlocks --network-id $networkID
42        break
43        ;;
44      "Check Block Hash")
45        echo "You selected 'Check Block Hash'. Please enter the block height:"
46        read blockHeight
47        echo ""
48        # Validate input is an integer
49        if ! [[ "$blockHeight" =~ ^[0-9]+$ ]]; then
50          echo "Error: Block height must be a positive integer."
51          exit 1
52        fi
53        node analytics.js --metric-type checkBlockHash --block-height "$blockHeight" --network-id $networkID
54        break
55        ;;
56      "Quit")
57        echo "Quitting..."
58        break
59        ;;
60      *) echo "Invalid option";;
61    esac
62  done