#!/bin/bash

# Comprehensive Dashboard Section Verification Script
# Tests each dashboard and its individual sections/panels

GRAFANA_URL="http://localhost:3300"
GRAFANA_USER="admin"
GRAFANA_PASS="REDACTED_PASSWORD"

echo "=== Comprehensive Dashboard Section Verification ==="
echo "Grafana URL: $GRAFANA_URL"
echo

# Function to test a metric query
test_metric() {
    local metric="$1"
    local description="$2"
    local result=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASS" "$GRAFANA_URL/api/datasources/proxy/1/api/v1/query?query=$metric" | jq '.data.result | length')
    if [ "$result" -gt 0 ]; then
        echo "  ✅ $description: $result data points"
    else
        echo "  ❌ $description: No data"
    fi
}

# Function to test a dashboard's panels
test_dashboard_panels() {
    local uid="$1"
    local name="$2"
    echo
    echo "=== Testing $name Dashboard (UID: $uid) ==="

    # Get dashboard JSON
    local dashboard=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASS" "$GRAFANA_URL/api/dashboards/uid/$uid")
    local panel_count=$(echo "$dashboard" | jq '.dashboard.panels | length')
    echo "📊 Total panels: $panel_count"

    # Get template variables
    echo
    echo "🔧 Template Variables:"
    echo "$dashboard" | jq -r '.dashboard.templating.list[] | "  • \(.name): \(.current.text // "N/A")"'

    # Test some key metrics based on dashboard type
    echo
    echo "📈 Testing Key Metrics:"
}

# Test API connectivity
echo "1. Testing API connectivity..."
if curl -s -u "$GRAFANA_USER:$GRAFANA_PASS" "$GRAFANA_URL/api/health" | grep -q "ok"; then
    echo "✅ API connectivity: OK"
else
    echo "❌ API connectivity: FAILED"
    exit 1
fi

# Test data source
echo
echo "2. Testing Prometheus data source..."
PROMETHEUS_STATUS=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASS" "$GRAFANA_URL/api/datasources/1/health" | jq -r '.status')
echo "✅ Prometheus status: $PROMETHEUS_STATUS"

# Test Node Exporter Dashboard
test_dashboard_panels "rYdddlPWk" "Node Exporter Full"

# Test key Node Exporter metrics
test_metric "up%7Bjob%3D~%22.*-node%22%7D" "Node Exporter targets up"
test_metric "node_load1" "CPU Load (1m)"
test_metric "node_memory_MemAvailable_bytes" "Memory Available"
test_metric "node_filesystem_avail_bytes" "Filesystem Available"
test_metric "node_disk_io_time_seconds_total" "Disk I/O Time"
test_metric "node_network_receive_bytes_total" "Network Receive Bytes"
test_metric "node_cpu_seconds_total" "CPU Usage"
test_metric "node_boot_time_seconds" "Boot Time"

# Test Synology Dashboard
test_dashboard_panels "synology-dashboard-v2" "Synology NAS Monitoring"

# Test key Synology/SNMP metrics
test_metric "up%7Bjob%3D~%22.*-snmp%22%7D" "SNMP targets up"
test_metric "diskTemperature" "Disk Temperature"
test_metric "hrStorageSize" "Storage Size"
test_metric "hrStorageUsed" "Storage Used"
test_metric "sysUpTime" "System Uptime"

# Test Node Details Dashboard
test_dashboard_panels "node-details-v2" "Node Details"

# Test Infrastructure Overview Dashboard
test_dashboard_panels "infrastructure-overview-v2" "Infrastructure Overview"

echo
echo "=== Detailed Panel Testing ==="

# Test specific dashboard sections
echo
echo "🔍 Node Exporter Dashboard Sections:"
echo "  Testing CPU, Memory, Disk, Network, and System panels..."

# CPU metrics
test_metric "100%20-%20%28avg%20by%20%28instance%29%20%28irate%28node_cpu_seconds_total%7Bmode%3D%22idle%22%7D%5B5m%5D%29%29%20*%20100%29" "CPU Usage Percentage"

# Memory metrics
test_metric "%28node_memory_MemTotal_bytes%20-%20node_memory_MemAvailable_bytes%29%20/%20node_memory_MemTotal_bytes%20*%20100" "Memory Usage Percentage"

# Disk metrics
test_metric "100%20-%20%28node_filesystem_avail_bytes%20/%20node_filesystem_size_bytes%29%20*%20100" "Disk Usage Percentage"

# Network metrics
test_metric "irate%28node_network_receive_bytes_total%5B5m%5D%29" "Network Receive Rate"
test_metric "irate%28node_network_transmit_bytes_total%5B5m%5D%29" "Network Transmit Rate"

echo
echo "🔍 Synology Dashboard Sections:"
echo "  Testing Storage, Temperature, and System panels..."

# Storage metrics
test_metric "hrStorageUsed%20/%20hrStorageSize%20*%20100" "Storage Usage Percentage"

# Temperature metrics (if available)
test_metric "diskTemperature" "Disk Temperatures"

echo
echo "=== Target Health Summary ==="

# Get all targets and their health
echo "📡 All Prometheus Targets:"
curl -s -u "$GRAFANA_USER:$GRAFANA_PASS" "$GRAFANA_URL/api/datasources/proxy/1/api/v1/targets" | jq -r '.data.activeTargets[] | "  \(if .health == "up" then "✅" else "❌" end) \(.labels.job): \(.labels.instance // "N/A") (\(.health))"'

echo
echo "=== Dashboard URLs ==="
echo "🌐 Access your dashboards:"
echo "  • Node Exporter Full: $GRAFANA_URL/d/rYdddlPWk"
echo "  • Synology NAS: $GRAFANA_URL/d/synology-dashboard-v2"
echo "  • Node Details: $GRAFANA_URL/d/node-details-v2"
echo "  • Infrastructure Overview: $GRAFANA_URL/d/infrastructure-overview-v2"

echo
echo "=== Verification Complete ==="
echo "✅ All dashboard sections have been tested"
echo "📊 Check the results above for any issues"
echo "🔧 Template variables and data sources verified"
