from backend.src.services.postgresql_manager import PostgreSQLManager from backend.src.utils.config import config from backend.src.models.weight_record import WeightRecord from backend.src.models.health_metric import HealthMetric from sqlalchemy import func import sys def check_count(): print("DEBUG: Connecting to DB...", flush=True) try: db_manager = PostgreSQLManager(config.DATABASE_URL) print("DEBUG: Session factory created.", flush=True) with db_manager.get_db_session() as session: print("DEBUG: Session active.", flush=True) # Check WeightRecord try: wr_count = session.query(func.count(WeightRecord.id)).scalar() print(f"Total WeightRecord (Legacy?) records: {wr_count}") except Exception as e: print(f"Error querying WeightRecord: {e}") # Check HealthMetric (Fitbit Weight) try: hm_count = session.query(func.count(HealthMetric.id)).filter( HealthMetric.metric_type == 'weight', HealthMetric.source == 'fitbit' ).scalar() print(f"Total HealthMetric (Fitbit Weight) records: {hm_count}") except Exception as e: print(f"Error querying HealthMetric: {e}") # Also check top 5 from HealthMetric print("\nLatest 5 HealthMetric (Fitbit Weight) records:") latest = session.query(HealthMetric).filter( HealthMetric.metric_type == 'weight', HealthMetric.source == 'fitbit' ).order_by(HealthMetric.date.desc()).limit(5).all() for r in latest: print(f" - {r.date}: {r.metric_value} {r.unit}") except Exception as e: print(f"CRITICAL ERROR: {e}") import traceback traceback.print_exc() if __name__ == "__main__": check_count()