feat: Phase 0a - Minimal Goal System (Strategic + Tactical)
- Strategic Layer: Goal modes (weight_loss, strength, endurance, recomposition, health) - Tactical Layer: Concrete goal targets with progress tracking - Training phases (manual + auto-detection framework) - Fitness tests (standardized performance tracking) Backend: - Migration 022: goal_mode in profiles, goals, training_phases, fitness_tests tables - New router: routers/goals.py with full CRUD for goals, phases, tests - API endpoints: /api/goals/* (mode, list, create, update, delete) Frontend: - GoalsPage: Goal mode selector + goal management UI - Dashboard: Goals preview card with link - API integration: goal mode, CRUD operations, progress calculation Basis for 120+ placeholders and goal-aware analyses (Phase 0b) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae93b9d428
commit
337667fc07
8 files changed
+1813
No files matched your search
@@ -23,6 +23,7 @@ from routers import user_restrictions, access_grants, training_types, admin_trai
|
||||
from routers import admin_activity_mappings, sleep, rest_days
|
||||
from routers import vitals_baseline, blood_pressure # v9d Phase 2d Refactored
|
||||
from routers import evaluation # v9d/v9e Training Type Profiles (#15)
|
||||
from routers import goals # v9e Goal System (Strategic + Tactical)
|
||||
|
||||
# ── App Configuration ─────────────────────────────────────────────────────────
|
||||
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
||||
@@ -97,6 +98,7 @@ app.include_router(rest_days.router) # /api/rest-days/* (v9d Phase 2a
|
||||
app.include_router(vitals_baseline.router) # /api/vitals/baseline/* (v9d Phase 2d Refactored)
|
||||
app.include_router(blood_pressure.router) # /api/blood-pressure/* (v9d Phase 2d Refactored)
|
||||
app.include_router(evaluation.router) # /api/evaluation/* (v9d/v9e Training Profiles #15)
|
||||
app.include_router(goals.router) # /api/goals/* (v9e Goal System Strategic + Tactical)
|
||||
|
||||
# ── Health Check ──────────────────────────────────────────────────────────────
|
||||
@app.get("/")
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
-- Migration 022: Goal System (Strategic + Tactical)
|
||||
-- Date: 2026-03-26
|
||||
-- Purpose: Two-level goal architecture for AI-driven coaching
|
||||
|
||||
-- ============================================================================
|
||||
-- STRATEGIC LAYER: Goal Modes
|
||||
-- ============================================================================
|
||||
|
||||
-- Add goal_mode to profiles (strategic training direction)
|
||||
ALTER TABLE profiles ADD COLUMN IF NOT EXISTS goal_mode VARCHAR(50) DEFAULT 'health';
|
||||
|
||||
COMMENT ON COLUMN profiles.goal_mode IS
|
||||
'Strategic goal mode: weight_loss, strength, endurance, recomposition, health.
|
||||
Determines score weights and interpretation context for all analyses.';
|
||||
|
||||
-- ============================================================================
|
||||
-- TACTICAL LAYER: Concrete Goal Targets
|
||||
-- ============================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS goals (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
|
||||
-- Goal Classification
|
||||
goal_type VARCHAR(50) NOT NULL, -- weight, body_fat, lean_mass, vo2max, strength, flexibility, bp, rhr
|
||||
is_primary BOOLEAN DEFAULT false,
|
||||
status VARCHAR(20) DEFAULT 'active', -- draft, active, reached, abandoned, expired
|
||||
|
||||
-- Target Values
|
||||
target_value DECIMAL(10,2),
|
||||
current_value DECIMAL(10,2),
|
||||
start_value DECIMAL(10,2),
|
||||
unit VARCHAR(20), -- kg, %, ml/kg/min, bpm, mmHg, cm, reps
|
||||
|
||||
-- Timeline
|
||||
start_date DATE DEFAULT CURRENT_DATE,
|
||||
target_date DATE,
|
||||
reached_date DATE,
|
||||
|
||||
-- Metadata
|
||||
name VARCHAR(100), -- e.g., "Sommerfigur 2026"
|
||||
description TEXT,
|
||||
|
||||
-- Progress Tracking
|
||||
progress_pct DECIMAL(5,2), -- Auto-calculated: (current - start) / (target - start) * 100
|
||||
projection_date DATE, -- Prognose wann Ziel erreicht wird
|
||||
on_track BOOLEAN, -- true wenn Prognose <= target_date
|
||||
|
||||
-- Timestamps
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_goals_profile ON goals(profile_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_goals_status ON goals(profile_id, status);
|
||||
CREATE INDEX IF NOT EXISTS idx_goals_primary ON goals(profile_id, is_primary) WHERE is_primary = true;
|
||||
|
||||
COMMENT ON TABLE goals IS 'Concrete user goals (tactical targets)';
|
||||
COMMENT ON COLUMN goals.goal_type IS 'Type of goal: weight, body_fat, lean_mass, vo2max, strength, flexibility, bp, rhr';
|
||||
COMMENT ON COLUMN goals.is_primary IS 'Primary goal gets highest priority in scoring and charts';
|
||||
COMMENT ON COLUMN goals.status IS 'draft = not yet started, active = in progress, reached = successfully completed, abandoned = given up, expired = deadline passed';
|
||||
COMMENT ON COLUMN goals.progress_pct IS 'Percentage progress: (current_value - start_value) / (target_value - start_value) * 100';
|
||||
COMMENT ON COLUMN goals.projection_date IS 'Projected date when goal will be reached based on current trend';
|
||||
COMMENT ON COLUMN goals.on_track IS 'true if projection_date <= target_date (goal reachable on time)';
|
||||
|
||||
-- ============================================================================
|
||||
-- TRAINING PHASES (Auto-Detection)
|
||||
-- ============================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS training_phases (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
|
||||
-- Phase Classification
|
||||
phase_type VARCHAR(50) NOT NULL, -- calorie_deficit, calorie_surplus, deload, maintenance, periodization
|
||||
detected_automatically BOOLEAN DEFAULT false,
|
||||
confidence_score DECIMAL(3,2), -- 0.00 - 1.00 (Wie sicher ist die Erkennung?)
|
||||
status VARCHAR(20) DEFAULT 'suggested', -- suggested, accepted, active, completed, rejected
|
||||
|
||||
-- Timeframe
|
||||
start_date DATE NOT NULL,
|
||||
end_date DATE,
|
||||
duration_days INT,
|
||||
|
||||
-- Detection Criteria (JSONB für Flexibilität)
|
||||
detection_params JSONB, -- { "avg_calories": 1800, "weight_trend": -0.3, ... }
|
||||
|
||||
-- User Notes
|
||||
notes TEXT,
|
||||
|
||||
-- Timestamps
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_training_phases_profile ON training_phases(profile_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_training_phases_status ON training_phases(profile_id, status);
|
||||
CREATE INDEX IF NOT EXISTS idx_training_phases_dates ON training_phases(profile_id, start_date, end_date);
|
||||
|
||||
COMMENT ON TABLE training_phases IS 'Training phases detected from data patterns or manually defined';
|
||||
COMMENT ON COLUMN training_phases.phase_type IS 'calorie_deficit, calorie_surplus, deload, maintenance, periodization';
|
||||
COMMENT ON COLUMN training_phases.detected_automatically IS 'true if AI detected this phase from data patterns';
|
||||
COMMENT ON COLUMN training_phases.confidence_score IS 'AI confidence in detection (0.0 - 1.0)';
|
||||
COMMENT ON COLUMN training_phases.status IS 'suggested = AI proposed, accepted = user confirmed, active = currently running, completed = finished, rejected = user dismissed';
|
||||
COMMENT ON COLUMN training_phases.detection_params IS 'JSON with detection criteria: avg_calories, weight_trend, activity_volume, etc.';
|
||||
|
||||
-- ============================================================================
|
||||
-- FITNESS TESTS (Standardized Performance Tests)
|
||||
-- ============================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS fitness_tests (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
|
||||
-- Test Type
|
||||
test_type VARCHAR(50) NOT NULL, -- cooper_12min, step_test, pushups_max, plank_max, flexibility_sit_reach, vo2max_est, strength_1rm_squat, strength_1rm_bench
|
||||
result_value DECIMAL(10,2) NOT NULL,
|
||||
result_unit VARCHAR(20) NOT NULL, -- meters, bpm, reps, seconds, cm, ml/kg/min, kg
|
||||
|
||||
-- Test Metadata
|
||||
test_date DATE NOT NULL,
|
||||
test_conditions TEXT, -- Optional: Notizen zu Bedingungen
|
||||
norm_category VARCHAR(30), -- sehr gut, gut, durchschnitt, unterdurchschnitt, schlecht
|
||||
|
||||
-- Timestamps
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_fitness_tests_profile ON fitness_tests(profile_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_fitness_tests_type ON fitness_tests(profile_id, test_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_fitness_tests_date ON fitness_tests(profile_id, test_date);
|
||||
|
||||
COMMENT ON TABLE fitness_tests IS 'Standardized fitness tests (Cooper, step test, strength tests, etc.)';
|
||||
COMMENT ON COLUMN fitness_tests.test_type IS 'cooper_12min, step_test, pushups_max, plank_max, flexibility_sit_reach, vo2max_est, strength_1rm_squat, strength_1rm_bench';
|
||||
COMMENT ON COLUMN fitness_tests.norm_category IS 'Performance category based on age/gender norms';
|
||||
|
||||
-- ============================================================================
|
||||
-- VERSION UPDATE
|
||||
-- ============================================================================
|
||||
|
||||
-- Track migration
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = '022') THEN
|
||||
INSERT INTO schema_migrations (version, applied_at) VALUES ('022', NOW());
|
||||
END IF;
|
||||
END $$;
|
||||
@@ -0,0 +1,473 @@
|
||||
"""
|
||||
Goals Router - Goal System (Strategic + Tactical)
|
||||
|
||||
Endpoints for managing:
|
||||
- Strategic goal modes (weight_loss, strength, etc.)
|
||||
- Tactical goal targets (concrete values with deadlines)
|
||||
- Training phase detection
|
||||
- Fitness tests
|
||||
|
||||
Part of v9e Goal System implementation.
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List
|
||||
from datetime import date, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
import json
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
|
||||
router = APIRouter(prefix="/api/goals", tags=["goals"])
|
||||
|
||||
# ============================================================================
|
||||
# Pydantic Models
|
||||
# ============================================================================
|
||||
|
||||
class GoalModeUpdate(BaseModel):
|
||||
"""Update strategic goal mode"""
|
||||
goal_mode: str # weight_loss, strength, endurance, recomposition, health
|
||||
|
||||
class GoalCreate(BaseModel):
|
||||
"""Create or update a concrete goal"""
|
||||
goal_type: str # weight, body_fat, lean_mass, vo2max, strength, flexibility, bp, rhr
|
||||
is_primary: bool = False
|
||||
target_value: float
|
||||
unit: str # kg, %, ml/kg/min, bpm, mmHg, cm, reps
|
||||
target_date: Optional[date] = None
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
class GoalUpdate(BaseModel):
|
||||
"""Update existing goal"""
|
||||
target_value: Optional[float] = None
|
||||
target_date: Optional[date] = None
|
||||
status: Optional[str] = None # active, reached, abandoned, expired
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
class TrainingPhaseCreate(BaseModel):
|
||||
"""Create training phase (manual or auto-detected)"""
|
||||
phase_type: str # calorie_deficit, calorie_surplus, deload, maintenance, periodization
|
||||
start_date: date
|
||||
end_date: Optional[date] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
class FitnessTestCreate(BaseModel):
|
||||
"""Record fitness test result"""
|
||||
test_type: str
|
||||
result_value: float
|
||||
result_unit: str
|
||||
test_date: date
|
||||
test_conditions: Optional[str] = None
|
||||
|
||||
# ============================================================================
|
||||
# Strategic Layer: Goal Modes
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/mode")
|
||||
def get_goal_mode(session: dict = Depends(require_auth)):
|
||||
"""Get user's current strategic goal mode"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"SELECT goal_mode FROM profiles WHERE id = %s",
|
||||
(pid,)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Profil nicht gefunden")
|
||||
|
||||
return {
|
||||
"goal_mode": row['goal_mode'] or 'health',
|
||||
"description": _get_goal_mode_description(row['goal_mode'] or 'health')
|
||||
}
|
||||
|
||||
@router.put("/mode")
|
||||
def update_goal_mode(data: GoalModeUpdate, session: dict = Depends(require_auth)):
|
||||
"""Update user's strategic goal mode"""
|
||||
pid = session['profile_id']
|
||||
|
||||
# Validate goal mode
|
||||
valid_modes = ['weight_loss', 'strength', 'endurance', 'recomposition', 'health']
|
||||
if data.goal_mode not in valid_modes:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Ungültiger Goal Mode. Erlaubt: {', '.join(valid_modes)}"
|
||||
)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"UPDATE profiles SET goal_mode = %s WHERE id = %s",
|
||||
(data.goal_mode, pid)
|
||||
)
|
||||
|
||||
return {
|
||||
"goal_mode": data.goal_mode,
|
||||
"description": _get_goal_mode_description(data.goal_mode)
|
||||
}
|
||||
|
||||
def _get_goal_mode_description(mode: str) -> str:
|
||||
"""Get description for goal mode"""
|
||||
descriptions = {
|
||||
'weight_loss': 'Gewichtsreduktion (Kaloriendefizit, Fettabbau)',
|
||||
'strength': 'Kraftaufbau (Muskelwachstum, progressive Belastung)',
|
||||
'endurance': 'Ausdauer (VO2Max, aerobe Kapazität)',
|
||||
'recomposition': 'Körperkomposition (gleichzeitig Fett ab- und Muskeln aufbauen)',
|
||||
'health': 'Allgemeine Gesundheit (ausgewogen, präventiv)'
|
||||
}
|
||||
return descriptions.get(mode, 'Unbekannt')
|
||||
|
||||
# ============================================================================
|
||||
# Tactical Layer: Concrete Goals
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/list")
|
||||
def list_goals(session: dict = Depends(require_auth)):
|
||||
"""List all goals for current user"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT id, goal_type, is_primary, status,
|
||||
target_value, current_value, start_value, unit,
|
||||
start_date, target_date, reached_date,
|
||||
name, description,
|
||||
progress_pct, projection_date, on_track,
|
||||
created_at, updated_at
|
||||
FROM goals
|
||||
WHERE profile_id = %s
|
||||
ORDER BY is_primary DESC, created_at DESC
|
||||
""", (pid,))
|
||||
|
||||
goals = [r2d(row) for row in cur.fetchall()]
|
||||
|
||||
# Update current values for each goal
|
||||
for goal in goals:
|
||||
_update_goal_progress(conn, pid, goal)
|
||||
|
||||
return goals
|
||||
|
||||
@router.post("/create")
|
||||
def create_goal(data: GoalCreate, session: dict = Depends(require_auth)):
|
||||
"""Create new goal"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# If this is set as primary, unset other primary goals
|
||||
if data.is_primary:
|
||||
cur.execute(
|
||||
"UPDATE goals SET is_primary = false WHERE profile_id = %s",
|
||||
(pid,)
|
||||
)
|
||||
|
||||
# Get current value for this goal type
|
||||
current_value = _get_current_value_for_goal_type(conn, pid, data.goal_type)
|
||||
|
||||
# Insert goal
|
||||
cur.execute("""
|
||||
INSERT INTO goals (
|
||||
profile_id, goal_type, is_primary,
|
||||
target_value, current_value, start_value, unit,
|
||||
target_date, name, description
|
||||
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
pid, data.goal_type, data.is_primary,
|
||||
data.target_value, current_value, current_value, data.unit,
|
||||
data.target_date, data.name, data.description
|
||||
))
|
||||
|
||||
goal_id = cur.fetchone()['id']
|
||||
|
||||
return {"id": goal_id, "message": "Ziel erstellt"}
|
||||
|
||||
@router.put("/{goal_id}")
|
||||
def update_goal(goal_id: str, data: GoalUpdate, session: dict = Depends(require_auth)):
|
||||
"""Update existing goal"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Verify ownership
|
||||
cur.execute(
|
||||
"SELECT id FROM goals WHERE id = %s AND profile_id = %s",
|
||||
(goal_id, pid)
|
||||
)
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=404, detail="Ziel nicht gefunden")
|
||||
|
||||
# Build update query dynamically
|
||||
updates = []
|
||||
params = []
|
||||
|
||||
if data.target_value is not None:
|
||||
updates.append("target_value = %s")
|
||||
params.append(data.target_value)
|
||||
|
||||
if data.target_date is not None:
|
||||
updates.append("target_date = %s")
|
||||
params.append(data.target_date)
|
||||
|
||||
if data.status is not None:
|
||||
updates.append("status = %s")
|
||||
params.append(data.status)
|
||||
if data.status == 'reached':
|
||||
updates.append("reached_date = CURRENT_DATE")
|
||||
|
||||
if data.name is not None:
|
||||
updates.append("name = %s")
|
||||
params.append(data.name)
|
||||
|
||||
if data.description is not None:
|
||||
updates.append("description = %s")
|
||||
params.append(data.description)
|
||||
|
||||
if not updates:
|
||||
raise HTTPException(status_code=400, detail="Keine Änderungen angegeben")
|
||||
|
||||
updates.append("updated_at = NOW()")
|
||||
params.extend([goal_id, pid])
|
||||
|
||||
cur.execute(
|
||||
f"UPDATE goals SET {', '.join(updates)} WHERE id = %s AND profile_id = %s",
|
||||
tuple(params)
|
||||
)
|
||||
|
||||
return {"message": "Ziel aktualisiert"}
|
||||
|
||||
@router.delete("/{goal_id}")
|
||||
def delete_goal(goal_id: str, session: dict = Depends(require_auth)):
|
||||
"""Delete goal"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"DELETE FROM goals WHERE id = %s AND profile_id = %s",
|
||||
(goal_id, pid)
|
||||
)
|
||||
|
||||
if cur.rowcount == 0:
|
||||
raise HTTPException(status_code=404, detail="Ziel nicht gefunden")
|
||||
|
||||
return {"message": "Ziel gelöscht"}
|
||||
|
||||
# ============================================================================
|
||||
# Training Phases
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/phases")
|
||||
def list_training_phases(session: dict = Depends(require_auth)):
|
||||
"""List training phases"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT id, phase_type, detected_automatically, confidence_score,
|
||||
status, start_date, end_date, duration_days,
|
||||
detection_params, notes, created_at
|
||||
FROM training_phases
|
||||
WHERE profile_id = %s
|
||||
ORDER BY start_date DESC
|
||||
""", (pid,))
|
||||
|
||||
return [r2d(row) for row in cur.fetchall()]
|
||||
|
||||
@router.post("/phases")
|
||||
def create_training_phase(data: TrainingPhaseCreate, session: dict = Depends(require_auth)):
|
||||
"""Create training phase (manual)"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
duration = None
|
||||
if data.end_date:
|
||||
duration = (data.end_date - data.start_date).days
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO training_phases (
|
||||
profile_id, phase_type, detected_automatically,
|
||||
status, start_date, end_date, duration_days, notes
|
||||
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
pid, data.phase_type, False,
|
||||
'active', data.start_date, data.end_date, duration, data.notes
|
||||
))
|
||||
|
||||
phase_id = cur.fetchone()['id']
|
||||
|
||||
return {"id": phase_id, "message": "Trainingsphase erstellt"}
|
||||
|
||||
@router.put("/phases/{phase_id}/status")
|
||||
def update_phase_status(
|
||||
phase_id: str,
|
||||
status: str,
|
||||
session: dict = Depends(require_auth)
|
||||
):
|
||||
"""Update training phase status (accept/reject auto-detected phases)"""
|
||||
pid = session['profile_id']
|
||||
|
||||
valid_statuses = ['suggested', 'accepted', 'active', 'completed', 'rejected']
|
||||
if status not in valid_statuses:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Ungültiger Status. Erlaubt: {', '.join(valid_statuses)}"
|
||||
)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"UPDATE training_phases SET status = %s WHERE id = %s AND profile_id = %s",
|
||||
(status, phase_id, pid)
|
||||
)
|
||||
|
||||
if cur.rowcount == 0:
|
||||
raise HTTPException(status_code=404, detail="Trainingsphase nicht gefunden")
|
||||
|
||||
return {"message": "Status aktualisiert"}
|
||||
|
||||
# ============================================================================
|
||||
# Fitness Tests
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/tests")
|
||||
def list_fitness_tests(session: dict = Depends(require_auth)):
|
||||
"""List all fitness tests"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT id, test_type, result_value, result_unit,
|
||||
test_date, test_conditions, norm_category, created_at
|
||||
FROM fitness_tests
|
||||
WHERE profile_id = %s
|
||||
ORDER BY test_date DESC
|
||||
""", (pid,))
|
||||
|
||||
return [r2d(row) for row in cur.fetchall()]
|
||||
|
||||
@router.post("/tests")
|
||||
def create_fitness_test(data: FitnessTestCreate, session: dict = Depends(require_auth)):
|
||||
"""Record fitness test result"""
|
||||
pid = session['profile_id']
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Calculate norm category (simplified for now)
|
||||
norm_category = _calculate_norm_category(
|
||||
data.test_type,
|
||||
data.result_value,
|
||||
data.result_unit
|
||||
)
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO fitness_tests (
|
||||
profile_id, test_type, result_value, result_unit,
|
||||
test_date, test_conditions, norm_category
|
||||
) VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
pid, data.test_type, data.result_value, data.result_unit,
|
||||
data.test_date, data.test_conditions, norm_category
|
||||
))
|
||||
|
||||
test_id = cur.fetchone()['id']
|
||||
|
||||
return {"id": test_id, "norm_category": norm_category}
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
def _get_current_value_for_goal_type(conn, profile_id: str, goal_type: str) -> Optional[float]:
|
||||
"""Get current value for a goal type from latest data"""
|
||||
cur = get_cursor(conn)
|
||||
|
||||
if goal_type == 'weight':
|
||||
cur.execute("""
|
||||
SELECT weight FROM weight_log
|
||||
WHERE profile_id = %s
|
||||
ORDER BY date DESC LIMIT 1
|
||||
""", (profile_id,))
|
||||
row = cur.fetchone()
|
||||
return float(row['weight']) if row else None
|
||||
|
||||
elif goal_type == 'body_fat':
|
||||
cur.execute("""
|
||||
SELECT body_fat_pct FROM caliper_log
|
||||
WHERE profile_id = %s
|
||||
ORDER BY date DESC LIMIT 1
|
||||
""", (profile_id,))
|
||||
row = cur.fetchone()
|
||||
return float(row['body_fat_pct']) if row else None
|
||||
|
||||
elif goal_type == 'vo2max':
|
||||
cur.execute("""
|
||||
SELECT vo2max FROM vitals_baseline
|
||||
WHERE profile_id = %s AND vo2max IS NOT NULL
|
||||
ORDER BY date DESC LIMIT 1
|
||||
""", (profile_id,))
|
||||
row = cur.fetchone()
|
||||
return float(row['vo2max']) if row else None
|
||||
|
||||
elif goal_type == 'rhr':
|
||||
cur.execute("""
|
||||
SELECT resting_hr FROM vitals_baseline
|
||||
WHERE profile_id = %s AND resting_hr IS NOT NULL
|
||||
ORDER BY date DESC LIMIT 1
|
||||
""", (profile_id,))
|
||||
row = cur.fetchone()
|
||||
return float(row['resting_hr']) if row else None
|
||||
|
||||
return None
|
||||
|
||||
def _update_goal_progress(conn, profile_id: str, goal: dict):
|
||||
"""Update goal progress (modifies goal dict in-place)"""
|
||||
# Get current value
|
||||
current = _get_current_value_for_goal_type(conn, profile_id, goal['goal_type'])
|
||||
|
||||
if current is not None and goal['start_value'] is not None and goal['target_value'] is not None:
|
||||
goal['current_value'] = current
|
||||
|
||||
# Calculate progress percentage
|
||||
total_delta = float(goal['target_value']) - float(goal['start_value'])
|
||||
current_delta = current - float(goal['start_value'])
|
||||
|
||||
if total_delta != 0:
|
||||
progress_pct = (current_delta / total_delta) * 100
|
||||
goal['progress_pct'] = round(progress_pct, 2)
|
||||
|
||||
# Simple linear projection
|
||||
if goal['start_date'] and current_delta != 0:
|
||||
days_elapsed = (date.today() - goal['start_date']).days
|
||||
if days_elapsed > 0:
|
||||
days_per_unit = days_elapsed / current_delta
|
||||
remaining_units = float(goal['target_value']) - current
|
||||
remaining_days = int(days_per_unit * remaining_units)
|
||||
goal['projection_date'] = date.today() + timedelta(days=remaining_days)
|
||||
|
||||
# Check if on track
|
||||
if goal['target_date'] and goal['projection_date']:
|
||||
goal['on_track'] = goal['projection_date'] <= goal['target_date']
|
||||
|
||||
def _calculate_norm_category(test_type: str, value: float, unit: str) -> Optional[str]:
|
||||
"""
|
||||
Calculate norm category for fitness test
|
||||
(Simplified - would need age/gender-specific norms)
|
||||
"""
|
||||
# Placeholder - should use proper norm tables
|
||||
return None
|
||||
Reference in new issue
Block a user