resume-api / gamification /pointLogic.py
Nattyboi's picture
updated codebase to make it faster
bbd997e
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
from gamification.imports import *
from gamification.levelLogic import get_all_levels_func
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=5)
# utils
def get_particular_level(totalPoints,dreamJob)->UserLevel:
# query db and get the results of all the level probably re use a function
levels = get_all_levels_func()
particularLevel= [level for level in levels if (level.maxPoints >= totalPoints) and (level.minPoints<=totalPoints)and (level.careerPath==dreamJob)]
defaulLevel= [level for level in levels if level.levelName=="default" ]
if len(particularLevel)>0:
return particularLevel
return defaulLevel
def get_dream_job(userId):
db_uri=MONGO_URI
db_name = "crayonics"
collection_name = "Questionaire"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
questionaire_doc = collection.find_one({"userId": userId})
if questionaire_doc:
return questionaire_doc['dreamRole']
else: return False
def create_points_func(document:UserPoints)->bool:
from app import handle_change3
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
if document!=None:
doc = document.model_dump()
doc['earnedAt']=datetime.now()
result = collection.insert_one(doc)
executor.submit (handle_change3 , document.userId)
return True
else:
client.close()
return False
def get_all_points_func(userId) -> IndividualUserLevel:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name = "Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
dreamJob = get_dream_job(userId=userId)
point_cursor = collection.find({"userId": userId})
# Convert the cursor to a list so we can reuse it
points_list = list(point_cursor)
# Calculate the total points
totalPoints = sum([point['numOfPoints'] for point in points_list])
particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
# Create the individual points list
individualPoints = [indpoint for indpoint in points_list]
print([pointss for pointss in individualPoints if all(val is not None for val in pointss.values())])
# Create the IndividualUserLevel object with totalPoints and individualPoints
points = IndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,minPoints=particularLevelInfo[0].minPoints,maxPoints=particularLevelInfo[0].maxPoints,levelNumber=particularLevelInfo[0].levelNumber, individualPoints=individualPoints)
return points
def get_all_simple_points_func(userId) -> SimpleIndividualUserLevel:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name = "Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
dreamJob = get_dream_job(userId=userId)
point_cursor = collection.find({"userId": userId})
try:
points_list = list(point_cursor)
totalPoints = sum([point['numOfPoints'] for point in points_list])
particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
points = SimpleIndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,maxPoints=particularLevelInfo[0].maxPoints,minPoints=particularLevelInfo[0].minPoints,levelNumber=particularLevelInfo[0].levelNumber)
except:
totalPoints = 0
points = SimpleIndividualUserLevel(totalpoints=totalPoints)
return points