# kilapkilap25/emr_diseasetrendanalytics architecture

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

- Interactive diagram: https://gitdiagram.com/kilapkilap25/emr_diseasetrendanalytics
- Repository: https://github.com/kilapkilap25/emr_diseasetrendanalytics

## Overview

Valenzuela District 1 is a standalone, browser-based barangay EMR: health workers use it to manage patient care, records, prescriptions and inventory, while administrators also manage accounts and generate reports. Analytics, disease surveillance, pregnancy tracking, AI insights and real-time updates are additional product branches. The README documents the browser/API and database roles; source-index references establish many backend module relationships. Some frontend-to-feature wiring and unsampled router behavior are not shown, so those links are omitted rather than inferred.

Main entry points: [`emr_system/backend/main.py`](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/main.py) (FastAPI application), [`emr_system/backend/database.py`](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/database.py) (Barangay database), [`emr_system/backend/routers/prescriptions.py`](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/prescriptions.py) (Prescriptions), [`emr_system/backend/routers/patients.py`](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/patients.py) (Patient records)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Browser experience"]
  node_browser["EMR browser<br/>[api.js]"]
  node_patient_ui["Patient workspace<br/>[patients.js]"]
  node_records_ui["Visit records<br/>[records.js]"]
  node_dashboard_ui["Dashboard<br/>[dashboard.js]"]
  node_inventory_ui["Inventory UI<br/>[inventory.js]"]
  node_ai_ui["AI insights UI<br/>[ai-insights.js]"]
end

subgraph group_api["Clinical API"]
  node_app["FastAPI application<br/>[main.py]"]
  node_auth_routes["Authentication<br/>[auth.py]"]
  node_users["User management<br/>[users.py]"]
  node_ws_routes["WebSocket endpoint<br/>[websocket.py]"]
end

subgraph group_care["Care operations"]
  node_patients["Patient records<br/>[patients.py]"]
  node_pregnancy["Pregnancy care<br/>[pregnancy.py]"]
  node_prescriptions["Prescriptions<br/>[prescriptions.py]"]
  node_inventory["Inventory<br/>[inventory.py]"]
  node_queue["Visit queue<br/>[queue.py]"]
  node_notifications["Patient notifications<br/>[notifications.py]"]
end

subgraph group_insights["Insights and reporting"]
  node_diseases["Disease surveillance<br/>[diseases.py]"]
  node_analytics["Analytics<br/>[analytics.py]"]
  node_geo["Geographic analytics<br/>[analytics_geo.py]"]
  node_ai_insights["AI insights API<br/>[ai_insights.py]"]
  node_ai_resources["AI resources<br/>[ai_resources.py]"]
  node_reports["PDF reports<br/>[reports.py]"]
end

subgraph group_foundation["Data and security"]
  node_database[("Barangay database<br/>[database.py]")]
  node_models["EMR data models<br/>[models.py]"]
  node_middleware["Auth and permissions<br/>[auth.py]"]
  node_security["Token security<br/>[security.py]"]
  node_batches["Inventory batches"]
  node_rx_status["Prescription status"]
  node_ws_manager["Realtime events"]
end

node_staff(("Health staff"))

node_staff -->|"uses"| node_browser
node_browser -->|"calls API"| node_app
node_app -->|"registers"| node_auth_routes
node_app -->|"registers"| node_users
node_app -->|"registers"| node_patients
node_app -->|"registers"| node_prescriptions
node_app -->|"registers"| node_inventory
node_app -->|"registers"| node_queue
node_app -->|"registers"| node_pregnancy
node_app -->|"registers"| node_analytics
node_app -->|"registers"| node_reports
node_app -->|"registers"| node_ai_insights
node_app -->|"registers"| node_ai_resources
node_app -->|"registers"| node_ws_routes
node_auth_routes -->|"uses"| node_security
node_middleware -->|"verifies tokens"| node_security
node_middleware -->|"queries users"| node_database
node_patients -->|"uses rules"| node_pregnancy
node_prescriptions -->|"allocates stock"| node_batches
node_prescriptions -->|"checks status"| node_rx_status
node_inventory -->|"manages batches"| node_batches
node_inventory -->|"recomputes status"| node_rx_status
node_prescriptions -->|"broadcasts events"| node_ws_manager
node_queue -->|"broadcasts events"| node_ws_manager
node_ws_routes -->|"manages connections"| node_ws_manager
node_geo -->|"uses analytics"| node_analytics
node_analytics -.->|"serves analytics"| node_dashboard_ui
node_ai_insights -.->|"serves insights"| node_ai_ui
node_staff -.->|"manages patients"| node_patient_ui
node_staff -.->|"records visits"| node_records_ui
node_staff -.->|"handles stock"| node_inventory_ui
node_app -->|"uses sessions"| node_database
node_patients -->|"queries records"| node_database
node_prescriptions -->|"reads and writes"| node_database
node_analytics -->|"queries data"| node_database
node_reports -->|"queries data"| node_database
node_ai_insights -->|"queries data"| node_database
node_ai_resources -->|"queries data"| node_database
node_patients -->|"uses models"| node_models
node_prescriptions -->|"uses models"| node_models
node_diseases -->|"uses models"| node_models
node_reports -->|"uses models"| node_models
node_notifications -->|"queries data"| node_database

