{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "api-error-budget-burn-rate",
    "title": "Calculate API Error-Budget Burn Rate",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-27"
  },
  "contract": {
    "tableName": "api_requests",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Request completion time in UTC."
      },
      {
        "name": "status_code",
        "type": "Int64",
        "description": "HTTP response status code."
      },
      {
        "name": "environment",
        "type": "Utf8",
        "description": "Deployment environment."
      }
    ]
  },
  "query": "WITH hourly AS (\n  SELECT\n    date_trunc('hour', timestamp_utc) AS hour,\n    COUNT(*) AS requests,\n    SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) AS bad_requests\n  FROM api_requests\n  WHERE timestamp_utc >= now() - INTERVAL '24 hours'\n    AND environment = 'production'\n  GROUP BY date_trunc('hour', timestamp_utc)\n)\nSELECT\n  hour,\n  requests,\n  bad_requests,\n  100.0 * bad_requests / NULLIF(requests, 0) AS error_rate_pct,\n  (1.0 * bad_requests / NULLIF(requests, 0)) / 0.001 AS burn_rate\nFROM hourly\nORDER BY hour;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "status_code": 200,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "status_code": 503,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "status_code": 200,
      "environment": "production"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "status_code": 200,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "status_code": 503,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "status_code": 200,
      "environment": "production"
    }
  ],
  "expectedOutput": {
    "columns": [
      "hour",
      "requests",
      "bad_requests",
      "error_rate_pct",
      "burn_rate"
    ],
    "rows": [
      {
        "hour": "12:00",
        "requests": 18420,
        "bad_requests": 9,
        "error_rate_pct": 0.05,
        "burn_rate": 0.49
      },
      {
        "hour": "13:00",
        "requests": 19110,
        "bad_requests": 71,
        "error_rate_pct": 0.37,
        "burn_rate": 3.72
      },
      {
        "hour": "14:00",
        "requests": 18780,
        "bad_requests": 24,
        "error_rate_pct": 0.13,
        "burn_rate": 1.28
      }
    ]
  },
  "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."
  ]
}