Skip to content

Commit 92b4bfa

Browse files
committed
Fix analytics bugs: anomaly z-score sign, cache mode_config, bot logging
1 parent 9d7b240 commit 92b4bfa

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/analytics/clinical.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ def check_metric(
272272
if d.value is None:
273273
continue
274274
raw_z = (d.value - bl.mean) / bl.std
275-
z = -raw_z if invert else raw_z
276-
if not both_directions and z < 0:
275+
directional_z = -raw_z if invert else raw_z
276+
if not both_directions and directional_z < 0:
277277
continue
278-
abs_z = abs(z)
278+
abs_z = abs(raw_z)
279279
if abs_z >= ANOMALY_THRESHOLDS["warning"]:
280280
if abs_z >= ANOMALY_THRESHOLDS["critical"]:
281281
severity = "critical"
@@ -288,7 +288,7 @@ def check_metric(
288288
date=d.date,
289289
metric=display_name,
290290
value=d.value,
291-
z_score=z,
291+
z_score=raw_z,
292292
severity=severity,
293293
)
294294
)

src/analytics/pipeline.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from sqlalchemy.orm import Session
88

9+
from .constants import TREND_MODES
910
from .date_utils import local_today
1011
from .service import compute_health_analysis
1112
from .types import HealthAnalysis, TrendMode
@@ -79,8 +80,13 @@ def get_cached_snapshot(
7980
return None
8081

8182
try:
83+
config = TREND_MODES[mode]
8284
return HealthAnalysis.model_validate( # type: ignore[no-any-return]
83-
{**existing.snapshot_json, "mode": mode.value, "mode_config": {}}
85+
{
86+
**existing.snapshot_json,
87+
"mode": mode.value,
88+
"mode_config": config.model_dump(),
89+
}
8490
)
8591
except Exception:
8692
logger.warning(

src/analytics/smart_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ def build_smart_context(analysis: HealthAnalysis, max_anomalies: int = 3) -> dic
134134

135135
ctx["day_completeness"] = analysis.day_completeness
136136

137+
if analysis.data_source_summary:
138+
ctx["data_source_summary"] = [
139+
s.model_dump(exclude_none=True) for s in analysis.data_source_summary
140+
]
141+
137142
return ctx
138143

139144

src/bot/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from bot.bot import create_bot
22
from bot.config import BotConfig
3+
from logging_config import configure_logging
34

45

56
def main():
7+
configure_logging()
68
config = BotConfig()
79
app = create_bot(config)
810
app.run_polling()

0 commit comments

Comments
 (0)