NIAMR · National Integrated AMR Surveillance System

System Architecture
Dataflow & Database Design

Full DFD (Level 0 → Level 3), database schema, and tech-stack breakdown for a functional AMR surveillance prototype targeting high-burden districts.

PHP · Web API Layer
Flutter · Mobile App
Python · AI/ML Engine
MySQL · Primary DB
FHIR / HL7 · Interop Layer
Redis · Cache / Queue
DFD · L0 Context Diagram — The Big Picture

Level 0 shows the NIAMR system as a single process bubble, its external entities, and the high-level data flows between them. No internal detail is shown at this level.

LEVEL 0 — CONTEXT DIAGRAM NIAMR Surveillance System PHP · Python · Flutter Health Facilities (Labs, Clinics, Hospitals) Flutter App / USSD Patients / Community SMS / USSD District Health Officers Web Dashboard MoH / WHO GLASS / AfricaCDC FHIR API DHIS2 / HMIS Uganda REST / HL7 FHIR Open Data Sources (ATLAS / GLASS) ETL Pipeline Clinicians / Prescribers Treatment Reports, e-Prescriptions Web / Mobile Lab Results · Culture Data SMS Reports Surveillance Data FHIR Reports Sync Data ETL Ingestion Prescriptions Alerts / Reports Guidance LEGEND Inbound data flow Outbound / bidirectional flow
Health Facilities
Send lab culture results, antibiogram data, and prescription records via the Flutter mobile app (online) or USSD/SMS (offline/low-resource). Data is received by the PHP REST API.
DHIS2 / HMIS Uganda
Bidirectional sync via FHIR R4 compliant API layer built in PHP. NIAMR pushes standardized AMR bundles and pulls aggregate health metrics for context modeling.
AI/ML Engine (Python)
Python microservice receives preprocessed data from the ETL pipeline via internal API. Runs predictive models (scikit-learn, TensorFlow) and returns predictions stored in MySQL.
MoH / WHO GLASS
NIAMR exports surveillance summaries as HL7 FHIR bundles (MeasureReport, DiagnosticReport) mapped with LOINC and SNOMED CT codes for international compliance.
DFD · L1 Major Processes — System Decomposition

Level 1 explodes the single NIAMR bubble into 6 major functional processes. Data stores and inter-process flows become visible.

LEVEL 1 — MAJOR PROCESSES DS1: Raw Ingestion DS2: Clean Data DS3: FHIR Resources DS4: ML Predictions P1 Data Collection & Validation PHP API P2 ETL Pipeline Extract-Transform-Load Python P3 Interoperability FHIR/HL7 Layer PHP P4 ML Analytics & Prediction Engine Python · scikit-learn P5 Early Warning & Alert Engine Python · PHP P6 Dashboard & Reporting API PHP · Chart.js Health Facilities DHIS2 / MoH Open Datasets Clinicians / DHO WHO GLASS / AfricaCDC Lab data raw records extract CSV/JSON clean data features predictions alerts processed predictions SMS/Push alert standardize FHIR Bundle Sync export Dashboard views
P1 — Data Collection & Validation (PHP)
REST API endpoints receive lab results, prescriptions, antibiogram data. Validates completeness, range checks, and duplication via PHP validation middleware. Queues records in Redis.
P2 — ETL Pipeline (Python)
Celery workers extract from Redis queue & open datasets. Applies SMOTE for class imbalance, KNN imputation for missing values. Outputs standardised records to DS2: Clean Data.
P3 — Interoperability Layer (PHP)
Maps clean data to FHIR R4 resources: DiagnosticReport, MedicationRequest. Applies LOINC (lab codes), SNOMED CT (organisms), ICD-11 (diagnoses). Syncs with DHIS2.
P4 — ML Analytics Engine (Python)
Scikit-learn Random Forest for resistance prediction. DBSCAN for geographic clustering. ARIMA/Prophet for consumption trend forecasting. Results persisted to DS4.
P5 — Early Warning System (Python + PHP)
Threshold rules + anomaly detection trigger SMS/push alerts via Africa's Talking API. Tracks prescriber behaviour indices and generates non-conformance flags.
P6 — Dashboard & Reporting (PHP)
PHP API serves Chart.js/Leaflet visualizations. Endpoints: /api/resistance-trends, /api/geo-clusters, /api/prescriber-report. Role-based access (DHO, Clinician, Admin).
DFD · L2 Sub-Process Decomposition — P2 ETL & P4 ML Engine

