-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect dates of occurrences & detections (#360)
* Fix Detection timestamps that do not use the date they were "captured" * Allow sorting by species / determination name * It appears we don't need first & last detection objects, speed up query * Use timestamps from query annotation instead of join, sort by timestamps * Add some methods for troubleshooting bad event/session groupings
- Loading branch information
Showing
10 changed files
with
192 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,39 @@ | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand, CommandError # noqa | ||
from django.db.models import OuterRef, Subquery | ||
|
||
from ...models import Detection, SourceImage | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def fix_detection_timestamps(dry_run=True) -> str: | ||
# Subquery to get the timestamp from the related SourceImage | ||
source_image_timestamp_subquery = SourceImage.objects.filter(id=OuterRef("source_image_id")).values("timestamp")[ | ||
:1 | ||
] | ||
|
||
if dry_run: | ||
# Count all Detection objects where timestamp does not match their SourceImage.timestamp | ||
count = Detection.objects.exclude(timestamp=Subquery(source_image_timestamp_subquery)).count() | ||
return f"Would update {count} Detection objects where timestamp does not match their SourceImage.timestamp" | ||
|
||
# Update all Detection objects where timestamp does not match their SourceImage.timestamp | ||
updated = Detection.objects.exclude(timestamp=Subquery(source_image_timestamp_subquery)).update( | ||
timestamp=Subquery(source_image_timestamp_subquery) | ||
) | ||
return f"Updated {updated} Detection objects where timestamp does not match their SourceImage.timestamp" | ||
|
||
|
||
class Command(BaseCommand): | ||
r"""Audit and fix timestamps on Detection objects.""" | ||
|
||
help = "Audit and fix timestamps on Detection objects" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--dry-run", action="store_true", help="Do not make any changes") | ||
|
||
def handle(self, *args, **options): | ||
msg = fix_detection_timestamps(dry_run=options["dry_run"]) | ||
self.stdout.write(msg) |
This file contains 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 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 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.