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