Level 2 decomposes the two most complex processes: the ETL pipeline (P2) and the ML Analytics Engine (P4). Each sub-process maps to a specific module in the Python codebase.

LEVEL 2 — P2: ETL PIPELINE DECOMPOSITION ATLAS/GLASS Open Datasets Redis Queue Live Submissions CSV/JSON Files Uploaded Datasets P2.1 Extraction Pandas / Requests P2.2 Validation Great Expectations P2.3 Cleaning & Imputation KNN · MICE P2.4 Transformation Normalization · Encoding P2.5 Class Balancing SMOTE · ADASYN P2.6 Loading to DB SQLAlchemy · MySQL DS2: Clean Data DS-Err: Rejected raw extracted valid clean balanced transformed
LEVEL 2 — P4: ML ANALYTICS ENGINE DECOMPOSITION DS2: Clean Data P4.1 Feature Engineering scikit-learn Pipeline P4.2 Resistance Prediction Random Forest · XGBoost P4.3 Geo Clustering DBSCAN · GeoPandas P4.4 Temporal Forecasting Prophet · ARIMA P4.5 Model Scoring & Explainability SHAP · Calibration DS4: ML Predictions
DFD · L3 Detail Level — P1 Data Collection & P3 Interoperability

Level 3 shows granular internal logic for data ingestion (P1) and the FHIR interoperability layer (P3), including specific API endpoints, transformation rules, and terminology mappings.

LEVEL 3 — P1: DATA COLLECTION & VALIDATION (PHP) Flutter App /api/submit USSD Gateway Africa's Talking SMS Gateway Structured SMS Web Portal Batch Upload P1.1 Auth & Rate Limit JWT · Vanilla Php P1.2 Schema Validation JSON Schema P1.3 Business Rule Check MIC ranges · ATC codes P1.4 Dedup & Enrichment Fuzzy Match · GeoCode P1.5 Queue Dispatch Redis · Celery DS1: Raw Queue DS-Error: Rejected DS-Audit: Access Log HTTP POST auth'd valid enriched audit
LEVEL 3 — P3: FHIR/HL7 INTEROPERABILITY LAYER (PHP) DS2: Clean Data P3.1 Terminology Mapping LOINC · SNOMED · ICD-11 P3.2 FHIR Resource Builder DiagnosticReport · MedReq P3.3 Bundle Assembly & Validation HAPI FHIR Validator P3.4 External Push (DHIS2 / WHO) REST · OAuth2 P3.5 Internal FHIR Store Write MySQL · DS3 DS3: FHIR Resources DHIS2 Uganda HMIS WHO GLASS AfricaCDC
LOINC Mapping
Lab tests mapped: 18769-0 (Microbial susceptibility), 664-3 (Culture), 29576-6 (Antimicrobial MIC). Lookup table stored in MySQL with versioning.
SNOMED CT Mapping
Organisms: 59560006 (MRSA), 372687004 (E. coli), 88274000 (Klebsiella). Resistance mechanisms via SNOMED procedure codes.
ICD-11 Mapping
Diagnoses: XN109 (MRSA infection), 1C3Y (resistant bacterial infection). Used in FHIR Condition resources linked to each encounter.
FHIR R4 Resources Built
DiagnosticReport, Observation (MIC values), MedicationRequest, Patient, Encounter, Organization (facility), MeasureReport (aggregates).
DB MySQL Database Schema

All tables use UUID primary keys. Timestamps are stored as TIMESTAMPTZ. FHIR resources are also stored as JSONB for full-fidelity retrieval. Redis handles ephemeral queues and ML result caching.

