feat: Integrate caliper data enrichment and weight loading in API responses
- Enhanced the caliper listing and export functionalities to include enriched data from weight logs. - Updated the upsert and update operations to utilize new composition functions for body composition calculations. - Refactored the CaliperScreen component to streamline payload construction by removing unnecessary parameters.
This commit is contained in:
1 parent
00437a92ab
commit
49e9c9c214
6 files changed
+178
-19
No files matched your search
@@ -14,6 +14,7 @@ from auth import require_auth, check_feature_access, increment_feature_usage
|
||||
from models import CaliperEntry
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
from caliper_composition import enrich_caliper_row_for_response, fill_caliper_body_comp, load_weight_rows
|
||||
|
||||
router = APIRouter(prefix="/api/caliper", tags=["caliper"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -27,7 +28,11 @@ def list_caliper(limit: int=100, x_profile_id: Optional[str]=Header(default=None
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"SELECT * FROM caliper_log WHERE profile_id=%s ORDER BY date DESC LIMIT %s", (pid,limit))
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
rows = [r2d(r) for r in cur.fetchall()]
|
||||
weight_rows = load_weight_rows(conn, pid)
|
||||
for row in rows:
|
||||
enrich_caliper_row_for_response(row, weight_rows)
|
||||
return rows
|
||||
|
||||
|
||||
@router.post("")
|
||||
@@ -55,6 +60,7 @@ def upsert_caliper(e: CaliperEntry, x_profile_id: Optional[str]=Header(default=N
|
||||
cur.execute("SELECT id FROM caliper_log WHERE profile_id=%s AND date=%s", (pid,e.date))
|
||||
ex = cur.fetchone()
|
||||
d = e.model_dump()
|
||||
fill_caliper_body_comp(d, conn, pid)
|
||||
is_new_entry = not ex
|
||||
|
||||
if ex:
|
||||
@@ -86,6 +92,7 @@ def update_caliper(eid: str, e: CaliperEntry, x_profile_id: Optional[str]=Header
|
||||
pid = get_pid(x_profile_id)
|
||||
with get_db() as conn:
|
||||
d = e.model_dump()
|
||||
fill_caliper_body_comp(d, conn, pid)
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(f"UPDATE caliper_log SET {', '.join(f'{k}=%s' for k in d)} WHERE id=%s AND profile_id=%s",
|
||||
list(d.values())+[eid,pid])
|
||||
|
||||
@@ -21,6 +21,7 @@ from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth, check_feature_access, increment_feature_usage
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
from caliper_composition import enrich_caliper_row_for_response, load_weight_rows
|
||||
|
||||
router = APIRouter(prefix="/api/export", tags=["export"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -68,10 +69,21 @@ def export_csv(x_profile_id: Optional[str]=Header(default=None), session: dict=D
|
||||
details = f"Taille:{float(r['c_waist'])}cm Bauch:{float(r['c_belly'])}cm Hüfte:{float(r['c_hip'])}cm"
|
||||
writer.writerow(["Umfänge", r['date'], "", details])
|
||||
|
||||
# Caliper
|
||||
cur.execute("SELECT date, body_fat_pct, lean_mass FROM caliper_log WHERE profile_id=%s ORDER BY date", (pid,))
|
||||
for r in cur.fetchall():
|
||||
writer.writerow(["Caliper", r['date'], f"{float(r['body_fat_pct'])}%", f"Magermasse:{float(r['lean_mass'])}kg"])
|
||||
# Caliper (Magermasse aus Gewicht + KF% nachziehen wenn in DB leer)
|
||||
cur.execute(
|
||||
"SELECT date, body_fat_pct, lean_mass, fat_mass FROM caliper_log WHERE profile_id=%s ORDER BY date",
|
||||
(pid,),
|
||||
)
|
||||
cal_rows = [r2d(r) for r in cur.fetchall()]
|
||||
weight_rows = load_weight_rows(conn, pid)
|
||||
for r in cal_rows:
|
||||
enrich_caliper_row_for_response(r, weight_rows)
|
||||
if r.get("body_fat_pct") is None:
|
||||
continue
|
||||
bf = float(r["body_fat_pct"])
|
||||
lm = r.get("lean_mass")
|
||||
details = f"Magermasse:{float(lm)}kg" if lm is not None else ""
|
||||
writer.writerow(["Caliper", r["date"], f"{bf}%", details])
|
||||
|
||||
# Nutrition
|
||||
cur.execute("SELECT date, kcal, protein_g FROM nutrition_log WHERE profile_id=%s ORDER BY date", (pid,))
|
||||
@@ -141,6 +153,10 @@ def export_json(x_profile_id: Optional[str]=Header(default=None), session: dict=
|
||||
cur.execute("SELECT * FROM ai_insights WHERE profile_id=%s ORDER BY created DESC", (pid,))
|
||||
data['insights'] = [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
w_rows = [{'date': x['date'], 'weight': x['weight']} for x in data.get('weight', [])]
|
||||
for row in data.get('caliper', []):
|
||||
enrich_caliper_row_for_response(row, w_rows)
|
||||
|
||||
def decimal_handler(obj):
|
||||
if isinstance(obj, Decimal):
|
||||
return float(obj)
|
||||
|
||||
@@ -16,6 +16,7 @@ from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth, check_feature_access, increment_feature_usage
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
from caliper_composition import compute_lean_fat_kg, nearest_weight_kg_from_map
|
||||
|
||||
router = APIRouter(prefix="/api/nutrition", tags=["nutrition"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -202,8 +203,12 @@ def nutrition_correlations(x_profile_id: Optional[str]=Header(default=None), ses
|
||||
if d in cal_by_date:
|
||||
lm = cal_by_date[d].get('lean_mass')
|
||||
bf = cal_by_date[d].get('body_fat_pct')
|
||||
row['lean_mass']=float(lm) if lm is not None else None
|
||||
row['body_fat_pct']=float(bf) if bf is not None else None
|
||||
if bf is not None and lm is None:
|
||||
wkg = nearest_weight_kg_from_map(wlog, d)
|
||||
if wkg is not None:
|
||||
lm, _fat = compute_lean_fat_kg(wkg, float(bf))
|
||||
row['lean_mass'] = float(lm) if lm is not None else None
|
||||
row['body_fat_pct'] = float(bf) if bf is not None else None
|
||||
result.append(row)
|
||||
return result
|
||||
|
||||
|
||||
Reference in new issue
Block a user