From 82d06fbff1a52893e29fa81d95350d024fb644ec Mon Sep 17 00:00:00 2001 From: RV Date: Wed, 11 Dec 2024 12:03:47 +0100 Subject: [PATCH] fix: #315 avoid request when no vessel_id found (no excursion, no segment) --- .../infra/repositories/repository_segment.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/backend/bloom/infra/repositories/repository_segment.py b/backend/bloom/infra/repositories/repository_segment.py index 6c6c72ce..ddd64668 100644 --- a/backend/bloom/infra/repositories/repository_segment.py +++ b/backend/bloom/infra/repositories/repository_segment.py @@ -292,15 +292,17 @@ def update_last_segments(self, session: Session, vessel_ids: list[int]) -> int: values(last_vessel_segment=False)) session.execute(upd1) session.flush() - last_segments = session.execute(text("""SELECT DISTINCT ON (vessel_id) s.id FROM fct_segment s - JOIN fct_excursion e ON e.id = s.excursion_id - WHERE vessel_id in :vessel_ids - ORDER BY vessel_id, timestamp_start DESC"""), - {"vessel_ids": tuple(vessel_ids)}).all() - ids = [r[0] for r in last_segments] - upd2 = update(sql_model.Segment).where(sql_model.Segment.id.in_(ids)).values( - last_vessel_segment=True) - session.execute(upd2) + ids=[] + if(vessel_ids): + last_segments = session.execute(text("""SELECT DISTINCT ON (vessel_id) s.id FROM fct_segment s + JOIN fct_excursion e ON e.id = s.excursion_id + WHERE vessel_id in :vessel_ids + ORDER BY vessel_id, timestamp_start DESC"""), + {"vessel_ids": tuple(vessel_ids)}).all() + ids = [r[0] for r in last_segments] + upd2 = update(sql_model.Segment).where(sql_model.Segment.id.in_(ids)).values( + last_vessel_segment=True) + session.execute(upd2) return len(ids) @staticmethod