{
  "license": "CC BY 4.0",
  "recipe": {
    "slug": "kubernetes-restart-rate-by-workload",
    "title": "Find Kubernetes Restarts by Workload",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-28"
  },
  "contract": {
    "tableName": "kubernetes_workload_events",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Sample or transition time in UTC."
      },
      {
        "name": "cluster",
        "type": "Utf8",
        "description": "Controlled cluster name."
      },
      {
        "name": "namespace",
        "type": "Utf8",
        "description": "Kubernetes namespace."
      },
      {
        "name": "workload",
        "type": "Utf8",
        "description": "Deployment, StatefulSet, or Job owner name."
      },
      {
        "name": "pod",
        "type": "Utf8",
        "description": "Pod identifier for restricted drill-downs."
      },
      {
        "name": "event_name",
        "type": "Utf8",
        "description": "Controlled sampled or container_restarted event."
      },
      {
        "name": "restart_count",
        "type": "Int64",
        "description": "Observed cumulative container restart count."
      },
      {
        "name": "ready",
        "type": "Boolean",
        "description": "Whether the pod was ready at the sample."
      },
      {
        "name": "environment",
        "type": "Utf8",
        "description": "Deployment environment."
      }
    ]
  },
  "query": "SELECT\n  cluster,\n  namespace,\n  workload,\n  COUNT(*) AS observations,\n  SUM(CASE WHEN event_name = 'container_restarted' THEN 1 ELSE 0 END) AS restart_events,\n  MAX(restart_count) AS max_restart_count,\n  SUM(CASE WHEN ready THEN 0 ELSE 1 END) AS not_ready_observations,\n  100.0 * SUM(CASE WHEN ready THEN 0 ELSE 1 END)\n    / NULLIF(COUNT(*), 0) AS not_ready_rate_pct\nFROM kubernetes_workload_events\nWHERE timestamp_utc >= now() - INTERVAL '24 hours'\n  AND environment = 'production'\nGROUP BY cluster, namespace, workload\nORDER BY restart_events DESC, not_ready_rate_pct DESC, workload;",
  "fixtureKind": "reproducible",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-1",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-2",
      "event_name": "container_restarted",
      "restart_count": 3,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-3",
      "event_name": "sampled",
      "restart_count": 3,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-4",
      "event_name": "container_restarted",
      "restart_count": 4,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:56:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-5",
      "event_name": "sampled",
      "restart_count": 4,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:30:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-1",
      "event_name": "sampled",
      "restart_count": 0,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:29:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-2",
      "event_name": "container_restarted",
      "restart_count": 1,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:28:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-3",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:27:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-4",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-1",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-2",
      "event_name": "container_restarted",
      "restart_count": 3,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-3",
      "event_name": "sampled",
      "restart_count": 3,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-4",
      "event_name": "container_restarted",
      "restart_count": 4,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:56:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "checkout-api",
      "pod": "checkout-api-5",
      "event_name": "sampled",
      "restart_count": 4,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:30:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-1",
      "event_name": "sampled",
      "restart_count": 0,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:29:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-2",
      "event_name": "container_restarted",
      "restart_count": 1,
      "ready": false,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:28:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-3",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:27:00.000Z",
      "cluster": "production-us",
      "namespace": "application",
      "workload": "billing-worker",
      "pod": "billing-worker-4",
      "event_name": "sampled",
      "restart_count": 2,
      "ready": true,
      "environment": "production"
    }
  ],
  "expectedOutput": {
    "columns": [
      "cluster",
      "namespace",
      "workload",
      "observations",
      "restart_events",
      "max_restart_count",
      "not_ready_observations",
      "not_ready_rate_pct"
    ],
    "rows": [
      {
        "cluster": "production-us",
        "namespace": "application",
        "workload": "checkout-api",
        "observations": 5,
        "restart_events": 2,
        "max_restart_count": 4,
        "not_ready_observations": 2,
        "not_ready_rate_pct": 40
      },
      {
        "cluster": "production-us",
        "namespace": "application",
        "workload": "billing-worker",
        "observations": 4,
        "restart_events": 1,
        "max_restart_count": 2,
        "not_ready_observations": 1,
        "not_ready_rate_pct": 25
      }
    ]
  },
  "notes": [
    "The included synthetic input rows generate the expected output when timestamps are shifted into the documented query window.",
    "Run this fixture in the public browser SQL playground; the selected query executes locally with DuckDB-Wasm.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}