@@ -1933,11 +1933,32 @@ def __str__(self) -> str:
1933
1933
return f"#{ self .pk } from SourceImage #{ self .source_image_id } with Algorithm #{ self .detection_algorithm_id } "
1934
1934
1935
1935
1936
- @final
1936
+ class OccurrenceQuerySet (models .QuerySet ):
1937
+ def with_detections_count (self ) -> models .QuerySet :
1938
+ return self .annotate (detections_count = models .Count ("detections" , distinct = True ))
1939
+
1940
+ def with_timestamps (self ) -> models .QuerySet :
1941
+ """
1942
+ These are timestamps used for filtering and ordering in the UI.
1943
+ """
1944
+ return self .annotate (
1945
+ first_appearance_timestamp = models .Min ("detections__timestamp" ),
1946
+ last_appearance_timestamp = models .Max ("detections__timestamp" ),
1947
+ first_appearance_time = models .Min ("detections__timestamp__time" ),
1948
+ duration = models .ExpressionWrapper (
1949
+ models .F ("last_appearance_timestamp" ) - models .F ("first_appearance_timestamp" ),
1950
+ output_field = models .DurationField (),
1951
+ ),
1952
+ )
1953
+
1954
+
1937
1955
class OccurrenceManager (models .Manager ):
1938
- def get_queryset (self ):
1939
- # prefetch determination, deployment, project
1940
- return super ().get_queryset ().select_related ("determination" , "deployment" , "project" )
1956
+ def get_queryset (self ) -> OccurrenceQuerySet :
1957
+ return OccurrenceQuerySet (self .model , using = self ._db ).select_related (
1958
+ "determination" ,
1959
+ "deployment" ,
1960
+ "project" ,
1961
+ )
1941
1962
1942
1963
1943
1964
@final
0 commit comments