Core Entities
facilities CORE
ColumnTypeNotes
PKidUUIDPrimary key
mfl_codeVARCHAR(20)Master Facility List code
nameVARCHAR(255)Facility name
facility_typeENUMHC2/HC3/HC4/Hospital
district_idUUID FK→ districts
latitudeDECIMAL(9,6)GPS coordinate
longitudeDECIMAL(9,6)GPS coordinate
dhis2_org_unitVARCHAR(100)DHIS2 UID for sync
fhir_organization_idVARCHAR(100)FHIR Organization.id
created_atTIMESTAMPTZAuto-set
patients CORE
ColumnTypeNotes
PKidUUIDPrimary key
patient_codeVARCHAR(50)De-identified code
age_groupENUM0-5,6-14,15-49,50+
sexENUMM/F/Unknown
district_idUUID FK→ districts
facility_idUUID FK→ facilities
fhir_patient_idVARCHAR(100)FHIR Patient.id
created_atTIMESTAMPTZAuto-set
districts CORE
ColumnTypeNotes
PKidUUIDPrimary key
nameVARCHAR(100)District name
regionVARCHAR(100)Region of Uganda
populationINTEGERLatest census
risk_levelENUMgreen/yellow/red
geomGEOMETRYPostGIS polygon
users CORE
ColumnTypeNotes
PKidUUIDPrimary key
emailVARCHAR(255)Unique
phoneVARCHAR(20)For SMS alerts
roleENUMadmin/dho/clinician/lab
facility_idUUID FK→ facilities
password_hashVARCHAR(255)bcrypt
last_loginTIMESTAMPTZ
Lab & Clinical Data
lab_specimens LAB
ColumnTypeNotes
PKidUUIDPrimary key
patient_idUUID FK→ patients
facility_idUUID FK→ facilities
specimen_typeVARCHAR(100)Blood/Urine/Sputum…
loinc_codeVARCHAR(20)LOINC specimen code
collection_dateDATE
diagnosis_icd11VARCHAR(20)ICD-11 code
fhir_specimen_idVARCHAR(100)FHIR Specimen.id
sourceENUMlive/atlas/glass/upload
microbiology_results LAB
ColumnTypeNotes
PKidUUIDPrimary key
specimen_idUUID FK→ lab_specimens
organism_nameVARCHAR(200)e.g. E. coli
snomed_organismVARCHAR(20)SNOMED CT code
report_dateDATE
fhir_diagnostic_idVARCHAR(100)FHIR DiagnosticReport
data_quality_scoreDECIMAL(4,2)0-1 completeness score
susceptibility_tests LAB
ColumnTypeNotes
PKidUUIDPrimary key
result_idUUID FK→ microbiology_results
antibiotic_idUUID FK→ antibiotics
mic_valueDECIMAL(8,3)Minimum inhibitory conc.
interpretationENUMS/I/R (EUCAST)
test_methodENUMdisk/MIC/VITEK
loinc_test_codeVARCHAR(20)LOINC 18769-0
fhir_observation_idVARCHAR(100)FHIR Observation.id
antibiotics REFERENCE
ColumnTypeNotes
PKidUUIDPrimary key
nameVARCHAR(200)Generic name
atc_codeVARCHAR(20)WHO ATC code
classVARCHAR(100)e.g. Cephalosporin
who_access_groupENUMACCESS/WATCH/RESERVE
snomed_codeVARCHAR(20)SNOMED substance code
prescriptions CLINICAL
ColumnTypeNotes
PKidUUIDPrimary key
patient_idUUID FK→ patients
prescriber_idUUID FK→ users
antibiotic_idUUID FK→ antibiotics
indication_icd11VARCHAR(20)ICD-11 indication
dose_mgDECIMAL(8,2)
routeENUMoral/IV/IM
duration_daysINTEGER
is_empiricBOOLEANBefore culture result
guideline_compliantBOOLEANAuto-checked
fhir_med_request_idVARCHAR(100)FHIR MedicationRequest
prescription_dateDATE
ETL Pipeline & Data Quality
etl_jobs PIPELINE
ColumnTypeNotes
PKidUUIDPrimary key
job_typeENUMingestion/validation/load
source_nameVARCHAR(100)ATLAS/GLASS/Live
statusENUMqueued/running/done/failed
records_inINTEGERRecords processed
records_acceptedINTEGER
records_rejectedINTEGER
started_atTIMESTAMPTZ
completed_atTIMESTAMPTZ
error_logJSONBStructured error details
data_quality_flags QA
ColumnTypeNotes
PKidUUIDPrimary key
record_typeVARCHAR(100)Table name of flagged row
record_idUUIDFK to the flagged record
flag_typeENUMmissing/outlier/duplicate
field_nameVARCHAR(100)Column with issue
original_valueTEXT
imputed_valueTEXTAfter KNN/MICE
resolvedBOOLEANDefault false
ML Models & Predictions
ml_models ML
ColumnTypeNotes
PKidUUIDPrimary key
model_nameVARCHAR(100)e.g. resistance_rf_v3
model_typeENUMclassification/cluster/forecast
algorithmVARCHAR(100)RandomForest/DBSCAN/Prophet
versionVARCHAR(20)Semantic version
accuracyDECIMAL(5,4)Test set score
auc_rocDECIMAL(5,4)
artifact_pathVARCHAR(500)S3/local .pkl path
trained_atTIMESTAMPTZ
is_activeBOOLEANCurrently serving
resistance_predictions ML
ColumnTypeNotes
PKidUUIDPrimary key
model_idUUID FK→ ml_models
organism_snomedVARCHAR(20)SNOMED CT code
antibiotic_atcVARCHAR(20)ATC code
district_idUUID FK→ districts
predicted_resistance_rateDECIMAL(5,4)0-1 probability
confidence_interval_lowDECIMAL(5,4)
confidence_interval_highDECIMAL(5,4)
prediction_periodDATEMonth of prediction
shap_valuesJSONBFeature importance
geo_clusters ML
ColumnTypeNotes
PKidUUIDPrimary key
model_idUUID FK→ ml_models
cluster_labelINTEGER-1 = noise
centroid_latDECIMAL(9,6)
centroid_lonDECIMAL(9,6)
radius_kmDECIMAL(8,3)Cluster extent
resistance_rateDECIMAL(5,4)Avg within cluster
organism_snomedVARCHAR(20)
detected_atTIMESTAMPTZ
consumption_forecasts ML
ColumnTypeNotes
PKidUUIDPrimary key
model_idUUID FK→ ml_models
antibiotic_idUUID FK→ antibiotics
district_idUUID FK→ districts
forecast_monthDATEFirst of month
forecasted_ddd_per_1000DECIMAL(10,3)WHO DDD metric
lower_boundDECIMAL(10,3)95% CI lower
upper_boundDECIMAL(10,3)95% CI upper
Alerts, FHIR & Audit
alerts ALERT
ColumnTypeNotes
PKidUUIDPrimary key
alert_typeENUMoutbreak/prescribing/threshold
severityENUMlow/medium/high/critical
district_idUUID FK→ districts
facility_idUUID FK→ facilities (nullable)
trigger_valueDECIMAL(10,4)Value that triggered
threshold_valueDECIMAL(10,4)Configured threshold
messageTEXTHuman-readable message
notified_usersUUID[]Array of user IDs
sms_sentBOOLEAN
push_sentBOOLEAN
resolvedBOOLEAN
created_atTIMESTAMPTZ
fhir_resources FHIR
ColumnTypeNotes
PKidUUIDInternal PK
fhir_idVARCHAR(100)FHIR resource ID
resource_typeVARCHAR(50)DiagnosticReport/Obs…
version_idINTEGERFHIR version
resource_jsonJSONBFull FHIR resource
synced_to_dhis2BOOLEAN
synced_to_glassBOOLEAN
last_updatedTIMESTAMPTZ
audit_logs AUDIT
ColumnTypeNotes
PKidUUIDPrimary key
user_idUUID FK→ users (nullable)
actionVARCHAR(100)CREATE/UPDATE/DELETE/VIEW
resource_typeVARCHAR(100)Table/endpoint name
resource_idUUIDAffected record
ip_addressINET
changesJSONBBefore/after diff
created_atTIMESTAMPTZIndexed
prescriber_behaviour_scores ANALYTICS
ColumnTypeNotes
PKidUUIDPrimary key
prescriber_idUUID FK→ users
period_monthDATEFirst of month
total_prescriptionsINTEGER
guideline_compliance_rateDECIMAL(5,4)0-1
watch_group_rateDECIMAL(5,4)WHO WATCH antibiotics %
empiric_rateDECIMAL(5,4)% without culture
amr_risk_scoreDECIMAL(5,2)Composite 0-100
STACK Technology Stack Map & Responsibility Boundaries

