From a6e43b044b43920f1a7c0bdb6fcc263c5e5e012f Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 25 Oct 2024 19:19:08 +0200 Subject: [PATCH] # Wartungskommando Optimization * extends filtering for recalculatable geometries to records without started calculation at any point (parcel_update_start is null) * catches exceptions on geometries which could not be recalculated properly, adds them to output for further analysis * simplifies complexity factor calculation --- .../commands/recalculate_parcels.py | 20 +++++++++++++++---- konova/models/geometry.py | 5 ++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/konova/management/commands/recalculate_parcels.py b/konova/management/commands/recalculate_parcels.py index e3768f77..f42fd228 100644 --- a/konova/management/commands/recalculate_parcels.py +++ b/konova/management/commands/recalculate_parcels.py @@ -8,10 +8,11 @@ Created on: 04.01.22 import datetime from django.contrib.gis.db.models.functions import Area +from django.db.models import Q from django.utils.timezone import now from konova.management.commands.setup import BaseKonovaCommand -from konova.models import Geometry, Parcel, District +from konova.models import Geometry class Command(BaseKonovaCommand): @@ -41,7 +42,10 @@ class Command(BaseKonovaCommand): _today = now().date() _date_threshold = _today - datetime.timedelta(days=1) geometry_objects = Geometry.objects.filter( - parcel_update_start__date__lte=_date_threshold, + Q( + Q(parcel_update_start__date__lte=_date_threshold) | + Q(parcel_update_start__isnull=True) + ), parcel_update_end__isnull=True ) @@ -55,12 +59,20 @@ class Command(BaseKonovaCommand): self._write_warning(f"Process parcels for {geometries.count()} geometry entries now ...") i = 0 num_geoms = geometries.count() + geoms_with_errors = {} for geometry in geometries: self._write_warning(f"--- {datetime.datetime.now()} Process {geometry.id} now ...") - geometry.update_parcels() - self._write_warning(f"--- Processed {geometry.get_underlying_parcels().count()} underlying parcels") + try: + geometry.update_parcels() + self._write_warning(f"--- Processed {geometry.get_underlying_parcels().count()} underlying parcels") + except Exception as e: + geoms_with_errors[geometry.id] = str(e) i += 1 self._write_warning(f"--- {i}/{num_geoms} processed") self._write_success("Updating parcels done!") + + for key, val in geoms_with_errors.items(): + self._write_error(f" Error on {key}: {val}") + self._write_success(f"{num_geoms - len(geoms_with_errors)} geometries successfuly recalculated!") self._break_line() diff --git a/konova/models/geometry.py b/konova/models/geometry.py index 09d81af4..c0d55b9e 100644 --- a/konova/models/geometry.py +++ b/konova/models/geometry.py @@ -366,11 +366,10 @@ class Geometry(BaseResource): diff = geom_envelope - self.geom if diff.area == 0: - ratio = 1 + complexity_factor = 1 else: - ratio = self.geom.area / diff.area + complexity_factor = self.geom.area / diff.area - complexity_factor = 1 - ratio return complexity_factor