Files
Traffic-Intersection-Monito…/qt_app_pyside1/emergency_grafana_fix.py
2025-08-26 13:24:53 -07:00

310 lines
12 KiB
Python

"""
Emergency Grafana fix script - creates an automatic test dashboard
"""
import requests
import json
import time
from datetime import datetime
from influxdb_client import InfluxDBClient, Point
# Configuration
INFLUX_URL = "http://localhost:8086"
INFLUX_TOKEN = "kNFfXEpPQoWrk5Tteowda21Dzv6xD3jY7QHSHHQHb5oYW6VH6mkAgX9ZMjQJkaHHa8FwzmyVFqDG7qqzxN09uQ=="
INFLUX_ORG = "smart-intersection-org"
INFLUX_BUCKET = "traffic_monitoring"
GRAFANA_URL = "http://localhost:3000"
GRAFANA_USER = "admin"
GRAFANA_PASSWORD = "admin" # Default password, change if you've modified it
def create_test_points():
"""Create test points with timestamps in InfluxDB"""
print("\n===== WRITING TEST DATA TO INFLUXDB =====")
try:
client = InfluxDBClient(url=INFLUX_URL, token=INFLUX_TOKEN, org=INFLUX_ORG)
write_api = client.write_api()
# Write test performance data with timestamps spaced a few seconds apart
for i in range(20):
timestamp = datetime.utcnow()
# Performance metrics
perf_point = Point("performance") \
.tag("camera_id", "emergency_test") \
.field("fps", 25.0 + i/2) \
.field("processing_time_ms", 30.0 - i/4) \
.field("gpu_usage", 50.0 + i) \
.time(timestamp)
# Detection events
detect_point = Point("detection_events") \
.tag("camera_id", "emergency_test") \
.field("vehicle_count", 5 + (i % 3)) \
.field("pedestrian_count", 2 + (i % 2)) \
.time(timestamp)
# Violation events (every 3rd point)
if i % 3 == 0:
violation_point = Point("violation_events") \
.tag("camera_id", "emergency_test") \
.tag("violation_type", "red_light") \
.field("count", 1) \
.time(timestamp)
write_api.write(bucket=INFLUX_BUCKET, org=INFLUX_ORG, record=violation_point)
# Write the points
write_api.write(bucket=INFLUX_BUCKET, org=INFLUX_ORG, record=[perf_point, detect_point])
print(f"Written data point {i+1}/20")
time.sleep(0.1) # Small delay between writes
return True
except Exception as e:
print(f"❌ Error writing test data: {e}")
return False
def create_test_dashboard():
"""Create a test dashboard in Grafana"""
print("\n===== CREATING TEST DASHBOARD IN GRAFANA =====")
# Get InfluxDB data source ID
try:
# Get data sources
response = requests.get(
f"{GRAFANA_URL}/api/datasources",
auth=(GRAFANA_USER, GRAFANA_PASSWORD)
)
if response.status_code != 200:
print(f"❌ Failed to get data sources: {response.status_code}")
print(f"Response: {response.text}")
return False
data_sources = response.json()
influx_source = None
for source in data_sources:
if "influx" in source["name"].lower():
influx_source = source
break
if not influx_source:
print("❌ No InfluxDB data source found. Please add one first.")
return False
# Create the test dashboard
dashboard = {
"dashboard": {
"id": None,
"uid": "test-traffic-monitoring",
"title": "EMERGENCY TEST - Traffic Monitoring",
"tags": ["test", "traffic-monitoring"],
"timezone": "browser",
"refresh": "5s",
"time": {
"from": "now-15m",
"to": "now"
},
"panels": [
{
"id": 1,
"title": "Test FPS (Simple)",
"type": "stat",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"targets": [
{
"refId": "A",
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"query": f"from(bucket: \"{INFLUX_BUCKET}\")\n |> range(start: -15m)\n |> filter(fn: (r) => r._measurement == \"performance\" and r._field == \"fps\")\n |> last()"
}
],
"fieldConfig": {
"defaults": {
"color": {"mode": "thresholds"},
"thresholds": {
"steps": [
{"color": "red", "value": 0},
{"color": "yellow", "value": 15},
{"color": "green", "value": 25}
]
},
"unit": "fps"
}
}
},
{
"id": 2,
"title": "Test Processing Time (Simple)",
"type": "stat",
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 0},
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"targets": [
{
"refId": "A",
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"query": f"from(bucket: \"{INFLUX_BUCKET}\")\n |> range(start: -15m)\n |> filter(fn: (r) => r._measurement == \"performance\" and r._field == \"processing_time_ms\")\n |> last()"
}
],
"fieldConfig": {
"defaults": {
"color": {"mode": "thresholds"},
"thresholds": {
"steps": [
{"color": "green", "value": 0},
{"color": "yellow", "value": 70},
{"color": "red", "value": 90}
]
},
"unit": "ms"
}
}
},
{
"id": 3,
"title": "Vehicle Counts (Simple)",
"type": "timeseries",
"gridPos": {"h": 8, "w": 24, "x": 0, "y": 8},
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"targets": [
{
"refId": "A",
"datasource": {
"type": influx_source["type"],
"uid": influx_source["uid"]
},
"query": f"from(bucket: \"{INFLUX_BUCKET}\")\n |> range(start: -15m)\n |> filter(fn: (r) => r._measurement == \"detection_events\" and r._field == \"vehicle_count\")"
}
]
}
]
},
"folderUid": None,
"message": "Created emergency test dashboard",
"overwrite": True
}
response = requests.post(
f"{GRAFANA_URL}/api/dashboards/db",
auth=(GRAFANA_USER, GRAFANA_PASSWORD),
headers={"Content-Type": "application/json"},
data=json.dumps(dashboard)
)
if response.status_code != 200:
print(f"❌ Failed to create dashboard: {response.status_code}")
print(f"Response: {response.text}")
return False
result = response.json()
print(f"✅ Test dashboard created: {GRAFANA_URL}/d/{result['uid']}")
print(f"👉 Please open this URL in your browser: {GRAFANA_URL}/d/{result['uid']}")
return True
except Exception as e:
print(f"❌ Error creating test dashboard: {e}")
return False
def add_influxdb_datasource():
"""Add InfluxDB data source to Grafana if it doesn't exist"""
print("\n===== CHECKING/CREATING INFLUXDB DATA SOURCE =====")
try:
# Check if InfluxDB datasource exists
response = requests.get(
f"{GRAFANA_URL}/api/datasources",
auth=(GRAFANA_USER, GRAFANA_PASSWORD)
)
if response.status_code != 200:
print(f"❌ Failed to get data sources: {response.status_code}")
print(f"Response: {response.text}")
return False
data_sources = response.json()
influx_exists = False
for source in data_sources:
if "influx" in source["name"].lower():
print(f"✅ Found existing InfluxDB data source: {source['name']}")
influx_exists = True
break
if not influx_exists:
# Create InfluxDB data source
datasource = {
"name": "InfluxDB_Traffic",
"type": "influxdb",
"url": INFLUX_URL,
"access": "proxy",
"basicAuth": False,
"isDefault": True,
"jsonData": {
"defaultBucket": INFLUX_BUCKET,
"organization": INFLUX_ORG,
"version": "Flux"
},
"secureJsonData": {
"token": INFLUX_TOKEN
}
}
response = requests.post(
f"{GRAFANA_URL}/api/datasources",
auth=(GRAFANA_USER, GRAFANA_PASSWORD),
headers={"Content-Type": "application/json"},
data=json.dumps(datasource)
)
if response.status_code != 200:
print(f"❌ Failed to create data source: {response.status_code}")
print(f"Response: {response.text}")
return False
print("✅ Created new InfluxDB data source")
return True
except Exception as e:
print(f"❌ Error checking/creating data source: {e}")
return False
if __name__ == "__main__":
print("==== EMERGENCY GRAFANA FIX SCRIPT ====")
# Write test data to InfluxDB
data_success = create_test_points()
# Add/check InfluxDB data source in Grafana
source_success = add_influxdb_datasource()
# Create test dashboard
dashboard_success = create_test_dashboard()
print("\n===== SUMMARY =====")
print(f"Test Data Created: {'' if data_success else ''}")
print(f"Data Source Check: {'' if source_success else ''}")
print(f"Test Dashboard: {'' if dashboard_success else ''}")
if data_success and source_success and dashboard_success:
print("\n✅ Emergency fix completed successfully!")
print("Please check the test dashboard URL provided above.")
print("If you see data in the test dashboard but not in your original dashboard,")
print("the issue is likely with your original dashboard's queries.")
else:
print("\n❌ Some steps failed. Please check the errors above.")
print("Try running the script again or manually check your Grafana and InfluxDB setup.")