fixes graphics

This commit is contained in:
2026-07-01 05:04:47 +00:00
parent 1313a12a20
commit 3e48b3a41c
9 changed files with 543 additions and 50 deletions
+50 -1
View File
@@ -4,13 +4,15 @@ from sqlalchemy import inspect, select, text
from sqlalchemy.orm import Session
from app.db.base import engine
from app.db.models import FitnessProfile, StepLog
from app.db.models import BodyMetric, FitnessProfile, StepLog
from app.fitness.body_composition import compute_body_composition
from app.fitness.calculators import DEFAULT_NEAT_KCAL, compute_targets, macro_targets
logger = logging.getLogger(__name__)
TDEE_V2_BACKFILL = "fitness_tdee_v2_backfill"
MACROS_GKG_BACKFILL = "fitness_macros_gkg_v1"
NAVY_BODY_FAT_CM_BACKFILL = "fitness_navy_body_fat_cm_v1"
def _table_exists(table: str) -> bool:
@@ -142,6 +144,52 @@ def backfill_macros_gkg(*, force: bool = False) -> int:
return updated
def backfill_navy_body_fat_cm(*, force: bool = False) -> int:
"""Recalculate Navy body composition after fixing the cm-based formula."""
if not _table_exists("body_metrics"):
_ensure_schema_migrations_table()
if not _migration_applied(NAVY_BODY_FAT_CM_BACKFILL):
_mark_migration_applied(NAVY_BODY_FAT_CM_BACKFILL)
return 0
_ensure_schema_migrations_table()
if not force and _migration_applied(NAVY_BODY_FAT_CM_BACKFILL):
return 0
updated = 0
with Session(engine) as db:
profiles = {
profile.user_id: profile
for profile in db.scalars(select(FitnessProfile)).all()
}
rows = db.scalars(
select(BodyMetric).where(BodyMetric.body_fat_method == "navy")
).all()
for row in rows:
profile = profiles.get(row.user_id)
computed = compute_body_composition(
sex=str(profile.sex if profile else "male"),
height_cm=float(profile.height_cm if profile else 170),
weight_kg=row.weight_kg,
neck_cm=row.neck_cm,
waist_cm=row.waist_cm,
hip_cm=row.hip_cm,
)
if computed.get("body_fat_pct") is None:
continue
row.body_fat_pct = computed.get("body_fat_pct")
row.body_fat_method = computed.get("body_fat_method")
row.whr = computed.get("whr")
row.lbm_kg = computed.get("lbm_kg")
row.ffmi = computed.get("ffmi")
updated += 1
db.commit()
if not _migration_applied(NAVY_BODY_FAT_CM_BACKFILL):
_mark_migration_applied(NAVY_BODY_FAT_CM_BACKFILL)
logger.info("Navy body fat cm backfill: updated %s body metric(s)", updated)
return updated
def run_fitness_migrations() -> None:
inspector = inspect(engine)
@@ -223,3 +271,4 @@ def run_fitness_migrations() -> None:
backfill_tdee_targets()
backfill_macros_gkg()
backfill_navy_body_fat_cm()