# isoughtajam-learns/gtfs-realtime architecture

> Architecture diagram of the GitHub repository isoughtajam-learns/gtfs-realtime, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/isoughtajam-learns/gtfs-realtime
- Repository: https://github.com/isoughtajam-learns/gtfs-realtime

## Overview

GTFS Realtime is a FastAPI backend for ingesting transit schedules and live GTFS-Realtime feeds, then serving live positions, trip details, service alerts, and transit-system metadata. The main user path is a client request to the API, which obtains cached or freshly fetched realtime data, enriches it with schedule data where applicable, and returns an SSE stream or API response. Schedule ingestion is a separate operational path. The graph follows the README and source-index references; some runtime wiring is not shown in the excerpts.

Main entry points: [`src/main.py`](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/main.py) (FastAPI endpoints), [`src/database.py`](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/database.py) (Database engine), [`src/services/quota_group_scheduler.py`](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/quota_group_scheduler.py) (Quota scheduler), [`src/commands/fetcher.py`](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/commands/fetcher.py) (Schedule ingestion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API delivery"]
  node_api["FastAPI endpoints<br/>[main.py]"]
  node_systems["System registry"]
  node_tripdetail["Trip detail<br/>[trip_detail.py]"]
  node_alerts["Alert assembly<br/>[service_alerts.py]"]
end

subgraph group_realtime["Realtime processing"]
  node_positions["Position mapping<br/>[positioning.py]"]
  node_recent["Recent positions"]
  node_feedcache["Realtime feed cache"]
  node_regional["Regional feed split<br/>[regional_feed.py]"]
end

subgraph group_schedule["Schedule data"]
  node_schedcache["Schedule cache<br/>[schedule_cache.py]"]
  node_schedutils["Schedule utilities<br/>[schedule_utils.py]"]
  node_fetcher["Schedule ingestion<br/>[fetcher.py]"]
end

subgraph group_operations["Operations and state"]
  node_database["Database engine<br/>[database.py]"]
  node_models["Transit data models<br/>[models.py]"]
  node_quotascheduler["Quota scheduler"]
  node_sharedquota["Shared feed quota"]
  node_tasks["Scheduled fetch tasks<br/>[tasks.py]"]
  node_settings["Application settings<br/>[settings.py]"]
  node_telemetry["Log telemetry<br/>[telemetry.py]"]
end

node_riders(("Transit clients"))
node_sources{{"GTFS feed providers"}}
node_postgres[("Postgres")]

node_riders -->|"requests streams"| node_api
node_api -->|"looks up systems"| node_systems
node_api -->|"gets feeds"| node_feedcache
node_api -->|"replays positions"| node_recent
node_api -->|"maps positions"| node_positions
node_api -->|"enriches events"| node_schedcache
node_api -->|"builds trip detail"| node_tripdetail
node_api -->|"assembles alerts"| node_alerts
node_api -->|"fetches realtime feeds"| node_sources
node_tripdetail -->|"queries schedule"| node_database
node_alerts -->|"loads alert details"| node_database
node_schedcache -->|"loads schedule"| node_database
node_fetcher -->|"downloads schedules"| node_sources
node_fetcher -->|"uses schedule helpers"| node_schedutils
node_fetcher -->|"upserts schedule"| node_database
node_api -->|"starts schedulers"| node_quotascheduler
node_quotascheduler -->|"warms feeds"| node_feedcache
node_quotascheduler -->|"uses shared budget"| node_sharedquota
node_quotascheduler -->|"demuxes regional feed"| node_regional
node_regional -->|"sets feed entries"| node_feedcache
node_tasks -->|"runs schedule fetch"| node_fetcher
node_tasks -->|"lists active systems"| node_systems
node_systems -->|"queries registry"| node_database
node_database -->|"configures connection"| node_settings
node_models -.->|"defines stored records"| node_postgres
node_api -->|"emits telemetry"| node_telemetry
node_telemetry -->|"reads configuration"| node_settings

click node_api "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/main.py"
click node_systems "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/transit_system_detail.py"
click node_tripdetail "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/trip_detail.py"
click node_alerts "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/service_alerts.py"
click node_positions "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/positioning.py"
click node_recent "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/recent_events_cache.py"
click node_feedcache "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/realtime_feed_cache.py"
click node_regional "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/regional_feed.py"
click node_schedcache "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/schedule_cache.py"
click node_schedutils "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/schedule_utils.py"
click node_fetcher "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/commands/fetcher.py"
click node_database "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/database.py"
click node_models "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/models.py"
click node_quotascheduler "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/quota_group_scheduler.py"
click node_sharedquota "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/shared_feed_quota.py"
click node_tasks "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/tasks.py"
click node_settings "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/settings.py"
click node_telemetry "https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/telemetry.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_systems,node_tripdetail,node_alerts,node_riders toneBlue
class node_positions,node_recent,node_feedcache,node_regional,node_postgres toneAmber
class node_schedcache,node_schedutils,node_fetcher toneMint
class node_database,node_models,node_quotascheduler,node_sharedquota,node_tasks,node_settings,node_telemetry toneRose
class node_sources toneIndigo
```

## Components

### API delivery

- [FastAPI endpoints](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/main.py): `src/main.py`
- [System registry](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/transit_system_detail.py): `src/services/transit_system_detail.py`
- [Trip detail](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/trip_detail.py): `src/services/trip_detail.py`
- [Alert assembly](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/service_alerts.py): `src/services/service_alerts.py`

### Realtime processing

- [Position mapping](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/positioning.py): `src/services/positioning.py`
- [Recent positions](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/recent_events_cache.py): `src/services/recent_events_cache.py`
- [Realtime feed cache](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/realtime_feed_cache.py): `src/services/realtime_feed_cache.py`
- [Regional feed split](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/regional_feed.py): `src/services/regional_feed.py`

### Schedule data

- [Schedule cache](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/schedule_cache.py): `src/services/schedule_cache.py`
- [Schedule utilities](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/schedule_utils.py): `src/services/schedule_utils.py`
- [Schedule ingestion](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/commands/fetcher.py): `src/commands/fetcher.py`

### Operations and state

- [Database engine](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/database.py): `src/database.py`
- [Transit data models](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/models.py): `src/models.py`
- [Quota scheduler](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/quota_group_scheduler.py): `src/services/quota_group_scheduler.py`
- [Shared feed quota](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/services/shared_feed_quota.py): `src/services/shared_feed_quota.py`
- [Scheduled fetch tasks](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/tasks.py): `src/tasks.py`
- [Application settings](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/settings.py): `src/settings.py`
- [Log telemetry](https://github.com/isoughtajam-learns/gtfs-realtime/blob/main/src/telemetry.py): `src/telemetry.py`

### Other components

- **Transit clients**
- **GTFS feed providers**
- **Postgres**

## Connections

- Transit clients → FastAPI endpoints: requests streams (evidence: `README.md`)
- FastAPI endpoints → System registry: looks up systems (evidence: `src/main.py`)
- FastAPI endpoints → Realtime feed cache: gets feeds (evidence: `src/main.py`)
- FastAPI endpoints → Recent positions: replays positions (evidence: `src/main.py`)
- FastAPI endpoints → Position mapping: maps positions (evidence: `src/main.py`)
- FastAPI endpoints → Schedule cache: enriches events (evidence: `src/main.py`)
- FastAPI endpoints → Trip detail: builds trip detail (evidence: `src/main.py`)
- FastAPI endpoints → Alert assembly: assembles alerts (evidence: `src/main.py`)
- FastAPI endpoints → GTFS feed providers: fetches realtime feeds (evidence: `README.md`)
- Trip detail → Database engine: queries schedule (evidence: `src/services/trip_detail.py`)
- Alert assembly → Database engine: loads alert details (evidence: `src/services/service_alerts.py`)
- Schedule cache → Database engine: loads schedule (evidence: `src/services/schedule_cache.py`)
- Schedule ingestion → GTFS feed providers: downloads schedules (evidence: `README.md`)
- Schedule ingestion → Schedule utilities: uses schedule helpers (evidence: `src/commands/fetcher.py`)
- Schedule ingestion → Database engine: upserts schedule (evidence: `README.md`)
- FastAPI endpoints → Quota scheduler: starts schedulers (evidence: `src/main.py`)
- Quota scheduler → Realtime feed cache: warms feeds (evidence: `src/services/quota_group_scheduler.py`)
- Quota scheduler → Shared feed quota: uses shared budget (evidence: `src/services/quota_group_scheduler.py`)
- Quota scheduler → Regional feed split: demuxes regional feed (evidence: `src/services/quota_group_scheduler.py`)
- Regional feed split → Realtime feed cache: sets feed entries (evidence: `src/services/quota_group_scheduler.py`)
- Scheduled fetch tasks → Schedule ingestion: runs schedule fetch (evidence: `src/tasks.py`)
- Scheduled fetch tasks → System registry: lists active systems (evidence: `src/tasks.py`)
- System registry → Database engine: queries registry (evidence: `src/services/transit_system_detail.py`)
- Database engine → Application settings: configures connection (evidence: `src/database.py`)
- Transit data models → Postgres: defines stored records
- FastAPI endpoints → Log telemetry: emits telemetry (evidence: `src/main.py`)
- Log telemetry → Application settings: reads configuration (evidence: `src/telemetry.py`)
