# natesil/trout-api architecture

> Architecture diagram of the GitHub repository natesil/trout-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/natesil/trout-api
- Repository: https://github.com/natesil/trout-api

## Overview

trout-api is a read-only Connecticut trout-stocking history API backed by a warehouse rebuilt from daily trout-data snapshots. Its main workflow checks for a new source commit, downloads snapshots, builds a DuckDB warehouse through dbt, and swaps in the successful build while retaining the prior one on failure. API routes query that store for sites, events, metadata, and health. The graph preserves the documented staging-to-intermediate-to-marts pipeline and major warehouse tables; unsampled dbt model internals are represented at their exact tree paths.

Main entry points: [`app/store.py`](https://github.com/natesil/trout-api/blob/main/app/store.py) (Build and query store), [`dbt/models/intermediate`](https://github.com/natesil/trout-api/tree/main/dbt/models/intermediate) (Intermediate models), [`app/main.py`](https://github.com/natesil/trout-api/blob/main/app/main.py) (HTTP routes), [`app/source.py`](https://github.com/natesil/trout-api/blob/main/app/source.py) (Snapshot source)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_api_routes["HTTP routes<br/>[main.py]"]
  node_site_api["Site endpoints<br/>[main.py]"]
  node_event_api["Event endpoint<br/>[main.py]"]
  node_meta_api["Metadata and health<br/>[main.py]"]
  node_rate_limit["Rate limiting<br/>[ratelimit.py]"]
end

subgraph group_refresh["Refresh and storage"]
  node_refresh_loop["Refresh scheduler<br/>[main.py]"]
  node_store["Build and query store<br/>[store.py]"]
  node_source_fetch["Snapshot source<br/>[source.py]"]
  node_warehouse[("DuckDB warehouse")]
end

subgraph group_transform["Warehouse transforms"]
  node_staging["Staging models"]
  node_intermediate["Intermediate models"]
end

subgraph group_mart["Warehouse marts"]
  node_fact_events["Stocking events"]
  node_dim_site["Site dimension<br/>[dim_site.sql]"]
  node_site_town["Site-town bridge"]
  node_dim_town["Town dimension<br/>[dim_town.sql]"]
  node_dim_date["Date dimension<br/>[dim_date.sql]"]
end

node_client(("API client"))
node_github["GitHub trout-data"]

node_client -->|"requests"| node_api_routes
node_api_routes -->|"applies limits"| node_rate_limit
node_api_routes -->|"starts refresh"| node_refresh_loop
node_refresh_loop -->|"refreshes"| node_store
node_store -->|"checks and downloads"| node_source_fetch
node_source_fetch -->|"reads snapshots"| node_github
node_store -->|"runs dbt build"| node_staging
node_staging -->|"transforms into"| node_intermediate
node_intermediate -->|"builds marts"| node_fact_events
node_intermediate -->|"builds marts"| node_dim_site
node_intermediate -->|"builds marts"| node_site_town
node_intermediate -->|"builds marts"| node_dim_town
node_intermediate -->|"builds marts"| node_dim_date
node_store -->|"opens and swaps"| node_warehouse
node_site_api -->|"queries"| node_store
node_event_api -->|"queries"| node_store
node_meta_api -->|"checks and queries"| node_store
node_store -->|"serves query results"| node_warehouse

click node_api_routes "https://github.com/natesil/trout-api/blob/main/app/main.py"
click node_site_api "https://github.com/natesil/trout-api/blob/main/app/main.py"
click node_event_api "https://github.com/natesil/trout-api/blob/main/app/main.py"
click node_meta_api "https://github.com/natesil/trout-api/blob/main/app/main.py"
click node_rate_limit "https://github.com/natesil/trout-api/blob/main/app/ratelimit.py"
click node_refresh_loop "https://github.com/natesil/trout-api/blob/main/app/main.py"
click node_store "https://github.com/natesil/trout-api/blob/main/app/store.py"
click node_source_fetch "https://github.com/natesil/trout-api/blob/main/app/source.py"
click node_staging "https://github.com/natesil/trout-api/tree/main/dbt/models/staging"
click node_intermediate "https://github.com/natesil/trout-api/tree/main/dbt/models/intermediate"
click node_fact_events "https://github.com/natesil/trout-api/blob/main/dbt/models/marts/fct_stocking_event.sql"
click node_dim_site "https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_site.sql"
click node_site_town "https://github.com/natesil/trout-api/blob/main/dbt/models/marts/bridge_site_town.sql"
click node_dim_town "https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_town.sql"
click node_dim_date "https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_date.sql"

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_routes,node_site_api,node_event_api,node_meta_api,node_rate_limit,node_client toneBlue
class node_refresh_loop,node_store,node_source_fetch,node_warehouse toneAmber
class node_staging,node_intermediate toneMint
class node_fact_events,node_dim_site,node_site_town,node_dim_town,node_dim_date toneRose
class node_github toneIndigo
```

## Components

### API

- [HTTP routes](https://github.com/natesil/trout-api/blob/main/app/main.py): `app/main.py`
- [Site endpoints](https://github.com/natesil/trout-api/blob/main/app/main.py): `app/main.py`
- [Event endpoint](https://github.com/natesil/trout-api/blob/main/app/main.py): `app/main.py`
- [Metadata and health](https://github.com/natesil/trout-api/blob/main/app/main.py): `app/main.py`
- [Rate limiting](https://github.com/natesil/trout-api/blob/main/app/ratelimit.py): `app/ratelimit.py`

### Refresh and storage

- [Refresh scheduler](https://github.com/natesil/trout-api/blob/main/app/main.py): `app/main.py`
- [Build and query store](https://github.com/natesil/trout-api/blob/main/app/store.py): `app/store.py`
- [Snapshot source](https://github.com/natesil/trout-api/blob/main/app/source.py): `app/source.py`
- **DuckDB warehouse**

### Warehouse transforms

- [Staging models](https://github.com/natesil/trout-api/tree/main/dbt/models/staging): `dbt/models/staging`
- [Intermediate models](https://github.com/natesil/trout-api/tree/main/dbt/models/intermediate): `dbt/models/intermediate`

### Warehouse marts

- [Stocking events](https://github.com/natesil/trout-api/blob/main/dbt/models/marts/fct_stocking_event.sql): `dbt/models/marts/fct_stocking_event.sql`
- [Site dimension](https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_site.sql): `dbt/models/marts/dim_site.sql`
- [Site-town bridge](https://github.com/natesil/trout-api/blob/main/dbt/models/marts/bridge_site_town.sql): `dbt/models/marts/bridge_site_town.sql`
- [Town dimension](https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_town.sql): `dbt/models/marts/dim_town.sql`
- [Date dimension](https://github.com/natesil/trout-api/blob/main/dbt/models/marts/dim_date.sql): `dbt/models/marts/dim_date.sql`

### Other components

- **API client**
- **GitHub trout-data**

## Connections

- API client → HTTP routes: requests (evidence: `README.md`)
- HTTP routes → Rate limiting: applies limits (evidence: `app/main.py`)
- HTTP routes → Refresh scheduler: starts refresh (evidence: `app/main.py`)
- Refresh scheduler → Build and query store: refreshes (evidence: `app/main.py`)
- Build and query store → Snapshot source: checks and downloads (evidence: `app/store.py`)
- Snapshot source → GitHub trout-data: reads snapshots (evidence: `app/source.py`)
- Build and query store → Staging models: runs dbt build (evidence: `app/store.py`)
- Staging models → Intermediate models: transforms into (evidence: `README.md`)
- Intermediate models → Stocking events: builds marts (evidence: `README.md`)
- Intermediate models → Site dimension: builds marts (evidence: `README.md`)
- Intermediate models → Site-town bridge: builds marts (evidence: `README.md`)
- Intermediate models → Town dimension: builds marts (evidence: `README.md`)
- Intermediate models → Date dimension: builds marts (evidence: `README.md`)
- Build and query store → DuckDB warehouse: opens and swaps (evidence: `app/store.py`)
- Site endpoints → Build and query store: queries (evidence: `app/main.py`)
- Event endpoint → Build and query store: queries (evidence: `app/main.py`)
- Metadata and health → Build and query store: checks and queries (evidence: `app/main.py`)
- Build and query store → DuckDB warehouse: serves query results (evidence: `app/store.py`)
