Skip to content

Commit

Permalink
add category filter to /metrics/zone-visited
Browse files Browse the repository at this point in the history
  • Loading branch information
marthevienne authored and rv2931 committed Nov 29, 2024
1 parent b6237f4 commit 02ebde5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
22 changes: 14 additions & 8 deletions backend/bloom/routers/v1/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
return jsonable_encoder(payload)
8 changes: 6 additions & 2 deletions backend/bloom/services/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
Expand Down

0 comments on commit 02ebde5

Please sign in to comment.