Spaces:
Running
Running
Update database/connections.py
Browse files- database/connections.py +17 -8
database/connections.py
CHANGED
|
@@ -6,14 +6,23 @@ import streamlit as st
|
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
def get_mongo_client():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def get_results_collection():
|
| 19 |
db = get_mongo_client()
|
|
|
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
def get_mongo_client():
|
| 9 |
+
try:
|
| 10 |
+
uri = cnf.MONGO_URI
|
| 11 |
+
dbname = cnf.MONGO_DB
|
| 12 |
+
|
| 13 |
+
# Check if required environment variables are set
|
| 14 |
+
if not uri or not dbname:
|
| 15 |
+
logger.warning("MongoDB configuration not found. Running without database.")
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
client = MongoClient(uri)
|
| 19 |
+
db = client[dbname]
|
| 20 |
+
db.command("ping")
|
| 21 |
+
logger.info("MongoDB connection established successfully")
|
| 22 |
+
return db
|
| 23 |
+
except Exception as e:
|
| 24 |
+
logger.error(f"MongoDB connection failed: {e}")
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
def get_results_collection():
|
| 28 |
db = get_mongo_client()
|