{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "queue-wait-time-by-job",
    "title": "Measure Queue Wait Time by Job",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-27"
  },
  "contract": {
    "tableName": "job_runs",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Run completion time."
      },
      {
        "name": "job_name",
        "type": "Utf8",
        "description": "Stable logical job name."
      },
      {
        "name": "queue_wait_ms",
        "type": "Float64",
        "description": "Milliseconds from scheduled to started."
      },
      {
        "name": "duration_ms",
        "type": "Float64",
        "description": "Execution duration after start."
      }
    ]
  },
  "query": "SELECT\n  job_name,\n  COUNT(*) AS runs,\n  approx_percentile_cont(queue_wait_ms, 0.50) AS p50_wait_ms,\n  approx_percentile_cont(queue_wait_ms, 0.95) AS p95_wait_ms,\n  approx_percentile_cont(duration_ms, 0.95) AS p95_run_ms\nFROM job_runs\nWHERE timestamp_utc >= now() - INTERVAL '24 hours'\nGROUP BY job_name\nHAVING COUNT(*) >= 20\nORDER BY p95_wait_ms DESC;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "job_name": "job_name_example_1",
      "queue_wait_ms": 12,
      "duration_ms": 12
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "job_name": "job_name_example_2",
      "queue_wait_ms": 37,
      "duration_ms": 37
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "job_name": "job_name_example_3",
      "queue_wait_ms": 24,
      "duration_ms": 24
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "job_name": "job_name_example_1",
      "queue_wait_ms": 12,
      "duration_ms": 12
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "job_name": "job_name_example_2",
      "queue_wait_ms": 37,
      "duration_ms": 37
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "job_name": "job_name_example_3",
      "queue_wait_ms": 24,
      "duration_ms": 24
    }
  ],
  "expectedOutput": {
    "columns": [
      "job_name",
      "runs",
      "p50_wait_ms",
      "p95_wait_ms",
      "p95_run_ms"
    ],
    "rows": [
      {
        "job_name": "generate_report",
        "runs": 340,
        "p50_wait_ms": 1800,
        "p95_wait_ms": 44200,
        "p95_run_ms": 21800
      },
      {
        "job_name": "sync_subscription",
        "runs": 980,
        "p50_wait_ms": 420,
        "p95_wait_ms": 7200,
        "p95_run_ms": 8400
      },
      {
        "job_name": "send_email",
        "runs": 12400,
        "p50_wait_ms": 80,
        "p95_wait_ms": 460,
        "p95_run_ms": 930
      }
    ]
  },
  "notes": [
    "The compact input rows demonstrate field names and JSON types; they are not claimed to generate the displayed aggregate by themselves.",
    "The expected output is deterministic synthetic review data for the documented output shape.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}