{
  "uid": "gmlm-platform-overview",
  "title": "GMLM Platform Overview",
  "description": "Key metrics for the GMLM SaaS platform: response times, queue depths, database, Redis, financial activity",
  "tags": ["gmlm", "platform", "overview"],
  "timezone": "browser",
  "refresh": "30s",
  "time": { "from": "now-3h", "to": "now" },
  "schemaVersion": 38,

  "templating": {
    "list": [
      {
        "name": "deployment",
        "label": "Deployment",
        "type": "query",
        "datasource": { "type": "prometheus", "uid": "prometheus" },
        "query": "label_values(up, deployment)",
        "refresh": 2,
        "sort": 1,
        "current": {}
      }
    ]
  },

  "panels": [

    {
      "id": 1,
      "title": "Platform Status",
      "type": "stat",
      "gridPos": { "h": 3, "w": 4, "x": 0, "y": 0 },
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "textMode": "value_and_name",
        "reduceOptions": { "calcs": ["lastNotNull"] }
      },
      "fieldConfig": {
        "defaults": {
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "red",   "value": null },
              { "color": "green", "value": 1 }
            ]
          },
          "mappings": [
            { "options": { "0": { "text": "DOWN" }, "1": { "text": "HEALTHY" } }, "type": "value" }
          ]
        }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "up{job=\"gmlm_app\",deployment=\"$deployment\"}",
          "legendFormat": "Application"
        }
      ]
    },

    {
      "id": 2,
      "title": "P95 Response Time",
      "type": "stat",
      "gridPos": { "h": 3, "w": 4, "x": 4, "y": 0 },
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "reduceOptions": { "calcs": ["lastNotNull"] }
      },
      "fieldConfig": {
        "defaults": {
          "unit": "ms",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green",  "value": null },
              { "color": "yellow", "value": 500 },
              { "color": "red",    "value": 2000 }
            ]
          }
        }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{deployment=\"$deployment\"}[5m])) by (le)) * 1000",
          "legendFormat": "P95"
        }
      ]
    },

    {
      "id": 3,
      "title": "Active Members",
      "type": "stat",
      "gridPos": { "h": 3, "w": 4, "x": 8, "y": 0 },
      "options": { "colorMode": "none", "graphMode": "area", "reduceOptions": { "calcs": ["lastNotNull"] } },
      "fieldConfig": { "defaults": { "unit": "short", "color": { "mode": "thresholds" } } },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "gmlm_active_members{deployment=\"$deployment\"}",
          "legendFormat": "Active Members"
        }
      ]
    },

    {
      "id": 4,
      "title": "Pending Queue Jobs",
      "type": "stat",
      "gridPos": { "h": 3, "w": 4, "x": 12, "y": 0 },
      "options": { "colorMode": "background", "graphMode": "area", "reduceOptions": { "calcs": ["lastNotNull"] } },
      "fieldConfig": {
        "defaults": {
          "unit": "short",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green",  "value": null },
              { "color": "yellow", "value": 500 },
              { "color": "red",    "value": 2000 }
            ]
          }
        }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "gmlm_queue_pending_jobs{deployment=\"$deployment\"}",
          "legendFormat": "Pending Jobs"
        }
      ]
    },

    {
      "id": 5,
      "title": "Redis Cache Hit Rate",
      "type": "gauge",
      "gridPos": { "h": 3, "w": 4, "x": 16, "y": 0 },
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "horizontal" },
      "fieldConfig": {
        "defaults": {
          "unit": "percentunit",
          "min": 0, "max": 1,
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "red",    "value": null },
              { "color": "yellow", "value": 0.7 },
              { "color": "green",  "value": 0.9 }
            ]
          }
        }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "rate(redis_keyspace_hits_total[5m]) / (rate(redis_keyspace_hits_total[5m]) + rate(redis_keyspace_misses_total[5m]))",
          "legendFormat": "Hit Rate"
        }
      ]
    },

    {
      "id": 6,
      "title": "HTTP Request Rate (req/min)",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 },
      "options": { "legend": { "displayMode": "table", "placement": "bottom" }, "tooltip": { "mode": "multi" } },
      "fieldConfig": {
        "defaults": { "unit": "reqpm", "custom": { "lineWidth": 2, "fillOpacity": 10 } },
        "overrides": [
          { "matcher": { "id": "byName", "options": "5xx" }, "properties": [ { "id": "color", "value": { "fixedColor": "red", "mode": "fixed" } } ] },
          { "matcher": { "id": "byName", "options": "4xx" }, "properties": [ { "id": "color", "value": { "fixedColor": "yellow", "mode": "fixed" } } ] }
        ]
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum(rate(http_requests_total{status=~\"2..\",deployment=\"$deployment\"}[1m])) * 60",
          "legendFormat": "2xx"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum(rate(http_requests_total{status=~\"4..\",deployment=\"$deployment\"}[1m])) * 60",
          "legendFormat": "4xx"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "sum(rate(http_requests_total{status=~\"5..\",deployment=\"$deployment\"}[1m])) * 60",
          "legendFormat": "5xx"
        }
      ]
    },

    {
      "id": 7,
      "title": "Response Time Percentiles",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 },
      "fieldConfig": {
        "defaults": { "unit": "ms", "custom": { "lineWidth": 2 } }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.50, sum(rate(http_request_duration_seconds_bucket{deployment=\"$deployment\"}[5m])) by (le)) * 1000",
          "legendFormat": "P50"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{deployment=\"$deployment\"}[5m])) by (le)) * 1000",
          "legendFormat": "P95"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket{deployment=\"$deployment\"}[5m])) by (le)) * 1000",
          "legendFormat": "P99"
        }
      ]
    },

    {
      "id": 8,
      "title": "MySQL: Threads & Slow Queries",
      "type": "timeseries",
      "gridPos": { "h": 6, "w": 12, "x": 0, "y": 11 },
      "fieldConfig": { "defaults": { "custom": { "lineWidth": 2 } } },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "mysql_global_status_threads_connected",
          "legendFormat": "Threads Connected"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "rate(mysql_global_status_slow_queries[5m]) * 60",
          "legendFormat": "Slow Queries/min"
        }
      ]
    },

    {
      "id": 9,
      "title": "Redis Memory",
      "type": "timeseries",
      "gridPos": { "h": 6, "w": 12, "x": 12, "y": 11 },
      "fieldConfig": { "defaults": { "unit": "bytes", "custom": { "lineWidth": 2 } } },
      "targets": [
        {
          "datasource": { "type": "prometheus" },
          "expr": "redis_memory_used_bytes",
          "legendFormat": "Used"
        },
        {
          "datasource": { "type": "prometheus" },
          "expr": "redis_memory_max_bytes",
          "legendFormat": "Max Limit"
        }
      ]
    },

    {
      "id": 10,
      "title": "Security Events (Loki)",
      "type": "logs",
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 17 },
      "options": {
        "showTime":        true,
        "showLabels":      true,
        "sortOrder":       "Descending",
        "dedupStrategy":   "none",
        "wrapLogMessage":  false
      },
      "targets": [
        {
          "datasource": { "type": "loki" },
          "expr": "{channel=\"security\", deployment=\"$deployment\"} |= ``",
          "legendFormat": ""
        }
      ]
    }
  ]
}
