Skip to main content

Reports pipeline

:::warning Planned — not yet callable ReportsStack (report-dispatch and report-runs queues, …-reports Scheduler group, dispatcher, schedule runner, sweeper, 1 alarm ReportsOverdue; its DLQs have no alarm — checked by hand) is built in code and created only with -c busEnabled=true; it has not been deployed, and no renderer exists yet (KI-41). The report routes, including GET /v1/reports/{reportId}/download, are routed in api-reports behind the JWT authorizer and do not depend on this pipeline. See Infrastructure → Reports stack. :::

The HTTP API never renders a report: it has no telemetry. The TCP side renders the file and reports status back.

Stages​

  1. Request — H02/H03a/H06/H05g write the report record (QUEUED) and an outbox entry in one transaction.

  2. Dispatch — the dispatcher resolves the scope to plant ids, writes the render manifest (v5, below) to reports/manifests/<reportId>/<attempt>/manifest.json and publishes report.render.requested.v1 to the TCP side.

  3. Render (TCP, not built — KI-41) — the renderer reads the manifest and uploads to reports/incoming/<reportId>/<attempt>/<fileId>.<ext>, then sends report.render.status.v1.

  4. Status — the render-status consumer moves the record to RENDERING, READY or FAILED (reasonCode). A legacy UPLOAD_URL_EXPIRED is stored as UPLOAD_FAILED, with no re-dispatch.

  5. Sweeper — every 5 minutes fails reports stuck in QUEUED/RENDERING past their deadline (RENDER_TIMEOUT); reports > 10 min overdue raise the ReportsOverdue alarm.

  6. Retention — the bucket lifecycle deletes reports/ after 90 days; reads then report EXPIRED (FILES_EXPIRED). With reportDispatchQueueArn (set for dev in cdk.json) on the data stack, the S3 expiry event goes to report-dispatch and the dispatcher writes EXPIRED back.

  7. Download — once READY, GET /v1/reports/{reportId}/download returns a presigned S3 GET (default 300 s, hard cap 900 s, Content-Disposition: attachment with a sanitised name). The client only ever talks to this API and S3; it never fetches files from the TCP Server.

Dispatch pacing​

