{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": null,
  "links": [],
  "liveNow": true,
  "panels": [
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 10
              },
              {
                "color": "orange",
                "value": 30
              },
              {
                "color": "red",
                "value": 50
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 6,
        "w": 6,
        "x": 0,
        "y": 0
      },
      "id": 1,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COUNT(*) as count FROM races WHERE \"startTime\" > NOW() AND \"startTime\" < NOW() + INTERVAL '2 hours'",
          "refId": "A"
        }
      ],
      "title": "Upcoming Races (Next 2 Hours)",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 6,
        "w": 6,
        "x": 6,
        "y": 0
      },
      "id": 2,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus"
          },
          "expr": "sum(scheduler_currently_running)",
          "refId": "A"
        }
      ],
      "title": "Active Scrapes",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [
            {
              "options": {
                "0": {
                  "color": "red",
                  "text": "Never"
                }
              },
              "type": "value"
            }
          ],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "red",
                "value": null
              },
              {
                "color": "yellow",
                "value": 1
              },
              {
                "color": "green",
                "value": 300
              }
            ]
          },
          "unit": "dateTimeFromNow"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 6,
        "w": 6,
        "x": 12,
        "y": 0
      },
      "id": 3,
      "options": {
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus"
          },
          "expr": "scheduler_last_success_timestamp_seconds{schedule_type=\"morning_scrape\"} * 1000",
          "refId": "A"
        }
      ],
      "title": "Last Morning Scrape",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 5
              },
              {
                "color": "red",
                "value": 10
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 6,
        "w": 6,
        "x": 18,
        "y": 0
      },
      "id": 4,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus"
          },
          "expr": "count(race_data_freshness_seconds{freshness_status=\"missing\"})",
          "refId": "A"
        }
      ],
      "title": "Races Missing Data",
      "type": "stat"
    },
    {
      "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": [
          {
            "matcher": {
              "id": "byName",
              "options": "minutes_until_start"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "red",
                      "value": null
                    },
                    {
                      "color": "orange",
                      "value": 15
                    },
                    {
                      "color": "yellow",
                      "value": 60
                    },
                    {
                      "color": "green",
                      "value": 120
                    }
                  ]
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "data_age_minutes"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "green",
                      "value": null
                    },
                    {
                      "color": "yellow",
                      "value": 15
                    },
                    {
                      "color": "orange",
                      "value": 60
                    },
                    {
                      "color": "red",
                      "value": 120
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 12,
        "w": 24,
        "x": 0,
        "y": 6
      },
      "id": 5,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  r.name,\n  m.name as meeting_name,\n  r.\"startTime\",\n  ROUND(EXTRACT(EPOCH FROM (r.\"startTime\" - NOW())) / 60) as minutes_until_start,\n  r.country,\n  m.category,\n  r.status,\n  COUNT(ru.id) as runner_count,\n  COUNT(CASE WHEN ru.scratched THEN 1 END) as scratched_count,\n  MAX(s.\"scrapedAt\") as last_scraped,\n  ROUND(EXTRACT(EPOCH FROM (NOW() - MAX(s.\"scrapedAt\"))) / 60) as data_age_minutes\nFROM races r\nJOIN meetings m ON r.\"meetingId\" = m.id\nLEFT JOIN runners ru ON r.id = ru.\"raceId\"\nLEFT JOIN scrapes s ON r.id = s.\"raceId\"\nWHERE r.\"startTime\" > NOW() \n  AND r.\"startTime\" < NOW() + INTERVAL '2 hours'\nGROUP BY r.id, r.name, m.name, r.\"startTime\", r.country, m.category, r.status\nORDER BY r.\"startTime\" ASC",
          "refId": "A"
        }
      ],
      "title": "Upcoming Races - Next 2 Hours",
      "type": "table"
    },
    {
      "datasource": {
        "type": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "Seconds Since Last Scrape",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 10,
            "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": "none"
            },
            "thresholdsStyle": {
              "mode": "line"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 900
              },
              {
                "color": "red",
                "value": 3600
              }
            ]
          },
          "unit": "s"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 18
      },
      "id": 6,
      "options": {
        "legend": {
          "calcs": [
            "last",
            "max"
          ],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": {
          "mode": "multi",
          "sort": "none"
        }
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus"
          },
          "expr": "race_data_freshness_seconds",
          "legendFormat": "{{country}}_{{category}} - {{freshness_status}}",
          "refId": "A"
        }
      ],
      "title": "Race Data Freshness",
      "type": "timeseries"
    },
    {
      "datasource": {
        "type": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "hideFrom": {
              "tooltip": false,
              "viz": false,
              "legend": false
            }
          },
          "mappings": []
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 18
      },
      "id": 7,
      "options": {
        "displayLabels": [
          "name",
          "value"
        ],
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "showLegend": true,
          "values": [
            "value"
          ]
        },
        "pieType": "donut",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus"
          },
          "expr": "count by(freshness_status) (race_data_freshness_seconds)",
          "legendFormat": "{{freshness_status}}",
          "refId": "A"
        }
      ],
      "title": "Data Freshness Distribution",
      "type": "piechart"
    },
    {
      "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": [
          {
            "matcher": {
              "id": "byName",
              "options": "success"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "mappings",
                "value": [
                  {
                    "options": {
                      "false": {
                        "color": "red",
                        "text": "Failed"
                      },
                      "true": {
                        "color": "green",
                        "text": "Success"
                      }
                    },
                    "type": "value"
                  }
                ]
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 26
      },
      "id": 8,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  s.\"scrapedAt\",\n  s.\"scrapeType\",\n  r.name as race_name,\n  m.name as meeting_name,\n  r.\"startTime\",\n  s.success,\n  s.\"errorMessage\"\nFROM scrapes s\nJOIN races r ON s.\"raceId\" = r.id\nJOIN meetings m ON r.\"meetingId\" = m.id\nWHERE s.\"scrapedAt\" > NOW() - INTERVAL '1 hour'\nORDER BY s.\"scrapedAt\" DESC\nLIMIT 100",
          "refId": "A"
        }
      ],
      "title": "Recent Scrape Activity (Last Hour)",
      "type": "table"
    },
    {
      "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": [
          {
            "matcher": {
              "id": "byName",
              "options": "runner_count"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "green",
                      "value": null
                    },
                    {
                      "color": "yellow",
                      "value": 16
                    },
                    {
                      "color": "orange",
                      "value": 20
                    },
                    {
                      "color": "red",
                      "value": 24
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 34
      },
      "id": 9,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  r.name as race_name,\n  m.name as meeting_name,\n  r.\"startTime\",\n  COUNT(ru.id) as runner_count,\n  r.country,\n  m.category\nFROM races r\nJOIN meetings m ON r.\"meetingId\" = m.id\nLEFT JOIN runners ru ON r.id = ru.\"raceId\"\nWHERE r.\"startTime\" > NOW() \n  AND r.\"startTime\" < NOW() + INTERVAL '4 hours'\nGROUP BY r.id, r.name, m.name, r.\"startTime\", r.country, m.category\nHAVING COUNT(ru.id) >= 16\nORDER BY COUNT(ru.id) DESC, r.\"startTime\" ASC\nLIMIT 20",
          "refId": "A"
        }
      ],
      "title": "Large Fields Alert (16+ Runners)",
      "type": "table"
    },
    {
      "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": [
          {
            "matcher": {
              "id": "byName",
              "options": "minutes_since_scratched"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "red",
                      "value": null
                    },
                    {
                      "color": "orange",
                      "value": 30
                    },
                    {
                      "color": "yellow",
                      "value": 120
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 34
      },
      "id": 10,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  ru.name as runner_name,\n  r.name as race_name,\n  m.name as meeting_name,\n  r.\"startTime\",\n  ru.\"updatedAt\" as scratched_at,\n  ROUND(EXTRACT(EPOCH FROM (NOW() - ru.\"updatedAt\")) / 60) as minutes_since_scratched,\n  r.country,\n  m.category\nFROM runners ru\nJOIN races r ON ru.\"raceId\" = r.id\nJOIN meetings m ON r.\"meetingId\" = m.id\nWHERE ru.scratched = true\n  AND ru.\"updatedAt\" > NOW() - INTERVAL '4 hours'\n  AND r.\"startTime\" > NOW()\nORDER BY ru.\"updatedAt\" DESC\nLIMIT 30",
          "refId": "A"
        }
      ],
      "title": "Recent Scratchings (Last 4 Hours)",
      "type": "table"
    },
    {
      "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": [
          {
            "matcher": {
              "id": "byName",
              "options": "minutes_since_finish"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "yellow",
                      "value": null
                    },
                    {
                      "color": "orange",
                      "value": 15
                    },
                    {
                      "color": "red",
                      "value": 30
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 42
      },
      "id": 11,
      "options": {
        "cellHeight": "sm",
        "footer": {
          "countRows": false,
          "fields": "",
          "reducer": [
            "sum"
          ],
          "show": false
        },
        "showHeader": true
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT \n  r.name as race_name,\n  m.name as meeting_name,\n  r.\"startTime\",\n  ROUND(EXTRACT(EPOCH FROM (NOW() - r.\"startTime\")) / 60) as minutes_since_finish,\n  r.country,\n  m.category,\n  r.status\nFROM races r\nJOIN meetings m ON r.\"meetingId\" = m.id\nLEFT JOIN results res ON r.id = res.\"raceId\"\nWHERE r.\"startTime\" < NOW()\n  AND r.\"startTime\" > NOW() - INTERVAL '2 hours'\n  AND res.id IS NULL\nORDER BY r.\"startTime\" DESC\nLIMIT 30",
          "refId": "A"
        }
      ],
      "title": "Races Awaiting Results",
      "type": "table"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 5
              },
              {
                "color": "orange",
                "value": 10
              },
              {
                "color": "red",
                "value": 15
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 12,
        "y": 42
      },
      "id": 12,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COALESCE(COUNT(*), 0) as value\nFROM runners\nWHERE scratched = true\n  AND \"updatedAt\" > NOW() - INTERVAL '1 hour'",
          "refId": "A"
        }
      ],
      "title": "Scratchings (Last Hour)",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "grafana-postgresql-datasource"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "yellow",
                "value": 3
              },
              {
                "color": "orange",
                "value": 5
              },
              {
                "color": "red",
                "value": 10
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 6,
        "x": 18,
        "y": 42
      },
      "id": 13,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "textMode": "value"
      },
      "pluginVersion": "10.0.0",
      "targets": [
        {
          "datasource": {
            "type": "grafana-postgresql-datasource"
          },
          "format": "table",
          "rawSql": "SELECT COALESCE(COUNT(*), 0) as value\nFROM races r\nLEFT JOIN results res ON r.id = res.\"raceId\"\nWHERE r.\"startTime\" < NOW()\n  AND r.\"startTime\" > NOW() - INTERVAL '1 hour'\n  AND res.id IS NULL",
          "refId": "A"
        }
      ],
      "title": "Results Delayed (Last Hour)",
      "type": "stat"
    }
  ],
  "refresh": "30s",
  "schemaVersion": 38,
  "style": "dark",
  "tags": [
    "racing",
    "operations",
    "live"
  ],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-2h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "Racing Scraper - Live Operations",
  "uid": "racing-live-ops",
  "version": 1,
  "weekStart": ""
}
