Skip to content

Fix deployment sessions creation & counts #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 21, 2025

Conversation

mohamedelabbas1996
Copy link
Contributor

@mohamedelabbas1996 mohamedelabbas1996 commented Jul 11, 2025

Summary

This PR ensures that sessions (Events) are created and updated when captures are synced for a given deployment (station), so that users inspecting newly synced data immediately see grouped sessions without requiring manual intervention.

List of Changes

  • Added utility function deployment_event_needs_update(deployment) to check whether new SourceImages have been added since the last Event was created.

  • Used this utility in sync_captures to conditionally call group_images_into_events() only when needed.

Related Issues

Closes #872

Detailed Description

This PR ensures that sessions (Events) are created and updated after syncing captures from a data source. Previously, sessions had to be manually triggered from the admin, which often led to confusion for users trying to inspect newly synced data. To solve this, a new utility function deployment_event_needs_update(deployment) was added to compare the latest SourceImage creation time with the latest Event creation time, and only run group_images_into_events if new images exist that haven’t been grouped yet. This approach keeps session data up-to-date without running redundant grouping operations and improves the overall user experience by ensuring that session information is available immediately after sync.

How to Test the Changes

  1. Create a test station (deployment) from the the front-end.

  2. Add a data source to the deployment.

  3. Trigger the sync job to pull images from the data source.

  4. After the sync job finishes successfully:

    a. Verify that sessions (Events) have been created for the synced images.

    b. Confirm that the session's count field has been updated accordingly.

Screenshots

Screenshot 2025-07-14 at 16 14 42

Deployment Notes

N/A

Checklist

  • I have tested these changes appropriately.
  • I have added and/or modified relevant tests.
  • I updated relevant documentation or comments.
  • I have verified that this PR follows the project's coding standards.
  • Any dependent changes have already been merged to main.

…to make sure that sessions are created and session counts are updated
@mohamedelabbas1996 mohamedelabbas1996 self-assigned this Jul 11, 2025
Copy link

netlify bot commented Jul 11, 2025

Deploy Preview for antenna-preview canceled.

Name Link
🔨 Latest commit 113d466
🔍 Latest deploy log https://app.netlify.com/projects/antenna-preview/deploys/6877d1c5ff2393000856f869

… to determine if we need to regroup images into events
@mohamedelabbas1996 mohamedelabbas1996 marked this pull request as ready for review July 14, 2025 21:45
@mohamedelabbas1996 mohamedelabbas1996 changed the title [Draft] Fix deployment sessions creation & counts Fix deployment sessions creation & counts Jul 14, 2025
@mohamedelabbas1996 mohamedelabbas1996 requested a review from mihow July 14, 2025 22:37
return True

# Compare timestamps
needs_update = latest_image_time > latest_event_time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will always be true. Since the timestamps are not updated. There will always be source images created after the event was created.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two other options

  • Run the grouping function without actually saving sessions and use that output to compare the images in the current sessions to the would-be sessions.
  • Add a last_checked field to the Event (session) model and update that after regrouping.

I'm okay to postpone those checks and just run the session regrouping at the end of the sync for now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regrouping "audit" function would need to be efficient - just fetch the timestamp field from all source images from the deployment and determine the would-be sessions.

See

antenna/ami/utils/dates.py

Lines 98 to 101 in 147183b

def group_datetimes_by_gap(
timestamps: list[datetime.datetime],
max_time_gap=datetime.timedelta(minutes=120),
) -> list[list[datetime.datetime]]:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will always be true. Since the timestamps are not updated. There will always be source images created after the event was created.

Good point! thanks for flagging that. One idea I had is to add a small time delta (e.g., a few seconds) when comparing the timestamps, so we only regroup if the newest image was created meaningfully after the latest event.

Copy link
Collaborator

@mihow mihow Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember the create time for an event is the first time the object was created. Same for captures.

Perhaps here is what you meant?

SourceImage.timestamp > Event.end_datetime

Then you’re comparing the time that a capture was captured with the current date range of the session.

Another idea, What if you check and see if the source image does not belong to an event yet?

SourceImage.event = None

SourceImage.filter(deployment=self, event=None)

Or event__isnull=True

@mihow
Copy link
Collaborator

mihow commented Jul 15, 2025 via email

…he object store

for a deployment, resyncing triggers session creation or updates accordingly.
Copy link
Collaborator

@mihow mihow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and it works!

@mihow mihow merged commit 04ae3fa into main Jul 21, 2025
6 checks passed
@mihow mihow deleted the fix/auto-update-sessions-on-capture-sync branch July 21, 2025 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sessions not auto updated when captures are synced
2 participants