# gcaguilar/bizidashboard architecture

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

- Interactive diagram: https://gitdiagram.com/gcaguilar/bizidashboard
- Repository: https://github.com/gcaguilar/bizidashboard

## Overview

1. **Overall shape**
   BiziDashboard is a full-stack TanStack Start application that serves both public SEO pages and a data-rich dashboard, while also exposing a large API surface for mobile apps and operational jobs. The same repo contains the web app, backend route handlers, analytics SQL/logic, ingestion jobs, and database schema/migrations.

2. **Primary runtime boundaries**
   There are three major execution modes:
   - the **main web/API server** running on Bun, handling SSR pages and `/api/*` routes,
   - **background jobs/cron workers** that collect GBFS snapshots and compute analytics,
   - **database maintenance/ops scripts** used for schema repair, migrations, audits, and QA.  
   Docker Compose and separate job/server entrypoints keep those concerns isolated even though they share code.

3. **Ingestion pipeline**
   The collection flow starts from scheduled cron jobs or manual triggers and ends in `GET/POST /api/collect`, which is protected by ops API keys. That endpoint pulls live GBFS data from city-specific discovery URLs, validates it, and writes snapshot data plus run metadata into PostgreSQL. Collection runs are tracked as first-class records, including request IDs, warnings, errors, and trigger source.

4. **Storage model**
   Persistence is PostgreSQL via Prisma, but the key architectural choice is **multi-tenant by schema**: each city lives in its own schema with its own operational and analytics tables. This means city isolation happens at the database layer rather than in application logic. Redis is used separately for short-lived concerns such as rate limiting, job locks, cache, and token/session-related flows.

5. **Analytics subsystem**
   The `src/analytics` area is a dedicated analytical engine on top of stored snapshots. It contains rollups, watermarks, retention rules, time buckets, and domain queries for rankings, heatmaps, hourly/daily trends, alerts, rebalancing, and pattern analysis. The API routes in `src/app/api/*` expose those derived datasets, while dashboard widgets consume them through server functions and client hooks.

6. **Dashboard and public UI**
   The UI is split between public content pages and the authenticated/operational dashboard. Public pages cover city exploration, station pages, statistics, reports, and SEO landing pages; the dashboard provides richer operational and research views such as alerts, station detail, flow analysis, redistribution, and system health. TanStack Router routes are generated and grouped by feature area, with server functions supplying page-specific data.

7. **Mobile and authenticated API surface**
   The repo also supports a mobile app integration path. Mobile endpoints require bearer auth plus installation identity, and some geo/auth routes support signed requests with timestamp/signature enforcement. Refresh tokens are hashed at rest and rotated on use, so the auth boundary is designed to detect token reuse and revoke compromised installations.

8. **Public vs protected API boundaries**
   The API design is intentionally tiered:
   - some endpoints are low-cost and anonymous, such as status, stations, rankings, alerts, patterns, heatmap, docs, and app versions,
   - heavier public reads require a public API key,
   - ops and mobile mutation/auth routes require stricter auth and rate limits.  
   This split matters architecturally because it separates public browsing traffic from sensitive operational and mobile flows.

9. **External service dependencies**
   The application depends on several external systems that shape its behavior:
   - **GBFS feeds** as the source of live bike-system data,
   - **Nominatim/OpenStreetMap** for geocoding and reverse geocoding,
   - **Sentry** for client/server error reporting,
   - **Umami** for privacy-conscious analytics,
   - **MapLibre** for map rendering in the public/dashboard UI.  
   These services are not decorative; they directly affect data acquisition, location search, observability, and dashboard features.

10. **SEO and content generation**
   A large portion of the repo is dedicated to SEO-friendly pages and machine-readable outputs such as sitemap, robots, llm text files, manifests, and city/station/district landing pages. This suggests the site is both a data product and a discovery surface, with structured data, breadcrumbs, and page metadata treated as core architecture rather than afterthoughts.

