feat: Phase 0c - activity_metrics.py module complete
Data Layer: - get_activity_summary_data() - count, duration, calories, frequency - get_activity_detail_data() - detailed activity log with all fields - get_training_type_distribution_data() - category distribution with percentages Placeholder Layer: - get_activity_summary() - refactored to use data layer - get_activity_detail() - refactored to use data layer - get_trainingstyp_verteilung() - refactored to use data layer All 3 activity data functions + 3 placeholder refactors complete. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
e1d7670971
commit
6b2ad9fa1c
3 files changed
+332
-60
No files matched your search
@@ -23,6 +23,11 @@ from data_layer.nutrition_metrics import (
|
||||
get_nutrition_days_data,
|
||||
get_protein_targets_data
|
||||
)
|
||||
from data_layer.activity_metrics import (
|
||||
get_activity_summary_data,
|
||||
get_activity_detail_data,
|
||||
get_training_type_distribution_data
|
||||
)
|
||||
|
||||
|
||||
# ── Helper Functions ──────────────────────────────────────────────────────────
|
||||
@@ -223,25 +228,18 @@ def get_protein_ziel_high(profile_id: str) -> str:
|
||||
|
||||
|
||||
def get_activity_summary(profile_id: str, days: int = 14) -> str:
|
||||
"""Get activity summary for recent period."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT COUNT(*) as count,
|
||||
SUM(duration_min) as total_min,
|
||||
SUM(kcal_active) as total_kcal
|
||||
FROM activity_log
|
||||
WHERE profile_id=%s AND date >= %s""",
|
||||
(profile_id, cutoff)
|
||||
)
|
||||
row = r2d(cur.fetchone())
|
||||
"""
|
||||
Get activity summary for recent period.
|
||||
|
||||
if row['count'] == 0:
|
||||
return f"Keine Aktivitäten in den letzten {days} Tagen"
|
||||
Phase 0c: Refactored to use data_layer.activity_metrics.get_activity_summary_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_activity_summary_data(profile_id, days)
|
||||
|
||||
avg_min = int(row['total_min'] / row['count']) if row['total_min'] else 0
|
||||
return f"{row['count']} Einheiten in {days} Tagen (Ø {avg_min} min/Einheit, {int(row['total_kcal'] or 0)} kcal gesamt)"
|
||||
if data['confidence'] == 'insufficient':
|
||||
return f"Keine Aktivitäten in den letzten {days} Tagen"
|
||||
|
||||
return f"{data['activity_count']} Einheiten in {days} Tagen (Ø {data['avg_duration_min']} min/Einheit, {data['total_kcal']} kcal gesamt)"
|
||||
|
||||
|
||||
def calculate_age(dob) -> str:
|
||||
@@ -263,55 +261,47 @@ def calculate_age(dob) -> str:
|
||||
|
||||
|
||||
def get_activity_detail(profile_id: str, days: int = 14) -> str:
|
||||
"""Get detailed activity log for analysis."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT date, activity_type, duration_min, kcal_active, hr_avg
|
||||
FROM activity_log
|
||||
WHERE profile_id=%s AND date >= %s
|
||||
ORDER BY date DESC
|
||||
LIMIT 50""",
|
||||
(profile_id, cutoff)
|
||||
"""
|
||||
Get detailed activity log for analysis.
|
||||
|
||||
Phase 0c: Refactored to use data_layer.activity_metrics.get_activity_detail_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_activity_detail_data(profile_id, days)
|
||||
|
||||
if data['confidence'] == 'insufficient':
|
||||
return f"Keine Aktivitäten in den letzten {days} Tagen"
|
||||
|
||||
# Format as readable list (max 20 entries to avoid token bloat)
|
||||
lines = []
|
||||
for activity in data['activities'][:20]:
|
||||
hr_str = f" HF={activity['hr_avg']}" if activity['hr_avg'] else ""
|
||||
lines.append(
|
||||
f"{activity['date']}: {activity['activity_type']} "
|
||||
f"({activity['duration_min']}min, {activity['kcal_active']}kcal{hr_str})"
|
||||
)
|
||||
rows = [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
if not rows:
|
||||
return f"Keine Aktivitäten in den letzten {days} Tagen"
|
||||
|
||||
# Format as readable list
|
||||
lines = []
|
||||
for r in rows:
|
||||
hr_str = f" HF={r['hr_avg']}" if r.get('hr_avg') else ""
|
||||
lines.append(
|
||||
f"{r['date']}: {r['activity_type']} ({r['duration_min']}min, {r.get('kcal_active', 0)}kcal{hr_str})"
|
||||
)
|
||||
|
||||
return '\n'.join(lines[:20]) # Max 20 entries to avoid token bloat
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
def get_trainingstyp_verteilung(profile_id: str, days: int = 14) -> str:
|
||||
"""Get training type distribution."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT training_category, COUNT(*) as count
|
||||
FROM activity_log
|
||||
WHERE profile_id=%s AND date >= %s AND training_category IS NOT NULL
|
||||
GROUP BY training_category
|
||||
ORDER BY count DESC""",
|
||||
(profile_id, cutoff)
|
||||
)
|
||||
rows = [r2d(r) for r in cur.fetchall()]
|
||||
"""
|
||||
Get training type distribution.
|
||||
|
||||
if not rows:
|
||||
return "Keine kategorisierten Trainings"
|
||||
Phase 0c: Refactored to use data_layer.activity_metrics.get_training_type_distribution_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_training_type_distribution_data(profile_id, days)
|
||||
|
||||
total = sum(r['count'] for r in rows)
|
||||
parts = [f"{r['training_category']}: {int(r['count']/total*100)}%" for r in rows[:3]]
|
||||
return ", ".join(parts)
|
||||
if data['confidence'] == 'insufficient' or not data['distribution']:
|
||||
return "Keine kategorisierten Trainings"
|
||||
|
||||
# Format top 3 categories with percentages
|
||||
parts = [
|
||||
f"{dist['category']}: {int(dist['percentage'])}%"
|
||||
for dist in data['distribution'][:3]
|
||||
]
|
||||
return ", ".join(parts)
|
||||
|
||||
|
||||
def get_sleep_avg_duration(profile_id: str, days: int = 7) -> str:
|
||||
|
||||
Reference in new issue
Block a user