Spaces:
Sleeping
Sleeping
Update src/database.py
Browse files- 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
MONGO_DB = os.getenv("MONGO_DB")
|
| 14 |
-
COLLECTION_NAME = os.getenv("MONGO_COLLECTION", "analyses")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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 |
-
|
| 67 |
-
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|