57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
from datetime import datetime
|
|
from typing import Annotated
|
|
|
|
import asyncpg
|
|
from dishka.integrations.fastapi import DishkaRoute, FromDishka
|
|
from fastapi import APIRouter, Query
|
|
|
|
from utils.read import presence
|
|
from utils.read.models import DEFAULT_LIMIT, Page, PresenceHourly, PresenceSample
|
|
|
|
router = APIRouter(prefix="/api/presence", tags=["presence"], route_class=DishkaRoute)
|
|
|
|
AccountId = Annotated[int, Query()]
|
|
PeerId = Annotated[int, Query()]
|
|
DateFrom = Annotated[datetime | None, Query()]
|
|
DateTo = Annotated[datetime | None, Query()]
|
|
|
|
|
|
@router.get("")
|
|
async def presence_history(
|
|
pool: FromDishka[asyncpg.Pool],
|
|
account_id: AccountId,
|
|
peer_id: PeerId,
|
|
date_from: DateFrom = None,
|
|
date_to: DateTo = None,
|
|
limit: Annotated[int, Query()] = DEFAULT_LIMIT,
|
|
offset: Annotated[int, Query()] = 0,
|
|
) -> list[PresenceSample]:
|
|
return await presence.presence_history(
|
|
pool,
|
|
account_id,
|
|
peer_id,
|
|
Page(limit=limit, offset=offset),
|
|
date_from=date_from,
|
|
date_to=date_to,
|
|
)
|
|
|
|
|
|
@router.get("/current")
|
|
async def current_presence(
|
|
pool: FromDishka[asyncpg.Pool], account_id: AccountId, peer_id: PeerId
|
|
) -> PresenceSample | None:
|
|
return await presence.current_presence(pool, account_id, peer_id)
|
|
|
|
|
|
@router.get("/hourly")
|
|
async def presence_hourly(
|
|
pool: FromDishka[asyncpg.Pool],
|
|
account_id: AccountId,
|
|
peer_id: PeerId,
|
|
date_from: DateFrom = None,
|
|
date_to: DateTo = None,
|
|
) -> list[PresenceHourly]:
|
|
return await presence.presence_hourly(
|
|
pool, account_id, peer_id, date_from=date_from, date_to=date_to
|
|
)
|