Backend Data Flow
Audience
Backend contributors changing auth, ingestion, storage, or route behavior.
Source Of Truth
This document is canonical for the high-level backend flow. Update it when the API runtime path or ingestion architecture changes materially.
Flow Summary
- User authenticates with Riviamigo through the auth routes.
- Vehicle credentials and session state are stored by the API.
- Per-vehicle ingestion workers maintain Rivian connectivity through WebSocket and supporting poll flows, with a watchdog that restarts a collector if the WebSocket stream goes silent while still holding the worker lock.
- Parsed telemetry updates a canonical
vehicle_latest_statusrow using per-field Rivian timestamps so older partial payloads cannot overwrite fresher SoC, range, charge-state, or odometer values. - Supporting poll flows reconcile completed charging sessions and live charge-curve data into canonical
charge_sessions, preserving telemetry-backed windows as the public session timeline while storing Rivian aliases and API-only history as enrichment evidence. While a vehicle is actively charging, the same poll loop fetches the normalized live-session payload and writesvehicle:{vehicle_id}:live_sessionto Redis with a 120-second TTL. The key is deleted when charging ends or the upstream reports no active session; transient polling failures leave the existing value to expire naturally. - API routes expose typed data to the frontend through
packages/typesandpackages/hooks. - Completed trips enqueue an idempotent weather-enrichment job. The worker samples the exact route at endpoints and 15-minute intervals, derives rounded provider cells, batches Open-Meteo requests, stores
trip_weather_samples, and updates the time-weightedtrips.outside_temp_csummary used by trip and efficiency APIs.
Runtime feed health is separate from telemetry freshness. Authentication errors, collector failures, degraded subscriptions, and a silent WebSocket can make the feed unhealthy. Older battery, range, or charging timestamps are reported as field freshness diagnostics instead; parked vehicles are not expected to emit continuous trip or charging telemetry.
Telemetry is written to the timeseries.telemetry hypertable. The
telemetry_1min continuous aggregate incrementally materializes the prior
seven days once an hour, ending five minutes before the present. It remains a
real-time aggregate, so queries include the unmaterialized recent tail from
the hypertable. This keeps active dashboards and charge curves current without
running a refresh every five minutes. Do not stretch this policy to 12 hours or
daily: doing so makes dashboard reads carry an increasingly large raw-data
tail. odometer_daily has a separate hourly, materialized-only policy.
Optional outbound services are governed by external_connection_settings, not environment variables. Weather and Nominatim execute on the server. Basemap and Iconify browser requests terminate at authenticated same-origin proxy routes. Custom endpoints are validated before storage, secrets are age-encrypted and write-only, and disabling a provider is enforced at the shared service seam.
Experimental Parallax protobuf discovery is intentionally kept out of the
production ingestion worker. The local graph-exploration harness owns that
read-only subscription and stores captures outside the application database;
only verified fields should later be promoted through the normal typed
telemetry path.
Major Backend Areas
apps/api/src/routesPublic HTTP surface.apps/api/src/ingestionRivian auth integration, WebSocket/poll workers, parser, detector logic.apps/api/src/servicesShared backend business logic.apps/api/src/modelsDB-facing types and helpers.apps/api/migrationsSchema evolution.
Operational Rules
- New env vars must be reflected in
compose/.env.full.example, the short Compose template when needed, and any relevant user-facing docs. - New routes or route removals must update the relevant developer docs and any public-facing API references.
- Changes to auth, ingestion, or backup behavior must update runbooks if maintainers will need new recovery steps.