Skip to content

Commit 61e800c

Browse files
authored
Merge pull request #596 from mapswipe/dev
Dev - add generate stats
2 parents 759c172 + 835f113 commit 61e800c

File tree

18 files changed

+567
-164
lines changed

18 files changed

+567
-164
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/delete_project.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def delete_project(project_ids: list) -> bool:
2222
Deletes project, groups, tasks and results from Firebase and Postgres.
2323
"""
2424
for project_id in project_ids:
25+
if project_id is None:
26+
logger.info("Will not delete Null project_id.")
27+
continue
28+
2529
logger.info(
2630
f"Delete project, groups, tasks and results of project: {project_id}"
2731
)
@@ -90,7 +94,18 @@ def delete_project(project_ids: list) -> bool:
9094
ref.delete()
9195

9296
pg_db = auth.postgresDB()
93-
sql_query = "DELETE FROM results WHERE project_id = %(project_id)s;"
97+
sql_query = """
98+
DELETE FROM mapping_sessions_results msr
99+
USING mapping_sessions ms
100+
WHERE ms.mapping_session_id = msr.mapping_session_id
101+
AND ms.project_id = %(project_id)s;
102+
"""
103+
pg_db.query(sql_query, {"project_id": project_id})
104+
sql_query = """
105+
DELETE
106+
FROM mapping_sessions
107+
WHERE project_id = %(project_id)s ;
108+
"""
94109
pg_db.query(sql_query, {"project_id": project_id})
95110
sql_query = "DELETE FROM tasks WHERE project_id = %(project_id)s;"
96111
pg_db.query(sql_query, {"project_id": project_id})

mapswipe_workers/mapswipe_workers/firebase_to_postgres/update_data.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,22 @@ def update_project_data(project_ids: list = []):
567567
logger.info("Finished status update projects.")
568568

569569

570-
def set_progress_in_firebase(project_id: str):
571-
"""Update the project progress value in Firebase."""
572-
570+
def get_project_progress(project_id: str) -> int:
571+
"""
572+
Calculate overall project progress as the average progress for all groups.
573+
This is not hundred percent exact, since groups can have a different number of tasks
574+
but it is still "good enough" and gives almost correct progress.
575+
But it is easier to compute than considering the actual number of tasks per group.
576+
577+
NOTE: the cast to integer in postgres rounds decimals. This means that for 99.5%
578+
progress, we return 100% here. We should evaluate if this is what we want if/when
579+
we introduce automated project rotation upon completion (as the reported completion
580+
would happen 0.5% before actual completion).
581+
"""
573582
pg_db = auth.postgresDB()
574583
query = """
575-
-- Calculate overall project progress as
576-
-- the average progress for all groups.
577-
-- This is not hundred percent exact,
578-
-- since groups can have a different number of tasks
579-
-- but it is still "good enough" and gives almost correct progress.
580-
-- But it is easier to compute
581-
-- than considering the actual number of tasks per group.
582584
select
583-
project_id
584-
,avg(group_progress)::integer as progress
585+
avg(group_progress)::integer as progress
585586
from
586587
(
587588
-- Get all groups for this project and
@@ -605,46 +606,52 @@ def set_progress_in_firebase(project_id: str):
605606
-- even if more users than required submitted results.
606607
-- The verification number of a project is used here.
607608
select
608-
r.group_id
609-
,r.project_id
609+
ms.group_id
610+
,ms.project_id
610611
,case
611612
when count(distinct user_id) >= p.verification_number then 100
612613
else 100 * count(distinct user_id) / p.verification_number
613614
end as group_progress
614-
from results r, projects p
615-
where r.project_id = p.project_id
616-
group by group_id, r.project_id, p.verification_number
615+
from mapping_sessions ms, projects p
616+
where ms.project_id = p.project_id
617+
group by group_id, ms.project_id, p.verification_number
617618
) bar
618619
on bar.group_id = g.group_id and bar.project_id = g.project_id
619620
where g.project_id = %s
620621
) foo
621622
group by project_id
622623
"""
623624
data = [project_id]
624-
progress = pg_db.retr_query(query, data)[0][1]
625+
return pg_db.retr_query(query, data)[0][0]
626+
627+
628+
def set_progress_in_firebase(project_id: str):
629+
"""Update the project progress value in Firebase."""
630+
progress = get_project_progress(project_id)
625631

626632
fb_db = auth.firebaseDB()
627633
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/progress")
628634
project_progress_ref.set(progress)
629635
logger.info(f"set progress for project {project_id}: {progress}")
630636

631637

632-
def set_contributor_count_in_firebase(project_id: str):
633-
"""Update the project contributors value in Firebase."""
634-
638+
def get_contributor_count_from_postgres(project_id: str) -> int:
635639
pg_db = auth.postgresDB()
636640
query = """
637641
select
638-
project_id
639-
,count(distinct user_id) contributor_count
640-
from results r
642+
count(distinct user_id)
643+
from mapping_sessions ms
641644
where
642645
project_id = %s
643-
group by project_id
644646
"""
645647
data = [project_id]
646-
contributor_count = pg_db.retr_query(query, data)[0][1]
648+
return pg_db.retr_query(query, data)[0][0]
649+
650+
651+
def set_contributor_count_in_firebase(project_id: str):
652+
"""Update the project contributors value in Firebase."""
647653

654+
contributor_count = get_contributor_count_from_postgres(project_id)
648655
fb_db = auth.firebaseDB()
649656
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/contributorCount")
650657
project_progress_ref.set(contributor_count)

mapswipe_workers/mapswipe_workers/generate_stats/project_stats.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,19 @@ def get_results(filename: str, project_id: str) -> pd.DataFrame:
104104
sql_query = sql.SQL(
105105
"""
106106
COPY (
107-
SELECT results.*, U.username
108-
FROM results
107+
SELECT
108+
ms.project_id,
109+
ms.group_id,
110+
ms.user_id,
111+
msr.task_id,
112+
ms.start_time as timestamp,
113+
ms.start_time,
114+
ms.end_time,
115+
msr.result,
116+
U.username
117+
FROM mapping_sessions_results msr
118+
LEFT JOIN mapping_sessions ms ON
119+
ms.mapping_session_id = msr.mapping_session_id
109120
LEFT JOIN users U USING (user_id)
110121
WHERE project_id = {}
111122
) TO STDOUT WITH CSV HEADER

mapswipe_workers/tests/integration/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ 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 CASCADE;
6160
TRUNCATE TABLE results_user_groups CASCADE;
6261
TRUNCATE TABLE tasks CASCADE;
6362
TRUNCATE TABLE user_groups_user_memberships CASCADE;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_build_area g115 test_build_area 123 2020-02-03 15:39:39.332 2020-02-03 15:39:42.332 252

0 commit comments

Comments
 (0)