11. **Operational safety and resilience**
   There is a strong ops layer around security, auditability, and health checking. The app emits structured logs, attaches request IDs, validates startup configuration, applies CSP rules, rate limits sensitive endpoints, and exposes live/ready probes separately from richer status pages. Scripts and SQL helpers exist for schema fixes, migration from SQLite, database health checks, retention cleanup, and site-audit/vitals gates.

12. **Code organization and data flow**
   The repo’s internal layering is consistent: route handlers and server functions sit at the edge, `src/lib` holds shared domain logic, `src/services` wraps external data sources and validation, `src/analytics` computes derived datasets, and `src/jobs` orchestrates periodic work. The main data flow is: external GBFS feed → collection API/job → PostgreSQL schema for the active city → analytics rollups/queries → public dashboard/API consumers, with Redis and observability systems supporting the edges.

Main entry points: [`src/app/api/status/index.ts`](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/api/status/index.ts) (API surface), [`src/app/dashboard/index.tsx`](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/dashboard/index.tsx) (Dashboard UI), [`src/analytics/rollup-engine.ts`](https://github.com/gcaguilar/bizidashboard/blob/main/src/analytics/rollup-engine.ts) (Analytics engine), [`src/routeTree.gen.ts`](https://github.com/gcaguilar/bizidashboard/blob/main/src/routeTree.gen.ts) (Route tree)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtimes"]
  node_server["Web server<br/>bun runtime<br/>[server-entry.ts]"]
  node_jobs["Background jobs<br/>worker runtime<br/>[standalone.ts]"]
  node_ops["Ops scripts<br/>maintenance tools<br/>[start-server.mjs]"]
end

subgraph group_platform["Platform"]
  node_analytics{{"Analytics engine<br/>rollups and queries<br/>[rollup-engine.ts]"}}
  node_collection{{"Collection pipeline<br/>gbfs ingest"}}
  node_storage[("Data storage<br/>prisma postgres<br/>[data-storage.ts]")]
  node_auth{{"Auth & access<br/>security boundary"}}
  node_cache[("Cache & Redis<br/>ephemeral state<br/>[cache.ts]")]
  node_geo{{"Geo services<br/>nominatim client<br/>[nominatim.ts]"}}
  node_observability{{"Observability<br/>logs and sentry<br/>[observability.ts]"}}
  node_shared{{"Shared domain<br/>lib layer<br/>[routes.ts]"}}
end

subgraph group_surface["Surfaces"]
  node_routes["Route tree<br/>tanstack router<br/>[routeTree.gen.ts]"]
  node_public["Public pages<br/>seo web<br/>[index.tsx]"]
  node_dashboard["Dashboard UI<br/>data app<br/>[index.tsx]"]
  node_api["API surface<br/>http routes<br/>[index.ts]"]
  node_serverfx["Server functions<br/>page data<br/>[dashboard.ts]"]
  node_seo["SEO assets<br/>metadata outputs<br/>[sitemap[.]xml.ts]"]
  node_mobile["Mobile API<br/>authenticated api<br/>[index.ts]"]
end

node_server -->|"serves"| node_routes
node_routes -->|"maps"| node_public
node_routes -->|"maps"| node_dashboard
node_server -->|"hosts"| node_api
node_public -->|"loads data"| node_serverfx
node_dashboard -->|"loads data"| node_serverfx
node_serverfx -->|"uses"| node_shared
node_api -->|"protected by"| node_auth
node_api -->|"triggers"| node_collection
node_jobs -->|"runs"| node_collection
node_collection -->|"writes"| node_storage
node_jobs -->|"computes"| node_analytics
node_analytics -->|"reads"| node_storage
node_analytics -->|"serves"| node_api
node_dashboard -->|"consumes"| node_analytics
node_dashboard -->|"uses"| node_geo
node_public -->|"emits"| node_seo
node_api -->|"includes"| node_mobile
node_auth -->|"uses"| node_cache
node_observability -->|"monitors"| node_server
node_ops -->|"maintains"| node_storage
node_ops -->|"checks"| node_observability

click node_server "https://github.com/gcaguilar/bizidashboard/blob/main/src/server-entry.ts"
click node_jobs "https://github.com/gcaguilar/bizidashboard/blob/main/src/jobs/standalone.ts"
click node_ops "https://github.com/gcaguilar/bizidashboard/blob/main/ops/start-server.mjs"
click node_routes "https://github.com/gcaguilar/bizidashboard/blob/main/src/routeTree.gen.ts"
click node_public "https://github.com/gcaguilar/bizidashboard/blob/main/src/app/index.tsx"
click node_dashboard "https://github.com/gcaguilar/bizidashboard/blob/main/src/app/dashboard/index.tsx"
click node_api "https://github.com/gcaguilar/bizidashboard/blob/main/src/app/api/status/index.ts"
click node_serverfx "https://github.com/gcaguilar/bizidashboard/blob/main/src/server-functions/dashboard.ts"
click node_analytics "https://github.com/gcaguilar/bizidashboard/blob/main/src/analytics/rollup-engine.ts"
click node_collection "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/collection-pipeline.ts"
click node_storage "https://github.com/gcaguilar/bizidashboard/blob/main/src/services/data-storage.ts"
click node_auth "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/security/route-protection.ts"
click node_cache "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/cache/cache.ts"
click node_geo "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/geo/nominatim.ts"
click node_observability "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/observability.ts"
click node_seo "https://github.com/gcaguilar/bizidashboard/blob/main/src/app/sitemap[.]xml.ts"
click node_mobile "https://github.com/gcaguilar/bizidashboard/blob/main/src/app/api/install/register/index.ts"
click node_shared "https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/routes.ts"

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_server,node_jobs,node_ops toneBlue
class node_analytics,node_collection,node_storage,node_auth,node_cache,node_geo,node_observability,node_shared toneAmber
class node_routes,node_public,node_dashboard,node_api,node_serverfx,node_seo,node_mobile toneMint
```

## Components

### Runtimes

Primary execution modes for the app, jobs, and ops scripts.

- [Web server](https://github.com/gcaguilar/bizidashboard/blob/main/src/server-entry.ts): bun runtime · SSR pages and API routes for the main application. · `src/server-entry.ts`
- [Background jobs](https://github.com/gcaguilar/bizidashboard/blob/main/src/jobs/standalone.ts): worker runtime · Scheduled collection and analytics aggregation jobs. · `src/jobs/standalone.ts`
- [Ops scripts](https://github.com/gcaguilar/bizidashboard/blob/main/ops/start-server.mjs): maintenance tools · Schema repair, audits, cleanup, and operational utilities. · `ops/start-server.mjs`

### Platform

Shared data, security, analytics, and infrastructure layers.

- [Analytics engine](https://github.com/gcaguilar/bizidashboard/blob/main/src/analytics/rollup-engine.ts): rollups and queries · Derived datasets, time buckets, retention, watermarks, and analytical queries. · `src/analytics/rollup-engine.ts`
- [Collection pipeline](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/collection-pipeline.ts): gbfs ingest · Feed validation, snapshot capture, and collection run tracking. · `src/lib/collection-pipeline.ts`
- [Data storage](https://github.com/gcaguilar/bizidashboard/blob/main/src/services/data-storage.ts): prisma postgres · Persistence layer and city-scoped schema model. · `src/services/data-storage.ts`
- [Auth & access](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/security/route-protection.ts): security boundary · API keys, bearer/mobile auth, signatures, rate limits, and route protection. · `src/lib/security/route-protection.ts`
- [Cache & Redis](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/cache/cache.ts): ephemeral state · Redis-backed locks, rate limiting, and short-lived cached data. · `src/lib/cache/cache.ts`
- [Geo services](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/geo/nominatim.ts): nominatim client · Forward and reverse geocoding integrations used by search and map features. · `src/lib/geo/nominatim.ts`
- [Observability](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/observability.ts): logs and sentry · Structured logging, request context, Sentry, health, and metrics. · `src/lib/observability.ts`
- [Shared domain](https://github.com/gcaguilar/bizidashboard/blob/main/src/lib/routes.ts): lib layer · Common domain models, routes, formatting, and public-facing helpers. · `src/lib/routes.ts`

### Surfaces

Public site, dashboard, and API entrypoints.

- [Route tree](https://github.com/gcaguilar/bizidashboard/blob/main/src/routeTree.gen.ts): tanstack router · Generated route map and browser-side routing boundary. · `src/routeTree.gen.ts`
- [Public pages](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/index.tsx): seo web · SEO landing pages, city pages, stations, and reports. · `src/app/index.tsx`
- [Dashboard UI](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/dashboard/index.tsx): data app · Authenticated operational and research interface. · `src/app/dashboard/index.tsx`
- [API surface](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/api/status/index.ts): http routes · Public, protected, ops, and mobile API endpoints. · `src/app/api/status/index.ts`
- [Server functions](https://github.com/gcaguilar/bizidashboard/blob/main/src/server-functions/dashboard.ts): page data · Per-page data loading and feature-specific server logic. · `src/server-functions/dashboard.ts`
- [SEO assets](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/sitemap[.]xml.ts): metadata outputs · Sitemap, robots, manifests, and machine-readable text outputs. · `src/app/sitemap[.]xml.ts`
- [Mobile API](https://github.com/gcaguilar/bizidashboard/blob/main/src/app/api/install/register/index.ts): authenticated api · Installation-bound mobile endpoints and token rotation flows. · `src/app/api/install/register/index.ts`

## Connections

- Web server → Route tree: serves · The Bun server hosts the generated TanStack route tree.
- Route tree → Public pages: maps · Routes resolve to public SEO pages and discovery surfaces.
- Route tree → Dashboard UI: maps · Routes resolve to authenticated dashboard views.
- Web server → API surface: hosts · The server exposes the HTTP API surface.
- Public pages → Server functions: loads data · Public pages use server functions for page-specific data.
- Dashboard UI → Server functions: loads data · Dashboard views rely on server functions and hooks.
- Server functions → Shared domain: uses · Server functions share domain helpers and navigation logic.
- API surface → Auth & access: protected by · API endpoints are tiered by public, ops, and mobile auth rules.
- API surface → Collection pipeline: triggers · Collection endpoint accepts ops-triggered ingestion requests.
- Background jobs → Collection pipeline: runs · Cron/worker jobs initiate feed collection.
- Collection pipeline → Data storage: writes · Validated snapshots and run metadata are persisted in PostgreSQL.
- Background jobs → Analytics engine: computes · Background aggregation builds rollups and derived datasets.
- Analytics engine → Data storage: reads · Analytical queries operate on stored snapshots and rollup tables.
- Analytics engine → API surface: serves · Analytics-backed endpoints expose rankings, heatmaps, alerts, and trends.
- Dashboard UI → Analytics engine: consumes · Widgets and panels read derived analytics datasets.
- Dashboard UI → Geo services: uses · Maps and station search depend on geocoding services.
- Public pages → SEO assets: emits · Public pages generate sitemap, robots, manifests, and text outputs.
- API surface → Mobile API: includes · The API surface includes mobile registration and token flows.
- Auth & access → Cache & Redis: uses · Rate limits, locks, and short-lived state rely on Redis.
- Observability → Web server: monitors · Runtime instrumentation and error reporting wrap the main server.
- Ops scripts → Data storage: maintains · Ops scripts repair schemas, migrations, and database health.
- Ops scripts → Observability: checks · Audit and vitals scripts validate runtime health and reporting.
