-
Notifications
You must be signed in to change notification settings - Fork 15
fix(occurrences): remove all n+1 queries from occurrence API #1274
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
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3e7d9dd
fix(occurrences): prefetch detections/classifications in list view to…
mihow b62fb39
fix(occurrences): address PR #1274 review feedback
mihow 80d4404
refactor(occurrences): centralize BEST_IDENTIFICATION_ORDER constant
mihow 22995b3
refactor(occurrences): require prefetch in serializer; drop fallback …
mihow 454ec77
refactor(occurrences): strict prefetch helpers + bounded list detecti…
mihow 38b04cb
test(occurrences): trim test surface
mihow 8f7db77
fix(occurrences): apply detail prefetches to JSON exporter; cap list …
mihow 1f5918a
test(occurrence): cover multi-detection scaling + drop None scores
mihow cbae3fc
chore(scripts): add reproducible benchmark for occurrences list endpoint
mihow 80f36cb
refactor(occurrence): drop empty wrappers, trim prefetch helper docst…
mihow 9b1c532
test: add N+1 regression guards for SourceImage and Taxon list endpoints
mihow d41deb7
fix(api): make TaxonListSerializer.parents actually return parent chain
mihow 054d331
chore: address CodeRabbit nits on test fixture and benchmark script
mihow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| """ | ||
| Reusable Prefetch factories for Occurrence list-view rendering. | ||
|
|
||
| Centralising these lets the queryset, serializer, and any future caller share | ||
| a single source of truth for what data the list view needs eagerly loaded. | ||
|
|
||
| Tracking issue: https://github.com/RolnickLab/antenna/issues/1271 | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from django.db.models import Prefetch | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ami.main.models import Classification, Identification, Occurrence | ||
|
|
||
|
|
||
| def prefetch_detections_for_list() -> Prefetch: | ||
| """Single detections prefetch covering image URL listing AND best-prediction selection. | ||
|
|
||
| One pass loads detections (ordered for stable image lists) plus their | ||
| classifications with `taxon`/`algorithm` joined. The serializer derives | ||
| image URLs by filtering `path is not None` in Python and picks the best | ||
| machine prediction from the same cache. | ||
|
|
||
| Replaces the previous pair (a `to_attr` filtered list for image paths | ||
| plus a separate `detections__classifications` prefetch) which loaded the | ||
| detections relation twice. | ||
| """ | ||
| from ami.main.models import Classification, Detection | ||
|
|
||
| return Prefetch( | ||
| "detections", | ||
| queryset=( | ||
| Detection.objects.prefetch_related( | ||
| Prefetch( | ||
| "classifications", | ||
| queryset=Classification.objects.select_related("taxon", "algorithm"), | ||
| ) | ||
| ).order_by("frame_num", "timestamp") | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def prefetches_for_list_serializer() -> list[Prefetch]: | ||
| """All prefetches `OccurrenceListSerializer` needs to render without N+1. | ||
|
|
||
| Identifications are covered by `OccurrenceQuerySet.with_identifications()` | ||
| which is already applied in the list viewset; intentionally not duplicated | ||
| here. | ||
| """ | ||
| return [prefetch_detections_for_list()] | ||
|
|
||
|
|
||
| def has_prefetched_classifications(occurrence: Occurrence) -> bool: | ||
| """Return True iff `detections` AND each detection's `classifications` are prefetched. | ||
|
|
||
| The list path prefetches both via `prefetch_detections_for_list()`. The | ||
| detail path prefetches only `detections` — calling | ||
| `best_prediction_from_prefetch()` there would walk `det.classifications.all()` | ||
| and reintroduce an N+1 (one query per detection). | ||
| """ | ||
| cache = getattr(occurrence, "_prefetched_objects_cache", {}) | ||
| detections = cache.get("detections") | ||
| if detections is None: | ||
| return False | ||
| return all("classifications" in getattr(det, "_prefetched_objects_cache", {}) for det in detections) | ||
|
|
||
|
|
||
| def best_prediction_from_prefetch(occurrence: Occurrence) -> Classification | None: | ||
| """Pick the best machine prediction from a prefetched occurrence in Python. | ||
|
|
||
| Mirrors `Occurrence.best_prediction`, which calls `Occurrence.predictions()` | ||
| (per-algorithm max-score filtering) and then orders by `-terminal, -score`. | ||
| Replicating that grouping in Python keeps list and detail responses | ||
| consistent for occurrences whose top-scoring classification is non-terminal. | ||
|
|
||
| Requires `prefetch_detections_for_list()` (or equivalent) to have been | ||
| applied; walks `obj.detections.all()` -> `det.classifications.all()` from | ||
| the prefetch cache. | ||
| """ | ||
| classifications = [c for det in occurrence.detections.all() for c in det.classifications.all()] | ||
| if not classifications: | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return None | ||
|
|
||
| max_score_per_algo: dict[object, float] = {} | ||
| for c in classifications: | ||
| score = c.score if c.score is not None else float("-inf") | ||
| existing = max_score_per_algo.get(c.algorithm_id) | ||
| if existing is None or score > existing: | ||
| max_score_per_algo[c.algorithm_id] = score | ||
|
|
||
| candidates = [ | ||
| c | ||
| for c in classifications | ||
| if (c.score if c.score is not None else float("-inf")) == max_score_per_algo[c.algorithm_id] | ||
| ] | ||
| if not candidates: | ||
| return None | ||
| candidates.sort( | ||
| key=lambda c: ( | ||
| 0 if getattr(c, "terminal", False) else 1, | ||
| -(c.score or 0.0), | ||
|
mihow marked this conversation as resolved.
Outdated
|
||
| -c.pk, | ||
| ) | ||
| ) | ||
| return candidates[0] | ||
|
|
||
|
|
||
|
mihow marked this conversation as resolved.
Outdated
|
||
| def best_identification_from_prefetch(occurrence: Occurrence) -> Identification | None: | ||
| """Pick the most recent non-withdrawn identification from prefetched data. | ||
|
|
||
| Mirrors `Occurrence.best_identification`, which uses | ||
| `BEST_IDENTIFICATION_ORDER = ("-created_at", "-pk")`. `created_at=None` is | ||
| treated as lower than any real timestamp. | ||
| """ | ||
| best: Identification | None = None | ||
| best_key: tuple[bool, object, int] | None = None | ||
| for ident in occurrence.identifications.all(): | ||
| if ident.withdrawn: | ||
| continue | ||
| ident_key: tuple[bool, object, int] = (ident.created_at is not None, ident.created_at, ident.pk) | ||
| if best_key is None or ident_key > best_key: | ||
| best = ident | ||
|
mihow marked this conversation as resolved.
|
||
| best_key = ident_key | ||
| return best | ||
|
|
||
|
|
||
| def detection_image_urls_from_prefetch(occurrence: Occurrence, limit: int | None = None) -> list[str]: | ||
| """Return media URLs for the prefetched detections (filtering out `path=None`). | ||
|
|
||
| Requires `prefetch_detections_for_list()` to have been applied. | ||
| """ | ||
| from ami.main.models import get_media_url | ||
|
|
||
| detections = [det for det in occurrence.detections.all() if det.path] | ||
| if limit is not None: | ||
| detections = detections[:limit] | ||
| return [get_media_url(det.path) for det in detections] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.