Skip to content

Commit

Permalink
Add command to update project groups agg data
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Jun 22, 2023
1 parent f0fb426 commit 1b4c03a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,62 @@
# |1|00:00:00.208768|00:00:01.398161|00:00:28.951521|
# |2|00:00:01.330297|00:00:06.076814|00:00:03.481192|
# |3|00:00:02.092967|00:00:11.271081|00:00:06.045881|
UPDATE_PROJECT_GROUP_DATA = f"""

UPDATE_PROJECT_GROUP_DATA_USING_PROJECT_ID = f"""
WITH to_calculate_groups AS (
SELECT
project_id,
group_id
FROM groups
WHERE
(project_id, group_id) in (
SELECT
MS.project_id,
MS.group_id
FROM mapping_sessions MS
WHERE
project_id = %(project_id)s
GROUP BY MS.project_id, MS.group_id
) AND
(
total_area is NULL OR time_spent_max_allowed is NULL
)
),
groups_data AS (
SELECT
T.project_id,
T.group_id,
SUM( -- sqkm
ST_Area(T.geom::geography(GEOMETRY,4326)) / 1000000
) as total_task_group_area,
(
CASE
-- Using 95_percent value of existing data for each project_type
WHEN P.project_type = {Project.Type.BUILD_AREA.value} THEN 1.4
WHEN P.project_type = {Project.Type.COMPLETENESS.value} THEN 1.4
WHEN P.project_type = {Project.Type.CHANGE_DETECTION.value} THEN 11.2
-- FOOTPRINT: Not calculated right now
WHEN P.project_type = {Project.Type.FOOTPRINT.value} THEN 6.1
ELSE 1
END
) * COUNT(*) as time_spent_max_allowed
FROM tasks T
INNER JOIN to_calculate_groups G USING (project_id, group_id)
INNER JOIN projects P USING (project_id)
GROUP BY project_id, P.project_type, group_id
)
UPDATE groups G
SET
total_area = GD.total_task_group_area,
time_spent_max_allowed = GD.time_spent_max_allowed
FROM groups_data GD
WHERE
G.project_id = GD.project_id AND
G.group_id = GD.group_id;
"""


UPDATE_PROJECT_GROUP_DATA_USING_TIME_RANGE = f"""
WITH to_calculate_groups AS (
SELECT
project_id,
Expand Down Expand Up @@ -290,7 +345,7 @@ def _track(self, tracker_type, label, sql):
)
with transaction.atomic():
with connection.cursor() as cursor:
cursor.execute(UPDATE_PROJECT_GROUP_DATA, params)
cursor.execute(UPDATE_PROJECT_GROUP_DATA_USING_TIME_RANGE, params)
self.stdout.write(
self.style.SUCCESS(
f"Successfull. Runtime: {time.time() - start_time} seconds"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import time

from apps.existing_database.models import Project
from django.core.management.base import BaseCommand
from django.db import connection, transaction

from .update_aggregated_data import UPDATE_PROJECT_GROUP_DATA_USING_PROJECT_ID


class Command(BaseCommand):
def handle(self, **_):
project_qs = Project.objects.all()
total_projects = project_qs.count()
self.stdout.write(f"Total projects: {total_projects}")
for index, project_id in enumerate(
project_qs.values_list("project_id", flat=True),
start=1,
):
self.stdout.write(
"Running calculation for project ID "
f"({index}/{total_projects}): {project_id}"
)
with transaction.atomic():
start_time = time.time()
with connection.cursor() as cursor:
cursor.execute(
UPDATE_PROJECT_GROUP_DATA_USING_PROJECT_ID,
dict(project_id=project_id),
)
self.stdout.write(
self.style.SUCCESS(
f"- Successfull. Runtime: {time.time() - start_time} seconds"
)
)

0 comments on commit 1b4c03a

Please sign in to comment.