How phuc-nt/my-epidemic-monitor works
1. High-level shape
- This repo is a public single-page app plus serverless API for epidemic risk monitoring in Vietnam.
- The browser UI is built with Vanilla TypeScript + Vite, rendered on Cloudflare Pages, and backed by Cloudflare Pages Functions and D1 for data.
2. Primary runtime boundaries
- Client/browser: map, panels, chat, local UI state, and data fetching.
- Cloudflare edge/serverless: /api/health/v1/* and /api/chat endpoints, plus shared caching/CORS helpers.
- External services: LLM inference via OpenRouter and optional local/provider abstractions (mlx, ollama, proxy).
- Upstream data pipeline: the README says crawl/extract runs separately on a Mac Mini and pushes snapshots into D1 every 6 hours, so the frontend does not depend on crawler uptime.
3. Frontend application shell
- src/main.ts boots the app.
- src/app/* (app-init, app-context, app-layout) establishes global state and overall page composition.
- The app is organized around a map-centric layout with side panels, a mobile tab bar, banners, and a disclaimer flow.
4. Map and geospatial visualization subsystem
- src/components/map-shell.ts is the main map container.
- src/components/map-layers/* defines layered risk visualizations:
- country choropleth
- district choropleth
- outbreak markers
- early warning layer
- severity heatmap
- The app uses MapLibre GL for basemap rendering and deck.gl for overlay layers.
- Geospatial boundary data includes public/data/vietnam-districts.geojson.
5. Health risk content panels
- The left/right informational panels are split by topic:
- breaking-news-banner
- top-diseases-panel
- disease-outbreaks-panel
- climate-alerts-panel
- case-report-panel-data
- chat-panel
- panel-base and panel-persistence provide shared panel behavior and remembered UI state.
- On mobile, mobile-tab-bar swaps the desktop panel affordance into a tabbed navigation pattern.
6. Data access layer in the client
- src/services/api-client.ts is the common API entry point.
- Domain-specific clients separate concerns:
- bulk-data-service
- climate-service
- disease-outbreak-service
- news-feed-service
- fetch-cache.ts adds client-side request caching behavior.
- snapshot-store.ts, trend-calculator.ts, and related services suggest the UI is fed by time-series snapshots and precomputed trend data rather than live scraping.
7. Serverless API surface
- The Cloudflare Functions entry points are under functions/api/.
- Health data is exposed through:
- functions/api/health/v1/all.ts
- functions/api/health/v1/climate.ts
- functions/api/health/v1/news.ts
- functions/api/health/v1/outbreaks.ts
- functions/api/chat.ts handles the conversational AI endpoint.
- functions/_middleware.ts and shared helpers manage cross-cutting behavior such as CORS, caching, and request shaping.
8. Shared server-side data normalization
- functions/_shared/* contains data/lookup logic reused across endpoints:
- outbreak query helpers
- disease label mapping
- Vietnam province population data
- cache utilities
- CORS utilities
- This indicates the API is not just pass-through; it also normalizes and enriches raw health records before returning them to the UI.
9. LLM/chat architecture
- Chat is abstracted behind a provider/router layer in src/services/llm-*.
- Providers include:
- llm-provider-openrouter
- llm-provider-ollama
- llm-provider-mlx
- llm-provider-proxy
- llm-router selects the backend, llm-context-builder assembles grounded prompt context from app data, and llm-sse-stream-reader handles streamed responses.
- llm-data-pipeline and news-dedup-rules show the AI experience is designed around curated, deduplicated news and health snapshots rather than generic chat.
10. Presentation and UX utilities
- Styling is split by concern in src/styles/* rather than one global stylesheet.
- Small utilities cover DOM helpers, sanitization, sparkline rendering, and storage.
- map-popup and breaking-news.css/climate.css etc. suggest the UI is strongly data-card driven with visual emphasis on alerts and trends.
11. External data model and enrichment
- The app appears to combine several data classes into one health-risk view:
- outbreak events
- climate-related alerts
- news-derived signals
- province/district population context
- Population normalization matters because per-capita comparisons and choropleth severity depend on regional context, not just raw counts.
12. Deployment and operational boundary
- wrangler.toml, public/_headers, public/_redirects, and public/_routes.json show deployment is tuned for Cloudflare Pages/Workers routing and static delivery.
- dev-api-middleware.ts supports local development by bridging the frontend to the API layer.
- The README’s note that data ingestion happens on a separate machine is important architecturally: the repo is the presentation + API + AI layer, not the crawler itself.
13. Quality and verification
- e2e/app-smoke.spec.ts with Playwright covers a smoke path for the integrated app.
- This suggests the critical system contract is: static app loads, API endpoints respond, and key map/panel flows render against seeded data.
14. How to diagram it
- Draw three main zones: browser UI, Cloudflare serverless/API, and external LLM/data sources.
- Show the browser consuming health/news/climate snapshots from the API, rendering them on a map and in panels.
- Show chat as a separate request path that goes through the serverless endpoint into an LLM provider.
- Show the upstream crawler/Mac Mini feeding D1 as an off-repo ingestion pipeline that populates what the API serves.
Main entry points: app-layout.ts Layout · api-client.ts API client · _middleware.ts Middleware · index.ts Map layers
Components 27
Browser UI
Client-side single-page app, map, panels, and local state.
- App boot · Entry point that starts the client application.
src/main.ts - App init · Initializes global app state and wiring.
src/app/app-init.ts - Layout · Composes the map-centric page structure and responsive regions.
src/app/app-layout.ts - App context · Shared client state and context for the UI.
src/app/app-context.ts - Map shell · Main map container and interaction surface.
src/components/map-shell.ts - Map layers · Country, district, outbreak, early-warning, and severity overlays.
src/components/map-layers/index.ts - Info panels · Disease, climate, news, and case-report panels shown alongside the map.
src/components/index.ts - Chat panel · Conversational UI for asking questions about health risk signals.
src/components/chat-panel.ts - Mobile tabs · Mobile affordance that replaces desktop panel navigation.
src/components/mobile-tab-bar.ts - Panel state · Shared panel behavior and persistence across visits.
src/components/panel-base.ts - API client · Common client entry point for fetching health data.
src/services/api-client.ts - Snapshots · Client-side view of precomputed time-series and cached snapshots.
src/services/snapshot-store.ts - Trend calc · Derives trend signals used in map layers and panels.
src/services/trend-calculator.ts - LLM router · Selects and drives chat model backends.
src/services/llm-router.ts - Prompt context · Builds grounded context for chat from app data.
src/services/llm-context-builder.ts
Cloudflare Edge
Serverless API and shared middleware deployed on Cloudflare Pages/Workers.
- Middleware · Cross-cutting request handling such as CORS and caching.
functions/_middleware.ts - Health API · Aggregated health data endpoints served from Cloudflare Functions.
functions/api/health/v1/all.ts - News API · News signal endpoint for the UI.
functions/api/health/v1/news.ts - Climate API · Climate alert endpoint for the UI.
functions/api/health/v1/climate.ts - Outbreak API · Outbreak endpoint for disease events and normalization.
functions/api/health/v1/outbreaks.ts - Chat API · Serverless conversational endpoint that streams AI responses.
functions/api/chat.ts - Shared data · Lookup, cache, CORS, and outbreak normalization helpers.
functions/_shared/outbreak-query.ts
Data Sources
External model providers and upstream data ingestion feeding the app.
- D1 snapshots · Persisted health snapshots and precomputed data served by the API.
- Crawler pipeline · Separate crawl/extract pipeline on a Mac Mini that refreshes D1.
- OpenRouter · Primary external model gateway for chat inference.
- Local providers · Optional Ollama, MLX, and proxy backends for chat.
- District geojson · Vietnam district boundaries used for choropleths and map overlays.
public/data/vietnam-districts.geojson
Connections 35
- App boot → App init: boots · Starts the application shell.
- App init → App context: creates · Initializes shared state.
- App init → Layout: mounts · Composes the page structure.
- Layout → Map shell: contains · Places the map as the central workspace.
- Layout → Info panels: contains · Places health panels around the map.
- Layout → Chat panel: includes · Exposes the AI chat surface.
- Layout → Mobile tabs: adapts · Switches panel navigation on small screens.
- Info panels → Panel state: shares · Uses common panel persistence and behavior.
- Map shell → Map layers: renders · Adds choropleths, markers, and heat overlays.
- Map layers → District geojson: reads boundaries · Uses district geometry for geographic visualization.
- Info panels → API client: fetches data · Loads health, climate, news, and outbreak content.
- Map layers → Snapshots: consumes · Uses snapshot-backed metrics for visualization.
- Trend calc → Map layers: feeds trends · Provides derived signals for overlays.
- API client → Health API: requests · Fetches aggregated health data.
- API client → News API: requests · Fetches news signals.
- API client → Climate API: requests · Fetches climate alerts.
- API client → Outbreak API: requests · Fetches outbreak records.
- Middleware → Health API: wraps · Applies cross-cutting request handling.
- Middleware → News API: wraps · Applies cross-cutting request handling.
- Middleware → Climate API: wraps · Applies cross-cutting request handling.
- Middleware → Outbreak API: wraps · Applies cross-cutting request handling.
- Health API → Shared data: uses · Normalizes and enriches returned records.
- News API → Shared data: uses · Shares cache and lookup helpers.
- Climate API → Shared data: uses · Shares cache and lookup helpers.
- Outbreak API → Shared data: uses · Shares outbreak and population helpers.
- Health API → D1 snapshots: reads · Serves persisted health snapshots.
- News API → D1 snapshots: reads · Serves news-backed snapshots.
- Climate API → D1 snapshots: reads · Serves climate-backed snapshots.
- Outbreak API → D1 snapshots: reads · Serves outbreak-backed snapshots.
- Crawler pipeline → D1 snapshots: writes · Refreshes the stored dataset out of band.
- Chat panel → Chat API: calls · Sends chat requests to the serverless endpoint.
- Chat API → LLM router: routes · Chooses the active inference backend.
- LLM router → OpenRouter: uses · Primary hosted model path.
- LLM router → Local providers: falls back to · Optional local or proxy providers.
- Chat API → Prompt context: builds context · Assembles grounded prompt context from app data.