feat: enhance session metrics handling in activity summaries
- Integrated compact JSON payload generation for session metrics in `get_training_sessions_recent_weeks_data`. - Updated the registration of activity session insights to reflect the new compact format for session metrics. - Improved documentation to clarify the structure and semantics of the session metrics in the JSON output. - Added normalization for prompt numbers to ensure consistent formatting in the metrics.
This commit is contained in:
1 parent
7226e04e9c
commit
6756dc60f3
5 files changed
+188
-15
No files matched your search
@@ -25,6 +25,10 @@ import statistics
|
||||
from db import get_db, get_cursor, r2d
|
||||
from data_layer.activity_session_metrics import enrich_sessions_with_metrics
|
||||
from data_layer.utils import calculate_confidence, safe_float, safe_int, serialize_dates
|
||||
from data_layer.prompt_output_compact import (
|
||||
normalize_prompt_number,
|
||||
session_metrics_list_to_key_value_compact,
|
||||
)
|
||||
|
||||
|
||||
def get_activity_summary_data(
|
||||
@@ -1094,6 +1098,10 @@ def get_training_sessions_recent_weeks_data(
|
||||
Letzte Wochen mit Einzeltrainings für KI-Kontext (Dauer, kcal, HF, Typ).
|
||||
|
||||
weeks: Anzahl zurückliegender ISO-Kalenderwochen (Default 4).
|
||||
|
||||
session_metrics pro Einheit: kompaktes Objekt ``{key: Wert}`` (keine wiederholten
|
||||
Namen/Beschreibungen). Bedeutung der Keys: Platzhalter ``{{training_parameters_glossary_md}}``.
|
||||
Zahlen werden für Prompt-Token kompakt gerundet.
|
||||
"""
|
||||
days = max(weeks * 7, 7)
|
||||
with get_db() as conn:
|
||||
@@ -1131,6 +1139,8 @@ def get_training_sessions_recent_weeks_data(
|
||||
"days_loaded": days,
|
||||
"session_count": 0,
|
||||
"confidence": "insufficient",
|
||||
"session_metrics_shape": "key_value",
|
||||
"metric_semantics_placeholder": "{{training_parameters_glossary_md}}",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1149,6 +1159,7 @@ def get_training_sessions_recent_weeks_data(
|
||||
kcal_f = float(kcal) if kcal is not None else None
|
||||
hr_a = r.get("hr_avg")
|
||||
hr_m = r.get("hr_max")
|
||||
sm_compact = session_metrics_list_to_key_value_compact(r.get("session_metrics"))
|
||||
by_week[wk].append(
|
||||
{
|
||||
"id": str(r["id"]),
|
||||
@@ -1157,12 +1168,12 @@ def get_training_sessions_recent_weeks_data(
|
||||
"activity_type": r.get("activity_type"),
|
||||
"training_category": r.get("training_category"),
|
||||
"training_type_name": r.get("training_type_name"),
|
||||
"duration_min": dur_f,
|
||||
"kcal_active": kcal_f,
|
||||
"duration_min": normalize_prompt_number(dur_f) if dur_f is not None else None,
|
||||
"kcal_active": normalize_prompt_number(kcal_f) if kcal_f is not None else None,
|
||||
"hr_avg": int(hr_a) if hr_a is not None else None,
|
||||
"hr_max": int(hr_m) if hr_m is not None else None,
|
||||
"rpe": int(r["rpe"]) if r.get("rpe") is not None else None,
|
||||
"session_metrics": r.get("session_metrics", []),
|
||||
"session_metrics": sm_compact,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1177,6 +1188,8 @@ def get_training_sessions_recent_weeks_data(
|
||||
"days_loaded": days,
|
||||
"session_count": len(rows),
|
||||
"confidence": confidence,
|
||||
"session_metrics_shape": "key_value",
|
||||
"metric_semantics_placeholder": "{{training_parameters_glossary_md}}",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
Kompakte Zahlen- und JSON-Aufbereitung für KI-Platzhalter (Token sparen).
|
||||
|
||||
- Floats: sinnvolle Nachkommastellen je nach Größenordnung (kleine Werte <0,1 mehr Präzision).
|
||||
- ≥10 meist ganzzahlig; Prozent/Verhältnisse über denselben Mechanismus lesbar.
|
||||
- Rekursiv auf dict/list-Strukturen vor json.dumps in _safe_json anwendbar.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
|
||||
def compact_float_for_prompt(x: float) -> float | int:
|
||||
"""
|
||||
Reduziert unnötige Nachkommastellen; erhält kleine Beträge (<0,1) mit mehr Stellen.
|
||||
"""
|
||||
if not math.isfinite(x):
|
||||
return x
|
||||
ax = abs(x)
|
||||
if ax == 0.0:
|
||||
return 0
|
||||
if ax >= 100.0:
|
||||
return int(round(x))
|
||||
if ax >= 10.0:
|
||||
return int(round(x))
|
||||
if ax >= 1.0:
|
||||
r = round(x, 2)
|
||||
return int(r) if abs(r - int(round(r))) < 1e-6 else r
|
||||
if ax >= 0.1:
|
||||
r = round(x, 2)
|
||||
return int(r) if abs(r - int(round(r))) < 1e-6 else r
|
||||
if ax >= 0.01:
|
||||
return round(x, 3)
|
||||
return round(x, 4)
|
||||
|
||||
|
||||
def normalize_prompt_number(x: Any) -> Any:
|
||||
"""int/Decimal/float kompakt; Rest unverändert."""
|
||||
if x is None:
|
||||
return None
|
||||
if isinstance(x, bool):
|
||||
return x
|
||||
if isinstance(x, int) and not isinstance(x, bool):
|
||||
return x
|
||||
if isinstance(x, Decimal):
|
||||
try:
|
||||
xf = float(x)
|
||||
except Exception:
|
||||
return x
|
||||
return compact_float_for_prompt(xf)
|
||||
if isinstance(x, float):
|
||||
return compact_float_for_prompt(x)
|
||||
return x
|
||||
|
||||
|
||||
def compact_json_payload_for_prompts(obj: Any) -> Any:
|
||||
"""
|
||||
Tiefe Kopie mit kompakten Zahlen (dicts/list/tuples rekursiv).
|
||||
Strings und dict-Keys werden nicht verändert.
|
||||
"""
|
||||
if obj is None:
|
||||
return None
|
||||
if isinstance(obj, dict):
|
||||
return {k: compact_json_payload_for_prompts(v) for k, v in obj.items()}
|
||||
if isinstance(obj, (list, tuple)):
|
||||
t = [compact_json_payload_for_prompts(v) for v in obj]
|
||||
return tuple(t) if isinstance(obj, tuple) else t
|
||||
return normalize_prompt_number(obj)
|
||||
|
||||
|
||||
def session_metrics_list_to_key_value_compact(metrics: list[Any] | None) -> dict[str, Any]:
|
||||
"""
|
||||
Session-Metriken für KI-JSON: nur key → Wert (keine wiederholten Namen/Beschreibungen).
|
||||
|
||||
Semantik: {{training_parameters_glossary_md}} im Prompt ergänzen.
|
||||
"""
|
||||
out: dict[str, Any] = {}
|
||||
for m in metrics or []:
|
||||
if not isinstance(m, dict):
|
||||
continue
|
||||
k = m.get("key")
|
||||
if not k:
|
||||
continue
|
||||
v = m.get("value")
|
||||
dt = (m.get("data_type") or "").lower()
|
||||
if v is None:
|
||||
out[str(k)] = None
|
||||
continue
|
||||
if dt == "integer":
|
||||
try:
|
||||
out[str(k)] = int(v)
|
||||
except (TypeError, ValueError):
|
||||
out[str(k)] = normalize_prompt_number(v)
|
||||
elif dt == "boolean":
|
||||
out[str(k)] = bool(v)
|
||||
elif dt == "string":
|
||||
out[str(k)] = str(v)
|
||||
else:
|
||||
out[str(k)] = normalize_prompt_number(v)
|
||||
return out
|
||||
Reference in new issue
Block a user