Adjust life status endpoint, pre-GG report adjustment, and migrate user adjustment

#10
Files changed (3) hide show
  1. app/main.py +24 -1
  2. app/user.py +10 -0
  3. app/utils.py +77 -13
app/main.py CHANGED
@@ -491,6 +491,23 @@ async def migrate_user(
491
  user.done_first_reflection = old_user_object.done_first_reflection
492
  user.client = client
493
  user.conversations.client = client
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
 
495
  api_response = {
496
  "user": user.user_info,
@@ -504,7 +521,13 @@ async def migrate_user(
504
  "challenges": user.challenges,
505
  "other_focusses": user.other_focusses,
506
  "scores": f"Personal Growth: {user.personal_growth_score} || Career: {user.career_growth_score} || Health/Wellness: {user.health_and_wellness_score} || Relationships: {user.relationship_score} || Mental Health: {user.mental_well_being_score}",
507
- "recent_wins": user.recent_wins
 
 
 
 
 
 
508
  }
509
 
510
  add_to_cache(user)
 
491
  user.done_first_reflection = old_user_object.done_first_reflection
492
  user.client = client
493
  user.conversations.client = client
494
+ if hasattr(old_user_object, "goal"): setattr(user, "goal", old_user_object.goal)
495
+ if hasattr(old_user_object, "last_gg_session"): setattr(user, "last_gg_session", old_user_object.last_gg_session)
496
+ if hasattr(old_user_object, "micro_actions"): setattr(user, "micro_actions", old_user_object.micro_actions)
497
+ if hasattr(old_user_object, "recommended_micro_actions"): setattr(user, "recommended_micro_actions", old_user_object.recommended_micro_actions)
498
+ if hasattr(old_user_object, "challenges"): setattr(user, "challenges", old_user_object.challenges)
499
+ if hasattr(old_user_object, "other_focusses"): setattr(user, "other_focusses", old_user_object.other_focusses)
500
+ if hasattr(old_user_object, "personal_growth_score"): setattr(user, "personal_growth_score", old_user_object.personal_growth_score)
501
+ if hasattr(old_user_object, "career_growth_score"): setattr(user, "career_growth_score", old_user_object.career_growth_score)
502
+ if hasattr(old_user_object, "relationship_score"): setattr(user, "relationship_score", old_user_object.relationship_score)
503
+ if hasattr(old_user_object, "mental_well_being_score"): setattr(user, "mental_well_being_score", old_user_object.mental_well_being_score)
504
+ if hasattr(old_user_object, "health_and_wellness_score"): setattr(user, "health_and_wellness_score", old_user_object.health_and_wellness_score)
505
+ if hasattr(old_user_object, "reminders"): setattr(user, "reminders", old_user_object.reminders)
506
+ if hasattr(old_user_object, "recent_wins"): setattr(user, "recent_wins", old_user_object.recent_wins)
507
+ if hasattr(old_user_object, "recommended_gg_topics"): setattr(user, "recommended_gg_topics", old_user_object.recommended_gg_topics)
508
+ if hasattr(old_user_object, "growth_plan"): setattr(user, "growth_plan", old_user_object.growth_plan)
509
+ if hasattr(old_user_object, "user_interaction_guidelines"): setattr(user, "user_interaction_guidelines", old_user_object.user_interaction_guidelines)
510
+ if hasattr(old_user_object, "score_history"): setattr(user, "score_history", old_user_object.score_history)
511
 
512
  api_response = {
513
  "user": user.user_info,
 
521
  "challenges": user.challenges,
522
  "other_focusses": user.other_focusses,
523
  "scores": f"Personal Growth: {user.personal_growth_score} || Career: {user.career_growth_score} || Health/Wellness: {user.health_and_wellness_score} || Relationships: {user.relationship_score} || Mental Health: {user.mental_well_being_score}",
524
+ "recent_wins": user.recent_wins,
525
+ "last_gg_session": user.last_gg_session,
526
+ "reminders": user.reminders,
527
+ "recommended_gg_topics": user.recommended_gg_topics,
528
+ "growth_plan": user.growth_plan,
529
+ "user_interaction_guidelines": user.user_interaction_guidelines,
530
+ "score_history": user.score_history
531
  }
532
 
533
  add_to_cache(user)
app/user.py CHANGED
@@ -117,6 +117,8 @@ class User:
117
  self.user_interaction_guidelines = self.generate_user_interaction_guidelines(user_info, client)
118
  self.conversations = ConversationManager(client, self, asst_id)
119
 
 
 
120
  @catch_error
121
  def extend_growth_plan(self):
122
  # Change current growth plan to 14d growth plan
@@ -267,6 +269,14 @@ class User:
267
  elif variable == 'Relationship':
268
  self.relationship_score += points_added
269
  logger.info(f"Added {points_added} points to Relationship for {notes}", extra={"user_id": self.user_id, "endpoint": "add_life_score_point"})
 
 
 
 
 
 
 
 
270
 
271
  @catch_error
272
  def get_current_goal(self, full=False):
 
117
  self.user_interaction_guidelines = self.generate_user_interaction_guidelines(user_info, client)
118
  self.conversations = ConversationManager(client, self, asst_id)
119
 
120
+ self.score_history = []
121
+
122
  @catch_error
123
  def extend_growth_plan(self):
124
  # Change current growth plan to 14d growth plan
 
269
  elif variable == 'Relationship':
270
  self.relationship_score += points_added
271
  logger.info(f"Added {points_added} points to Relationship for {notes}", extra={"user_id": self.user_id, "endpoint": "add_life_score_point"})
272
+ # Add historical data (append) to score_history
273
+ historical_entry = {
274
+ "area": variable,
275
+ "points_added": points_added,
276
+ "notes": notes,
277
+ "created_at": pd.Timestamp.now()
278
+ }
279
+ self.score_history.append(historical_entry)
280
 
281
  @catch_error
282
  def get_current_goal(self, full=False):
app/utils.py CHANGED
@@ -371,6 +371,9 @@ def get_user_summary(user_id):
371
 
372
  3. **30-Minute Coaching Session Script**: A detailed, partitioned script to help the coach prepare for the session, including dialogue, questions, and guidance tailored to the client's needs, covering the five key areas. The script should be partitioned into several sections in the JSON output, similar to the structure provided for the Pre-Growth Guide Session Report.
373
 
 
 
 
374
  ---
375
 
376
  **Instructions:**
@@ -600,12 +603,19 @@ def get_user_summary(user_id):
600
  # Combine user information and chat history for context
601
  user_context = f"""
602
  Based on the following user profile and chat history, generate the required reports.
603
-
604
- ### USER PROFILE ###
605
- {user_info}
606
 
607
  ### CHAT HISTORY ###
608
  {chat_history}
 
 
 
 
 
 
 
609
  """
610
 
611
  # Step 3: Call the OpenAI API using the specified function
@@ -975,16 +985,69 @@ def get_user_life_status(user_id):
975
 
976
  # Get response and convert into dictionary
977
  mantra = json.loads(response.choices[0].message.content)["mantra_of_the_week"]
 
 
 
 
 
 
 
 
 
 
 
978
 
979
  # Get current life score
980
- life_score = {
981
- "overall": user.personal_growth_score + user.career_growth_score + user.relationship_score + user.mental_well_being_score + user.health_and_wellness_score,
982
- "personal_growth": user.personal_growth_score,
983
- "health_and_wellness": user.health_and_wellness_score,
984
- "mental_well_being": user.mental_well_being_score,
985
- "career_growth": user.career_growth_score,
986
- "relationship": user.relationship_score
987
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
988
  # Get current goal
989
  current_goal = '' if not user.goal else user.goal[-1].content
990
  # Get life score achievements in list
@@ -992,14 +1055,15 @@ def get_user_life_status(user_id):
992
  # Combine everything
993
 
994
  reports = {
995
- "life_score": life_score,
 
996
  "mantra_of_the_week": mantra,
997
  "goal": current_goal,
998
  "recent_wins": recent_wins
999
  }
1000
 
1001
  # Step 4: Return the JSON reports
1002
- logger.info(f"User life status generated successfully for user {user_id}", extra={'user_id': user_id, 'endpoint': function_name})
1003
  return reports
1004
 
1005
  async def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
 
371
 
372
  3. **30-Minute Coaching Session Script**: A detailed, partitioned script to help the coach prepare for the session, including dialogue, questions, and guidance tailored to the client's needs, covering the five key areas. The script should be partitioned into several sections in the JSON output, similar to the structure provided for the Pre-Growth Guide Session Report.
373
 
374
+ ---
375
+ **Important Note**
376
+ The **chat history** shows the most updated information. Hence, if there is a difference between the goal/challenge/other key information in the user's chat history and the user's profile, you must create the reports based on the chat history!
377
  ---
378
 
379
  **Instructions:**
 
603
  # Combine user information and chat history for context
604
  user_context = f"""
605
  Based on the following user profile and chat history, generate the required reports.
606
+
607
+ **Important Note**
608
+ The **chat history** shows the most updated information. Hence, if there is a difference between the goal/challenge/other key information in the user's chat history and the user's profile, you must create the reports based on the chat history!
609
 
610
  ### CHAT HISTORY ###
611
  {chat_history}
612
+
613
+ ### USER GOAL ###
614
+ {user_goal}
615
+
616
+ ### USER PROFILE ###
617
+ {user_info}
618
+
619
  """
620
 
621
  # Step 3: Call the OpenAI API using the specified function
 
985
 
986
  # Get response and convert into dictionary
987
  mantra = json.loads(response.choices[0].message.content)["mantra_of_the_week"]
988
+
989
+ cumulative_life_score = {
990
+ "overall": user.personal_growth_score + user.career_growth_score + user.relationship_score + user.mental_well_being_score + user.health_and_wellness_score,
991
+ "personal_growth": user.personal_growth_score,
992
+ "health_and_wellness": user.health_and_wellness_score,
993
+ "mental_well_being": user.mental_well_being_score,
994
+ "career_growth": user.career_growth_score,
995
+ "relationship": user.relationship_score
996
+ }
997
+
998
+ logger.info(f"{user.score_history}",extra={'user_id': user_id, 'endpoint': function_name})
999
 
1000
  # Get current life score
1001
+ if len(user.score_history) == 0:
1002
+ thirtydays_life_score = cumulative_life_score
1003
+ else:
1004
+ # Calculate previous 30 days date
1005
+ now = pd.Timestamp.now()
1006
+ thirty_days_ago = now - pd.Timedelta(days=30)
1007
+
1008
+ # Filter the data
1009
+ filtered_data = [entry for entry in user.score_history if thirty_days_ago <= entry["created_at"] <= now]
1010
+ logger.info(f"Filtered Data: {filtered_data}", extra={'user_id': user_id, 'endpoint': function_name})
1011
+
1012
+ # Normalize area names to match expected keys
1013
+ area_mapping = {
1014
+ "Personal Growth": "personal_growth",
1015
+ "Health and Wellness": "health_and_wellness",
1016
+ "Mental Well-being": "mental_well_being",
1017
+ "Career Growth": "career_growth",
1018
+ "Relationship": "relationship"
1019
+ }
1020
+
1021
+ # Normalize area names in filtered data
1022
+ for entry in filtered_data:
1023
+ entry["area"] = area_mapping.get(entry["area"], entry["area"])
1024
+
1025
+ # Sum points_added, group by area
1026
+ temp_df = pd.DataFrame(filtered_data)
1027
+ grouped_points = temp_df.groupby("area")["points_added"].sum()
1028
+
1029
+ # Debug: Check the grouped points result
1030
+ logger.info(f"Grouped Points: {grouped_points}", extra={'user_id': user_id, 'endpoint': function_name})
1031
+
1032
+ # Structure the output safely
1033
+ thirtydays_life_score = {
1034
+ "overall": int(sum([
1035
+ grouped_points.get("personal_growth", 0),
1036
+ grouped_points.get("career_growth", 0),
1037
+ grouped_points.get("health_and_wellness", 0),
1038
+ grouped_points.get("mental_well_being", 0),
1039
+ grouped_points.get("relationship", 0),
1040
+ ])),
1041
+ "personal_growth": int(grouped_points.get("personal_growth", 0)),
1042
+ "health_and_wellness": int(grouped_points.get("health_and_wellness", 0)),
1043
+ "mental_well_being": int(grouped_points.get("mental_well_being", 0)),
1044
+ "career_growth": int(grouped_points.get("career_growth", 0)),
1045
+ "relationship": int(grouped_points.get("relationship", 0))
1046
+ }
1047
+
1048
+ # Debug: Check the final structured result
1049
+ logger.info(f"Final Thirty Days Life Score: {thirtydays_life_score}", extra={'user_id': user_id, 'endpoint': function_name})
1050
+
1051
  # Get current goal
1052
  current_goal = '' if not user.goal else user.goal[-1].content
1053
  # Get life score achievements in list
 
1055
  # Combine everything
1056
 
1057
  reports = {
1058
+ "life_score": thirtydays_life_score,
1059
+ "cumulative_life_score": cumulative_life_score,
1060
  "mantra_of_the_week": mantra,
1061
  "goal": current_goal,
1062
  "recent_wins": recent_wins
1063
  }
1064
 
1065
  # Step 4: Return the JSON reports
1066
+ logger.info(f"User life status generated successfully for user {user_id}: {reports}", extra={'user_id': user_id, 'endpoint': function_name})
1067
  return reports
1068
 
1069
  async def get_api_key(api_key_header: str = Security(api_key_header)) -> str: