{
  "annotations": {
    "list": []
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "links": [],
  "liveNow": true,
  "panels": [
    {
      "collapsed": false,
      "gridPos": {
        "h": 1,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 100,
      "panels": [],
      "title": "System Heartbeat",
      "type": "row"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [
            {
              "options": {
                "0": {
                  "color": "red",
                  "index": 0,
                  "text": "INACTIVE"
                },
                "1": {
                  "color": "green",
                  "index": 1,
                  "text": "ACTIVE"
                }
              },
              "type": "value"
            }
          ],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "red",
                "value": null
              },
              {
                "color": "green",
                "value": 1
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 4,
        "w": 4,
        "x": 0,
        "y": 1
      },
      "id": 1,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END as value FROM job_runs WHERE \"startedAt\" > NOW() - INTERVAL '5 minutes'",
          "refId": "A"
        }
      ],
      "title": "System Status",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unit": "dateTimeAsIso"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 4,
        "w": 5,
        "x": 4,
        "y": 1
      },
      "id": 2,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COALESCE(EXTRACT(EPOCH FROM MAX(\"startedAt\")) * 1000, 0) as value FROM job_runs WHERE status = 'success'",
          "refId": "A"
        }
      ],
      "title": "Last Successful Job",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "yellow",
                "value": null
              },
              {
                "color": "green",
                "value": 10
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 9,
        "y": 1
      },
      "id": 3,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM job_runs WHERE \"startedAt\" > NOW() - INTERVAL '1 hour'",
          "refId": "A"
        }
      ],
      "title": "Job Runs (1h)",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 1
              },
              {
                "color": "red",
                "value": 5
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 12,
        "y": 1
      },
      "id": 4,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM job_runs WHERE status = 'failed' AND \"startedAt\" > NOW() - INTERVAL '1 hour'",
          "refId": "A"
        }
      ],
      "title": "Failed Jobs (1h)",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "max": 100,
          "min": 0,
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "red",
                "value": null
              },
              {
                "color": "yellow",
                "value": 90
              },
              {
                "color": "green",
                "value": 98
              }
            ]
          },
          "unit": "percent"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 4,
        "w": 9,
        "x": 15,
        "y": 1
      },
      "id": 5,
      "options": {
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "showThresholdLabels": false,
        "showThresholdMarkers": true
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT ROUND(100.0 * SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) / NULLIF(COUNT(*), 0), 2) as value FROM job_runs WHERE \"startedAt\" > NOW() - INTERVAL '24 hours'",
          "refId": "A"
        }
      ],
      "title": "Success Rate (24h)",
      "type": "gauge"
    },
    {
      "collapsed": false,
      "gridPos": {
        "h": 1,
        "w": 24,
        "x": 0,
        "y": 5
      },
      "id": 101,
      "panels": [],
      "title": "Job Activity",
      "type": "row"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "bars",
            "fillOpacity": 80,
            "gradientMode": "none",
            "hideFrom": {
              "tooltip": false,
              "viz": false,
              "legend": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "never",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "normal"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 6
      },
      "id": 6,
      "options": {
        "legend": {
          "calcs": ["sum"],
          "displayMode": "table",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "multi",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "time_series",
          "rawSql": "SELECT \n  date_trunc('minute', \"startedAt\") as time,\n  \"jobType\" as metric,\n  COUNT(*) as value\nFROM job_runs\nWHERE \"startedAt\" > NOW() - INTERVAL '6 hours'\nGROUP BY time, \"jobType\"\nORDER BY time",
          "refId": "A"
        }
      ],
      "title": "Job Executions by Type (6h)",
      "type": "timeseries"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "custom": {
            "align": "auto",
            "cellOptions": {
              "type": "auto"
            },
            "inspect": false
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 6
      },
      "id": 7,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": ["sum"],
          "show": false
        },
        "showHeader": true
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  \"jobType\",\n  COUNT(*) as runs,\n  SUM(\"itemsProcessed\") as items_processed,\n  ROUND(AVG(\"durationMs\") / 1000.0, 2) as avg_duration_sec,\n  SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as successful\nFROM job_runs\nWHERE \"startedAt\" > NOW() - INTERVAL '1 hour'\nGROUP BY \"jobType\"\nORDER BY runs DESC",
          "refId": "A"
        }
      ],
      "title": "Job Performance (Last Hour)",
      "type": "table"
    },
    {
      "collapsed": false,
      "gridPos": {
        "h": 1,
        "w": 24,
        "x": 0,
        "y": 14
      },
      "id": 102,
      "panels": [],
      "title": "Database Inventory",
      "type": "row"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 0,
        "y": 15
      },
      "id": 8,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM meetings",
          "refId": "A"
        }
      ],
      "title": "Total Meetings",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 3,
        "y": 15
      },
      "id": 9,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM races",
          "refId": "A"
        }
      ],
      "title": "Total Races",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 6,
        "y": 15
      },
      "id": 10,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM runners",
          "refId": "A"
        }
      ],
      "title": "Total Runners",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 9,
        "y": 15
      },
      "id": 11,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM results",
          "refId": "A"
        }
      ],
      "title": "Total Results",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 12,
        "y": 15
      },
      "id": 12,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as value FROM dividends",
          "refId": "A"
        }
      ],
      "title": "Total Dividends",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 15,
        "y": 15
      },
      "id": 13,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(DISTINCT \"horseId\") as value FROM runners WHERE \"horseId\" IS NOT NULL",
          "refId": "A"
        }
      ],
      "title": "Unique Horses",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 18,
        "y": 15
      },
      "id": 14,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(DISTINCT \"jockeyId\") as value FROM runners WHERE \"jockeyId\" IS NOT NULL",
          "refId": "A"
        }
      ],
      "title": "Unique Jockeys",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 3,
        "w": 3,
        "x": 21,
        "y": 15
      },
      "id": 15,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "center",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "value"
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(DISTINCT \"trainerId\") as value FROM runners WHERE \"trainerId\" IS NOT NULL",
          "refId": "A"
        }
      ],
      "title": "Unique Trainers",
      "type": "stat"
    },
    {
      "collapsed": false,
      "gridPos": {
        "h": 1,
        "w": 24,
        "x": 0,
        "y": 18
      },
      "id": 103,
      "panels": [],
      "title": "Processing Pipeline Distribution",
      "type": "row"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "hideFrom": {
              "tooltip": false,
              "viz": false,
              "legend": false
            }
          },
          "mappings": []
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 8,
        "x": 0,
        "y": 19
      },
      "id": 16,
      "options": {
        "displayLabels": ["name", "percent"],
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "showLegend": true,
          "values": []
        },
        "pieType": "donut",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT status as metric, COUNT(*) as value FROM races GROUP BY status",
          "refId": "A"
        }
      ],
      "title": "Races by Status",
      "type": "piechart"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "hideFrom": {
              "tooltip": false,
              "viz": false,
              "legend": false
            }
          },
          "mappings": []
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 8,
        "x": 8,
        "y": 19
      },
      "id": 17,
      "options": {
        "displayLabels": ["name", "percent"],
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "showLegend": true,
          "values": []
        },
        "pieType": "donut",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT m.category as metric, COUNT(r.id) as value FROM races r JOIN meetings m ON r.\"meetingId\" = m.id GROUP BY m.category",
          "refId": "A"
        }
      ],
      "title": "Races by Category",
      "type": "piechart"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "hideFrom": {
              "tooltip": false,
              "viz": false,
              "legend": false
            }
          },
          "mappings": []
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 8,
        "x": 16,
        "y": 19
      },
      "id": 18,
      "options": {
        "displayLabels": ["name", "percent"],
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "showLegend": true,
          "values": []
        },
        "pieType": "donut",
        "reduceOptions": {
          "values": false,
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT country as metric, COUNT(*) as value FROM races GROUP BY country",
          "refId": "A"
        }
      ],
      "title": "Races by Country",
      "type": "piechart"
    }
  ],
  "refresh": "30s",
  "schemaVersion": 38,
  "style": "dark",
  "tags": ["system", "operations", "monitoring"],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "Pacific/Auckland",
  "title": "TAB API - Operations Health",
  "uid": "operations-health",
  "version": 1,
  "weekStart": ""
}
