Commit 9016c3c
Feature/analytics engine (#9)
* feat: Add analytics baseline infrastructure (Sprint 1.1-1.6)
- Add UserBaseline model with IQR-based anomaly detection
- Add Alembic migration for user_baselines table and analytics indices
- Implement BaselineService with calculations for:
- HRV RMSSD (from nightly recharge)
- Sleep score
- Resting heart rate
- Training load and ratio
- Add baselines API endpoints:
- GET /users/{id}/baselines - List all baselines
- GET /users/{id}/baselines/{metric} - Get specific baseline
- POST /users/{id}/baselines/calculate - Trigger calculation
- GET /users/{id}/baselines/check/{metric}/{value} - Anomaly check
- GET /users/{id}/analytics/status - Data readiness and feature unlocks
Features available based on data days:
- 7 days: Basic stats
- 14 days: Trend analysis
- 21 days: Personalized baselines
- 30 days: Predictive models
- 60 days: Advanced ML
- 90 days: Long-term patterns
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat: Add baseline recalculation and comprehensive tests (Sprint 1.7-1.9)
- Add automatic baseline recalculation after sync operations
- Create test data seeding fixtures with 90 days of realistic data
- Weekly HRV patterns (Monday dips, Friday peaks)
- Sleep score variations
- Training load periodization (4-week cycles)
- Occasional anomalies (2% chance)
- Add analytics fixtures for different data scenarios (90d, 21d, 7d, 3d)
- Write 13 comprehensive tests covering:
- Baseline status levels (ready/partial/insufficient)
- IQR calculations and bounds
- Anomaly detection (warning/critical thresholds)
- Multi-metric baseline calculation
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* style: Fix import sorting and formatting
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: Add missing type parameters for mypy strict mode
- Add type params to ASGIConnection[Any, Any, Any, Any]
- Add type params to Response[Any]
- Add type params to Request[Any, Any, Any]
- Import ASGIApp from litestar.types
- Add type: ignore for scope comparison (litestar typing issue)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: Update Claude Code Review workflow to use sticky comments
- Enable use_sticky_comment for automatic PR comment posting
- Remove gh pr comment from allowed tools (was causing shell escaping issues)
- Add Write tool for flexibility
- Update prompt to output review directly instead of posting manually
The previous approach had Claude try to post via gh pr comment, but
markdown with code blocks caused shell escaping failures.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: Address code review findings for baselines API
- Fix type safety in is_anomaly(): Remove type: ignore comments by
checking all required values (q1, q3, iqr) before calculations
- Add user_id format validation: Regex-based validation prevents
injection attacks, limits to alphanumeric with _ and - only
- Add float input validation: Reject NaN, inf, -inf values in
check_anomaly endpoint to prevent edge case errors
- Remove redundant quantile check: quantiles(n=4) always returns
exactly [Q1, Q2, Q3], no length check needed
- Document service-level API key authorization model: Clarify that
user_id=None keys intentionally have admin access for SaaS backends
Security improvements:
- ValidationException for invalid user_id format (1-100 chars)
- ValidationException for non-finite float values
- Don't reveal all valid metric names in error messages
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent e9b0f8e commit 9016c3c
File tree
17 files changed
+1825
-12
lines changed- .github/workflows
- alembic/versions
- src/polar_flow_server
- api
- core
- middleware
- models
- services
- tests
- fixtures
17 files changed
+1825
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | | - | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
43 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
Lines changed: 136 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
0 commit comments