From 3b7bce8be90533a7b0ab9fedde1ad1ecb14cd884 Mon Sep 17 00:00:00 2001 From: Arne de Laat Date: Thu, 4 Apr 2024 14:02:26 +0200 Subject: [PATCH 1/2] Improve performance for retrieving the latest location for a station --- publicdb/inforecords/models.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/publicdb/inforecords/models.py b/publicdb/inforecords/models.py index bb00f615..b15845ba 100644 --- a/publicdb/inforecords/models.py +++ b/publicdb/inforecords/models.py @@ -276,22 +276,17 @@ def latest_location(self, date=None): if date is None: date = datetime.date.today() - # Initialize new config with all None values. - config = Configuration() - try: - summaries = Summary.objects.with_config().filter(station=self, date__lte=date).reverse() - for summary in summaries: - try: - config = ( - Configuration.objects.filter(summary=summary).exclude(gps_latitude=0, gps_longitude=0).latest() - ) - except Configuration.DoesNotExist: - pass - else: - break - except Summary.DoesNotExist: - pass + config = ( + Configuration.objects.exclude( + gps_latitude=0, + gps_longitude=0, + timestamp__gt=date, + ).filter(summary__station=self).latest() + ) + except Configuration.DoesNotExist: + # Initialize new config with all None values. + config = Configuration() return { 'latitude': (round(config.gps_latitude, 7) if config.gps_latitude is not None else None), From 34fb49677f9946a9b501fbc4ab93d37b61fc4a82 Mon Sep 17 00:00:00 2001 From: Arne de Laat Date: Fri, 12 Apr 2024 07:43:03 +0200 Subject: [PATCH 2/2] Remove unused import --- publicdb/inforecords/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/publicdb/inforecords/models.py b/publicdb/inforecords/models.py index b15845ba..d4676be3 100644 --- a/publicdb/inforecords/models.py +++ b/publicdb/inforecords/models.py @@ -8,7 +8,7 @@ from django.db.models import Max from django.utils.text import slugify -from ..histograms.models import Configuration, Summary +from ..histograms.models import Configuration FIRSTDATE = datetime.date(2004, 1, 1)