diff --git a/backend/goal_utils.py b/backend/goal_utils.py index 7378792..09bd9cc 100644 --- a/backend/goal_utils.py +++ b/backend/goal_utils.py @@ -285,6 +285,21 @@ def _fetch_by_aggregation_method( print(f"[WARNING] Missing source_table or source_column for aggregation") return None + # Table-specific date column mapping (some tables use different column names) + DATE_COLUMN_MAP = { + 'blood_pressure_log': 'measured_at', + 'activity_log': 'date', + 'weight_log': 'date', + 'circumference_log': 'date', + 'caliper_log': 'date', + 'nutrition_log': 'date', + 'sleep_log': 'date', + 'vitals_baseline': 'date', + 'rest_days': 'date', + 'fitness_tests': 'test_date' + } + date_col = DATE_COLUMN_MAP.get(table, 'date') + # Build filter SQL from JSON conditions filter_sql = "" filter_params = [] @@ -317,7 +332,7 @@ def _fetch_by_aggregation_method( cur.execute(f""" SELECT {column} FROM {table} WHERE profile_id = %s AND {column} IS NOT NULL{filter_sql} - ORDER BY date DESC LIMIT 1 + ORDER BY {date_col} DESC LIMIT 1 """, params) row = cur.fetchone() return float(row[column]) if row else None @@ -327,7 +342,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT AVG({column}) as avg_value FROM {table} - WHERE profile_id = %s AND date >= %s AND {column} IS NOT NULL{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s AND {column} IS NOT NULL{filter_sql} """, params) row = cur.fetchone() return float(row['avg_value']) if row and row['avg_value'] is not None else None @@ -337,7 +352,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT AVG({column}) as avg_value FROM {table} - WHERE profile_id = %s AND date >= %s AND {column} IS NOT NULL{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s AND {column} IS NOT NULL{filter_sql} """, params) row = cur.fetchone() return float(row['avg_value']) if row and row['avg_value'] is not None else None @@ -347,7 +362,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT SUM({column}) as sum_value FROM {table} - WHERE profile_id = %s AND date >= %s AND {column} IS NOT NULL{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s AND {column} IS NOT NULL{filter_sql} """, params) row = cur.fetchone() return float(row['sum_value']) if row and row['sum_value'] is not None else None @@ -357,7 +372,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT COUNT(*) as count_value FROM {table} - WHERE profile_id = %s AND date >= %s{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s{filter_sql} """, params) row = cur.fetchone() return float(row['count_value']) if row else 0.0 @@ -367,7 +382,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT COUNT(*) as count_value FROM {table} - WHERE profile_id = %s AND date >= %s{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s{filter_sql} """, params) row = cur.fetchone() return float(row['count_value']) if row else 0.0 @@ -377,7 +392,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT MIN({column}) as min_value FROM {table} - WHERE profile_id = %s AND date >= %s AND {column} IS NOT NULL{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s AND {column} IS NOT NULL{filter_sql} """, params) row = cur.fetchone() return float(row['min_value']) if row and row['min_value'] is not None else None @@ -387,7 +402,7 @@ def _fetch_by_aggregation_method( params = [profile_id, days_ago] + filter_params cur.execute(f""" SELECT MAX({column}) as max_value FROM {table} - WHERE profile_id = %s AND date >= %s AND {column} IS NOT NULL{filter_sql} + WHERE profile_id = %s AND {date_col} >= %s AND {column} IS NOT NULL{filter_sql} """, params) row = cur.fetchone() return float(row['max_value']) if row and row['max_value'] is not None else None