From 14c4ea13d914394e61fe90d37dc712faae7a0311 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 28 Mar 2026 10:45:36 +0100 Subject: [PATCH] feat: Phase 0b - add avg_per_week_30d aggregation method - Calculates average count per week over 30 days - Use case: Training frequency per week (smoothed) - Formula: (count in 30 days) / 4.285 weeks - Documentation: .claude/docs/technical/AGGREGATION_METHODS.md --- backend/goal_utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/goal_utils.py b/backend/goal_utils.py index 25cc5e5..f128325 100644 --- a/backend/goal_utils.py +++ b/backend/goal_utils.py @@ -407,6 +407,21 @@ def _fetch_by_aggregation_method( row = cur.fetchone() return float(row['max_value']) if row and row['max_value'] is not None else None + elif method == 'avg_per_week_30d': + # Average count per week over 30 days + # Use case: Training frequency per week (smoothed over 4.3 weeks) + days_ago = date.today() - timedelta(days=30) + params = [profile_id, days_ago] + filter_params + cur.execute(f""" + SELECT COUNT(*) as count_value FROM {table} + WHERE profile_id = %s AND {date_col} >= %s{filter_sql} + """, params) + row = cur.fetchone() + if row and row['count_value'] is not None: + # 30 days = 4.285 weeks (30/7) + return round(float(row['count_value']) / 4.285, 2) + return None + else: print(f"[WARNING] Unknown aggregation method: {method}") return None