File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
mapswipe_workers/python_scripts Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ from mapswipe_workers .auth import firebaseDB
2+ from mapswipe_workers .auth import postgresDB
3+ from mapswipe_workers .definitions import logger
4+
5+
6+ def update_task_contribution_count (uid ):
7+ """Set user's taskContributionCount in Firebase DB based on items_count in Postgres mapping_sessions."""
8+ p_con = postgresDB ()
9+ fb_db = firebaseDB ()
10+
11+ query = """
12+ SELECT COALESCE(SUM(items_count), 0)
13+ FROM public.mapping_sessions
14+ WHERE user_id = %(uid)s
15+ ;
16+ """
17+
18+ try :
19+ data = p_con .retr_query (query , {"uid" : uid })
20+ task_contribution_count = data [0 ][0 ]
21+ ref = fb_db .reference (f"v2/users/{ uid } /taskContributionCount" )
22+ ref .set (task_contribution_count )
23+ logger .info (f"Updated task contribution count to { task_contribution_count } for user { uid } in Firebase." )
24+ except Exception as e :
25+ logger .exception (e )
26+ logger .warning (f"Could NOT update task contribution count for user { uid } in Firebase." )
27+
28+
29+ def get_all_users ():
30+ """Get the user ids from all users in Firebase DB."""
31+ fb_db = firebaseDB ()
32+ users = fb_db .reference ("v2/users/" ).get (shallow = True )
33+ uid_list = users .keys ()
34+ return uid_list
35+
36+
37+ if __name__ == "__main__" :
38+ """Get all user ids from Firebase and update taskContributionCount based on Postgres mapping_sessions."""
39+ uid_list = get_all_users ()
40+ for uid in uid_list :
41+ update_task_contribution_count (uid )
42+
You can’t perform that action at this time.
0 commit comments