diff --git a/backend/bloom/routers/v1/metrics.py b/backend/bloom/routers/v1/metrics.py index 1a70c78f..732c54a7 100644 --- a/backend/bloom/routers/v1/metrics.py +++ b/backend/bloom/routers/v1/metrics.py @@ -49,22 +49,28 @@ async def read_metrics_vessels_in_activity_total(request: Request, return jsonable_encoder(payload) + @router.get("/metrics/zone-visited") @cache -async def read_zone_visited_total(request: Request, - datetime_range: DatetimeRangeRequest = Depends(), - pagination: PageParams = Depends(), - order: OrderByRequest = Depends(), - caching: CachedRequest = Depends(), - key: str = Depends(X_API_KEY_HEADER),): +async def read_zone_visited_total( + request: Request, + datetime_range: DatetimeRangeRequest = Depends(), + category: Optional[str] = None, + pagination: PageParams = Depends(), + order: OrderByRequest = Depends(), + caching: CachedRequest = Depends(), + key: str = Depends(X_API_KEY_HEADER), +): check_apikey(key) use_cases = UseCases() MetricsService=use_cases.metrics_service() payload=MetricsService.getZoneVisited(datetime_range=datetime_range, + category=category, pagination=pagination, order=order) return jsonable_encoder(payload) + @router.get("/metrics/zones/{zone_id}/visiting-time-by-vessel") @cache async def read_metrics_zone_visiting_time_by_vessel(request: Request, @@ -85,7 +91,7 @@ async def read_metrics_zone_visiting_time_by_vessel(request: Request, return jsonable_encoder(payload) @router.get("/metrics/vessels/time-by-zone") -#@cache +# @cache async def read_metrics_all_vessels_visiting_time_by_zone(request: Request, vessel_id: Optional[int] = None, category: Optional[str] = None, @@ -104,4 +110,4 @@ async def read_metrics_all_vessels_visiting_time_by_zone(request: Request, order=order, category=category, sub_category=sub_category) - return jsonable_encoder(payload) \ No newline at end of file + return jsonable_encoder(payload) diff --git a/backend/bloom/services/metrics.py b/backend/bloom/services/metrics.py index 20a2d3a1..6b1a185d 100644 --- a/backend/bloom/services/metrics.py +++ b/backend/bloom/services/metrics.py @@ -71,7 +71,9 @@ def getVesselsInActivity(self, def getZoneVisited(self, datetime_range: DatetimeRangeRequest, pagination: PageParams, - order: OrderByRequest): + order: OrderByRequest, + category: Optional[str]=None, + ): payload=[] with self.session_factory() as session: stmt=select( @@ -84,9 +86,11 @@ def getZoneVisited(self, .where( or_( sql_model.Segment.timestamp_start.between(datetime_range.start_at,datetime_range.end_at), - sql_model.Segment.timestamp_end.between(datetime_range.start_at,datetime_range.end_at),) + sql_model.Segment.timestamp_end.between(datetime_range.start_at,datetime_range.end_at)) )\ .group_by(sql_model.Zone.id) + if (category): + stmt = stmt.where(sql_model.Zone.category == category) stmt = stmt.order_by(func.sum(sql_model.Segment.segment_duration).asc())\ if order.order == OrderByEnum.ascending \ else stmt.order_by(func.sum(sql_model.Segment.segment_duration).desc())