fix: Phase 0b - fix remaining calculation errors
Deploy Development / deploy (push) Successful in 47s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 13s

Fixes applied:
1. WHERE clause column names (total_sleep_min → duration_minutes, resting_heart_rate → resting_hr)
2. COUNT() column names (avg_heart_rate → hr_avg, quality_label → rpe)
3. Type errors (Decimal * float) - convert to float before multiplication
4. rest_days table (type column removed in migration 010, now uses rest_config JSONB)
5. c_thigh_l → c_thigh (no separate left/right columns)
6. focus_area_definitions queries (focus_area_id → key, label_de → name_de)

Missing functions implemented:
- goal_utils.get_active_goals() - queries goals table for active goals
- goal_utils.get_goal_by_id() - gets single goal
- calculations.scores.calculate_category_progress() - maps categories to score functions

Changes:
- activity_metrics.py: Fixed Decimal/float type errors, rest_config JSONB, data quality query
- recovery_metrics.py: Fixed all WHERE clause column names
- body_metrics.py: Fixed c_thigh column reference
- scores.py: Fixed focus_area queries, added calculate_category_progress()
- goal_utils.py: Added get_active_goals(), get_goal_by_id()

All calculation functions should now work with correct schema.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 4.6 committed 2026-03-28 08:39:31 +01:00
1 parent 4817fd2b29
commit dd3a4111fc
6 files changed
+112 -36

No files matched your search

+48 -5
View File
@@ -109,12 +109,12 @@ def calculate_category_weight(profile_id: str, category: str) -> float:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT focus_area_id
SELECT key
FROM focus_area_definitions
WHERE category = %s
""", (category,))
focus_areas = [row['focus_area_id'] for row in cur.fetchall()]
focus_areas = [row['key'] for row in cur.fetchall()]
total_weight = sum(
focus_weights.get(fa, 0)
@@ -446,9 +446,9 @@ def get_top_focus_area(profile_id: str) -> Optional[Dict]:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT focus_area_id, label_de, category
SELECT key, name_de, category
FROM focus_area_definitions
WHERE focus_area_id = %s
WHERE key = %s
""", (top_fa_id,))
fa_def = cur.fetchone()
@@ -460,7 +460,7 @@ def get_top_focus_area(profile_id: str) -> Optional[Dict]:
return {
'focus_area_id': top_fa_id,
'label': fa_def['label_de'],
'label': fa_def['name_de'],
'category': fa_def['category'],
'weight': focus_weights[top_fa_id],
'progress': progress
@@ -495,3 +495,46 @@ def calculate_focus_area_progress(profile_id: str, focus_area_id: str) -> Option
total_weight = sum(g['contribution_weight'] for g in goals)
return int(total_progress / total_weight) if total_weight > 0 else None
def calculate_category_progress(profile_id: str, category: str) -> Optional[int]:
"""
Calculate progress score for a focus area category (0-100).
Args:
profile_id: User's profile ID
category: Category name ('körper', 'ernährung', 'aktivität', 'recovery', 'vitalwerte', 'mental', 'lebensstil')
Returns:
Progress score 0-100 or None if no data
"""
# Map category to score calculation functions
category_scores = {
'körper': 'body_progress_score',
'ernährung': 'nutrition_score',
'aktivität': 'activity_score',
'recovery': 'recovery_score',
'vitalwerte': 'recovery_score', # Use recovery score as proxy for vitals
'mental': 'recovery_score', # Use recovery score as proxy for mental (sleep quality)
'lebensstil': 'data_quality_score', # Use data quality as proxy for lifestyle consistency
}
score_func_name = category_scores.get(category.lower())
if not score_func_name:
return None
# Call the appropriate score function
if score_func_name == 'body_progress_score':
from calculations.body_metrics import calculate_body_progress_score
return calculate_body_progress_score(profile_id)
elif score_func_name == 'nutrition_score':
from calculations.nutrition_metrics import calculate_nutrition_score
return calculate_nutrition_score(profile_id)
elif score_func_name == 'activity_score':
from calculations.activity_metrics import calculate_activity_score
return calculate_activity_score(profile_id)
elif score_func_name == 'recovery_score':
return calculate_recovery_score_v2(profile_id)
elif score_func_name == 'data_quality_score':
return calculate_data_quality_score(profile_id)
return None