From 8701678bb635916db3ff9d5cc547d3abcf6457bd Mon Sep 17 00:00:00 2001 From: Jeremy Zilar Date: Tue, 21 Apr 2026 11:53:51 -0400 Subject: [PATCH 1/2] Add first_field_event to well details response The field_events query returns the 25 most recent events sorted newest-first. For wells with many visits the oldest event falls outside that window, making it impossible to retrieve first-visit participants from the paged list. Adds a separate targeted query that always fetches the single oldest field event (with participants) and returns it as first_field_event alongside the existing paged field_events list. --- schemas/well_details.py | 1 + services/well_details_helper.py | 40 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/schemas/well_details.py b/schemas/well_details.py index 2d058cd2..228e0847 100644 --- a/schemas/well_details.py +++ b/schemas/well_details.py @@ -51,3 +51,4 @@ class WellDetailsResponse(BaseModel): deployments: list[DeploymentResponse] = Field(default_factory=list) well_screens: list[WellScreenBaseResponse] = Field(default_factory=list) field_events: list[WellDetailsFieldEventResponse] = Field(default_factory=list) + first_field_event: WellDetailsFieldEventResponse | None = None diff --git a/services/well_details_helper.py b/services/well_details_helper.py index 1ce6f8fd..52f61580 100644 --- a/services/well_details_helper.py +++ b/services/well_details_helper.py @@ -111,27 +111,40 @@ def get_well_details_payload( .order_by(WellScreen.screen_depth_top.asc(), WellScreen.id.asc()) ).all() + _participant_options = ( + selectinload(FieldEvent.field_event_participants).selectinload( + FieldEventParticipant.participant + ) + ) + _activity_options = [ + selectinload(FieldEvent.field_activities) + .selectinload(FieldActivity.samples) + .selectinload(Sample.field_event_participant) + .selectinload(FieldEventParticipant.participant), + selectinload(FieldEvent.field_activities) + .selectinload(FieldActivity.samples) + .selectinload(Sample.observations) + .selectinload(Observation.parameter), + ] + with _payload_stage_timer("well_details", "load_field_events", thing_id): field_events = session.scalars( select(FieldEvent) .where(FieldEvent.thing_id == well.id) - .options( - selectinload(FieldEvent.field_event_participants).selectinload( - FieldEventParticipant.participant - ), - selectinload(FieldEvent.field_activities) - .selectinload(FieldActivity.samples) - .selectinload(Sample.field_event_participant) - .selectinload(FieldEventParticipant.participant), - selectinload(FieldEvent.field_activities) - .selectinload(FieldActivity.samples) - .selectinload(Sample.observations) - .selectinload(Observation.parameter), - ) + .options(_participant_options, *_activity_options) .order_by(FieldEvent.event_date.desc(), FieldEvent.id.desc()) .limit(field_event_limit) ).all() + with _payload_stage_timer("well_details", "load_first_field_event", thing_id): + first_field_event = session.scalars( + select(FieldEvent) + .where(FieldEvent.thing_id == well.id) + .options(_participant_options) + .order_by(FieldEvent.event_date.asc(), FieldEvent.id.asc()) + .limit(1) + ).first() + return { "well": well, "contacts": contacts, @@ -139,6 +152,7 @@ def get_well_details_payload( "deployments": deployments, "well_screens": well_screens, "field_events": field_events, + "first_field_event": first_field_event, } From 02b6ad33eff1d9d4b95335613fcf8fba1c3df423 Mon Sep 17 00:00:00 2001 From: jeremyzilar <395641+jeremyzilar@users.noreply.github.com> Date: Tue, 21 Apr 2026 15:55:35 +0000 Subject: [PATCH 2/2] Formatting changes --- services/well_details_helper.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/services/well_details_helper.py b/services/well_details_helper.py index 52f61580..53d40a02 100644 --- a/services/well_details_helper.py +++ b/services/well_details_helper.py @@ -111,11 +111,9 @@ def get_well_details_payload( .order_by(WellScreen.screen_depth_top.asc(), WellScreen.id.asc()) ).all() - _participant_options = ( - selectinload(FieldEvent.field_event_participants).selectinload( - FieldEventParticipant.participant - ) - ) + _participant_options = selectinload( + FieldEvent.field_event_participants + ).selectinload(FieldEventParticipant.participant) _activity_options = [ selectinload(FieldEvent.field_activities) .selectinload(FieldActivity.samples)