Each technology layer has a clearly defined responsibility boundary. Data crosses boundaries via well-defined interfaces (REST APIs, message queues, shared MySQL).

  • PHP
    PHP (Vanilla) — Web API & Interoperability
    REST API server, JWT auth, form validation, Redis queue dispatch, FHIR R4 resource builder, DHIS2/WHO sync, web dashboard rendering, role-based access, USSD webhook handler, SMS dispatch via Africa's Talking.
  • FLT
    Flutter — Mobile Application
    Cross-platform app (Android/iOS) for lab technicians and clinicians. Offline-first using SQLite with background sync. Forms for lab result submission, prescription entry, alert inbox. Barcode scanning for specimen ID. Low-bandwidth mode.
  • PY
    Python — ETL Pipeline & ML Engine
    Celery workers + Pandas for ETL. scikit-learn + XGBoost for resistance classification (Random Forest). DBSCAN (GeoPandas) for spatial clustering. Prophet + ARIMA for forecasting. SHAP for explainability. FastAPI microservice exposes /predict endpoints.
  • DB
    MySQL + PostGIS + Redis
    MySQL is the primary data store with PostGIS for geospatial queries. Redis handles the Celery job queue and caches ML predictions (TTL 6h). TimescaleDB extension for time-series aggregation on resistance trends.
  • HL7
    FHIR R4 / HL7 — Interoperability Layer
    HAPI FHIR validator for resource compliance. LOINC for lab test coding, SNOMED CT for organisms, ICD-11 for diagnoses, ATC for antimicrobials. Bidirectional sync with DHIS2 and one-way export to WHO GLASS / Africa CDC.
  • SMS
    USSD / SMS — Low-Resource Interface
    Africa's Talking USSD API with a session-based menu tree (PHP handler). Structured SMS parsing for basic-phone data entry. Two-way: facilities submit data + receive alerts. Offline community education via SMS broadcast.
COMPONENT ARCHITECTURE CLIENTS Flutter App iOS / Android Web Dashboard PHP + Chart.js USSD / SMS Africa's Talking External Systems DHIS2 · WHO GLASS AfricaCDC PHP API Gateway (Laravel) JWT Auth · Rate Limiting · Validation · Routing FHIR Builder · DHIS2 Sync · SMS Dispatch Redis Queue Celery Job Queue · ML Cache PYTHON SERVICES ETL Worker Pandas · Celery KNN Imputation SMOTE Balancing Great Expectations Validation ML Engine scikit-learn / XGBoost DBSCAN clustering Prophet forecasting SHAP explainability FastAPI /predict Alert Engine Threshold Rules Anomaly Detection SMS via AT API Push Notifications Email Reports DATA LAYER MySQL +PostGIS +TimescaleDB Redis Queue · Cache · Sessions FHIR Store JSONB Resources Open Data Sources (ETL Ingestion) WHO ATLAS · GLASS AMR · WHONET · ECCMID CSV / JSON / REST API