Update stats
This commit is contained in:
@@ -111,3 +111,24 @@ async def update_session(
|
||||
await session.commit()
|
||||
await session.refresh(sleep_session)
|
||||
return sleep_session
|
||||
|
||||
|
||||
async def get_all_time_stats(session: AsyncSession) -> tuple[int, float]:
|
||||
statement = select(func.count(), func.min(SleepSession.start_time))
|
||||
result = await session.exec(statement)
|
||||
total_sessions, first_session_time = result.one()
|
||||
|
||||
if total_sessions == 0 or first_session_time is None:
|
||||
return 0, 0.0
|
||||
|
||||
# DB has naive datetimes
|
||||
first_session_time_naive = first_session_time.replace(tzinfo=None)
|
||||
|
||||
# Get current date (naive for comparison)
|
||||
today_date = datetime.now().date()
|
||||
first_date = first_session_time_naive.date()
|
||||
|
||||
num_days = (today_date - first_date).days + 1
|
||||
avg_sleeps = total_sessions / num_days if num_days > 0 else 0.0
|
||||
|
||||
return total_sessions, avg_sleeps
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from app.database import get_session
|
||||
from app.crud import get_sessions
|
||||
from app.crud import get_sessions, get_all_time_stats
|
||||
from app.schemas import DailySummaryResponse, TimetableResponse, WakeupBedtimeResponse, SummaryResponse
|
||||
from app.utils.time_utils import get_day_night_splits
|
||||
|
||||
@@ -112,9 +112,13 @@ async def get_summary(
|
||||
num_days = (to_date.date() - from_date.date()).days + 1
|
||||
avg_sleeps_per_day = total_sleeps / num_days if num_days > 0 else 0.0
|
||||
|
||||
total_sleeps_all_time, avg_sleeps_per_day_all_time = await get_all_time_stats(session)
|
||||
|
||||
return {
|
||||
"total_sleeps": total_sleeps,
|
||||
"avg_sleeps_per_day": avg_sleeps_per_day,
|
||||
"total_sleeps_all_time": total_sleeps_all_time,
|
||||
"avg_sleeps_per_day_all_time": avg_sleeps_per_day_all_time,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,4 +62,6 @@ class WakeupBedtimeResponse(BaseModel):
|
||||
class SummaryResponse(BaseModel):
|
||||
total_sleeps: int
|
||||
avg_sleeps_per_day: float
|
||||
total_sleeps_all_time: int
|
||||
avg_sleeps_per_day_all_time: float
|
||||
|
||||
|
||||
Reference in New Issue
Block a user