Add detections_count to sequence and alert responses#559
Add detections_count to sequence and alert responses#559fe51 merged 10 commits intopyronear:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #559 +/- ##
==========================================
+ Coverage 88.75% 89.00% +0.25%
==========================================
Files 52 53 +1
Lines 2196 2219 +23
==========================================
+ Hits 1949 1975 +26
+ Misses 247 244 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
fe51
left a comment
There was a problem hiding this comment.
Hi @ThbltLmr , Thank you a lot for your PR !
I have a few comments, but no big changes !
Also, about the consideration of adding detection_counts to each sequences related endpoints, here is my views :
- frontend actually uses only /alerts/unlabeled/latest and /alerts/all/fromdate and we will need detections count (later or perform pagination and improve front endplayer)
- Elsewhere, we do not require performance, and the query added is no expensive, so lets add it. Could be deleted later if not needed and an issue.
Thanks again, I think after answering this comments we will be able to merge quite quickly :)
| ) | ||
| res = await session.exec(stmt) | ||
| return { | ||
| int(sequence_id): int(detections_count) |
There was a problem hiding this comment.
Why the int() wrapping? After a DB fetch, sequence.id/alert.id are already int per the model annotation, this seems redundant. it is about test/style/ruff ?
There was a problem hiding this comment.
Comment here apply in other occurence of int(sequence.id) and int(alert.id)
|
|
||
|
|
||
| def _serialize_alert(alert: Alert, sequences: List[Sequence]) -> AlertReadWithSequences: | ||
| def _serialize_sequence(sequence: Sequence, detection_counts: Dict[int, int]) -> SequenceRead: |
There was a problem hiding this comment.
_serialize_sequence has two different signatures across alerts.py and sequences.py. Could you align them? The sequences.py version (sequence, detections_count: int) seems simpler — the caller does the dict lookup, the function just serializes.
| await session.commit() | ||
| for seq in sequences: | ||
| await session.refresh(seq) | ||
| for sequence, detections_count in zip(sequences, detections_count_by_sequence, strict=False): |
There was a problem hiding this comment.
| for sequence, detections_count in zip(sequences, detections_count_by_sequence, strict=False): | |
| for sequence, detections_count in zip(sequences, detections_count_by_sequence, strict=True): |
strict=False silently ignores a length mismatch between sequences and detections_count_by_sequence. strict=True would catch it immediately if the fixture is ever changed.
Per review feedback on PR pyronear#559: get_detection_counts_by_sequence_ids is an aggregation query, not a CRUD/route helper, so it belongs in services/ where it can be reused outside of endpoint modules.
Per review feedback on PR pyronear#559: _serialize_sequence in alerts.py took the full Dict[int, int] of counts and did the lookup inside. Match the sequences.py version (scalar int param, caller does the lookup) so the helper has one shape repo-wide and only does serialization.
Per review feedback on PR pyronear#559: Sequence.id and Alert.id are typed int on the model, so int(sequence.id) / int(alert.id) is redundant. Same for the dict comprehension in get_detection_counts_by_sequence_ids where the row values are already int. The int(alert_id) at alerts.py:46 stays because alert_id there is unpacked from a raw SQL row tuple, not a model attribute.
Per review feedback on PR pyronear#559: strict=False silently swallows length mismatches. Both lists have length 3 today, so flipping to strict=True guards against future fixture edits drifting out of sync.
Per review feedback on PR pyronear#559: Sequence.id and Alert.id are typed int on the model, so int(sequence.id) / int(alert.id) is redundant. Same for the dict comprehension in get_detection_counts_by_sequence_ids where the row values are already int. The int(alert_id) at alerts.py:46 stays because alert_id there is unpacked from a raw SQL row tuple, not a model attribute.
Per review feedback on PR pyronear#559: strict=False silently swallows length mismatches. Both lists have length 3 today, so flipping to strict=True guards against future fixture edits drifting out of sync.
Rebase fallout from pyronear#574 (datetime.utcnow deprecation): one new test function still called datetime.utcnow() while the datetime import had been removed from the file, causing NameError on collection.
Closes #551
Summary
detections_counton sequence responses returned by sequence endpointsdetections_counton sequence payloads returned by alert endpointsTesting
Screen.Recording.2026-04-07.at.23.22.03.mov
Notes