-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add script to update users' taskContributionCount in Firebase fr…
…om Postgres mapping_sessions
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
mapswipe_workers/python_scripts/update_task_contribution_count_in_firebase.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from mapswipe_workers.auth import firebaseDB | ||
from mapswipe_workers.auth import postgresDB | ||
from mapswipe_workers.definitions import logger | ||
|
||
|
||
def update_task_contribution_count(uid): | ||
"""Set user's taskContributionCount in Firebase DB based on items_count in Postgres mapping_sessions.""" | ||
p_con = postgresDB() | ||
fb_db = firebaseDB() | ||
|
||
query = """ | ||
SELECT COALESCE(SUM(items_count), 0) | ||
FROM public.mapping_sessions | ||
WHERE user_id = %(uid)s | ||
; | ||
""" | ||
|
||
try: | ||
data = p_con.retr_query(query, {"uid": uid}) | ||
task_contribution_count = data[0][0] | ||
ref = fb_db.reference(f"v2/users/{uid}/taskContributionCount") | ||
ref.set(task_contribution_count) | ||
logger.info(f"Updated task contribution count to {task_contribution_count} for user {uid} in Firebase.") | ||
except Exception as e: | ||
logger.exception(e) | ||
logger.warning(f"Could NOT update task contribution count for user {uid} in Firebase.") | ||
|
||
|
||
def get_all_users(): | ||
"""Get the user ids from all users in Firebase DB.""" | ||
fb_db = firebaseDB() | ||
users = fb_db.reference("v2/users/").get(shallow=True) | ||
uid_list = users.keys() | ||
return uid_list | ||
|
||
|
||
if __name__ == "__main__": | ||
"""Get all user ids from Firebase and update taskContributionCount based on Postgres mapping_sessions.""" | ||
uid_list = get_all_users() | ||
for uid in uid_list: | ||
update_task_contribution_count(uid) | ||
|