userIdc2024 commited on
Commit
b4bb680
·
verified ·
1 Parent(s): 7395720

Update src/database.py

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