Skip to content

Commit

Permalink
feat #309 migrer endpoint /metrics/activity-in-mpas
Browse files Browse the repository at this point in the history
  • Loading branch information
marthevienne authored and rv2931 committed Dec 23, 2024
1 parent bf8ac26 commit 4ae6a37
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
5 changes: 5 additions & 0 deletions backend/bloom/domain/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ResponseMetricsVesselInActivitySchema(BaseModel):
vessel: VesselListView
total_time_at_sea: Optional[timedelta]

class ResponseMetricsVesselInMpasSchema(BaseModel):
model_config = ConfigDict(from_attributes=True)
vessel: VesselListView
total_time_in_mpas: Optional[timedelta]

class ResponseMetricsZoneVisitedSchema(BaseModel):
zone: ZoneListView
visiting_duration: timedelta
Expand Down
20 changes: 20 additions & 0 deletions backend/bloom/routers/v1/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,23 @@ async def read_metrics_all_vessels_visiting_time_by_zone(request: Request,
category=category,
sub_category=sub_category)
return jsonable_encoder(payload)


@router.get("/metrics/vessels/activity-in-mpas")
# @cache
async def read_metrics_all_vessels_visiting_time_in_mpas(
request: Request,
datetime_range: DatetimeRangeRequest = Depends(),
pagination: PageParams = Depends(),
order: OrderByRequest = Depends(),
key: str = Depends(X_API_KEY_HEADER),
):
check_apikey(key)
use_cases = UseCases()
MetricsService = use_cases.metrics_service()
payload = MetricsService.get_vessels_in_mpas(
datetime_range=datetime_range,
pagination=pagination,
order=order
)
return jsonable_encoder(payload)
62 changes: 51 additions & 11 deletions backend/bloom/services/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from bloom.domain.metrics import (ResponseMetricsVesselInActivitySchema,
ResponseMetricsZoneVisitedSchema,
ResponseMetricsVesselInMpasSchema,
ResponseMetricsZoneVisitingTimeByVesselSchema,
ResponseMetricsVesselTotalTimeActivityByActivityTypeSchema,
ResponseMetricsVesselVisitingTimeByZoneSchema)
Expand Down Expand Up @@ -72,7 +73,50 @@ def getVesselsInActivity(self,
total_time_at_sea=item[1]
)\
for item in payload]


def get_vessels_in_mpas(self,
datetime_range: DatetimeRangeRequest,
pagination: PageParams,
order: OrderByRequest):
payload=[]
with self.session_factory() as session:
stmt = (
select(
sql_model.Vessel,
func.sum(sql_model.Metrics.duration_total).label(
"total_time_in_mpas"
),
)
.select_from(sql_model.Metrics)
.join(
sql_model.Vessel,
sql_model.Vessel.id == sql_model.Metrics.vessel_id,
)
.where(
sql_model.Metrics.timestamp.between(
datetime_range.start_at, datetime_range.end_at
)
)
.where(sql_model.Metrics.zone_category == "amp")
.group_by(sql_model.Vessel)
)
stmt = stmt.offset(pagination.offset) if pagination.offset != None else stmt
stmt = (
stmt.order_by(asc("total_time_in_mpas"))
if order.order == OrderByEnum.ascending
else stmt.order_by(desc("total_time_in_mpas"))
)
stmt = stmt.limit(pagination.limit) if pagination.limit != None else stmt
payload=session.execute(stmt).all()

return [
ResponseMetricsVesselInMpasSchema(
vessel=VesselRepository.map_to_domain(item[0]).model_dump(),
total_time_in_mpas=item[1],
)
for item in payload
]

def getVesselsAtSea(self,
datetime_range: DatetimeRangeRequest,
):
Expand All @@ -95,7 +139,6 @@ def getVesselsAtSea(self,
)
return session.execute(stmt).scalar()


def getZoneVisited(self,
datetime_range: DatetimeRangeRequest,
pagination: PageParams,
Expand Down Expand Up @@ -137,15 +180,15 @@ def getZoneVisited(self,
visiting_duration=item[1]
)\
for item in payload]

def getZoneVisitingTimeByVessel(self,
zone_id: int,
datetime_range: DatetimeRangeRequest,
order: OrderByRequest,
pagination: PageParams,):
payload=[]
with self.session_factory() as session:

stmt=select(
sql_model.Zone,
sql_model.Vessel,
Expand All @@ -166,7 +209,7 @@ def getZoneVisitingTimeByVessel(self,
)
)\
.group_by(sql_model.Zone.id,sql_model.Vessel.id)

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 All @@ -185,7 +228,7 @@ def getZoneVisitingTimeByVessel(self,
zone_visiting_time_by_vessel=item[2]
)\
for item in payload]

def getVesselVisitingTimeByZone(self,
order: OrderByRequest,
datetime_range: DatetimeRangeRequest,
Expand Down Expand Up @@ -226,14 +269,12 @@ def getVesselVisitingTimeByZone(self,
stmt = stmt.limit(pagination.limit) if pagination.limit != None else stmt
if vessel_id is not None:
stmt=stmt.where(sql_model.Vessel.id==vessel_id)



return [ResponseMetricsVesselVisitingTimeByZoneSchema(
vessel=VesselListView(**VesselRepository.map_to_domain(model[0]).model_dump()),
zone=ZoneListView(**ZoneRepository.map_to_domain(model[1]).model_dump()),
vessel_visiting_time_by_zone=model[2]) for model in session.execute(stmt).all()]


def getVesselVisitsByActivityType(self,
vessel_id: int,
activity_type: TotalTimeActivityTypeRequest,
Expand Down Expand Up @@ -261,10 +302,9 @@ def getVesselVisitsByActivityType(self,
literal_column('0 seconds'),
))
payload=session.execute(stmt.limit(1)).scalar_one_or_none()

return [ ResponseMetricsVesselTotalTimeActivityByActivityTypeSchema(
vessel_id=item.id,
activity=item.activity,
total_activity_time=item.total_activity_time,
) for item in payload]

0 comments on commit 4ae6a37

Please sign in to comment.