Nattyboi commited on
Commit
294884c
·
1 Parent(s): beb1fb0

changed some stuff I can't remember no need for background processes

Browse files
app.py CHANGED
@@ -490,7 +490,9 @@ def handle_change(change=None,new_point=None):
490
  import logging
491
  logging.basicConfig(level=logging.INFO)
492
  logger = logging.getLogger(__name__)
 
493
  if new_point!=None:
 
494
  logger.info(f"Extra info: {new_point}")
495
  if "LeaderBoard" not in collections:
496
  print("No leaderboard so creating one now")
@@ -503,7 +505,6 @@ def handle_change(change=None,new_point=None):
503
  create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
504
  else:
505
 
506
- # implement logic that takes new_point['userId'] and does the same thing update would have
507
  user_id =new_point.get('userId')
508
  leveleduser = get_all_users(userId=user_id)
509
  points = get_all_simple_points_func(userId=user_id)
 
490
  import logging
491
  logging.basicConfig(level=logging.INFO)
492
  logger = logging.getLogger(__name__)
493
+
494
  if new_point!=None:
495
+ collections = db.list_collection_names()
496
  logger.info(f"Extra info: {new_point}")
497
  if "LeaderBoard" not in collections:
498
  print("No leaderboard so creating one now")
 
505
  create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
506
  else:
507
 
 
508
  user_id =new_point.get('userId')
509
  leveleduser = get_all_users(userId=user_id)
510
  points = get_all_simple_points_func(userId=user_id)
gamification/logic.py CHANGED
@@ -3,7 +3,8 @@ from gamification.objects import PlatformEngagement, UserFeedback, UserLevel, Us
3
  from gamification.levelLogic import create_level_func,get_all_levels_func,edit_level_func,delete_level_func
4
  from gamification.imports import *
5
  from gamification.pointLogic import create_points_func,get_all_simple_points_func,get_all_points_func
6
-
 
7
 
8
  # Normal Math
9
  def caculate_rate_change_func(c0,c1,days_ago):
@@ -62,7 +63,7 @@ def create_feedback_func(document:UserFeedback)->bool:
62
  # Insert the document
63
  if document!=None:
64
  feedbackPoints= Points(userId=document.userId,platformEngagement=PlatformEngagement(providing_feedback=15))
65
- create_points_func(document=feedbackPoints)
66
  result = collection.insert_one(document.model_dump())
67
  return True
68
  else:
 
3
  from gamification.levelLogic import create_level_func,get_all_levels_func,edit_level_func,delete_level_func
4
  from gamification.imports import *
5
  from gamification.pointLogic import create_points_func,get_all_simple_points_func,get_all_points_func
6
+ from concurrent.futures import ThreadPoolExecutor
7
+ executor = ThreadPoolExecutor(max_workers=5)
8
 
9
  # Normal Math
10
  def caculate_rate_change_func(c0,c1,days_ago):
 
63
  # Insert the document
64
  if document!=None:
65
  feedbackPoints= Points(userId=document.userId,platformEngagement=PlatformEngagement(providing_feedback=15))
66
+ executor.submit(create_points_func, document=feedbackPoints)
67
  result = collection.insert_one(document.model_dump())
68
  return True
69
  else:
gamification/pointLogic.py CHANGED
@@ -1,5 +1,5 @@
1
 
2
- from app import handle_change
3
  from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
4
  from gamification.imports import *
5
  from gamification.levelLogic import get_all_levels_func
@@ -29,7 +29,7 @@ def get_dream_job(userId):
29
 
30
 
31
  def create_points_func(document:UserPoints)->bool:
32
-
33
  db_uri = MONGO_URI
34
  db_name = "crayonics"
35
  collection_name="Points"
@@ -42,7 +42,7 @@ def create_points_func(document:UserPoints)->bool:
42
  doc = document.model_dump()
43
  doc['earnedAt']=datetime.now()
44
  result = collection.insert_one(doc)
45
- handle_change(new_point={"userId",document.userId})
46
  return True
47
  else:
48
  client.close()
 
1
 
2
+
3
  from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
4
  from gamification.imports import *
5
  from gamification.levelLogic import get_all_levels_func
 
29
 
30
 
31
  def create_points_func(document:UserPoints)->bool:
32
+ from app import handle_change
33
  db_uri = MONGO_URI
34
  db_name = "crayonics"
35
  collection_name="Points"
 
42
  doc = document.model_dump()
43
  doc['earnedAt']=datetime.now()
44
  result = collection.insert_one(doc)
