-
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.
Merge pull request #330 from RolnickLab/feat/precalculate-values
Cache and filter counts & scores. Improve determination calculation.
- Loading branch information
Showing
24 changed files
with
377 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.db.models import F, OrderBy | ||
from rest_framework.filters import OrderingFilter | ||
|
||
|
||
class NullsLastOrderingFilter(OrderingFilter): | ||
def get_ordering(self, request, queryset, view): | ||
values = super().get_ordering(request, queryset, view) | ||
if not values: | ||
return values | ||
return [OrderBy(F(value.lstrip("-")), descending=value.startswith("-"), nulls_last=True) for value in values] |
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 |
---|---|---|
|
@@ -84,6 +84,8 @@ class Meta: | |
"details", | ||
"name", | ||
"delay", | ||
"limit", | ||
"shuffle", | ||
"project", | ||
"project_id", | ||
"deployment", | ||
|
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
47 changes: 47 additions & 0 deletions
47
ami/main/migrations/0024_deployment_captures_count_and_more.py
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,47 @@ | ||
# Generated by Django 4.2.2 on 2023-12-01 21:42 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("main", "0023_taxon_main_taxon_orderin_4ffb7b_idx"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="captures_count", | ||
field=models.IntegerField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="detections_count", | ||
field=models.IntegerField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="events_count", | ||
field=models.IntegerField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="first_capture_timestamp", | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="last_capture_timestamp", | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="occurrences_count", | ||
field=models.IntegerField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="deployment", | ||
name="taxa_count", | ||
field=models.IntegerField(blank=True, null=True), | ||
), | ||
] |
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,27 @@ | ||
# Generated by Django 4.2.2 on 2023-12-01 21:43 | ||
|
||
from django.db import migrations | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# Save all Deployment objects to update their calculated fields. | ||
def update_deployment_aggregates(apps, schema_editor): | ||
# Deployment = apps.get_model("main", "Deployment") | ||
from ami.main.models import Deployment | ||
|
||
for deployment in Deployment.objects.all(): | ||
logger.info(f"Updating deployment {deployment}") | ||
deployment.save(update_calculated_fields=True) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("main", "0024_deployment_captures_count_and_more"), | ||
] | ||
|
||
# operations = [] | ||
operations = [ | ||
migrations.RunPython(update_deployment_aggregates, migrations.RunPython.noop), | ||
] |
17 changes: 17 additions & 0 deletions
17
ami/main/migrations/0026_occurrence_determination_score.py
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,17 @@ | ||
# Generated by Django 4.2.2 on 2023-12-02 01:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("main", "0025_update_deployment_aggregates"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="occurrence", | ||
name="determination_score", | ||
field=models.FloatField(blank=True, null=True), | ||
), | ||
] |
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,22 @@ | ||
# Generated by Django 4.2.2 on 2023-12-02 01:08 | ||
|
||
from django.db import migrations | ||
|
||
|
||
# Call save on all occurrences to update their scores | ||
def update_occurrence_scores(apps, schema_editor): | ||
# Occurrence = apps.get_model("main", "Occurrence") | ||
from ami.main.models import Occurrence | ||
|
||
for occurrence in Occurrence.objects.all(): | ||
occurrence.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("main", "0026_occurrence_determination_score"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_occurrence_scores, migrations.RunPython.noop), | ||
] |
Oops, something went wrong.