Skip to content

Commit 990f1d2

Browse files
committed
set progress and contributor count during transfer results process and not in generate stats
1 parent 4c123cb commit 990f1d2

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/update_data.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,72 @@ def update_project_data(project_ids: list = []):
122122
# if project.status is not existent
123123
data_update_project = [project.get("status", ""), project_id]
124124
pg_db.query(query_update_project, data_update_project)
125-
logger.info(f"Updated status for project {project_id} in postgres")
126125

127126
logger.info("Updated project data in Postgres")
127+
128+
129+
def set_progress_in_firebase(project_id: str):
130+
"""Update the project progress value in Firebase."""
131+
132+
pg_db = auth.postgresDB()
133+
query = """
134+
select
135+
project_id
136+
,avg(group_progress)::integer as progress
137+
from
138+
(
139+
select
140+
g.group_id
141+
,g.project_id
142+
,case
143+
when group_progress is null then 0
144+
else group_progress
145+
end as group_progress
146+
from groups g
147+
left join
148+
(
149+
select
150+
r.group_id
151+
,r.project_id
152+
,case
153+
when count(distinct user_id) >= p.verification_number then 100
154+
else 100* count(distinct user_id) / p.verification_number
155+
end as group_progress
156+
from results r, projects p
157+
where r.project_id = p.project_id
158+
group by group_id, r.project_id, p.verification_number
159+
) bar
160+
on bar.group_id = g.group_id and bar.project_id = g.project_id
161+
where g.project_id = %s
162+
) foo
163+
group by project_id
164+
"""
165+
data = [project_id]
166+
progress = pg_db.retr_query(query, data)[0][1]
167+
168+
fb_db = auth.firebaseDB()
169+
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/progress")
170+
project_progress_ref.set(progress)
171+
logger.info(f"set progress for project {project_id}: {progress}")
172+
173+
174+
def set_contributor_count_in_firebase(project_id: str):
175+
"""Update the project contributors value in Firebase."""
176+
177+
pg_db = auth.postgresDB()
178+
query = """
179+
select
180+
project_id
181+
,count(distinct user_id) contributor_count
182+
from results r
183+
where
184+
project_id = %s
185+
group by project_id
186+
"""
187+
data = [project_id]
188+
contributor_count = pg_db.retr_query(query, data)[0][1]
189+
190+
fb_db = auth.firebaseDB()
191+
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/contributorCount")
192+
project_progress_ref.set(contributor_count)
193+
logger.info(f"set contributorCount attribute for project {project_id}: {contributor_count}")

mapswipe_workers/mapswipe_workers/generate_stats/project_stats.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -320,24 +320,6 @@ def get_agg_results_by_task_id(
320320
return agg_results_df
321321

322322

323-
def set_progress_in_firebase(project_id: str, progress: int):
324-
"""Update the project progress value in Firebase."""
325-
326-
fb_db = auth.firebaseDB()
327-
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/progress")
328-
project_progress_ref.set(progress)
329-
logger.info(f"set progress for project {project_id}: {progress}")
330-
331-
332-
def set_contributor_count_in_firebase(project_id: str, contributor_count: int):
333-
"""Update the project contributors value in Firebase."""
334-
335-
fb_db = auth.firebaseDB()
336-
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/contributorCount")
337-
project_progress_ref.set(contributor_count)
338-
logger.info(f"set contributorCount attribute for project {project_id}: {contributor_count}")
339-
340-
341323
def get_per_project_statistics(project_id: str, project_info: pd.Series) -> dict:
342324
"""
343325
The function calculates all project related statistics.
@@ -416,21 +398,6 @@ def get_per_project_statistics(project_id: str, project_info: pd.Series) -> dict
416398
f"{project_stats_by_date_filename}"
417399
)
418400

419-
# update progress and contributors in firebase
420-
# cum_progress[-1] refers to the latest value available for project progress
421-
# cum_progess contains overall project progress reached at each day
422-
set_progress_in_firebase(
423-
project_id=project_id,
424-
progress=int(100*project_stats_by_date_df["cum_progress"][-1])
425-
)
426-
427-
# cum_number_of_users[-1] refers to the latest value available for contributorCount
428-
# cum_number_of_users contains overall contributor count reached at each day
429-
set_contributor_count_in_firebase(
430-
project_id=project_id,
431-
contributor_count=int(project_stats_by_date_df["cum_number_of_users"][-1])
432-
)
433-
434401
# generate geometries for HOT Tasking Manager
435402
if project_info.iloc[0]["project_type"] in [ProjectType.FOOTPRINT.value]:
436403
# do not do this for ArbitraryGeometry / BuildingFootprint projects

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def run_firebase_to_postgres() -> list:
115115
update_data.update_project_data()
116116
project_ids = transfer_results.transfer_results()
117117
for project_id in project_ids:
118+
update_data.set_progress_in_firebase(project_id)
119+
update_data.set_contributor_count_in_firebase(project_id)
118120
send_progress_notification(project_id)
119121
return project_ids
120122

0 commit comments

Comments
 (0)