Skip to content

Commit bcab10f

Browse files
committed
Update usergroups aggragated calc function.
1 parent e0a4558 commit bcab10f

File tree

8 files changed

+64
-42
lines changed

8 files changed

+64
-42
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@
109109
WITH used_tasks as (
110110
SELECT
111111
project_id, group_id, task_id
112-
From results_user_groups ug
113-
INNER JOIN results R USING (project_id, group_id, user_id)
112+
From mapping_sessions_user_groups MSUR
113+
INNER JOIN mapping_sessions MS USING (mapping_session_id)
114+
INNER JOIN mapping_sessions_results MSR USING (mapping_session_id)
114115
INNER JOIN tasks T USING (project_id, group_id, task_id)
115116
WHERE
116117
R.timestamp >= %(from_date)s and R.timestamp < %(until_date)s
@@ -129,17 +130,18 @@
129130
-- Aggregate data by group
130131
user_group_data as (
131132
SELECT
132-
ug.project_id,
133-
ug.group_id,
134-
ug.user_id,
135-
ug.user_group_id,
136-
MAX(R.timestamp::date) as timestamp_date,
137-
MIN(R.start_time) as start_time,
138-
MAX(R.end_time) as end_time,
139-
COUNT(DISTINCT R.task_id) as task_count,
133+
MS.project_id,
134+
MS.group_id,
135+
MS.user_id,
136+
MSUR.user_group_id,
137+
MAX(MS.start_time::date) as timestamp_date,
138+
MIN(MS.start_time) as start_time,
139+
MAX(MS.end_time) as end_time,
140+
COUNT(DISTINCT T.task_id) as task_count,
140141
SUM(T.area) as area_swiped
141-
From results_user_groups ug
142-
INNER JOIN results R USING (project_id, group_id, user_id)
142+
From mapping_sessions_user_groups MSUR
143+
INNER JOIN mapping_sessions MS USING (mapping_session_id)
144+
INNER JOIN mapping_sessions_results MSR USING (mapping_session_id)
143145
INNER JOIN task_data T USING (task_id)
144146
WHERE
145147
R.timestamp >= %(from_date)s and R.timestamp < %(until_date)s

mapswipe_workers/mapswipe_workers/firebase_to_postgres/transfer_results.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,12 @@ def save_user_group_results_to_postgres(
476476
p_con.query(filter_query, {"project_id": project_id})
477477

478478
query_insert_results = """
479-
INSERT INTO results_user_groups
480-
SELECT * FROM results_user_groups_temp
479+
INSERT INTO mapping_sessions_user_groups
480+
SELECT
481+
ms.mapping_session_id,
482+
rug_temp.user_group_id
483+
FROM results_user_groups_temp rug_temp
484+
JOIN mapping_sessions ms USING (project_id, group_id, user_id)
481485
ON CONFLICT (project_id, group_id, user_id, user_group_id)
482486
DO NOTHING;
483487
"""

mapswipe_workers/tests/integration/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _clear_all_data(cls):
5757
TRUNCATE TABLE users_temp;
5858
TRUNCATE TABLE user_groups_membership_logs_temp;
5959
-- normal tables
60-
TRUNCATE TABLE results_user_groups CASCADE;
60+
TRUNCATE TABLE mapping_sessions_user_groups CASCADE;
6161
TRUNCATE TABLE tasks CASCADE;
6262
TRUNCATE TABLE user_groups_user_memberships CASCADE;
6363
TRUNCATE TABLE user_groups CASCADE;

mapswipe_workers/tests/integration/set_up_db.sql

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,11 @@ CREATE TABLE IF NOT EXISTS user_groups_user_memberships_temp (
153153
);
154154

155155
-- Used to group results by user groups
156-
CREATE TABLE IF NOT EXISTS results_user_groups (
157-
-- result primary key (not using task_id as it is a flat field in results)
158-
project_id varchar,
159-
group_id varchar,
160-
user_id varchar,
161-
-- user group primary key
162-
user_group_id varchar,
163-
PRIMARY KEY (project_id, group_id, user_id, user_group_id),
164-
FOREIGN KEY (project_id) REFERENCES projects (project_id),
165-
FOREIGN KEY (user_id) REFERENCES users (user_id),
166-
FOREIGN KEY (user_group_id) REFERENCES user_groups (user_group_id),
167-
FOREIGN KEY (project_id, group_id) REFERENCES groups (project_id, group_id)
156+
CREATE TABLE IF NOT EXISTS mapping_sessions_user_groups (
157+
mapping_session_id int8,
158+
user_group_id varchar, -- user group primary key
159+
PRIMARY KEY (mapping_session_id, user_group_id),
160+
FOREIGN KEY (mapping_session_id) REFERENCES mapping_sessions (mapping_session_id),
168161
);
169162

170163
-- create table for user_group_results import through csv

mapswipe_workers/tests/integration/tear_down.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def delete_test_data(project_id: str) -> None:
5959
pg_db.query(sql_query, [project_id])
6060
sql_query = "DELETE FROM mapping_sessions WHERE project_id = %s"
6161
pg_db.query(sql_query, [project_id])
62-
sql_query = "DELETE FROM results_user_groups WHERE project_id = %s"
62+
sql_query = "DELETE FROM mapping_sessions_user_groups WHERE project_id = %s"
6363
pg_db.query(sql_query, [project_id])
6464
sql_query = "DELETE FROM results_temp WHERE project_id = %s"
6565
pg_db.query(sql_query, [project_id])

mapswipe_workers/tests/integration/test_transfer_results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_results_with_user_groups(self):
203203

204204
UG_QUERY = "SELECT user_group_id FROM user_groups ORDER BY user_group_id"
205205
RUG_QUERY = (
206-
"SELECT user_group_id FROM results_user_groups ORDER BY user_group_id"
206+
"SELECT user_group_id FROM mapping_sessions_user_groups ORDER BY user_group_id"
207207
)
208208
for query, expected_value in [
209209
(

postgres/initdb.sql

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,11 @@ CREATE TABLE IF NOT EXISTS user_groups_user_memberships_temp (
153153
);
154154

155155
-- Used to group results by user groups
156-
CREATE TABLE IF NOT EXISTS results_user_groups (
157-
-- result primary key (not using task_id as it is a flat field in results)
158-
project_id varchar,
159-
group_id varchar,
160-
user_id varchar,
161-
-- user group primary key
162-
user_group_id varchar,
163-
PRIMARY KEY (project_id, group_id, user_id, user_group_id),
164-
FOREIGN KEY (project_id) REFERENCES projects (project_id),
165-
FOREIGN KEY (user_id) REFERENCES users (user_id),
166-
FOREIGN KEY (user_group_id) REFERENCES user_groups (user_group_id),
167-
FOREIGN KEY (project_id, group_id) REFERENCES groups (project_id, group_id)
156+
CREATE TABLE IF NOT EXISTS mapping_sessions_user_groups (
157+
mapping_session_id int8,
158+
user_group_id varchar, -- user group primary key
159+
PRIMARY KEY (mapping_session_id, user_group_id),
160+
FOREIGN KEY (mapping_session_id) REFERENCES mapping_sessions (mapping_session_id),
168161
);
169162

170163
-- create table for user_group_results import through csv
@@ -223,7 +216,6 @@ BEGIN
223216
'Tried to insert invalid result: Project: % Group: % Task: % - User: %', v.project_id, v.group_id, NEW.task_id, v.user_id
224217
USING ERRCODE = '23503';
225218
END IF;
226-
227219
RETURN NEW;
228220
END;
229221
$$;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This script updates and tra the data from the 'results' table
3+
* and transfers them to the new tables
4+
* - 'mapping_sessions' and
5+
* - 'mapping_sessions_results'
6+
*/
7+
SET search_path = 'public';
8+
9+
-- Create new user_groups mapping sessions table
10+
CREATE TABLE IF NOT EXISTS mapping_sessions_user_groups (
11+
mapping_session_id int8,
12+
user_group_id varchar,
13+
PRIMARY KEY (mapping_session_id, user_group_id),
14+
FOREIGN KEY (mapping_session_id) REFERENCES mapping_sessions (mapping_session_id),
15+
FOREIGN KEY (user_group_id) REFERENCES user_groups (user_group_id)
16+
);
17+
18+
19+
-- Copy data to new table
20+
INSERT INTO mapping_sessions_user_groups
21+
(
22+
SELECT
23+
MS.mapping_session_id,
24+
rug.user_group_id
25+
FROM results_user_groups rug
26+
INNER JOIN mapping_sessions MS USING (project_id, group_id, user_id)
27+
)
28+
ON CONFLICT do nothing;
29+
30+
-- NOTE: Drop old table (Do this manually after checking if all data is transferred)
31+
-- DROP TABLE IF EXISTS results_user_groups;

0 commit comments

Comments
 (0)