feat: Add new profile and time period placeholders in placeholder_resolver.py
- Introduced functions to retrieve profile name, age, height, and gender for better placeholder resolution. - Added functions for displaying current date and time period labels (last 7, 30, and 90 days). - Updated PLACEHOLDER_MAP to utilize new functions for improved readability and maintainability. - Enhanced placeholder registrations in __init__.py to include new modules for sleep, vital metrics, and profile time periods. These changes enhance the flexibility and functionality of the placeholder system, allowing for more dynamic content generation.
This commit is contained in:
1 parent
e9e094c6a4
commit
2ea5f905c4
9 files changed
+1233
-68
No files matched your search
@@ -9,7 +9,7 @@ This module now focuses on FORMATTING for AI consumption.
|
||||
"""
|
||||
import re
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Dict, List, Optional, Callable
|
||||
from typing import Dict, List, Optional, Callable, Tuple
|
||||
from db import get_db, get_cursor, r2d
|
||||
|
||||
# Phase 0c: Import data layer
|
||||
@@ -277,6 +277,43 @@ def calculate_age(dob) -> str:
|
||||
return "unbekannt"
|
||||
|
||||
|
||||
def get_profile_name(profile_id: str) -> str:
|
||||
"""Profil-Platzhalter: Anzeigename (profiles.name)."""
|
||||
return get_profile_data(profile_id).get('name', 'Nutzer')
|
||||
|
||||
|
||||
def get_profile_age_display(profile_id: str) -> str:
|
||||
"""Profil-Platzhalter: Alter aus Geburtsdatum."""
|
||||
return calculate_age(get_profile_data(profile_id).get('dob'))
|
||||
|
||||
|
||||
def get_profile_height_display(profile_id: str) -> str:
|
||||
"""Profil-Platzhalter: Körpergröße (cm) als String."""
|
||||
return str(get_profile_data(profile_id).get('height', 'unbekannt'))
|
||||
|
||||
|
||||
def get_profile_geschlecht_display(profile_id: str) -> str:
|
||||
"""Profil-Platzhalter: Geschlecht aus profiles.sex (m/w)."""
|
||||
return 'männlich' if get_profile_data(profile_id).get('sex') == 'm' else 'weiblich'
|
||||
|
||||
|
||||
def get_datum_heute(_profile_id: str) -> str:
|
||||
"""Zeitraum-Platzhalter: heutiges Datum (dd.mm.yyyy)."""
|
||||
return datetime.now().strftime('%d.%m.%Y')
|
||||
|
||||
|
||||
def get_zeitraum_label_7d(_profile_id: str) -> str:
|
||||
return 'letzte 7 Tage'
|
||||
|
||||
|
||||
def get_zeitraum_label_30d(_profile_id: str) -> str:
|
||||
return 'letzte 30 Tage'
|
||||
|
||||
|
||||
def get_zeitraum_label_90d(_profile_id: str) -> str:
|
||||
return 'letzte 90 Tage'
|
||||
|
||||
|
||||
def get_activity_detail(profile_id: str, days: int = 14) -> str:
|
||||
"""
|
||||
Get detailed activity log for analysis.
|
||||
@@ -1136,10 +1173,10 @@ def _format_goals_on_track(profile_id: str, n: int = 3) -> str:
|
||||
|
||||
PLACEHOLDER_MAP: Dict[str, Callable[[str], str]] = {
|
||||
# Profil
|
||||
'{{name}}': lambda pid: get_profile_data(pid).get('name', 'Nutzer'),
|
||||
'{{age}}': lambda pid: calculate_age(get_profile_data(pid).get('dob')),
|
||||
'{{height}}': lambda pid: str(get_profile_data(pid).get('height', 'unbekannt')),
|
||||
'{{geschlecht}}': lambda pid: 'männlich' if get_profile_data(pid).get('sex') == 'm' else 'weiblich',
|
||||
'{{name}}': get_profile_name,
|
||||
'{{age}}': get_profile_age_display,
|
||||
'{{height}}': get_profile_height_display,
|
||||
'{{geschlecht}}': get_profile_geschlecht_display,
|
||||
|
||||
# Körper (21 Registry-Keys: body_metrics + body_extras — alles hier gebündelt)
|
||||
'{{weight_aktuell}}': get_latest_weight,
|
||||
@@ -1203,29 +1240,37 @@ PLACEHOLDER_MAP: Dict[str, Callable[[str], str]] = {
|
||||
'{{training_inter_session_gap_md}}': get_training_inter_session_gap_md,
|
||||
'{{training_sessions_recent_json}}': lambda pid: _safe_json('training_sessions_recent_json', pid),
|
||||
|
||||
# Schlaf & Erholung
|
||||
# Schlaf & Erholung (10 Registry-Keys; recovery_score hier, nicht unter Meta Scores)
|
||||
'{{sleep_avg_duration}}': lambda pid: get_sleep_avg_duration(pid, 7),
|
||||
'{{sleep_avg_quality}}': lambda pid: get_sleep_avg_quality(pid, 7),
|
||||
'{{rest_days_count}}': lambda pid: get_rest_days_count(pid, 30),
|
||||
'{{recovery_score}}': lambda pid: _safe_int('recovery_score_v2', pid),
|
||||
'{{sleep_avg_duration_7d}}': lambda pid: _safe_float('sleep_avg_duration_7d', pid),
|
||||
'{{sleep_debt_hours}}': lambda pid: _safe_float('sleep_debt_hours', pid),
|
||||
'{{sleep_regularity_proxy}}': lambda pid: _safe_float('sleep_regularity_proxy', pid),
|
||||
'{{recent_load_balance_3d}}': lambda pid: _safe_int('recent_load_balance_3d', pid),
|
||||
'{{sleep_quality_7d}}': lambda pid: _safe_int('sleep_quality_7d', pid),
|
||||
'{{correlation_sleep_recovery}}': lambda pid: _safe_json('correlation_sleep_recovery', pid),
|
||||
|
||||
# Vitalwerte
|
||||
# Vitalwerte (5 Registry-Keys: Mittelwerte + vs. Baseline)
|
||||
'{{vitals_avg_hr}}': lambda pid: get_vitals_avg_hr(pid, 7),
|
||||
'{{vitals_avg_hrv}}': lambda pid: get_vitals_avg_hrv(pid, 7),
|
||||
'{{vitals_vo2_max}}': get_vitals_vo2_max,
|
||||
'{{hrv_vs_baseline_pct}}': lambda pid: _safe_float('hrv_vs_baseline_pct', pid),
|
||||
'{{rhr_vs_baseline_pct}}': lambda pid: _safe_float('rhr_vs_baseline_pct', pid),
|
||||
|
||||
# Zeitraum
|
||||
'{{datum_heute}}': lambda pid: datetime.now().strftime('%d.%m.%Y'),
|
||||
'{{zeitraum_7d}}': lambda pid: 'letzte 7 Tage',
|
||||
'{{zeitraum_30d}}': lambda pid: 'letzte 30 Tage',
|
||||
'{{zeitraum_90d}}': lambda pid: 'letzte 90 Tage',
|
||||
'{{datum_heute}}': get_datum_heute,
|
||||
'{{zeitraum_7d}}': get_zeitraum_label_7d,
|
||||
'{{zeitraum_30d}}': get_zeitraum_label_30d,
|
||||
'{{zeitraum_90d}}': get_zeitraum_label_90d,
|
||||
|
||||
# ========================================================================
|
||||
# PHASE 0b: Goal-Aware Placeholders (Dynamic Focus Areas v2.0)
|
||||
# ========================================================================
|
||||
|
||||
# --- Meta Scores (Ebene 1: Aggregierte Scores; body/nutrition/activity scores → jeweilige Kategorie) ---
|
||||
# --- Meta Scores (Ebene 1; recovery_score → Schlaf & Erholung) ---
|
||||
'{{goal_progress_score}}': lambda pid: _safe_int('goal_progress_score', pid),
|
||||
'{{recovery_score}}': lambda pid: _safe_int('recovery_score_v2', pid),
|
||||
'{{data_quality_score}}': lambda pid: _safe_int('data_quality_score', pid),
|
||||
|
||||
# --- Top-Weighted Goals/Focus Areas (Ebene 2: statt Primary) ---
|
||||
@@ -1251,21 +1296,11 @@ PLACEHOLDER_MAP: Dict[str, Callable[[str], str]] = {
|
||||
'{{focus_cat_lebensstil_progress}}': lambda pid: _safe_int('focus_cat_lebensstil_progress', pid),
|
||||
'{{focus_cat_lebensstil_weight}}': lambda pid: _safe_float('focus_cat_lebensstil_weight', pid),
|
||||
|
||||
# --- Recovery Metrics (Recovery Score v2) ---
|
||||
'{{hrv_vs_baseline_pct}}': lambda pid: _safe_float('hrv_vs_baseline_pct', pid),
|
||||
'{{rhr_vs_baseline_pct}}': lambda pid: _safe_float('rhr_vs_baseline_pct', pid),
|
||||
'{{sleep_avg_duration_7d}}': lambda pid: _safe_float('sleep_avg_duration_7d', pid),
|
||||
'{{sleep_debt_hours}}': lambda pid: _safe_float('sleep_debt_hours', pid),
|
||||
'{{sleep_regularity_proxy}}': lambda pid: _safe_float('sleep_regularity_proxy', pid),
|
||||
'{{recent_load_balance_3d}}': lambda pid: _safe_int('recent_load_balance_3d', pid),
|
||||
'{{sleep_quality_7d}}': lambda pid: _safe_int('sleep_quality_7d', pid),
|
||||
|
||||
# --- Correlation Metrics (C1-C7) ---
|
||||
'{{correlation_energy_weight_lag}}': lambda pid: _safe_json('correlation_energy_weight_lag', pid),
|
||||
'{{correlation_protein_lbm}}': lambda pid: _safe_json('correlation_protein_lbm', pid),
|
||||
'{{correlation_load_hrv}}': lambda pid: _safe_json('correlation_load_hrv', pid),
|
||||
'{{correlation_load_rhr}}': lambda pid: _safe_json('correlation_load_rhr', pid),
|
||||
'{{correlation_sleep_recovery}}': lambda pid: _safe_json('correlation_sleep_recovery', pid),
|
||||
'{{plateau_detected}}': lambda pid: _safe_json('plateau_detected', pid),
|
||||
'{{top_drivers}}': lambda pid: _safe_json('top_drivers', pid),
|
||||
|
||||
@@ -1378,9 +1413,42 @@ def get_available_placeholders(categories: Optional[List[str]] = None) -> Dict[s
|
||||
'{{rest_day_compliance}}', '{{vo2max_trend_28d}}', '{{activity_score}}',
|
||||
'{{training_frequency_by_type_md}}', '{{training_inter_session_gap_md}}', '{{training_sessions_recent_json}}',
|
||||
],
|
||||
'schlaf': [
|
||||
'{{sleep_avg_duration}}', '{{sleep_avg_quality}}', '{{rest_days_count}}',
|
||||
'{{recovery_score}}',
|
||||
'{{sleep_avg_duration_7d}}', '{{sleep_debt_hours}}', '{{sleep_regularity_proxy}}',
|
||||
'{{recent_load_balance_3d}}', '{{sleep_quality_7d}}',
|
||||
'{{correlation_sleep_recovery}}',
|
||||
],
|
||||
'vitalwerte': [
|
||||
'{{vitals_avg_hr}}', '{{vitals_avg_hrv}}', '{{vitals_vo2_max}}',
|
||||
'{{hrv_vs_baseline_pct}}', '{{rhr_vs_baseline_pct}}',
|
||||
],
|
||||
'zeitraum': [
|
||||
'{{datum_heute}}', '{{zeitraum_7d}}', '{{zeitraum_30d}}', '{{zeitraum_90d}}'
|
||||
]
|
||||
],
|
||||
'phase0b_meta': [
|
||||
'{{goal_progress_score}}', '{{data_quality_score}}',
|
||||
],
|
||||
'ziele_fokus': [
|
||||
'{{top_goal_name}}', '{{top_goal_progress_pct}}', '{{top_goal_status}}',
|
||||
'{{top_focus_area_name}}', '{{top_focus_area_progress}}',
|
||||
'{{focus_cat_körper_progress}}', '{{focus_cat_körper_weight}}',
|
||||
'{{focus_cat_ernährung_progress}}', '{{focus_cat_ernährung_weight}}',
|
||||
'{{focus_cat_aktivität_progress}}', '{{focus_cat_aktivität_weight}}',
|
||||
'{{focus_cat_recovery_progress}}', '{{focus_cat_recovery_weight}}',
|
||||
'{{focus_cat_vitalwerte_progress}}', '{{focus_cat_vitalwerte_weight}}',
|
||||
'{{focus_cat_mental_progress}}', '{{focus_cat_mental_weight}}',
|
||||
'{{focus_cat_lebensstil_progress}}', '{{focus_cat_lebensstil_weight}}',
|
||||
'{{active_goals_json}}', '{{active_goals_md}}',
|
||||
'{{focus_areas_weighted_json}}', '{{focus_areas_weighted_md}}', '{{focus_area_weights_json}}',
|
||||
'{{top_3_focus_areas}}', '{{top_3_goals_behind_schedule}}', '{{top_3_goals_on_track}}',
|
||||
],
|
||||
'korrelationen': [
|
||||
'{{correlation_energy_weight_lag}}', '{{correlation_protein_lbm}}',
|
||||
'{{correlation_load_hrv}}', '{{correlation_load_rhr}}',
|
||||
'{{plateau_detected}}', '{{top_drivers}}',
|
||||
],
|
||||
}
|
||||
|
||||
if not categories:
|
||||
@@ -1460,50 +1528,7 @@ def get_placeholder_catalog(profile_id: str) -> Dict[str, List[Dict[str, str]]]:
|
||||
})
|
||||
|
||||
# Legacy placeholders (not in registry yet)
|
||||
legacy_placeholders = {
|
||||
'Profil': [
|
||||
('name', 'Name des Nutzers'),
|
||||
('age', 'Alter in Jahren'),
|
||||
('height', 'Körpergröße in cm'),
|
||||
('geschlecht', 'Geschlecht'),
|
||||
],
|
||||
'Schlaf & Erholung': [
|
||||
('sleep_avg_duration', 'Durchschn. Schlafdauer (7d)'),
|
||||
('sleep_avg_quality', 'Durchschn. Schlafqualität (7d)'),
|
||||
('rest_days_count', 'Anzahl Ruhetage (30d)'),
|
||||
('sleep_avg_duration_7d', 'Schlaf 7d (Stunden)'),
|
||||
('sleep_debt_hours', 'Schlafschuld (Stunden)'),
|
||||
('sleep_regularity_proxy', 'Schlaf-Regelmäßigkeit (Min Abweichung)'),
|
||||
('sleep_quality_7d', 'Schlafqualität 7d (0-100)'),
|
||||
],
|
||||
'Vitalwerte': [
|
||||
('vitals_avg_hr', 'Durchschn. Ruhepuls (7d)'),
|
||||
('vitals_avg_hrv', 'Durchschn. HRV (7d)'),
|
||||
('vitals_vo2_max', 'Aktueller VO2 Max'),
|
||||
('hrv_vs_baseline_pct', 'HRV vs. Baseline (%)'),
|
||||
('rhr_vs_baseline_pct', 'RHR vs. Baseline (%)'),
|
||||
],
|
||||
'Scores (Phase 0b)': [
|
||||
('goal_progress_score', 'Goal Progress Score (0-100)'),
|
||||
('recovery_score', 'Recovery Score (0-100)'),
|
||||
('data_quality_score', 'Data Quality Score (0-100)'),
|
||||
],
|
||||
'Focus Areas': [
|
||||
('top_focus_area_name', 'Top Focus Area Name'),
|
||||
('top_focus_area_progress', 'Top Focus Area Progress (%)'),
|
||||
('focus_cat_körper_progress', 'Kategorie Körper - Progress (%)'),
|
||||
('focus_cat_körper_weight', 'Kategorie Körper - Gewichtung (%)'),
|
||||
('focus_cat_ernährung_progress', 'Kategorie Ernährung - Progress (%)'),
|
||||
('focus_cat_ernährung_weight', 'Kategorie Ernährung - Gewichtung (%)'),
|
||||
('focus_cat_aktivität_progress', 'Kategorie Aktivität - Progress (%)'),
|
||||
('focus_cat_aktivität_weight', 'Kategorie Aktivität - Gewichtung (%)'),
|
||||
],
|
||||
'Zeitraum': [
|
||||
('datum_heute', 'Heutiges Datum'),
|
||||
('zeitraum_7d', '7-Tage-Zeitraum'),
|
||||
('zeitraum_30d', '30-Tage-Zeitraum'),
|
||||
],
|
||||
}
|
||||
legacy_placeholders: Dict[str, List[Tuple[str, str]]] = {}
|
||||
|
||||
# Add legacy placeholders (skip if already in registry)
|
||||
for category, items in legacy_placeholders.items():
|
||||
|
||||
Reference in new issue
Block a user