userIdc2024 commited on
Commit
aa44385
·
verified ·
1 Parent(s): ea450f6

Update src/database.py

Browse files
Files changed (1) hide show
  1. src/database.py +48 -24
src/database.py CHANGED
@@ -11,11 +11,22 @@ MONGO_URI = os.getenv("MONGO_URI")
11
  DB_NAME = os.getenv("MONGO_DB")
12
  COLLECTION_NAME = os.getenv("MONGO_COLLECTION")
13
 
14
- # Initialize MongoDB client
15
- client = MongoClient(MONGO_URI)
16
- db = client[DB_NAME]
17
- collection = db[COLLECTION_NAME]
18
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def insert_analysis_result(
21
  video_name: str,
@@ -26,32 +37,45 @@ def insert_analysis_result(
26
  response: dict
27
  ):
28
  """Insert a new video analysis result"""
29
- document = {
30
- "video_name": video_name,
31
- "offer_details": offer_details,
32
- "target_audience": target_audience,
33
- "specific_hook": specific_hook,
34
- "additional_context": additional_context,
35
- "response": response,
36
- "created_at": datetime.utcnow()
37
- }
38
- result = collection.insert_one(document)
39
- return str(result.inserted_id)
40
-
 
 
 
41
 
42
  def get_all_results(limit: int = 20):
43
  """Fetch all saved results, sorted by newest"""
44
- return list(collection.find().sort("created_at", -1).limit(limit))
45
-
 
 
 
46
 
47
  def get_result_by_id(doc_id):
48
  """Fetch a specific analysis by _id"""
49
- from bson import ObjectId
50
- return collection.find_one({"_id": ObjectId(doc_id)})
51
-
 
 
 
52
 
53
  def delete_result_by_id(doc_id):
54
  """Delete a specific analysis by _id"""
55
- from bson import ObjectId
56
- result = collection.delete_one({"_id": ObjectId(doc_id)})
57
- return result.deleted_count > 0
 
 
 
 
 
11
  DB_NAME = os.getenv("MONGO_DB")
12
  COLLECTION_NAME = os.getenv("MONGO_COLLECTION")
13
 
14
+ # Global variables for lazy initialization
15
+ _client = None
16
+ _db = None
17
+ _collection = None
18
 
19
+ def get_database_connection():
20
+ """Initialize MongoDB connection lazily"""
21
+ global _client, _db, _collection
22
+
23
+ if _client is None:
24
+ _client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
25
+ _client.admin.command('ping')
26
+ _db = _client[DB_NAME]
27
+ _collection = _db[COLLECTION_NAME]
28
+
29
+ return _client, _db, _collection
30
 
31
  def insert_analysis_result(
32
  video_name: str,
 
37
  response: dict
38
  ):
39
  """Insert a new video analysis result"""
40
+ try:
41
+ client, db, collection = get_database_connection()
42
+ document = {
43
+ "video_name": video_name,
44
+ "offer_details": offer_details,
45
+ "target_audience": target_audience,
46
+ "specific_hook": specific_hook,
47
+ "additional_context": additional_context,
48
+ "response": response,
49
+ "created_at": datetime.utcnow()
50
+ }
51
+ result = collection.insert_one(document)
52
+ return str(result.inserted_id)
53
+ except:
54
+ raise
55
 
56
  def get_all_results(limit: int = 20):
57
  """Fetch all saved results, sorted by newest"""
58
+ try:
59
+ client, db, collection = get_database_connection()
60
+ return list(collection.find().sort("created_at", -1).limit(limit))
61
+ except:
62
+ return []
63
 
64
  def get_result_by_id(doc_id):
65
  """Fetch a specific analysis by _id"""
66
+ try:
67
+ from bson import ObjectId
68
+ client, db, collection = get_database_connection()
69
+ return collection.find_one({"_id": ObjectId(doc_id)})
70
+ except:
71
+ return None
72
 
73
  def delete_result_by_id(doc_id):
74
  """Delete a specific analysis by _id"""
75
+ try:
76
+ from bson import ObjectId
77
+ client, db, collection = get_database_connection()
78
+ result = collection.delete_one({"_id": ObjectId(doc_id)})
79
+ return result.deleted_count > 0
80
+ except:
81
+ return False