{
  "title": "TipSharks API",
  "version": 1,
  "schemaVersion": 36,
  "refresh": "30s",
  "timezone": "utc",
  "tags": ["tipsharks", "api", "racing"],
  "editable": true,
  "graphTooltip": 0,
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "panels": [
    {
      "title": "API Request Rate",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
      "id": 1,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "reqps",
          "min": 0,
          "custom": {
            "lineInterpolation": "smooth",
            "showPoints": "never",
            "spanNulls": true
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "rate(tipsharks_api_requests_total[1m])",
          "legendFormat": "Requests/s",
          "refId": "A"
        },
        {
          "expr": "rate(tipsharks_api_requests_total{status=~\"5..\"}[1m])",
          "legendFormat": "5xx/s",
          "refId": "B"
        }
      ],
      "description": "API request rate in requests per second. Track overall load and error rate."
    },
    {
      "title": "API Latency p95 / p99",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
      "id": 2,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "s",
          "min": 0,
          "custom": {
            "lineInterpolation": "smooth",
            "showPoints": "never",
            "spanNulls": true
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "histogram_quantile(0.95, rate(tipsharks_api_request_duration_seconds_bucket[5m]))",
          "legendFormat": "p95",
          "refId": "A"
        },
        {
          "expr": "histogram_quantile(0.99, rate(tipsharks_api_request_duration_seconds_bucket[5m]))",
          "legendFormat": "p99",
          "refId": "B"
        }
      ],
      "description": "API response latency percentiles. Alert if p95 exceeds 500ms."
    },
    {
      "title": "HTTP Status Code Distribution",
      "type": "piechart",
      "gridPos": { "h": 8, "w": 8, "x": 0, "y": 8 },
      "id": 3,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "custom": {
            "displayMode": "all"
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "displayMode": "table",
          "placement": "right"
        },
        "pieType": "pie",
        "reduceOptions": {
          "calcs": ["sum"],
          "values": false
        },
        "tooltip": {
          "mode": "single"
        }
      },
      "targets": [
        {
          "expr": "sum(rate(tipsharks_api_requests_total[5m])) by (status)",
          "legendFormat": "{{status}}",
          "refId": "A"
        }
      ],
      "description": "Distribution of HTTP response status codes over the last 5 minutes."
    },
    {
      "title": "Database Connection Pool Usage",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 8, "x": 8, "y": 8 },
      "id": 4,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "short",
          "min": 0,
          "custom": {
            "lineInterpolation": "smooth",
            "showPoints": "never",
            "spanNulls": true
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "tipsharks_db_pool_active_connections",
          "legendFormat": "Active",
          "refId": "A"
        },
        {
          "expr": "tipsharks_db_pool_idle_connections",
          "legendFormat": "Idle",
          "refId": "B"
        },
        {
          "expr": "tipsharks_db_pool_max_connections",
          "legendFormat": "Max",
          "refId": "C"
        }
      ],
      "description": "Database connection pool utilization. Active + Idle approaching Max indicates pool exhaustion."
    },
    {
      "title": "Ingestion Success Rate",
      "type": "stat",
      "gridPos": { "h": 4, "w": 4, "x": 16, "y": 8 },
      "id": 5,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "red", "value": null },
              { "color": "yellow", "value": 90 },
              { "color": "green", "value": 99 }
            ]
          },
          "unit": "percent"
        },
        "overrides": []
      },
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "auto"
      },
      "targets": [
        {
          "expr": "tipsharks_ingestion_success_total / (tipsharks_ingestion_success_total + tipsharks_ingestion_failure_total) * 100",
          "legendFormat": "Success Rate",
          "refId": "A"
        }
      ],
      "description": "Percentage of ingestion runs that completed successfully."
    },
    {
      "title": "Ingestion Failure Count (Last 24h)",
      "type": "stat",
      "gridPos": { "h": 4, "w": 4, "x": 20, "y": 8 },
      "id": 6,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "yellow", "value": 3 },
              { "color": "red", "value": 10 }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": ["lastNotNull"],
          "fields": ""
        },
        "textMode": "auto"
      },
      "targets": [
        {
          "expr": "increase(tipsharks_ingestion_failure_total[24h])",
          "legendFormat": "Failures",
          "refId": "A"
        }
      ],
      "description": "Number of ingestion failures in the last 24 hours."
    },
    {
      "title": "Rating Distribution (Latest Snapshot)",
      "type": "histogram",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 12 },
      "id": 7,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "custom": {
            "fillOpacity": 70,
            "lineWidth": 0
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "tipsharks_rating_distribution_bucket",
          "legendFormat": "Rating",
          "refId": "A",
          "format": "heatmap"
        }
      ],
      "description": "Histogram of current rating values across all entities. Helps detect rating inflation or deflation."
    },
    {
      "title": "Top 10 Entities by Rating",
      "type": "table",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 12 },
      "id": 8,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "custom": {
            "align": "left"
          },
          "mappings": []
        },
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "Rating" },
            "properties": [
              { "id": "custom.width", "value": 80 },
              { "id": "custom.displayMode", "value": "color-background" },
              { "id": "color", "value": { "mode": "thresholds" } },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    { "color": "green", "value": null },
                    { "color": "yellow", "value": 1500 },
                    { "color": "red", "value": 2000 }
                  ]
                }
              }
            ]
          }
        ]
      },
      "options": {
        "sortBy": [
          { "displayName": "Rating", "desc": true }
        ]
      },
      "targets": [
        {
          "expr": "topk(10, tipsharks_entity_rating{entity_type=\"horse\"})",
          "legendFormat": "{{name}}",
          "refId": "horses",
          "format": "table"
        },
        {
          "expr": "topk(10, tipsharks_entity_rating{entity_type=\"driver\"})",
          "legendFormat": "{{name}}",
          "refId": "drivers",
          "format": "table"
        },
        {
          "expr": "topk(10, tipsharks_entity_rating{entity_type=\"trainer\"})",
          "legendFormat": "{{name}}",
          "refId": "trainers",
          "format": "table"
        }
      ],
      "description": "Top 10 entities by current rating, grouped by type (horse, driver, trainer)."
    },
    {
      "title": "Winner Accuracy Trend",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 20 },
      "id": 9,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "percent",
          "min": 0,
          "max": 100,
          "custom": {
            "lineInterpolation": "smooth",
            "showPoints": "auto",
            "spanNulls": true
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": ["mean", "last"],
          "displayMode": "table",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "tipsharks_eval_winner_accuracy",
          "legendFormat": "Winner Accuracy",
          "refId": "A"
        }
      ],
      "description": "Winner prediction accuracy over evaluation windows. Higher is better."
    },
    {
      "title": "Brier Score Trend",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 20 },
      "id": 10,
      "datasource": {
        "type": "prometheus",
        "uid": "prometheus"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "none",
          "min": 0,
          "custom": {
            "lineInterpolation": "smooth",
            "showPoints": "auto",
            "spanNulls": true
          }
        },
        "overrides": []
      },
      "options": {
        "legend": {
          "calcs": ["mean", "last"],
          "displayMode": "table",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "multi"
        }
      },
      "targets": [
        {
          "expr": "tipsharks_eval_brier_score",
          "legendFormat": "Brier Score",
          "refId": "A"
        }
      ],
      "description": "Brier score (mean squared error of probability predictions). Lower is better."
    }
  ]
}