The TCP renderer caps its own concurrency, and a report's render deadline starts when it is dispatched, so a burst (for example monthly schedules) is paced:

  • Limit: reportDispatchPerMinute (-c or cdk.json stages.<stage>; default 30, 0 = pacing off; dev 10), passed to the dispatcher as REPORT_DISPATCH_PER_MINUTE. Stage-wide.
  • Counter: one DynamoDB item per UTC minute (RATE#report-dispatch/W#<minuteStart>, atomic ADD, TTL window end + 60 s). Each message draws a ticket; tickets within the limit dispatch now.
  • Deferral: an over-limit message is re-sent to the same queue with DelaySeconds (slot ⌊(n−1)/limit⌋ minutes ahead, spread across that minute, max 900 s) and the original is acked. The copy starts a fresh receive count, so pacing never sends a message to the DLQ.
  • Deadlines: the render deadline is computed at the real dispatch, not at request time. Each deferral also pushes the undispatched (QUEUED) deadline forward, so the sweeper does not time out a paced report.

Render manifest v5​

A private S3 JSON for the renderer (not a client contract), written by the dispatcher (packages/core/src/reports/manifest.ts). manifestVersion is 5 and changes are additive only. v3 (2026-11-02) added plants[].latitude and plants[].longitude, rounded to 2 decimal places (about 1 km), when the plant has valid stored coordinates, for daylight availability. v4 (2026-11-04) added operator and plants[].regionName; the TCP renderer accepts any version from 3 up and falls back when they are absent. v5 (2026-11-25, deployed to dev 2026-11-26) added scopeLevel, operators[] and plants[].operatorOrgId for platform-wide reports. It never contains telemetry. Top-level fields: manifestVersion, reportId, attempt, templateKey, audience, plantCount, scopeLevel, operator, operators, plants, workOrders, money, branding. Redaction flags travel in the render request, not the manifest.

FieldPresent whenContent
scopeLevelAlways (v5)OPERATOR, or PLATFORM for a Crown-HQ report across Businesses. A PLATFORM report holds at most 5,000 plants; more fails the attempt SCOPE_TOO_LARGE before TCP is contacted.
operatorsAlways (v5)[{ orgId, name?, plantCount }]: the distinct Businesses of plants, sorted by name. One entry for an operator report.
plants[].operatorOrgIdAlways (v5)The plant's Business. For a PLATFORM report this differs from the render request's operatorOrgId, which is the platform org.
operatorAlways (v4){ orgId, name? }: the report's Business (the platform org, Crown, for a PLATFORM report, whose branding is then Crown's own). name comes from the org record and is absent when it has none; the renderer then uses its own copy of the org name for the cover.
plants[].regionNameThe plant has a regionTagId and that region tag still exists (v4)The region tag's label, for AVAILABILITY_BY_REGION. Without it the renderer prints the region tag id. One batch read over the distinct region ids per Business.
workOrdersTemplate has SERVICE_VISITS, SERVICE_BACKLOG, MAINTENANCE_CARRIED_OUT or SERVICE_ACTIONS; otherwise { "available": false }Per plant: completed orders closed in the period (items, newest first) and the open backlog at period end (openCount, byStatus, byPriority, closedSinceAsOf, oldest-open age). DOWNTIME_RECORD alone does not load work orders (downtime comes from telemetry, TCP-owned).
moneyTemplate has SAVINGS_ESTIMATE or COMMISSION_CALCULATION; otherwise { "available": false }Operator org currency (default PKR) and per plant the electricityRate and web tariff (SETTINGS#TARIFF) when set.
branding.logoTemplate branding: CUSTOMER_LOGO and the operator org has a READY REPORT_COVER (else PRIMARY_LOCKUP) brand asset{ key, contentType, sizeBytes, slot, orgId }. The dispatcher copies the asset from the service-files bucket to reports/manifests/<reportId>/<attempt>/logo.<ext>, so the renderer needs no new grant. Missing or failed copy → logo omitted, the report still renders (a warning is logged).

:::note Work-order lifecycle v2 (ADR 0023) Completed items are orders whose maintenance-history outcome is a completed visit: CLOSED (and a pre-v2 COMPLETED that carries closedAt) with closedAt in the period. The backlog counts the open set, which since v2 includes COMPLETED / VERIFIED orders waiting for the Business (packages/core/src/reports/manifest.ts). :::

Redaction. Internal work-order notes appear only for INTERNAL audience templates (redaction.omitInternalNotes = false), cut to 500 characters.

Caps. 50 completed items per plant, 2,000 per manifest, 300 plant-link rows read per plant (3 pages of 100), first 1,000 plants. Any cap hit sets truncated: true (per plant, per item list and overall), and the caps are echoed in workOrders.caps.

The render contract is in specs/009-analytics-reports/contracts/integration-events.md (cse-http-be repo).

Owner decisions (2026-11-02)​

TopicDecision
RenderingThe TCP side renders and generates report files; HTTP never touches telemetry
File storageFiles live in the HTTP reports bucket (reports/incoming/…)
AvailabilityCounted in daylight hours only; grid outages are excluded
CO₂ factor0.5 kg/kWh, provisional; one factor shared by TCP screens and reports
Missing dataOld or missing data is rendered as partial ("not available" plus a completeness figure); the report fails only when more than 50% of plants have no data
RetentionNo HTTP retention floor by default; only the 366-day span limit applies (see Reports API)
LanguageEnglish only; an Urdu locale is rejected with 422 until the renderer supports it
Expected yieldPlant targets first; otherwise annual expected yield ÷ 12 per month

Schedules​

EventBridge Scheduler (group cse-http-be-<stage>-reports) sends to the report-runs queue, which feeds the schedule runner, which re-checks the schedule (paused → SKIPPED_PAUSED, missing → SKIPPED_STALE) and files reports as SCHEDULED with author "Automation · SCH-NN". Scheduler entries are synced from the schedule record asynchronously. A schedule saved while Scheduler is not configured is stored but not armed.

Security​

  • The bucket blocks public access, enforces TLS, and keys are server-generated.
  • The TCP renderer role can only put under reports/incoming/* and get reports/manifests/* (manifest and logo copy), and only when its role ARN is supplied at deploy (tcpRendererRoleArn, from cdk.json context.stages.<stage> or -c). Dev sets it in cdk.json since 2026-11-04. Manifests v2 to v4 did not change it.
  • The dispatcher has read-only s3:GetObject on the service-files bucket's brand/* prefix (env SERVICE_FILES_BUCKET) to copy the logo.
  • Download URLs default to 300 s (max 900 s) and are never logged.

See Reports API and Infrastructure.