45
+ handle_change(new_point={"userId":document.userId})
46
  return True
47
  else:
48
  client.close()
s.py DELETED
File without changes
streaksManagement.py CHANGED
@@ -3,7 +3,8 @@ from datetime import datetime, timedelta
3
  from typing import Dict, List, Union
4
  from gamification.objects import Points,PointMultipliersWeekendWarrior,PlatformEngagement
5
  from gamification.logic import create_points_func
6
-
 
7
 
8
  def get_current_date() -> str:
9
  # Get the current date and return it in YYYY-MM-DD format
@@ -107,17 +108,20 @@ def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
107
  today = datetime.date(datetime.now())
108
  if (today.weekday()>=5):
109
  streakPoints= Points(userId=document['user_id'],platformEngagement=PlatformEngagement(daily_check_in=5),weekendWarrior=PointMultipliersWeekendWarrior())
110
- create_points_func(document=streakPoints)
 
111
  else:
112
  streakPoints= Points(userId=document['user_id'],platformEngagement=PlatformEngagement(daily_check_in=5))
113
- create_points_func(document=streakPoints)
 
114
  else:
115
 
116
  dates=found_user["streak_dates"]
117
- collection.update_one(
118
  {"user_id": document.get("user_id")},
119
  {"$set": {"streak_dates": dates}}
120
- )
 
121
  return True
122
  else:
123
  save_in_streaks_history(db_uri=db_uri,document=found_user)
 
3
  from typing import Dict, List, Union
4
  from gamification.objects import Points,PointMultipliersWeekendWarrior,PlatformEngagement
5
  from gamification.logic import create_points_func
6
+ from concurrent.futures import ThreadPoolExecutor
7
+ executor = ThreadPoolExecutor(max_workers=5)
8
 
9
  def get_current_date() -> str:
10
  # Get the current date and return it in YYYY-MM-DD format
 
108
  today = datetime.date(datetime.now())
109
  if (today.weekday()>=5):
110
  streakPoints= Points(userId=document['user_id'],platformEngagement=PlatformEngagement(daily_check_in=5),weekendWarrior=PointMultipliersWeekendWarrior())
111
+ executor.submit(create_points_func,document=streakPoints)
112
+ # create_points_func(document=streakPoints)
113
  else:
114
  streakPoints= Points(userId=document['user_id'],platformEngagement=PlatformEngagement(daily_check_in=5))
115
+ executor.submit(create_points_func,document=streakPoints)
116
+ # create_points_func(document=streakPoints)
117
  else:
118
 
119
  dates=found_user["streak_dates"]
120
+ result = collection.update_one(
121
  {"user_id": document.get("user_id")},
122
  {"$set": {"streak_dates": dates}}
123
+ )
124
+ print(result)
125
  return True
126
  else:
127
  save_in_streaks_history(db_uri=db_uri,document=found_user)
tokenManagement.py CHANGED
@@ -2,7 +2,8 @@
2
 
3
  import datetime
4
  from bson import ObjectId
5
-
 
6
  from streaksManagement import streaks_manager
7
 
8
 
@@ -78,6 +79,7 @@ def create_refreshToken(db_uri: str, user_id: str) -> str:
78
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
79
  streaks_doc={}
80
  streaks_doc['user_id'] = str(user_id)
 
81
  streaks_manager(db_uri=db_uri,document=streaks_doc)
82
  client.close()
83
  return str(result.inserted_id)
@@ -131,16 +133,19 @@ def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
131
  if isexpired(doc['expire_at'])!=False:
132
  streaks_doc={}
133
  streaks_doc['user_id'] = str(user_id)
 
134
  streaks_manager(db_uri=db_uri,document=streaks_doc)
135
  pass
136
  else:
137
  streaks_doc={}
138
  streaks_doc['user_id'] = str(user_id)
 
139
  streaks_manager(db_uri=db_uri,document=streaks_doc)
140
  return True
141
  else:
142
  streaks_doc={}
143
  streaks_doc['user_id'] = str(user_id)
 
144
  streaks_manager(db_uri=db_uri,document=streaks_doc)
145
  pass
146
  return False
