Spaces:
Running
Running
hot fix for leaderboard
Browse files- app.py +185 -51
- gamification/pointLogic.py +5 -2
- gamification/routes.py +0 -3
- testing.py +28 -0
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from io import BytesIO
|
2 |
from dotenv import load_dotenv
|
3 |
import os
|
|
|
4 |
from utils import *
|
5 |
from fastapi import FastAPI, File, HTTPException, Header, UploadFile,status
|
6 |
from tokenManagement import *
|
@@ -451,14 +452,12 @@ def create_leaderboard_ranking( document: LeaderBoardRanking) -> bool:
|
|
451 |
|
452 |
|
453 |
def get_all_users(user_id =None) -> List:
|
454 |
-
|
455 |
client = MongoClient(MONGO_URI)
|
456 |
db = client.crayonics
|
457 |
collection = db['users']
|
458 |
# Insert the document
|
459 |
if user_id==None:
|
460 |
results= collection.find()
|
461 |
-
|
462 |
if results:
|
463 |
result = [result for result in results]
|
464 |
return result
|
@@ -533,67 +532,201 @@ def handle_change2(new_point):
|
|
533 |
logger.error(f"Error adding user {user_id} to leaderboard: {e}")
|
534 |
|
535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
# A function to handle changes
|
537 |
-
def handle_change(change=None
|
538 |
-
|
539 |
-
|
540 |
-
logger
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
logger.info(f"Extra info: {new_point}")
|
545 |
print("No leaderboard so creating one now")
|
546 |
users = get_all_users()
|
547 |
for user in users:
|
548 |
print("inserting user",f"user id {user['_id']}")
|
549 |
points = get_all_simple_points_func(userId=str(user['_id']))
|
550 |
tempDreamJob = get_dream_job(userId=str(user['_id']))
|
|
|
551 |
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
|
|
552 |
create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
elif new_point==None and change!=None:
|
557 |
-
print("Change detected in point making changes immediately")
|
558 |
-
logger.info(f"Change detected: {change}")
|
559 |
-
# add everybodies points and add it to the leaderboard table
|
560 |
-
collections = db.list_collection_names()
|
561 |
-
if "LeaderBoard" not in collections:
|
562 |
-
print("No leaderboard so creating one now")
|
563 |
-
users = get_all_users()
|
564 |
-
for user in users:
|
565 |
-
print("inserting user",f"user id {user['_id']}")
|
566 |
-
points = get_all_simple_points_func(userId=str(user['_id']))
|
567 |
-
tempDreamJob = get_dream_job(userId=str(user['_id']))
|
568 |
-
|
569 |
-
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
570 |
-
|
571 |
-
create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
572 |
|
573 |
-
|
574 |
-
|
|
|
575 |
# Extract the full document
|
576 |
-
full_document = change
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
user_id
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
|
595 |
-
|
596 |
-
logger.info(f"Change detected: {dumps(change)}")
|
597 |
|
598 |
# Function to run the change stream in a separate thread (non-blocking)
|
599 |
def watch_change_stream():
|
@@ -606,3 +739,4 @@ def watch_change_stream():
|
|
606 |
@app.on_event("startup")
|
607 |
def start_change_stream():
|
608 |
threading.Thread(target=watch_change_stream, daemon=True).start()
|
|
|
|
1 |
from io import BytesIO
|
2 |
from dotenv import load_dotenv
|
3 |
import os
|
4 |
+
from gamification.objects import SimpleIndividualUserLevel
|
5 |
from utils import *
|
6 |
from fastapi import FastAPI, File, HTTPException, Header, UploadFile,status
|
7 |
from tokenManagement import *
|
|
|
452 |
|
453 |
|
454 |
def get_all_users(user_id =None) -> List:
|
|
|
455 |
client = MongoClient(MONGO_URI)
|
456 |
db = client.crayonics
|
457 |
collection = db['users']
|
458 |
# Insert the document
|
459 |
if user_id==None:
|
460 |
results= collection.find()
|
|
|
461 |
if results:
|
462 |
result = [result for result in results]
|
463 |
return result
|
|
|
532 |
logger.error(f"Error adding user {user_id} to leaderboard: {e}")
|
533 |
|
534 |
|
535 |
+
def handle_change3(userId:str):
|
536 |
+
logger.info(f"Extra info: {userId}")
|
537 |
+
print("No leaderboard so creating one now")
|
538 |
+
|
539 |
+
print("========================================================")
|
540 |
+
print("Leveled User", userId)
|
541 |
+
|
542 |
+
if not userId:
|
543 |
+
raise ValueError("User ID not found in inserted document")
|
544 |
+
|
545 |
+
# Fetch user details
|
546 |
+
try:
|
547 |
+
leveleduser = get_all_users(user_id=userId)
|
548 |
+
print("========================================================")
|
549 |
+
print("Leveled User", leveleduser)
|
550 |
+
except Exception as e:
|
551 |
+
logger.error(f"Error fetching user details for userId {userId}: {e}")
|
552 |
+
leveleduser = {} # Default empty dict to prevent KeyError
|
553 |
+
|
554 |
+
# Fetch user points
|
555 |
+
try:
|
556 |
+
points = get_all_simple_points_func(userId=userId)
|
557 |
+
print("========================================================")
|
558 |
+
print("Points", points)
|
559 |
+
except Exception as e:
|
560 |
+
logger.error(f"Error fetching points for userId {userId}: {e}")
|
561 |
+
points = SimpleIndividualUserLevel(totalpoints=0)
|
562 |
+
|
563 |
+
# Fetch dream job
|
564 |
+
try:
|
565 |
+
tempDreamJob = get_dream_job(userId=userId)
|
566 |
+
print("========================================================")
|
567 |
+
print("Temp dream job", tempDreamJob)
|
568 |
+
except Exception as e:
|
569 |
+
logger.error(f"Error fetching dream job for userId {userId}: {e}")
|
570 |
+
tempDreamJob = None
|
571 |
+
|
572 |
+
dreamJob = tempDreamJob if isinstance(tempDreamJob, str) else "IncompleteProfile"
|
573 |
+
print("DreamJob", dreamJob)
|
574 |
+
|
575 |
+
# Insert into leaderboard
|
576 |
+
try:
|
577 |
+
create_leaderboard_ranking(LeaderBoardRanking(
|
578 |
+
userId=userId,
|
579 |
+
firstName=leveleduser.get('first_name', 'Unknown'),
|
580 |
+
lastName=leveleduser.get('last_name', 'Unknown'),
|
581 |
+
totalpoints=points.totalpoints,
|
582 |
+
lastUpdated=datetime.now(),
|
583 |
+
careerPath=dreamJob,
|
584 |
+
))
|
585 |
+
except Exception as e:
|
586 |
+
logger.error(f"Error adding user {userId} to leaderboard: {e}")
|
587 |
+
|
588 |
+
except Exception as e:
|
589 |
+
logger.error(f"Unexpected error processing change document: {e}")
|
590 |
+
|
591 |
+
|
592 |
# A function to handle changes
|
593 |
+
def handle_change(change=None):
|
594 |
+
|
595 |
+
print("Change detected in point making changes immediately")
|
596 |
+
logger.info(f"Change detected: {change}")
|
597 |
+
# add everybodies points and add it to the leaderboard table
|
598 |
+
collections = db.list_collection_names()
|
599 |
+
if "LeaderBoard" not in collections:
|
|
|
600 |
print("No leaderboard so creating one now")
|
601 |
users = get_all_users()
|
602 |
for user in users:
|
603 |
print("inserting user",f"user id {user['_id']}")
|
604 |
points = get_all_simple_points_func(userId=str(user['_id']))
|
605 |
tempDreamJob = get_dream_job(userId=str(user['_id']))
|
606 |
+
|
607 |
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
608 |
+
|
609 |
create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
610 |
|
611 |
+
else:
|
612 |
+
if change['operationType'] == 'insert':
|
613 |
+
try:
|
614 |
# Extract the full document
|
615 |
+
full_document = change.get('fullDocument', {})
|
616 |
+
user_id = full_document.get('userId')
|
617 |
+
|
618 |
+
print("========================================================")
|
619 |
+
print("Leveled User", user_id)
|
620 |
+
|
621 |
+
if not user_id:
|
622 |
+
raise ValueError("User ID not found in inserted document")
|
623 |
+
|
624 |
+
# Fetch user details
|
625 |
+
try:
|
626 |
+
leveleduser = get_all_users(user_id=user_id)
|
627 |
+
print("========================================================")
|
628 |
+
print("Leveled User", leveleduser)
|
629 |
+
except Exception as e:
|
630 |
+
logger.error(f"Error fetching user details for userId {user_id}: {e}")
|
631 |
+
leveleduser = {} # Default empty dict to prevent KeyError
|
632 |
+
|
633 |
+
# Fetch user points
|
634 |
+
try:
|
635 |
+
points = get_all_simple_points_func(userId=user_id)
|
636 |
+
print("========================================================")
|
637 |
+
print("Points", points)
|
638 |
+
except Exception as e:
|
639 |
+
logger.error(f"Error fetching points for userId {user_id}: {e}")
|
640 |
+
points = SimpleIndividualUserLevel(totalpoints=0)
|
641 |
+
|
642 |
+
# Fetch dream job
|
643 |
+
try:
|
644 |
+
tempDreamJob = get_dream_job(userId=user_id)
|
645 |
+
print("========================================================")
|
646 |
+
print("Temp dream job", tempDreamJob)
|
647 |
+
except Exception as e:
|
648 |
+
logger.error(f"Error fetching dream job for userId {user_id}: {e}")
|
649 |
+
tempDreamJob = None
|
650 |
+
|
651 |
+
dreamJob = tempDreamJob if isinstance(tempDreamJob, str) else "IncompleteProfile"
|
652 |
+
print("DreamJob", dreamJob)
|
653 |
+
|
654 |
+
# Insert into leaderboard
|
655 |
+
try:
|
656 |
+
create_leaderboard_ranking(LeaderBoardRanking(
|
657 |
+
userId=user_id,
|
658 |
+
firstName=leveleduser.get('first_name', 'Unknown'),
|
659 |
+
lastName=leveleduser.get('last_name', 'Unknown'),
|
660 |
+
totalpoints=points.totalpoints,
|
661 |
+
lastUpdated=datetime.now(),
|
662 |
+
careerPath=dreamJob,
|
663 |
+
))
|
664 |
+
except Exception as e:
|
665 |
+
logger.error(f"Error adding user {user_id} to leaderboard: {e}")
|
666 |
+
|
667 |
+
except Exception as e:
|
668 |
+
logger.error(f"Unexpected error processing change document: {e}")
|
669 |
+
elif change['operationType'] == 'update':
|
670 |
+
try:
|
671 |
+
# Extract the full document
|
672 |
+
doc_id = str( change.get('documentKey')['_id'])
|
673 |
+
|
674 |
+
print("========================================================")
|
675 |
+
print("document Id", doc_id)
|
676 |
+
user_id = get_user_id_from_docKey(doc_id)
|
677 |
+
|
678 |
+
if not user_id:
|
679 |
+
raise ValueError("User ID not found in inserted document")
|
680 |
+
|
681 |
+
# Fetch user details
|
682 |
+
try:
|
683 |
+
leveleduser = get_all_users(user_id=user_id)
|
684 |
+
print("========================================================")
|
685 |
+
print("Leveled User", leveleduser)
|
686 |
+
except Exception as e:
|
687 |
+
logger.error(f"Error fetching user details for userId {user_id}: {e}")
|
688 |
+
leveleduser = {} # Default empty dict to prevent KeyError
|
689 |
+
|
690 |
+
# Fetch user points
|
691 |
+
try:
|
692 |
+
points = get_all_simple_points_func(userId=user_id)
|
693 |
+
print("========================================================")
|
694 |
+
print("Points", points)
|
695 |
+
except Exception as e:
|
696 |
+
logger.error(f"Error fetching points for userId {user_id}: {e}")
|
697 |
+
points = SimpleIndividualUserLevel(totalpoints=0)
|
698 |
+
|
699 |
+
# Fetch dream job
|
700 |
+
try:
|
701 |
+
tempDreamJob = get_dream_job(userId=user_id)
|
702 |
+
print("========================================================")
|
703 |
+
print("Temp dream job", tempDreamJob)
|
704 |
+
except Exception as e:
|
705 |
+
logger.error(f"Error fetching dream job for userId {user_id}: {e}")
|
706 |
+
tempDreamJob = None
|
707 |
+
|
708 |
+
dreamJob = tempDreamJob if isinstance(tempDreamJob, str) else "IncompleteProfile"
|
709 |
+
print("DreamJob", dreamJob)
|
710 |
+
|
711 |
+
# Insert into leaderboard
|
712 |
+
try:
|
713 |
+
create_leaderboard_ranking(LeaderBoardRanking(
|
714 |
+
userId=user_id,
|
715 |
+
firstName=leveleduser.get('first_name', 'Unknown'),
|
716 |
+
lastName=leveleduser.get('last_name', 'Unknown'),
|
717 |
+
totalpoints=points.totalpoints,
|
718 |
+
lastUpdated=datetime.now(),
|
719 |
+
careerPath=dreamJob,
|
720 |
+
))
|
721 |
+
except Exception as e:
|
722 |
+
logger.error(f"Error adding user {user_id} to leaderboard: {e}")
|
723 |
+
|
724 |
+
except Exception as e:
|
725 |
+
logger.error(f"Unexpected error processing change document: {e}")
|
726 |
+
|
727 |
+
|
728 |
|
729 |
+
logger.info(f"Change detected:")
|
|
|
730 |
|
731 |
# Function to run the change stream in a separate thread (non-blocking)
|
732 |
def watch_change_stream():
|
|
|
739 |
@app.on_event("startup")
|
740 |
def start_change_stream():
|
741 |
threading.Thread(target=watch_change_stream, daemon=True).start()
|
742 |
+
|
gamification/pointLogic.py
CHANGED
@@ -3,7 +3,8 @@
|
|
3 |
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
|
4 |
from gamification.imports import *
|
5 |
from gamification.levelLogic import get_all_levels_func
|
6 |
-
|
|
|
7 |
# utils
|
8 |
def get_particular_level(totalPoints,dreamJob)->UserLevel:
|
9 |
# query db and get the results of all the level probably re use a function
|
@@ -29,11 +30,13 @@ def get_dream_job(userId):
|
|
29 |
|
30 |
|
31 |
def create_points_func(document:UserPoints)->bool:
|
|
|
32 |
db_uri = MONGO_URI
|
33 |
db_name = "crayonics"
|
34 |
collection_name="Points"
|
35 |
client = MongoClient(db_uri)
|
36 |
db = client[db_name]
|
|
|
37 |
collection = db[collection_name]
|
38 |
# Insert the document
|
39 |
|
@@ -41,7 +44,7 @@ def create_points_func(document:UserPoints)->bool:
|
|
41 |
doc = document.model_dump()
|
42 |
doc['earnedAt']=datetime.now()
|
43 |
result = collection.insert_one(doc)
|
44 |
-
|
45 |
return True
|
46 |
else:
|
47 |
client.close()
|
|
|
3 |
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
|
4 |
from gamification.imports import *
|
5 |
from gamification.levelLogic import get_all_levels_func
|
6 |
+
from concurrent.futures import ThreadPoolExecutor
|
7 |
+
executor = ThreadPoolExecutor(max_workers=5)
|
8 |
# utils
|
9 |
def get_particular_level(totalPoints,dreamJob)->UserLevel:
|
10 |
# query db and get the results of all the level probably re use a function
|
|
|
30 |
|
31 |
|
32 |
def create_points_func(document:UserPoints)->bool:
|
33 |
+
from app import handle_change3
|
34 |
db_uri = MONGO_URI
|
35 |
db_name = "crayonics"
|
36 |
collection_name="Points"
|
37 |
client = MongoClient(db_uri)
|
38 |
db = client[db_name]
|
39 |
+
|
40 |
collection = db[collection_name]
|
41 |
# Insert the document
|
42 |
|
|
|
44 |
doc = document.model_dump()
|
45 |
doc['earnedAt']=datetime.now()
|
46 |
result = collection.insert_one(doc)
|
47 |
+
executor.submit (handle_change3 , document.userId)
|
48 |
return True
|
49 |
else:
|
50 |
client.close()
|
gamification/routes.py
CHANGED
@@ -132,10 +132,7 @@ def get_leaderboard(background_tasks: BackgroundTasks)->List[Ranker]:
|
|
132 |
list_of_rankers = []
|
133 |
result = get_top_30()
|
134 |
background_tasks.add_task(handle_change2, 2)
|
135 |
-
# executor.submit(handle_change2,2)
|
136 |
-
|
137 |
list_of_rankers = [Ranker(**ranker) for ranker in result]
|
138 |
-
# handle_change(new_point="userId")
|
139 |
return list_of_rankers
|
140 |
except Exception as e:
|
141 |
raise HTTPException(status_code=500,detail=f"{e}")
|
|
|
132 |
list_of_rankers = []
|
133 |
result = get_top_30()
|
134 |
background_tasks.add_task(handle_change2, 2)
|
|
|
|
|
135 |
list_of_rankers = [Ranker(**ranker) for ranker in result]
|
|
|
136 |
return list_of_rankers
|
137 |
except Exception as e:
|
138 |
raise HTTPException(status_code=500,detail=f"{e}")
|
testing.py
CHANGED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
from bson import ObjectId
|
5 |
+
from pymongo import MongoClient
|
6 |
+
|
7 |
+
from app import MONGO_URI
|
8 |
+
|
9 |
+
|
10 |
+
def get_all_users(user_id:str =None) -> List:
|
11 |
+
client = MongoClient(MONGO_URI)
|
12 |
+
db = client.crayonics
|
13 |
+
collection = db['users']
|
14 |
+
# Insert the document
|
15 |
+
if user_id==None:
|
16 |
+
results= collection.find()
|
17 |
+
if results:
|
18 |
+
result = [result for result in results]
|
19 |
+
return result
|
20 |
+
|
21 |
+
client.close()
|
22 |
+
else:
|
23 |
+
result = collection.find_one(filter={"_id":ObjectId(user_id.strip())})
|
24 |
+
return result
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
print(get_all_users(user_id="67c9b68678fbf39f4ed94e01"))
|