from typing import Annotated import asyncpg from dishka.integrations.fastapi import DishkaRoute, FromDishka from fastapi import APIRouter, Query from api.timezone import checked_tz from utils.read import analytics from utils.read.models import ResponseStats, VolumeBucket from utils.read.period import DEFAULT_TZ, MAX_DAYS router = APIRouter(prefix="/api/analytics", tags=["analytics"], route_class=DishkaRoute) AccountId = Annotated[int, Query()] ChatId = Annotated[int, Query()] @router.get("/volume") async def volume( pool: FromDishka[asyncpg.Pool], account_id: AccountId, chat_id: ChatId, days: Annotated[int, Query(ge=1, le=MAX_DAYS)] = 90, tz: Annotated[str, Query()] = DEFAULT_TZ, ) -> list[VolumeBucket]: return await analytics.message_volume( pool, account_id, chat_id, days=days, tz=await checked_tz(pool, tz) ) @router.get("/response-time") async def response_time( pool: FromDishka[asyncpg.Pool], account_id: AccountId, chat_id: ChatId ) -> ResponseStats: return await analytics.response_stats(pool, account_id, chat_id)