From 96b4fed18cc34d7028744e36a4a6fcb27c3a92d1 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 28 Oct 2023 05:14:32 +0300 Subject: [PATCH] Fix route --- .../api/response/analytics.py | 81 +++++++++++++++++-- .../db/repository/analytics.py | 2 +- .../py-conveyor-service/managers/analytics.py | 7 +- .../server/routers/analytics.py | 10 ++- 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/apps/py-conveyor-service/api/response/analytics.py b/apps/py-conveyor-service/api/response/analytics.py index 3efeca6..7a1bf17 100644 --- a/apps/py-conveyor-service/api/response/analytics.py +++ b/apps/py-conveyor-service/api/response/analytics.py @@ -1,18 +1,83 @@ from datetime import datetime from api.response.base import ResponseBase +from db.models.conveer import DBConveer +from pydantic import Field -class AreaCharts: - name: str - value: int +class AreaCharts(ResponseBase): + name: str = Field(...) + value: int = Field(...) class TimelineCharts(AreaCharts): - time: datetime + time: datetime = Field(...) -class RequestAnalytics(ResponseBase): - timeline_charts: list[TimelineCharts] - area_charts: list[AreaCharts] - bar_chars: list[AreaCharts] +class ResponseAnalytics(ResponseBase): + timeline_charts: list[TimelineCharts] = Field(...) + area_charts: list[AreaCharts] = Field(...) + total_numb: int = Field(...) + metal_numb: int = Field(...) + wood_numb: int = Field(...) + plastic_numb: int = Field(...) + glass_numb: int = Field(...) + + +class ResponseAnalyticsFactory: + + @staticmethod + def get_from_model_area_charts(value, name) -> AreaCharts: + return AreaCharts( + name=name, + value=value + ) + + @staticmethod + def count_categories(models: list[DBConveer]) -> list[int]: + wood = 0 + glass = 0 + metal = 0 + plastic = 0 + for i in models: + wood += i.wood + glass += i.glass + metal += i.metal + plastic += i.plastic + return [wood, glass, metal, plastic] + + @classmethod + def get_from_models_area_charts(cls, models: list[DBConveer]) -> list[AreaCharts]: + wood, glass, metal, plastic = cls.count_categories(models) + return [cls.get_from_model_area_charts(name='wood', value=wood), + cls.get_from_model_area_charts(name='glass', value=glass), + cls.get_from_model_area_charts(name='metal', value=metal), + cls.get_from_model_area_charts(name='plastic', value=plastic)] + + @classmethod + def get_from_model_timeline_charts(cls, name, value, time): + return TimelineCharts( + **cls.get_from_model_area_charts(name=name, value=value).__dict__, + time=time) + + @classmethod + def get_from_model_timeline_chart(cls, models: list[DBConveer]) -> list[TimelineCharts]: + response = [] + for i in models: + response.append(cls.get_from_model_timeline_charts(name='wood', value=i.wood, time=i.created_at)) + response.append(cls.get_from_model_timeline_charts(name='glass', value=i.glass, time=i.created_at)) + response.append(cls.get_from_model_timeline_charts(name='plastic', value=i.plastic, time=i.created_at)) + response.append(cls.get_from_model_timeline_charts(name='metal', value=i.metal, time=i.created_at)) + return response + + @classmethod + def get_from_models(cls, models: list[DBConveer]) -> ResponseAnalytics: + return ResponseAnalytics( + area_charts=cls.get_from_models_area_charts(models), + timeline_charts=cls.get_from_model_timeline_chart(models), + total_numb=sum(cls.count_categories(models)), + metal_numb=cls.count_categories(models)[2], + wood_numb=cls.count_categories(models)[0], + plastic_numb=cls.count_categories(models)[3], + glass_numb=cls.count_categories(models)[1] + ) diff --git a/apps/py-conveyor-service/db/repository/analytics.py b/apps/py-conveyor-service/db/repository/analytics.py index 9c347ca..46f30da 100644 --- a/apps/py-conveyor-service/db/repository/analytics.py +++ b/apps/py-conveyor-service/db/repository/analytics.py @@ -15,7 +15,7 @@ class AnalyticsRepository(BaseRepository): async def get_analytic(self, start_date: Optional[datetime], - end_date: Optional[datetime]): + end_date: Optional[datetime]) -> list[DBConveer]: query = ( select(DBConveer) .select_from(DBConveer) diff --git a/apps/py-conveyor-service/managers/analytics.py b/apps/py-conveyor-service/managers/analytics.py index 5564753..3883a60 100644 --- a/apps/py-conveyor-service/managers/analytics.py +++ b/apps/py-conveyor-service/managers/analytics.py @@ -3,6 +3,7 @@ from typing import Optional from sqlalchemy.ext.asyncio import AsyncSession +from db.models.conveer import DBConveer from db.repository.analytics import AnalyticsRepository @@ -12,7 +13,7 @@ class AnalyticsManager: async def get_by_filters(cls, session: AsyncSession, start_date: Optional[datetime], - end_date: Optional[datetime]): - datas = await AnalyticsRepository(session).get_analytic(start_date=start_date, - end_date=end_date) + end_date: Optional[datetime]) -> list[DBConveer]: + datas: list[DBConveer] = await AnalyticsRepository(session).get_analytic(start_date=start_date, + end_date=end_date) return datas diff --git a/apps/py-conveyor-service/server/routers/analytics.py b/apps/py-conveyor-service/server/routers/analytics.py index 14ed1d8..cef473f 100644 --- a/apps/py-conveyor-service/server/routers/analytics.py +++ b/apps/py-conveyor-service/server/routers/analytics.py @@ -2,21 +2,23 @@ from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from api.request.analytics import AnalyticsFilters +from api.response.analytics import ResponseAnalyticsFactory, ResponseAnalytics +from db.models.conveer import DBConveer from managers.analytics import AnalyticsManager -from server.depends import get_session, PagesPaginationParams +from server.depends import get_session router = APIRouter(prefix="/api/analytics", tags=['Ride']) -@router.post('/all') +@router.post('/all', response_model=ResponseAnalytics) async def get_all_analytics( filters: AnalyticsFilters, session: AsyncSession = Depends(get_session), ): - data = await AnalyticsManager.get_by_filters( + data: list[DBConveer] = await AnalyticsManager.get_by_filters( session=session, start_date=filters.start_time, end_date=filters.end_time ) - return data + return ResponseAnalyticsFactory.get_from_models(models=data)