{
  "__inputs": [
    {
      "name": "DS_CLICKHOUSE",
      "label": "ClickHouse",
      "description": "",
      "type": "datasource",
      "pluginId": "grafana-clickhouse-datasource",
      "pluginName": "ClickHouse"
    }
  ],
  "__requires": [
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "10.0.0"
    },
    {
      "type": "datasource",
      "id": "grafana-clickhouse-datasource",
      "name": "ClickHouse",
      "version": "4.0.0"
    },
    {
      "type": "panel",
      "id": "timeseries",
      "name": "Time series",
      "version": ""
    },
    {
      "type": "panel",
      "id": "stat",
      "name": "Stat",
      "version": ""
    },
    {
      "type": "panel",
      "id": "table",
      "name": "Table",
      "version": ""
    },
    {
      "type": "panel",
      "id": "barchart",
      "name": "Bar chart",
      "version": ""
    }
  ],
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "description": "One episode, from its primary events. Use it to explain a number the aggregates show and to check them against the stream. The person who acknowledged is not in the data: the envelope carries the actor kind, not an actor id.",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "links": [
    {
      "asDropdown": true,
      "icon": "external link",
      "includeVars": true,
      "keepTime": true,
      "tags": [
        "nxs-anomaly",
        "incident-analytics"
      ],
      "targetBlank": false,
      "title": "Incident analytics",
      "tooltip": "The other dashboards in this set",
      "type": "dashboards",
      "url": ""
    }
  ],
  "refresh": "5m",
  "schemaVersion": 39,
  "style": "dark",
  "tags": [
    "nxs-anomaly",
    "analytics",
    "clickhouse",
    "incident-analytics"
  ],
  "templating": {
    "list": [
      {
        "current": {},
        "hide": 0,
        "includeAll": false,
        "label": "Data source",
        "multi": false,
        "name": "DS_CLICKHOUSE",
        "options": [],
        "query": "grafana-clickhouse-datasource",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "type": "datasource"
      },
      {
        "current": {
          "text": "nxs_anomaly",
          "value": "nxs_anomaly"
        },
        "hide": 0,
        "label": "Database",
        "name": "db",
        "query": "nxs_anomaly",
        "skipUrlSync": false,
        "type": "constant",
        "description": "The analytics database — clickhouse.auth.database in the chart."
      },
      {
        "current": {
          "text": "All",
          "value": "All"
        },
        "datasource": {
          "type": "grafana-clickhouse-datasource",
          "uid": "${DS_CLICKHOUSE}"
        },
        "definition": "",
        "hide": 0,
        "includeAll": false,
        "label": "Team",
        "multi": false,
        "name": "team",
        "options": [],
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query",
        "query": {
          "refId": "team",
          "rawSql": "SELECT 'All' AS team UNION ALL SELECT DISTINCT team_id FROM $db.nxs_episode_fact WHERE team_id != ''",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        },
        "description": "Team scoping is off by default in the service, and events from before it was turned on carry no team. Those episodes sit under the empty team, not under All."
      },
      {
        "current": {
          "text": "All",
          "value": "All"
        },
        "datasource": {
          "type": "grafana-clickhouse-datasource",
          "uid": "${DS_CLICKHOUSE}"
        },
        "definition": "",
        "hide": 0,
        "includeAll": false,
        "label": "Service",
        "multi": false,
        "name": "service",
        "options": [],
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query",
        "query": {
          "refId": "service",
          "rawSql": "SELECT 'All' AS service UNION ALL SELECT DISTINCT service FROM $db.nxs_episode_fact WHERE service != ''",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        },
        "description": "Taken from the `service` label of the group's first firing alert. Groups whose alerts carried no such label are under 'unknown' — they are not spread across the real services."
      },
      {
        "current": {
          "text": "All",
          "value": "All"
        },
        "datasource": {
          "type": "grafana-clickhouse-datasource",
          "uid": "${DS_CLICKHOUSE}"
        },
        "definition": "",
        "hide": 0,
        "includeAll": false,
        "label": "Severity",
        "multi": false,
        "name": "severity",
        "options": [],
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query",
        "query": {
          "refId": "severity",
          "rawSql": "SELECT 'All' AS severity UNION ALL SELECT DISTINCT severity FROM $db.nxs_episode_fact WHERE severity != ''",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        },
        "description": "The severity of the opening event, falling back to the first firing alert. severity_origin on the episode says which was used."
      },
      {
        "current": {
          "text": "",
          "value": ""
        },
        "hide": 0,
        "label": "Group id",
        "name": "group",
        "query": "",
        "skipUrlSync": false,
        "type": "textbox",
        "description": "The alert_group_id to explore. Paste it from the table at the top, or from the group's URL in the product."
      },
      {
        "current": {
          "text": "All",
          "value": "All"
        },
        "datasource": {
          "type": "grafana-clickhouse-datasource",
          "uid": "${DS_CLICKHOUSE}"
        },
        "definition": "",
        "hide": 0,
        "includeAll": false,
        "label": "Episode",
        "multi": false,
        "name": "episode",
        "options": [],
        "refresh": 2,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query",
        "query": {
          "refId": "episode",
          "rawSql": "SELECT 'All' AS episode UNION ALL SELECT DISTINCT episode_id FROM $db.nxs_episode_fact WHERE alert_group_id = '$group'",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        },
        "description": "Which pass at the group. Refreshes when the group id changes."
      }
    ]
  },
  "time": {
    "from": "now-30d",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "utc",
  "title": "09 · Episode explorer",
  "uid": "nxs-anomaly-09-episode-explorer",
  "version": 1,
  "weekStart": "",
  "panels": [
    {
      "id": 1,
      "type": "row",
      "title": "Pick an episode, then read its primary events",
      "collapsed": false,
      "panels": [],
      "gridPos": {
        "h": 1,
        "w": 24,
        "x": 0,
        "y": 0
      }
    },
    {
      "id": 2,
      "type": "table",
      "title": "Episodes in range — copy a group id into the variable above",
      "description": "Start here. The group and episode variables at the top drive every panel below.",
      "datasource": {
        "type": "grafana-clickhouse-datasource",
        "uid": "${DS_CLICKHOUSE}"
      },
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 1
      },
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "options": {},
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource",
            "uid": "${DS_CLICKHOUSE}"
          },
          "rawSql": "SELECT alert_group_id, episode_id, alertname, service, severity, opened_at, round(mtta_seconds) AS ack_s, round(mttr_seconds) AS close_s, outcome, escalation_steps AS steps, delivery_attempts FROM $db.nxs_episode_fact WHERE $__timeFilter(opened_at) AND ($team = 'All' OR team_id = $team) AND ($service = 'All' OR service = $service) AND ($severity = 'All' OR severity = $severity) ORDER BY opened_at DESC LIMIT 100",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        }
      ]
    },
    {
      "id": 3,
      "type": "table",
      "title": "The episode",
      "description": "Its whole computed shape, including the flags that say whether it can be trusted: has_opening, purpose_known, has_negative_interval, source_time_after_open.",
      "datasource": {
        "type": "grafana-clickhouse-datasource",
        "uid": "${DS_CLICKHOUSE}"
      },
      "gridPos": {
        "h": 6,
        "w": 24,
        "x": 0,
        "y": 9
      },
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "options": {},
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource",
            "uid": "${DS_CLICKHOUSE}"
          },
          "rawSql": "SELECT * FROM $db.nxs_episode_fact WHERE alert_group_id = '$group' AND ($episode = 'All' OR episode_id = $episode)",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        }
      ]
    },
    {
      "id": 4,
      "type": "table",
      "title": "Every event of this group, in order",
      "description": "The primary record the aggregates are computed from. occurred_at is when the transition happened, published_at when the outbox row was written, ingested_at when Vector shaped the row — none of them is a Kafka or ClickHouse commit time. The raw payload is deliberately not shown: the legacy alert snapshot does not pass the same redaction as the lifecycle data.",
      "datasource": {
        "type": "grafana-clickhouse-datasource",
        "uid": "${DS_CLICKHOUSE}"
      },
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 15
      },
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "options": {},
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource",
            "uid": "${DS_CLICKHOUSE}"
          },
          "rawSql": "SELECT occurred_at, event_type, episode_id, notification_id, user_id, JSONExtractString(payload, 'data', 'attempt_status') AS attempt_status, JSONExtractString(payload, 'data', 'purpose') AS purpose, JSONExtractString(payload, 'data', 'actor_kind') AS actor_kind, JSONExtractString(payload, 'data', 'resolution') AS resolution, JSONExtractString(payload, 'data', 'reason') AS reason, JSONExtractInt(payload, 'data', 'step') AS step, published_at, ingested_at FROM $db.nxs_events_raw_deduplicated WHERE alert_group_id = '$group' ORDER BY occurred_at ASC, event_id ASC LIMIT 500",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        }
      ]
    },
    {
      "id": 5,
      "type": "table",
      "title": "Its delivery attempts",
      "description": "Every provider call for this group, with what it did and how long it took.",
      "datasource": {
        "type": "grafana-clickhouse-datasource",
        "uid": "${DS_CLICKHOUSE}"
      },
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 0,
        "y": 25
      },
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "options": {},
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource",
            "uid": "${DS_CLICKHOUSE}"
          },
          "rawSql": "SELECT occurred_at, episode_id, notification_id, user_id, channel, purpose, attempt, attempt_status, notification_status, provider_status, provider_code, duration_ms, round(wait_seconds, 1) AS wait_s, error_class FROM $db.nxs_delivery_attempts WHERE alert_group_id = '$group' ORDER BY occurred_at ASC LIMIT 200",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        }
      ]
    },
    {
      "id": 6,
      "type": "table",
      "title": "The other episodes of this group",
      "description": "The passes before and after this one. An episode with a successor and no resolution is superseded, not abandoned.",
      "datasource": {
        "type": "grafana-clickhouse-datasource",
        "uid": "${DS_CLICKHOUSE}"
      },
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 12,
        "y": 25
      },
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "options": {},
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource",
            "uid": "${DS_CLICKHOUSE}"
          },
          "rawSql": "SELECT episode_id, opened_at, resolved_at, next_episode_opened_at, superseded, is_reopen, reopen_reason, outcome, round(mtta_seconds) AS ack_s FROM $db.nxs_episode_fact WHERE alert_group_id = '$group' ORDER BY opened_at ASC",
          "format": 1,
          "queryType": "sql",
          "editorType": "sql"
        }
      ]
    }
  ]
}
