Skip to content

Commit bc5ff23

Browse files
committed
add more comments in firebaes functions and use contributor_count in python function
1 parent 10ef7f0 commit bc5ff23

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

firebase/functions/index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const admin = require('firebase-admin')
66
admin.initializeApp()
77

88

9-
// Calculate number of users who finished a group and
10-
// Gets triggered when new results of a group are written to the database.
9+
/*
10+
Log the userIds of all users who finished a group to /v2/userGroups/{projectId}/{groupId}/.
11+
Gets triggered when new results of a group are written to the database.
12+
This is the basis to calculate number of users who finished a group (requiredCount and finishedCount),
13+
which will be handled in the groupFinishedCountUpdater function.
14+
*/
1115
exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{groupId}/{userId}/').onCreate((snapshot, context) => {
1216
const promises = [] // List of promises to return
1317
const result = snapshot.val()
@@ -67,16 +71,26 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
6771
}
6872
}
6973
})
70-
promises.push(updateValues.userContribution)
71-
promises.push(updateValues.groupUsers)
72-
promises.push(updateValues.totalTaskContributionCount)
73-
promises.push(updateValues.taskContributionCount)
7474

75+
// Check if updateValues is null (happens when user submitted this group twice)
76+
// and return null in this case.
77+
if (updateValues === null) {
78+
return null
79+
} else {
80+
promises.push(updateValues.userContribution)
81+
promises.push(updateValues.groupUsers)
82+
promises.push(updateValues.totalTaskContributionCount)
83+
promises.push(updateValues.taskContributionCount)
84+
}
7585
})
7686

7787

78-
// set group finishedCount and group requiredCount.
79-
// Gets triggered when new userId key is written to groupsUsers.
88+
/*
89+
Set group finishedCount and group requiredCount.
90+
Gets triggered when new userId key is written to v2/groupsUsers/{projectId}/{groupId}.
91+
FinishedCount and requiredCount of a group are calculated based on the number of userIds
92+
that are present in v2/groupsUsers/{projectId}/{groupId}.
93+
*/
8094
exports.groupFinishedCountUpdater = functions.database.ref('/v2/groupsUsers/{projectId}/{groupId}/').onWrite((snapshot, context) => {
8195
const promises_new = []
8296

mapswipe_workers/mapswipe_workers/generate_stats/project_stats.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ def set_progress_in_firebase(project_id: str, progress: int):
329329
logger.info(f"set progress for project {project_id}: {progress}")
330330

331331

332-
def set_contributors_in_firebase(project_id: str, contributors: int):
332+
def set_contributor_count_in_firebase(project_id: str, contributor_count: int):
333333
"""Update the project contributors value in Firebase."""
334334

335335
fb_db = auth.firebaseDB()
336336
project_progress_ref = fb_db.reference(f"v2/projects/{project_id}/contributorCount")
337-
project_progress_ref.set(contributors)
338-
logger.info(f"set contributors for project {project_id}: {contributors}")
337+
project_progress_ref.set(contributor_count)
338+
logger.info(f"set contributorCount attribute for project {project_id}: {contributor_count}")
339339

340340

341341
def get_per_project_statistics(project_id: str, project_info: pd.Series) -> dict:
@@ -417,14 +417,18 @@ def get_per_project_statistics(project_id: str, project_info: pd.Series) -> dict
417417
)
418418

419419
# 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
420422
set_progress_in_firebase(
421423
project_id=project_id,
422424
progress=int(100*project_stats_by_date_df["cum_progress"][-1])
423425
)
424426

425-
set_contributors_in_firebase(
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(
426430
project_id=project_id,
427-
contributors=int(project_stats_by_date_df["cum_number_of_users"][-1])
431+
contributor_count=int(project_stats_by_date_df["cum_number_of_users"][-1])
428432
)
429433

430434
# generate geometries for HOT Tasking Manager

mapswipe_workers/mapswipe_workers/generate_stats/project_stats_by_date.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,22 @@ def get_progress_by_date(
5454
) -> pd.DataFrame:
5555
"""
5656
for each project we retrospectively generate the following attributes for a given
57-
date utilizing the results:
58-
number_of_results, cum_number_of_results, progress, cum_progress
57+
date utilizing the results.
58+
59+
number_of_results:
60+
- number of results that have been contributed per day
61+
- not used in firebase
62+
63+
cum_number_of_results:
64+
- sum of daily number of results up to that day
65+
66+
progress:
67+
- relative progress per day (e.g. overall progress increased by 0.15 on that day)
68+
- not used in firebase
69+
70+
cum_progress:
71+
- absolute progress up to that day
72+
- refers to the project progress attribute in firebase
5973
"""
6074

6175
groups_df["required_results"] = (
@@ -127,7 +141,18 @@ def get_contributors_by_date(results_df: pd.DataFrame) -> pd.DataFrame:
127141
"""
128142
for each project we retrospectively generate the following attributes for a given
129143
date utilizing the results:
130-
number_of_users, number_of_new_users, cum_number_of_users
144+
145+
number_of_users:
146+
- number of distinct users active per day
147+
- not used in firebase
148+
149+
number_of_new_users:
150+
- number of distinct users who mapped the first group in that project per day
151+
- not used in firebase
152+
153+
cum_number_of_users:
154+
- overall number of distinct users active up to that day
155+
- refers to the project contributorCount attribute in firebase
131156
"""
132157

133158
user_first_day_df = results_df.groupby(["user_id"]).agg(

0 commit comments

Comments
 (0)