Goalsystem V1 #50

Merged
Lars merged 51 commits from develop into main 2026-03-27 17:40:51 +01:00
Showing only changes of commit ca4f722b47 - Show all commits

View File

@ -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