{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "late-arriving-events",
    "title": "Measure Late-Arriving Events",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-28"
  },
  "contract": {
    "tableName": "ingestion_events",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "When the source says the event occurred."
      },
      {
        "name": "received_at",
        "type": "Timestamp",
        "description": "When Telemetry received the event."
      },
      {
        "name": "source",
        "type": "Utf8",
        "description": "Producer or ingestion path."
      },
      {
        "name": "event_name",
        "type": "Utf8",
        "description": "Stable event contract name."
      }
    ]
  },
  "query": "SELECT\n  source,\n  event_name,\n  COUNT(*) AS events,\n  AVG(date_part('second', received_at - timestamp_utc)) / 60.0\n    AS average_delay_minutes,\n  approx_percentile_cont(\n    date_part('second', received_at - timestamp_utc) / 60.0,\n    0.95\n  ) AS p95_delay_minutes,\n  SUM(CASE\n    WHEN received_at - timestamp_utc > INTERVAL '15 minutes' THEN 1\n    ELSE 0\n  END) AS events_over_15_minutes\nFROM ingestion_events\nWHERE received_at >= now() - INTERVAL '24 hours'\n  AND received_at >= timestamp_utc\nGROUP BY source, event_name\nHAVING COUNT(*) >= 20\nORDER BY p95_delay_minutes DESC;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "received_at": "2026-07-28T10:00:00Z",
      "source": "source_example_1",
      "event_name": "workflow_completed"
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "received_at": "2026-07-28T11:00:00Z",
      "source": "source_example_2",
      "event_name": "workflow_completed"
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "received_at": "2026-07-28T12:00:00Z",
      "source": "source_example_3",
      "event_name": "workflow_completed"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "received_at": "2026-07-28T10:00:00Z",
      "source": "source_example_1",
      "event_name": "workflow_completed"
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "received_at": "2026-07-28T11:00:00Z",
      "source": "source_example_2",
      "event_name": "workflow_completed"
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "received_at": "2026-07-28T12:00:00Z",
      "source": "source_example_3",
      "event_name": "workflow_completed"
    }
  ],
  "expectedOutput": {
    "columns": [
      "source",
      "event_name",
      "events",
      "average_delay_minutes",
      "p95_delay_minutes",
      "events_over_15_minutes"
    ],
    "rows": [
      {
        "source": "mobile_offline_sync",
        "event_name": "feature_used",
        "events": 4260,
        "average_delay_minutes": 18.4,
        "p95_delay_minutes": 96.2,
        "events_over_15_minutes": 1088
      },
      {
        "source": "billing_webhook",
        "event_name": "invoice_updated",
        "events": 1380,
        "average_delay_minutes": 1.2,
        "p95_delay_minutes": 4.8,
        "events_over_15_minutes": 11
      }
    ]
  },
  "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."
  ]
}