#!/usr/bin/env python3
import json

dashboard = {
    "annotations": {"list": []},
    "editable": True,
    "fiscalYearStartMonth": 0,
    "graphTooltip": 0,
    "id": None,
    "links": [],
    "panels": [
        {
            "title": "Air Quality (PM2.5)",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "airquality") |> filter(fn: (r) => r["_field"] == "pm25_ugm3") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}
        },
        {
            "title": "Traffic Flow (Vehicles)",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "traffic") |> filter(fn: (r) => r["_field"] == "vehicle_count") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0}
        },
        {
            "title": "Parking Occupancy (%)",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "parking") |> filter(fn: (r) => r["_field"] == "occupancy_percent") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}
        },
        {
            "title": "Noise Levels (dB)",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "noise") |> filter(fn: (r) => r["_field"] == "noise_level_db") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8}
        },
        {
            "title": "Weather (Temperature °C)",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "weather") |> filter(fn: (r) => r["_field"] == "temperature_c") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 0, "y": 16}
        },
        {
            "title": "Light Levels",
            "type": "timeseries",
            "datasource": {"type": "influxdb", "uid": "influxdb-smartcity"},
            "targets": [{"query": 'from(bucket:"smartcity") |> range(start: v.timeRangeStart, stop:v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "light") |> filter(fn: (r) => r["_field"] == "luminosity") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean")'}],
            "gridPos": {"h": 8, "w": 12, "x": 12, "y": 16}
        }
    ],
    "schemaVersion": 36,
    "style": "dark",
    "tags": ["smartcity", "martinique", "iot"],
    "templating": {"list": []},
    "time": {"from": "now-1h", "to": "now"},
    "title": "Smart City Digital Twin - Martinique",
    "uid": "smartcity-martinique-v2",
    "version": 1
}

with open('/home/eric/smart-city-digital-twin-martinique/grafana-dashboard-smartcity.json', 'w') as f:
    json.dump(dashboard, f, indent=2)
print("Dashboard JSON created successfully")
