{
  "license": "CC BY 4.0",
  "recipe": {
    "slug": "workflow-correlation-timeline",
    "title": "Reconstruct a Correlated Workflow Timeline",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-28"
  },
  "contract": {
    "tableName": "workflow_timeline_events",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Event time in UTC."
      },
      {
        "name": "workflow_id",
        "type": "Utf8",
        "description": "Identifier shared across workflow steps."
      },
      {
        "name": "event_name",
        "type": "Utf8",
        "description": "Bounded lifecycle event name."
      },
      {
        "name": "status",
        "type": "Utf8",
        "description": "ok or failed."
      },
      {
        "name": "service",
        "type": "Utf8",
        "description": "Service that emitted the event."
      },
      {
        "name": "correlation_id",
        "type": "Utf8",
        "description": "Optional request-to-workflow correlation identifier."
      },
      {
        "name": "environment",
        "type": "Utf8",
        "description": "Deployment environment."
      }
    ]
  },
  "query": "WITH target_workflow AS (\n  SELECT workflow_id\n  FROM workflow_timeline_events\n  WHERE timestamp_utc >= now() - INTERVAL '24 hours'\n    AND environment = 'production'\n    AND status = 'failed'\n  ORDER BY timestamp_utc DESC\n  LIMIT 1\n),\nworkflow_start AS (\n  SELECT\n    events.workflow_id,\n    MIN(events.timestamp_utc) AS started_at\n  FROM workflow_timeline_events AS events\n  JOIN target_workflow AS target\n    ON target.workflow_id = events.workflow_id\n  GROUP BY events.workflow_id\n)\nSELECT\n  events.workflow_id,\n  ROW_NUMBER() OVER (\n    PARTITION BY events.workflow_id\n    ORDER BY events.timestamp_utc, events.event_name\n  ) AS step_number,\n  events.event_name,\n  events.service,\n  events.status,\n  date_part('epoch', events.timestamp_utc - starts.started_at)\n    AS elapsed_seconds\nFROM workflow_timeline_events AS events\nJOIN workflow_start AS starts\n  ON starts.workflow_id = events.workflow_id\nORDER BY events.workflow_id, step_number;",
  "fixtureKind": "reproducible",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "workflow_started",
      "status": "ok",
      "service": "checkout-api",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:08.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "payment_authorized",
      "status": "ok",
      "service": "checkout-api",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:21.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "inventory_reserved",
      "status": "ok",
      "service": "fulfillment-worker",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:47.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "shipment_created",
      "status": "failed",
      "service": "fulfillment-worker",
      "correlation_id": "corr_7d91",
      "environment": "production"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "workflow_started",
      "status": "ok",
      "service": "checkout-api",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:08.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "payment_authorized",
      "status": "ok",
      "service": "checkout-api",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:21.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "inventory_reserved",
      "status": "ok",
      "service": "fulfillment-worker",
      "correlation_id": "corr_7d91",
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:47.000Z",
      "workflow_id": "workflow_1024",
      "event_name": "shipment_created",
      "status": "failed",
      "service": "fulfillment-worker",
      "correlation_id": "corr_7d91",
      "environment": "production"
    }
  ],
  "expectedOutput": {
    "columns": [
      "workflow_id",
      "step_number",
      "event_name",
      "service",
      "status",
      "elapsed_seconds"
    ],
    "rows": [
      {
        "workflow_id": "workflow_1024",
        "step_number": 1,
        "event_name": "workflow_started",
        "service": "checkout-api",
        "status": "ok",
        "elapsed_seconds": 0
      },
      {
        "workflow_id": "workflow_1024",
        "step_number": 2,
        "event_name": "payment_authorized",
        "service": "checkout-api",
        "status": "ok",
        "elapsed_seconds": 8
      },
      {
        "workflow_id": "workflow_1024",
        "step_number": 3,
        "event_name": "inventory_reserved",
        "service": "fulfillment-worker",
        "status": "ok",
        "elapsed_seconds": 21
      },
      {
        "workflow_id": "workflow_1024",
        "step_number": 4,
        "event_name": "shipment_created",
        "service": "fulfillment-worker",
        "status": "failed",
        "elapsed_seconds": 47
      }
    ]
  },
  "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."
  ]
}