|
| 1 | +# SPDX-FileCopyrightText: 2024-2025 MTS PJSC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Annotated |
| 5 | + |
| 6 | +from fastapi import Depends |
| 7 | +from sqlalchemy import Row |
| 8 | + |
| 9 | +from data_rentgen.db.models.location import Location |
| 10 | +from data_rentgen.dto.pagination import PaginationDTO |
| 11 | +from data_rentgen.services.uow import UnitOfWork |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class LocationServiceDatasetStatistics: |
| 16 | + total_datasets: int = 0 |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def from_row(cls, row: Row | None): |
| 20 | + if not row: |
| 21 | + return cls() |
| 22 | + |
| 23 | + return cls(total_datasets=row.total_datasets) |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class LocationServiceJobStatistics: |
| 28 | + total_jobs: int = 0 |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def from_row(cls, row: Row | None): |
| 32 | + if not row: |
| 33 | + return cls() |
| 34 | + |
| 35 | + return cls(total_jobs=row.total_jobs) |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class LocationServiceStatistics: |
| 40 | + datasets: LocationServiceDatasetStatistics |
| 41 | + jobs: LocationServiceJobStatistics |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class LocationServiceResult: |
| 46 | + data: Location |
| 47 | + statistics: LocationServiceStatistics |
| 48 | + |
| 49 | + |
| 50 | +class LocationServicePaginatedResult(PaginationDTO[LocationServiceResult]): |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +class LocationService: |
| 55 | + def __init__(self, uow: Annotated[UnitOfWork, Depends()]): |
| 56 | + self._uow = uow |
| 57 | + |
| 58 | + async def paginate( |
| 59 | + self, |
| 60 | + page: int, |
| 61 | + page_size: int, |
| 62 | + location_ids: list[int], |
| 63 | + location_type: str | None, |
| 64 | + search_query: str | None, |
| 65 | + ) -> LocationServicePaginatedResult: |
| 66 | + pagination = await self._uow.location.paginate( |
| 67 | + page=page, |
| 68 | + page_size=page_size, |
| 69 | + location_ids=location_ids, |
| 70 | + location_type=location_type, |
| 71 | + search_query=search_query, |
| 72 | + ) |
| 73 | + location_ids = [item.id for item in pagination.items] |
| 74 | + dataset_stats = await self._uow.dataset.get_stats_by_location_ids(location_ids) |
| 75 | + job_stats = await self._uow.job.get_stats_by_location_ids(location_ids) |
| 76 | + |
| 77 | + return LocationServicePaginatedResult( |
| 78 | + page=pagination.page, |
| 79 | + page_size=pagination.page_size, |
| 80 | + total_count=pagination.total_count, |
| 81 | + items=[ |
| 82 | + LocationServiceResult( |
| 83 | + data=location, |
| 84 | + statistics=LocationServiceStatistics( |
| 85 | + datasets=LocationServiceDatasetStatistics.from_row(dataset_stats.get(location.id)), |
| 86 | + jobs=LocationServiceJobStatistics.from_row(job_stats.get(location.id)), |
| 87 | + ), |
| 88 | + ) |
| 89 | + for location in pagination.items |
| 90 | + ], |
| 91 | + ) |
| 92 | + |
| 93 | + async def update_external_id( |
| 94 | + self, |
| 95 | + location_id: int, |
| 96 | + external_id: str | None, |
| 97 | + ) -> LocationServiceResult: |
| 98 | + location = await self._uow.location.update_external_id(location_id, external_id) |
| 99 | + dataset_stats = await self._uow.dataset.get_stats_by_location_ids([location.id]) |
| 100 | + job_stats = await self._uow.job.get_stats_by_location_ids([location.id]) |
| 101 | + return LocationServiceResult( |
| 102 | + data=location, |
| 103 | + statistics=LocationServiceStatistics( |
| 104 | + datasets=LocationServiceDatasetStatistics.from_row(dataset_stats.get(location.id)), |
| 105 | + jobs=LocationServiceJobStatistics.from_row(job_stats.get(location.id)), |
| 106 | + ), |
| 107 | + ) |
0 commit comments