Spaces:
Sleeping
Sleeping
File size: 9,323 Bytes
a30d85d 9173a54 a30d85d 9173a54 a30d85d 9173a54 a30d85d 9173a54 a30d85d 9173a54 |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
import json
import os
import datetime
import numpy as np
import pymongo
from pymongo import MongoClient
from bson.binary import Binary
from dotenv import load_dotenv
from bson import ObjectId
from loguru import logger
load_dotenv(override=True)
class Database:
def __init__(self):
self.client = None
self.db = None
def connect(self):
"""
Connect to the MongoDB database.
"""
try:
# Connect to MongoDB
self.client = MongoClient(os.environ.get("MONGODB_URL"))
self.db = self.client[os.environ.get("DB_NAME", "scheduling")]
# Create users collection if it doesn't exist
if "user" not in self.db.list_collection_names():
self.db.create_collection("user")
return True
except Exception as e:
logger.error(f"Error connecting to database: {e}")
return False
def close(self):
"""
Close the database connection.
"""
if self.client:
self.client.close()
def create_tables(self):
"""
Create the necessary collections if they don't exist.
Note: This method is kept for backward compatibility but is no longer used
for creating user-specific collections. Instead, user data is stored in collections
that are created on-demand when saving user data.
"""
try:
# Ensure users collection exists
if "user" not in self.db.list_collection_names():
self.db.create_collection("user")
return True
except Exception as e:
logger.error(f"Error creating collections: {e}")
return False
def create_user_table(self, user_id):
"""
Create a collection for a specific user if it doesn't exist.
Parameters:
user_id (str): The ID of the user.
Returns:
bool: True if the collection was created successfully, False otherwise.
"""
try:
# # User data is stored in the "user_data" collection
# if "user" not in self.db.list_collection_names():
# self.db.create_collection("user")
# Add user to the users collection if not exists
self.db.user.update_one(
{"_id": ObjectId(user_id)},
{"$setOnInsert": {"created_at": datetime.datetime.now()}},
upsert=True,
)
return True
except Exception as e:
logger.error(f"Error creating user collection: {e}")
return False
def save_user_weights(self, user_id, weights):
"""
Save user weights to the database.
Parameters:
user_id (str): The ID of the user.
weights (numpy.ndarray): The weights to save.
Returns:
bool: True if the weights were saved successfully, False otherwise.
"""
try:
# Create user entry if it doesn't exist
# if not self.create_user_table(user_id):
# return False
# Convert numpy array to bytes
weights_bytes = Binary(weights.tobytes())
# Update or insert weights
self.db.user.update_one(
{"_id": ObjectId(user_id)},
{
"$set": {
"weights": weights_bytes,
"weights_updated_at": datetime.datetime.now(),
}
},
)
return True
except Exception as e:
logger.error(f"Error saving user weights: {e}")
return False
def get_user_weights(self, user_id, default_weights):
"""
Get user weights from the database.
Parameters:
user_id (str): The ID of the user.
default_weights (numpy.ndarray): The default weights to use if the user doesn't have weights.
Returns:
numpy.ndarray: The weights for the user.
"""
try:
# Get weights from user_data collection
result = self.db.user.find_one({"_id": ObjectId(user_id)})
if result and "weights" in result:
# Convert bytes to numpy array
weights_bytes = result["weights"]
weights = np.frombuffer(weights_bytes, dtype=default_weights.dtype)
return weights.reshape(default_weights.shape)
return default_weights
except Exception as e:
logger.error(f"Error getting user weights: {e}")
return default_weights
def user_weights_exist(self, user_id):
"""
Check if weights exist for the given user.
Parameters:
user_id (str): The ID of the user.
Returns:
bool: True if weights exist for the user, False otherwise.
"""
try:
# Check if weights exist in user_data collection
result = self.db.user.find_one({"_id": ObjectId(user_id)})
return result is not None
except Exception as e:
logger.error(f"Error checking if user weights exist: {e}")
return False
def save_user_metadata(self, user_id, metadata):
"""
Save user metadata to the database.
Parameters:
user_id (str): The ID of the user.
metadata (dict): The metadata to save.
Returns:
bool: True if the metadata was saved successfully, False otherwise.
"""
try:
# Create user entry if it doesn't exist
# if not self.create_user_table(user_id):
# return False
# Update or insert metadata
self.db.user.update_one(
{"_id": ObjectId(user_id)},
{
"$set": {
"metadata": metadata,
"weights_updated_at": datetime.datetime.now(),
}
},
)
return True
except Exception as e:
logger.error(f"Error saving user metadata: {e}")
return False
def get_user_metadata(self, user_id):
"""
Get user metadata from the database.
Parameters:
user_id (str): The ID of the user.
Returns:
dict: The metadata for the user.
"""
try:
# Get metadata from user_data collection
result = self.db.user.find_one({"_id": ObjectId(user_id)})
if result and "metadata" in result:
return result["metadata"]
return {}
except Exception as e:
logger.error(f"Error getting user metadata: {e}")
return {}
def get_all_user_metadata(self):
"""
Get metadata for all users from the database.
Returns:
dict: A dictionary mapping user IDs to their metadata.
"""
try:
# Get metadata for all users
results = self.db.user.find({})
# Build a dictionary of user_id -> metadata
metadata = {}
for result in results:
if "_id" in result and "metadata" in result:
metadata[result["_id"]] = result["metadata"]
return metadata
except Exception as e:
logger.error(f"Error getting all user metadata: {e}")
return {}
def get_user_metadata(self, user_id):
"""
Get metadata for a specific user from the database.
Parameters:
user_id (str): The ID of the user.
Returns:
dict: The metadata for the user.
"""
try:
# Get metadata from user_data collection
result = self.db.user.find_one({"_id": ObjectId(user_id)})
if result and "metadata" in result:
return result["metadata"]
return {}
except Exception as e:
logger.error(f"Error getting user metadata: {e}")
return {}
def get_all_users(self):
"""
Get a list of all users from the database.
Returns:
list: A list of all user IDs.
"""
try:
# Get all users from users collection
results = self.db.user.find({})
# Extract user IDs
user_ids = [str(result["_id"]) for result in results]
print("user_ids", user_ids)
return user_ids
except Exception as e:
logger.error(f"Error getting all users: {e}")
return []
def get_destination_ids(self,destination_names):
"""
Get destination IDs from the database.
Parameters:
destination_names (list): A list of destination names.
Returns:
list: A list of destination IDs.
"""
try:
# Get destination IDs from the database
results = self.db.destination.find({"name": {"$in": destination_names}})
destination_ids = [str(result["_id"]) for result in results]
return destination_ids
except Exception as e:
print(f"Error getting destination IDs: {e}")
return []
# Create a singleton instance
db = Database()
|