Commit 7932589
authored
feat: v1.1.0 - Automatic Background Sync with Full Audit Trail (#12)
* feat: v1.1.0 - Automatic Background Sync with Full Audit Trail
## Added
**Automatic Background Sync**
- Smart sync scheduler with APScheduler for automatic background syncing
- Rate-limit-aware orchestration respecting Polar API limits (15-min and 24-hour windows)
- Priority queue system for efficient multi-user sync:
- CRITICAL: Users who haven't synced in 48h+ or have expiring tokens
- HIGH: Active users, hasn't synced in 12h+
- NORMAL: Regular users, hasn't synced in 24h+
- LOW: Dormant users, hasn't synced in 7d+
- Comprehensive `SyncLog` model for complete audit trail of every sync operation
- Consistent error classification with `SyncErrorType` enum covering:
- Authentication errors (TOKEN_EXPIRED, TOKEN_INVALID, TOKEN_REVOKED)
- Rate limiting (RATE_LIMITED_15M, RATE_LIMITED_24H)
- API errors (API_UNAVAILABLE, API_TIMEOUT, API_ERROR)
- Data errors (INVALID_RESPONSE, TRANSFORM_ERROR)
- Internal errors (DATABASE_ERROR, INTERNAL_ERROR)
- Post-sync analytics: Automatic baseline recalculation and pattern detection
**Configuration**
- `SYNC_ENABLED`: Enable/disable automatic syncing (default: true)
- `SYNC_INTERVAL_MINUTES`: Sync cycle interval (default: 60)
- `SYNC_ON_STARTUP`: Run sync immediately on startup (default: true)
- `SYNC_MAX_USERS_PER_RUN`: Maximum users per sync cycle (default: rate-limit aware)
- `SYNC_STAGGER_SECONDS`: Delay between user syncs (default: 5)
**Database**
- New `sync_logs` table with comprehensive fields for audit and debugging
- Composite indexes for efficient querying by user, status, and error type
Stu Mason + AI <me@stumason.dev>
* feat(admin): Update dashboard with sync status, history and biosensing
- Add sync scheduler status section with running state, next run time, 24h stats
- Add recent sync history table showing last 10 sync attempts
- Add biosensing data counts (SpO2, ECG, Temperature)
- Add analytics counts (Baselines, Patterns)
- Fix alertness scale display from /5 to /10 (Polar API uses 0-10 scale)
- Fix migration: Remove invalid 'comment' params from create_index calls
Stu Mason + AI <me@stumason.dev>
* style: Format routes.py
Stu Mason + AI <me@stumason.dev>
* fix: Address PR review - update last_synced_at and fix API call counting
Critical fixes from code review:
- Update User.last_synced_at after successful sync (priority queue now works)
- Fix API call counting: count data types with records, not record counts
- Clarify rate limit tracking limitation (requires SDK changes)
Note: APScheduler already in pyproject.toml, migration chain is correct.
Stu Mason + AI <me@stumason.dev>
* fix: Address additional PR review feedback
- Add warning log when user not found after sync (prevents silent failure)
- Track startup sync task and cancel on shutdown (fixes race condition)
- Update .env.example with all new sync settings
Note: "SQL injection" concern in review is incorrect - SQLAlchemy parameterizes
all queries. The actual concern (XSS) is mitigated by Jinja2 auto-escaping.
Stu Mason + AI <me@stumason.dev>
* chore: Bump version to 1.1.0 and finalize CHANGELOG
Stu Mason + AI <me@stumason.dev>1 parent 0e5244f commit 7932589
File tree
13 files changed
+2240
-29
lines changed- alembic/versions
- src/polar_flow_server
- admin
- core
- models
- services
- templates/admin
13 files changed
+2240
-29
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 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 | + | |
8 | 55 | | |
9 | 56 | | |
10 | 57 | | |
| |||
93 | 140 | | |
94 | 141 | | |
95 | 142 | | |
| 143 | + | |
96 | 144 | | |
97 | 145 | | |
98 | 146 | | |
| 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 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
0 commit comments