click node_browser "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/api.js"
click node_patient_ui "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/patients.js"
click node_records_ui "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/records.js"
click node_dashboard_ui "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/dashboard.js"
click node_inventory_ui "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/inventory.js"
click node_ai_ui "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/ai-insights.js"
click node_app "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/main.py"
click node_auth_routes "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/auth.py"
click node_users "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/users.py"
click node_patients "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/patients.py"
click node_pregnancy "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/pregnancy.py"
click node_prescriptions "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/prescriptions.py"
click node_inventory "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/inventory.py"
click node_queue "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/queue.py"
click node_notifications "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/notifications.py"
click node_diseases "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/diseases.py"
click node_analytics "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/analytics.py"
click node_geo "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/analytics_geo.py"
click node_ai_insights "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/ai_insights.py"
click node_ai_resources "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/ai_resources.py"
click node_reports "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/reports.py"
click node_database "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/database.py"
click node_models "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/models/models.py"
click node_middleware "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/middleware/auth.py"
click node_security "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/security.py"
click node_batches "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/inventory_batches.py"
click node_rx_status "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/prescription_status.py"
click node_ws_manager "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/websocket_manager.py"
click node_ws_routes "https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/websocket.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_browser,node_patient_ui,node_records_ui,node_dashboard_ui,node_inventory_ui,node_ai_ui toneBlue
class node_app,node_auth_routes,node_users,node_ws_routes toneAmber
class node_patients,node_pregnancy,node_prescriptions,node_inventory,node_queue,node_notifications toneMint
class node_diseases,node_analytics,node_geo,node_ai_insights,node_ai_resources,node_reports toneRose
class node_database,node_models,node_middleware,node_security,node_batches,node_rx_status,node_ws_manager,node_staff toneIndigo
```

## Components

### Browser experience

- [EMR browser](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/api.js): `emr_system/frontend/js/api.js`
- [Patient workspace](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/patients.js): `emr_system/frontend/js/patients.js`
- [Visit records](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/records.js): `emr_system/frontend/js/records.js`
- [Dashboard](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/dashboard.js): `emr_system/frontend/js/dashboard.js`
- [Inventory UI](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/inventory.js): `emr_system/frontend/js/inventory.js`
- [AI insights UI](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/frontend/js/ai-insights.js): `emr_system/frontend/js/ai-insights.js`

### Clinical API

- [FastAPI application](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/main.py): `emr_system/backend/main.py`
- [Authentication](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/auth.py): `emr_system/backend/routers/auth.py`
- [User management](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/users.py): `emr_system/backend/routers/users.py`
- [WebSocket endpoint](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/websocket.py): `emr_system/backend/routers/websocket.py`

### Care operations

- [Patient records](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/patients.py): `emr_system/backend/routers/patients.py`
- [Pregnancy care](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/pregnancy.py): `emr_system/backend/routers/pregnancy.py`
- [Prescriptions](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/prescriptions.py): `emr_system/backend/routers/prescriptions.py`
- [Inventory](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/inventory.py): `emr_system/backend/routers/inventory.py`
- [Visit queue](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/queue.py): `emr_system/backend/routers/queue.py`
- [Patient notifications](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/notifications.py): `emr_system/backend/routers/notifications.py`

### Insights and reporting

- [Disease surveillance](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/diseases.py): `emr_system/backend/routers/diseases.py`
- [Analytics](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/analytics.py): `emr_system/backend/routers/analytics.py`
- [Geographic analytics](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/analytics_geo.py): `emr_system/backend/routers/analytics_geo.py`
- [AI insights API](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/ai_insights.py): `emr_system/backend/routers/ai_insights.py`
- [AI resources](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/ai_resources.py): `emr_system/backend/routers/ai_resources.py`
- [PDF reports](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/routers/reports.py): `emr_system/backend/routers/reports.py`

### Data and security

- [Barangay database](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/database.py): `emr_system/backend/database.py`
- [EMR data models](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/models/models.py): `emr_system/backend/models/models.py`
- [Auth and permissions](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/middleware/auth.py): `emr_system/backend/middleware/auth.py`
- [Token security](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/security.py): `emr_system/backend/utils/security.py`
- [Inventory batches](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/inventory_batches.py): `emr_system/backend/utils/inventory_batches.py`
- [Prescription status](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/utils/prescription_status.py): `emr_system/backend/utils/prescription_status.py`
- [Realtime events](https://github.com/kilapkilap25/emr_diseasetrendanalytics/blob/main/emr_system/backend/websocket_manager.py): `emr_system/backend/websocket_manager.py`

### Other components

- **Health staff**

## Connections

- Health staff → EMR browser: uses
- EMR browser → FastAPI application: calls API
- FastAPI application → Authentication: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → User management: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Patient records: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Prescriptions: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Inventory: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Visit queue: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Pregnancy care: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → Analytics: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → PDF reports: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → AI insights API: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → AI resources: registers (evidence: `emr_system/backend/main.py`)
- FastAPI application → WebSocket endpoint: registers (evidence: `emr_system/backend/main.py`)
- Authentication → Token security: uses (evidence: `emr_system/backend/routers/auth.py`)
- Auth and permissions → Token security: verifies tokens (evidence: `emr_system/backend/middleware/auth.py`)
- Auth and permissions → Barangay database: queries users (evidence: `emr_system/backend/middleware/auth.py`)
- Patient records → Pregnancy care: uses rules (evidence: `emr_system/backend/routers/patients.py`)
- Prescriptions → Inventory batches: allocates stock (evidence: `emr_system/backend/routers/prescriptions.py`)
- Prescriptions → Prescription status: checks status (evidence: `emr_system/backend/routers/prescriptions.py`)
- Inventory → Inventory batches: manages batches (evidence: `emr_system/backend/routers/inventory.py`)
- Inventory → Prescription status: recomputes status (evidence: `emr_system/backend/routers/inventory.py`)
- Prescriptions → Realtime events: broadcasts events (evidence: `emr_system/backend/routers/prescriptions.py`)
- Visit queue → Realtime events: broadcasts events (evidence: `emr_system/backend/routers/queue.py`)
- WebSocket endpoint → Realtime events: manages connections (evidence: `emr_system/backend/routers/websocket.py`)
- Geographic analytics → Analytics: uses analytics (evidence: `emr_system/backend/routers/analytics_geo.py`)
- Analytics → Dashboard: serves analytics
- AI insights API → AI insights UI: serves insights
- Health staff → Patient workspace: manages patients
- Health staff → Visit records: records visits
- Health staff → Inventory UI: handles stock
- FastAPI application → Barangay database: uses sessions (evidence: `emr_system/backend/main.py`)
- Patient records → Barangay database: queries records (evidence: `emr_system/backend/routers/patients.py`)
- Prescriptions → Barangay database: reads and writes (evidence: `emr_system/backend/routers/prescriptions.py`)
- Analytics → Barangay database: queries data (evidence: `emr_system/backend/routers/analytics.py`)
- PDF reports → Barangay database: queries data (evidence: `emr_system/backend/routers/reports.py`)
- AI insights API → Barangay database: queries data (evidence: `emr_system/backend/routers/ai_insights.py`)
- AI resources → Barangay database: queries data (evidence: `emr_system/backend/routers/ai_resources.py`)
- Patient records → EMR data models: uses models (evidence: `emr_system/backend/routers/patients.py`)
- Prescriptions → EMR data models: uses models (evidence: `emr_system/backend/routers/prescriptions.py`)
- Disease surveillance → EMR data models: uses models (evidence: `emr_system/backend/routers/diseases.py`)
- PDF reports → EMR data models: uses models (evidence: `emr_system/backend/routers/reports.py`)
- Patient notifications → Barangay database: queries data (evidence: `emr_system/backend/routers/notifications.py`)
