Spaces:
Sleeping
Sleeping
Create database.py
Browse files- src/database.py +89 -0
src/database.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from pymongo import MongoClient
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 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,
|
| 41 |
+
offer_details: str,
|
| 42 |
+
target_audience: str,
|
| 43 |
+
specific_hook: str,
|
| 44 |
+
additional_context: str,
|
| 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
|