-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
138 lines (120 loc) · 5.27 KB
/
database.py
File metadata and controls
138 lines (120 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from pymongo import MongoClient
from datetime import datetime
import os
from typing import Dict, Any, Optional
import logging
logger = logging.getLogger(__name__)
class Database:
def __init__(self):
# Get MongoDB connection string from environment variable
mongodb_uri = os.getenv("MONGODB_URI")
if not mongodb_uri:
raise ValueError("MONGODB_URI environment variable not set")
self.client = MongoClient(mongodb_uri)
self.db = self.client.habit_tracker
self.users = self.db.users
def get_user_data(self, user_id: int) -> Optional[Dict[str, Any]]:
"""Retrieve user data from MongoDB"""
return self.users.find_one({"_id": user_id})
def create_user(self, user_id: int) -> Dict[str, Any]:
"""Create a new user document"""
current_date = datetime.now().strftime("%Y-%m-%d")
user_data = {
"_id": user_id,
"onboarded": False,
"fitness_goal": "",
"starting_metrics": {},
"experience_level": "",
"limitations": "",
"milestones": [],
"last_check_in": current_date,
"last_reminder_sent": current_date,
"reminder_time": "20:00",
"timezone": None, # Added timezone field
"current_streak": 0,
"longest_streak": 0,
"conversation_history": [],
"progress_log": {},
"rest_days": [],
# New fields for workout tracking
"exercise_history": {}, # Track performance for each exercise
"current_workout": None, # Store ongoing workout session
"workout_sessions": [], # Store completed workout sessions
"max_weights": {}, # Track max weights for progressive overload
"preferred_exercises": [] # Store exercises that work well for the user
}
self.users.insert_one(user_data)
return user_data
def update_user_data(self, user_id: int, update_data: Dict[str, Any]) -> None:
"""Update user data in MongoDB"""
self.users.update_one(
{"_id": user_id},
{"$set": update_data}
)
def update_conversation_history(self, user_id: int, message: Dict[str, Any]) -> None:
"""Append a message to the user's conversation history"""
self.users.update_one(
{"_id": user_id},
{"$push": {"conversation_history": message}}
)
def update_progress_log(self, user_id: int, date: str, entry: Dict[str, Any]) -> None:
"""Update the progress log for a specific date"""
try:
# First verify the user exists
user = self.users.find_one({"_id": user_id})
if not user:
logger.error(f"User {user_id} not found when updating progress log")
return
# Ensure progress_log exists
if "progress_log" not in user:
logger.info(f"Initializing progress_log for user {user_id}")
self.users.update_one(
{"_id": user_id},
{"$set": {"progress_log": {}}}
)
# Update the progress log for the specific date
result = self.users.update_one(
{"_id": user_id},
{"$set": {f"progress_log.{date}": entry}}
)
logger.info(f"Progress log update result - matched: {result.matched_count}, modified: {result.modified_count}")
# Verify the update
updated_user = self.users.find_one({"_id": user_id})
if date in updated_user.get("progress_log", {}):
logger.info(f"Successfully verified progress log update for user {user_id} on {date}")
else:
logger.error(f"Failed to verify progress log update for user {user_id} on {date}")
except Exception as e:
logger.error(f"Error updating progress log for user {user_id}: {e}")
raise
def get_all_users(self):
"""Get all users for reminder checking"""
return self.users.find({})
def delete_user(self, user_id: int) -> None:
"""Delete a user's data from the database"""
self.users.delete_one({"_id": user_id})
def update_exercise_history(self, user_id: int, exercise: str, performance: Dict[str, Any]) -> None:
"""Update the exercise history for a user"""
date = datetime.now().strftime("%Y-%m-%d")
self.users.update_one(
{"_id": user_id},
{"$push": {f"exercise_history.{exercise}": {
"date": date,
**performance
}}}
)
def start_workout_session(self, user_id: int, workout_plan: Dict[str, Any]) -> None:
"""Start a new workout session"""
self.users.update_one(
{"_id": user_id},
{"$set": {"current_workout": workout_plan}}
)
def complete_workout_session(self, user_id: int, session_data: Dict[str, Any]) -> None:
"""Complete a workout session and store the results"""
self.users.update_one(
{"_id": user_id},
{
"$push": {"workout_sessions": session_data},
"$set": {"current_workout": None}
}
)