Skip to content

Commit 245a613

Browse files
committed
Pre-calculate total geo area and time max limit for project groups
1 parent 1959557 commit 245a613

File tree

6 files changed

+94
-23
lines changed

6 files changed

+94
-23
lines changed

django/apps/aggregated/management/commands/update_aggregated_data.py

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,70 @@
1919
# |1|00:00:00.208768|00:00:01.398161|00:00:28.951521|
2020
# |2|00:00:01.330297|00:00:06.076814|00:00:03.481192|
2121
# |3|00:00:02.092967|00:00:11.271081|00:00:06.045881|
22-
TASK_GROUP_METADATA_QUERY = f"""
23-
SELECT
24-
project_id,
25-
group_id,
26-
SUM(
27-
ST_Area(geom::geography(GEOMETRY,4326)) / 1000000
28-
) as total_task_group_area, -- sqkm
29-
(
30-
CASE
31-
-- Using 95_percent value of existing data for each project_type
32-
WHEN UG.project_type = {Project.Type.BUILD_AREA.value} THEN 1.4
33-
WHEN UG.project_type = {Project.Type.COMPLETENESS.value} THEN 1.4
34-
WHEN UG.project_type = {Project.Type.CHANGE_DETECTION.value} THEN 11.2
35-
-- FOOTPRINT: Not calculated right now
36-
WHEN UG.project_type = {Project.Type.FOOTPRINT.value} THEN 6.1
37-
ELSE 1
38-
END
39-
) * COUNT(*) as time_spent_max_allowed
40-
FROM tasks T
41-
INNER JOIN used_task_groups UG USING (project_id, group_id)
42-
GROUP BY project_id, project_type, group_id
22+
UPDATE_PROJECT_GROUP_DATA = f"""
23+
WITH to_calculate_groups AS (
24+
SELECT
25+
project_id,
26+
group_id
27+
FROM groups
28+
WHERE
29+
(project_id, group_id) in (
30+
SELECT
31+
MS.project_id,
32+
MS.group_id
33+
FROM mapping_sessions MS
34+
WHERE
35+
MS.start_time >= %(from_date)s
36+
AND MS.start_time < %(until_date)s
37+
GROUP BY MS.project_id, MS.group_id
38+
) AND
39+
(
40+
total_area is NULL OR time_spent_max_allowed is NULL
41+
)
42+
),
43+
groups_data AS (
44+
SELECT
45+
T.project_id,
46+
T.group_id,
47+
SUM( -- sqkm
48+
ST_Area(T.geom::geography(GEOMETRY,4326)) / 1000000
49+
) as total_task_group_area,
50+
(
51+
CASE
52+
-- Using 95_percent value of existing data for each project_type
53+
WHEN P.project_type = {Project.Type.BUILD_AREA.value} THEN 1.4
54+
WHEN P.project_type = {Project.Type.COMPLETENESS.value} THEN 1.4
55+
WHEN P.project_type = {Project.Type.CHANGE_DETECTION.value} THEN 11.2
56+
-- FOOTPRINT: Not calculated right now
57+
WHEN P.project_type = {Project.Type.FOOTPRINT.value} THEN 6.1
58+
ELSE 1
59+
END
60+
) * COUNT(*) as time_spent_max_allowed
61+
FROM tasks T
62+
INNER JOIN to_calculate_groups G USING (project_id, group_id)
63+
INNER JOIN projects P USING (project_id)
64+
GROUP BY project_id, P.project_type, group_id
65+
)
66+
UPDATE groups G
67+
SET
68+
total_area = GD.total_task_group_area,
69+
time_spent_max_allowed = GD.time_spent_max_allowed
70+
FROM groups_data GD
71+
WHERE
72+
G.project_id = GD.project_id AND
73+
G.group_id = GD.group_id;
74+
"""
75+
76+
TASK_GROUP_METADATA_QUERY = """
77+
SELECT
78+
G.project_id,
79+
G.group_id,
80+
G.total_area as total_task_group_area,
81+
G.time_spent_max_allowed
82+
FROM groups G
83+
INNER JOIN used_task_groups UG USING (project_id, group_id)
84+
INNER JOIN projects P USING (project_id)
85+
GROUP BY G.project_id, P.project_type, G.group_id
4386
"""
4487

4588

@@ -239,6 +282,20 @@ def _track(self, tracker_type, label, sql):
239282
until_date=until_date.strftime("%Y-%m-%d"),
240283
)
241284
start_time = time.time()
285+
286+
self.stdout.write(
287+
f"Updating Project Group Data for {label.title()} for date: {params}"
288+
)
289+
with transaction.atomic():
290+
with connection.cursor() as cursor:
291+
cursor.execute(UPDATE_PROJECT_GROUP_DATA, params)
292+
self.stdout.write(
293+
self.style.SUCCESS(
294+
f"Successfull. Runtime: {time.time() - start_time} seconds"
295+
)
296+
)
297+
298+
start_time = time.time()
242299
self.stdout.write(f"Updating {label.title()} Data for date: {params}")
243300
with transaction.atomic():
244301
with connection.cursor() as cursor:

django/apps/existing_database/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class Group(Model):
106106
progress = models.IntegerField(blank=True, null=True)
107107
# Database uses JSON instead of JSONB (not supported by django)
108108
project_type_specifics = models.TextField(blank=True, null=True)
109+
total_area = models.FloatField(blank=True, null=True)
110+
time_spent_max_allowed = models.FloatField(blank=True, null=True)
109111

110112
# Django derived fields from ForeignKey
111113
project_id: str

mapswipe_workers/tests/integration/set_up_db.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ CREATE TABLE IF NOT EXISTS groups (
3030
required_count int,
3131
progress int,
3232
project_type_specifics json,
33+
-- total_area & time_spent_max_allowed are maintaned and used by aggregated module
34+
total_area float DEFAULT NULL,
35+
time_spent_max_allowed float DEFAULT NULL,
3336
PRIMARY KEY (project_id, group_id),
3437
FOREIGN KEY (project_id) REFERENCES projects (project_id)
3538
);

postgres/initdb.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ CREATE TABLE IF NOT EXISTS groups (
3030
required_count int,
3131
progress int,
3232
project_type_specifics json,
33+
-- total_area & time_spent_max_allowed are maintaned and used by aggregated module
34+
total_area float DEFAULT NULL,
35+
time_spent_max_allowed float DEFAULT NULL,
3336
PRIMARY KEY (project_id, group_id),
3437
FOREIGN KEY (project_id) REFERENCES projects (project_id)
3538
);

postgres/scripts/backtrack_user_group_mapping_sessions.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ INSERT INTO mapping_sessions_user_groups(
5555
WHERE MS.end_time >= '2022-01-01'
5656
) ON CONFLICT (mapping_session_id, user_group_id) DO NOTHING;
5757

58+
-- To update aggregated data: docker-compose exec django ./manage.py update_aggregated_data
5859
-- This table is maintaned using django
5960
-- type = 1 = USER_GROUP
61+
-- type = 0 = USER
6062
-- value = date from when the data is calculated >= 2022-01-01
61-
-- To update aggregated data: docker-compose exec django ./manage.py update_aggregated_data.py
6263
UPDATE aggregated_aggregatedtracking
6364
SET "value" = '2022-01-01'
64-
WHERE "type" = 1;
65+
WHERE "type" in (0, 1);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
-- total_area & time_spent_max_allowed are maintaned and used by aggregated module
3+
ALTER TABLE groups
4+
ADD COLUMN total_area float DEFAULT NULL,
5+
ADD COLUMN time_spent_max_allowed float DEFAULT NULL;

0 commit comments

Comments
 (0)