{
  "id": null,
  "uid": "leaderboard-demo",
  "title": "Live Leaderboard — pg_trickle",
  "tags": ["pg_trickle", "leaderboard"],
  "timezone": "browser",
  "refresh": "5s",
  "time": { "from": "now-15m", "to": "now" },
  "schemaVersion": 38,
  "panels": [
    {
      "id": 1,
      "title": "Top Players by Total Score",
      "type": "table",
      "gridPos": { "h": 12, "w": 12, "x": 0, "y": 0 },
      "datasource": { "type": "postgres", "uid": "${DS_POSTGRESQL}" },
      "targets": [
        {
          "rawSql": "SELECT player_name, total_score, ROW_NUMBER() OVER (ORDER BY total_score DESC) AS rank FROM top_players ORDER BY total_score DESC LIMIT 20",
          "format": "table",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": { "custom": { "displayMode": "auto" } },
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "total_score" },
            "properties": [{ "id": "custom.displayMode", "value": "gradient-gauge" }, { "id": "min", "value": 0 }]
          },
          {
            "matcher": { "id": "byName", "options": "rank" },
            "properties": [{ "id": "custom.width", "value": 60 }]
          }
        ]
      },
      "options": { "sortBy": [{ "displayName": "rank", "desc": false }] }
    },
    {
      "id": 2,
      "title": "High Score by Game",
      "type": "bargauge",
      "gridPos": { "h": 12, "w": 12, "x": 12, "y": 0 },
      "datasource": { "type": "postgres", "uid": "${DS_POSTGRESQL}" },
      "targets": [
        {
          "rawSql": "SELECT 'Game ' || game_id AS game, high_score FROM scores_by_game ORDER BY high_score DESC",
          "format": "table",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": { "min": 0, "max": 1000, "color": { "mode": "continuous-GrYlRd" } }
      },
      "options": { "orientation": "horizontal", "reduceOptions": { "calcs": ["lastNotNull"] }, "displayMode": "gradient" }
    },
    {
      "id": 3,
      "title": "Active Players",
      "type": "stat",
      "gridPos": { "h": 4, "w": 6, "x": 0, "y": 12 },
      "datasource": { "type": "postgres", "uid": "${DS_POSTGRESQL}" },
      "targets": [
        {
          "rawSql": "SELECT COUNT(*) AS players FROM top_players",
          "format": "table",
          "refId": "A"
        }
      ],
      "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "thresholds": { "steps": [{ "color": "green", "value": 0 }] } } },
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] } }
    },
    {
      "id": 4,
      "title": "Stream Table Last Refresh",
      "type": "stat",
      "gridPos": { "h": 4, "w": 6, "x": 6, "y": 12 },
      "datasource": { "type": "postgres", "uid": "${DS_POSTGRESQL}" },
      "targets": [
        {
          "rawSql": "SELECT EXTRACT(EPOCH FROM (now() - last_refresh_at))::int AS seconds_ago FROM pgtrickle.pgt_stream_tables WHERE pgt_name = 'top_players'",
          "format": "table",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "s",
          "color": { "mode": "thresholds" },
          "thresholds": { "steps": [{ "color": "green", "value": 0 }, { "color": "yellow", "value": 10 }, { "color": "red", "value": 30 }] }
        }
      },
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] } }
    }
  ],
  "__inputs": [
    {
      "name": "DS_POSTGRESQL",
      "label": "PostgreSQL",
      "description": "",
      "type": "datasource",
      "pluginId": "postgres",
      "pluginName": "PostgreSQL"
    }
  ],
  "__requires": [
    { "type": "datasource", "id": "postgres", "name": "PostgreSQL", "version": "1.0.0" }
  ]
}
