{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "slo-availability-compliance",
    "title": "Measure API Availability Against an SLO",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-27"
  },
  "contract": {
    "tableName": "api_requests",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Request completion time."
      },
      {
        "name": "service",
        "type": "Utf8",
        "description": "Stable service name."
      },
      {
        "name": "slo_eligible",
        "type": "Boolean",
        "description": "Whether the request belongs in the SLO denominator."
      },
      {
        "name": "slo_good",
        "type": "Boolean",
        "description": "Whether the request satisfied the SLO."
      }
    ]
  },
  "query": "SELECT\n  date_trunc('day', timestamp_utc) AS day,\n  service,\n  SUM(CASE WHEN slo_eligible THEN 1 ELSE 0 END) AS eligible_requests,\n  SUM(CASE WHEN slo_eligible AND slo_good THEN 1 ELSE 0 END)\n    AS good_requests,\n  100.0 * SUM(CASE WHEN slo_eligible AND slo_good THEN 1 ELSE 0 END)\n    / NULLIF(SUM(CASE WHEN slo_eligible THEN 1 ELSE 0 END), 0)\n    AS availability_pct,\n  99.9 AS objective_pct\nFROM api_requests\nWHERE timestamp_utc >= now() - INTERVAL '30 days'\nGROUP BY date_trunc('day', timestamp_utc), service\nHAVING SUM(CASE WHEN slo_eligible THEN 1 ELSE 0 END) > 0\nORDER BY day, service;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "service": "service_example_1",
      "slo_eligible": true,
      "slo_good": true
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "service": "service_example_2",
      "slo_eligible": false,
      "slo_good": false
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "service": "service_example_3",
      "slo_eligible": true,
      "slo_good": true
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "service": "service_example_1",
      "slo_eligible": true,
      "slo_good": true
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "service": "service_example_2",
      "slo_eligible": false,
      "slo_good": false
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "service": "service_example_3",
      "slo_eligible": true,
      "slo_good": true
    }
  ],
  "expectedOutput": {
    "columns": [
      "day",
      "service",
      "eligible_requests",
      "good_requests",
      "availability_pct",
      "objective_pct"
    ],
    "rows": [
      {
        "day": "2026-07-25",
        "service": "query_api",
        "eligible_requests": 182400,
        "good_requests": 182274,
        "availability_pct": 99.93,
        "objective_pct": 99.9
      },
      {
        "day": "2026-07-26",
        "service": "query_api",
        "eligible_requests": 176820,
        "good_requests": 176501,
        "availability_pct": 99.82,
        "objective_pct": 99.9
      },
      {
        "day": "2026-07-27",
        "service": "ingestion_api",
        "eligible_requests": 248100,
        "good_requests": 247976,
        "availability_pct": 99.95,
        "objective_pct": 99.9
      }
    ]
  },
  "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."
  ]
}