🔮 The Prediction Engine
The core value of Migraine Tracker lies in its ability to find hidden correlations between a user's health logs and the physical world. This is achieved through two highly specialized NestJS services within the src/predictions module: the Pattern Guardian and the Risk Calculator.
🛡️ The Pattern Guardian (PatternGuardianService)
Think of the Pattern Guardian as an asynchronous detective.
How it works:
- Observation: Every time a user logs a new
Incident(a migraine), the Guardian wakes up. - Correlation: It retrieves the exact environmental data (Weather, Barometric Pressure, Solar Flux, K-Index) for the exact location and time of that incident.
- Pattern Matching: It looks back at the user's historical logs. Did they also have a migraine the last time the Barometric pressure dropped by 5 hPa while the K-Index was above 4?
- Rule Creation: If a statistically significant pattern is found, the Guardian generates a
PredictionRule.
Example Rule generated by the Guardian:
IF Pressure < 1010 hPa AND Solar Flux > 150 THEN Risk = 80%
🧮 The Risk Calculator (RiskCalculatorService)
While the Pattern Guardian looks at the past, the Risk Calculator looks at today.
How it works:
- Every morning, or whenever the user loads their dashboard, the Risk Calculator fetches today's environmental forecast for the user's location.
- It then pulls all the active
PredictionRulescreated by the Pattern Guardian for that specific user. - It compares today's forecast against the rules.
- The Output: It calculates a final
RiskForecastpayload containing an overall probability percentage (e.g., "High Risk - 75%") and attaches a list of specific triggers that are active today.
🔒 Data Privacy in Predictions
It is crucial to note that rules are completely siloed per user. The engine does not share rules across accounts. User A's sensitivity to barometric drops will never influence User B's risk calculations.