@@ -164,11 +169,13 @@ def verify_refresh_access_token(db_uri: str, user_id: str, access_token: str,ref
164
  if str(doc['previous_access_token']) == access_token:
165
  streaks_doc={}
166
  streaks_doc['user_id'] = str(user_id)
 
167
  streaks_manager(db_uri=db_uri,document=streaks_doc)
168
  return True
169
  else:
170
  streaks_doc={}
171
  streaks_doc['user_id'] = str(user_id)
 
172
  streaks_manager(db_uri=db_uri,document=streaks_doc)
173
  pass
174
  return False
 
2
 
3
  import datetime
4
  from bson import ObjectId
5
+ from concurrent.futures import ThreadPoolExecutor
6
+ executor = ThreadPoolExecutor(max_workers=5)
7
  from streaksManagement import streaks_manager
8
 
9
 
 
79
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
80
  streaks_doc={}
81
  streaks_doc['user_id'] = str(user_id)
82
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
83
  streaks_manager(db_uri=db_uri,document=streaks_doc)
84
  client.close()
85
  return str(result.inserted_id)
 
133
  if isexpired(doc['expire_at'])!=False:
134
  streaks_doc={}
135
  streaks_doc['user_id'] = str(user_id)
136
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
137
  streaks_manager(db_uri=db_uri,document=streaks_doc)
138
  pass
139
  else:
140
  streaks_doc={}
141
  streaks_doc['user_id'] = str(user_id)
142
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
143
  streaks_manager(db_uri=db_uri,document=streaks_doc)
144
  return True
145
  else:
146
  streaks_doc={}
147
  streaks_doc['user_id'] = str(user_id)
148
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
149
  streaks_manager(db_uri=db_uri,document=streaks_doc)
150
  pass
151
  return False
 
169
  if str(doc['previous_access_token']) == access_token:
170
  streaks_doc={}
171
  streaks_doc['user_id'] = str(user_id)
172
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
173
  streaks_manager(db_uri=db_uri,document=streaks_doc)
174
  return True
175
  else:
176
  streaks_doc={}
177
  streaks_doc['user_id'] = str(user_id)
178
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
179
  streaks_manager(db_uri=db_uri,document=streaks_doc)
180
  pass
181
  return False
utils.py CHANGED
@@ -10,7 +10,8 @@ from gamification.logic import create_points_func
10
  from gamification.objects import PlatformEngagement, Points
11
  from password import *
12
  from streaksManagement import streaks_manager
13
-
 
14
  def google_search(query, api_key, cx):
15
  url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
16
 
@@ -149,6 +150,7 @@ def create_user(db_uri: str, db_name: str, collection_name: str, document: dict)
149
 
150
  streaks_doc={}
151
  streaks_doc['user_id'] = str(result.inserted_id)
 
152
  streaks_manager(db_uri=db_uri,document=streaks_doc)
153
  return str(result.inserted_id)
154
  else:
@@ -229,7 +231,9 @@ def login_user(db_uri: str, db_name: str, collection_name: str, document: dict)
229
 
230
  if check_password(password=document['password'],hashed_password=s['password']):
231
  streaks_doc['user_id'] = str(s["_id"])
 
232
  streaks_manager(db_uri=db_uri,document=streaks_doc)
 
233
  return str(s['_id'])
234
  else:
235
  return False
@@ -277,6 +281,7 @@ def user_details_func(db_uri: str, document: Dict) -> Optional[Dict]:
277
  streaks_doc['user_id'] = user_id
278
 
279
  # Call streaks_manager (assuming this function exists elsewhere)
 
280
  streaks_manager(db_uri=db_uri, document=streaks_doc)
281
 
282
  if streaks_collection_doc:
 
10
  from gamification.objects import PlatformEngagement, Points
11
  from password import *
12
  from streaksManagement import streaks_manager
13
+ from concurrent.futures import ThreadPoolExecutor
14
+ executor = ThreadPoolExecutor(max_workers=5)
15
  def google_search(query, api_key, cx):
16
  url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
17
 
 
150
 
151
  streaks_doc={}
152
  streaks_doc['user_id'] = str(result.inserted_id)
153
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
154
  streaks_manager(db_uri=db_uri,document=streaks_doc)
155
  return str(result.inserted_id)
156
  else:
 
231
 
232
  if check_password(password=document['password'],hashed_password=s['password']):
233
  streaks_doc['user_id'] = str(s["_id"])
234
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
235
  streaks_manager(db_uri=db_uri,document=streaks_doc)
236
+
237
  return str(s['_id'])
238
  else:
239
  return False
 
281
  streaks_doc['user_id'] = user_id
282
 
283
  # Call streaks_manager (assuming this function exists elsewhere)
284
+ # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
285
  streaks_manager(db_uri=db_uri, document=streaks_doc)
286
 
287
  if streaks_collection_doc: