import logging from typing import TYPE_CHECKING from src import domain, dto if TYPE_CHECKING: from .. import Usecase log = logging.getLogger(__name__) async def get_views_history(self: 'Usecase', input: dto.GetViewsHistoryInput) -> dto.GetViewsHistoryOutput: context = await self.ensure_workspace_permission( input.workspace_id, input.user_id, domain.PermissionKey.PLACEMENTS_READ ) placement = await self.database.get_placement(input.workspace_id, input.placement_id) if not placement: log.warning('Placement %s not found for user %s', input.placement_id, input.user_id) raise domain.PlacementNotFound(input.placement_id) context.ensure_project_permission(domain.PermissionKey.PLACEMENTS_READ, placement.project_id) histories = await self.database.get_views_history( input.placement_id, from_date=input.from_date, to_date=input.to_date, ) return dto.GetViewsHistoryOutput( histories=[ dto.PlacementViewsHistoryOutput( id=history.id, placement_id=history.placement_id, views_count=history.views_count, fetched_at=history.fetched_at, created_at=history.created_at, ) for history in histories ] )