Skip to content

Commit 3c4ab1c

Browse files
solving the db issue and mongo db issue
1 parent 1e43049 commit 3c4ab1c

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

eegdash/api.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .logging import logger
3636
from .mongodb import MongoConnectionManager
3737
from .paths import get_default_cache_dir
38+
from .utils import _init_mongo_client
3839

3940

4041
class EEGDash:
@@ -72,11 +73,22 @@ def __init__(self, *, is_public: bool = True, is_staging: bool = False) -> None:
7273

7374
if self.is_public:
7475
DB_CONNECTION_STRING = mne.utils.get_config("EEGDASH_DB_URI")
76+
if not DB_CONNECTION_STRING:
77+
try:
78+
_init_mongo_client()
79+
DB_CONNECTION_STRING = mne.utils.get_config("EEGDASH_DB_URI")
80+
except Exception:
81+
DB_CONNECTION_STRING = None
7582
else:
7683
load_dotenv()
7784
DB_CONNECTION_STRING = os.getenv("DB_CONNECTION_STRING")
7885

7986
# Use singleton to get MongoDB client, database, and collection
87+
if not DB_CONNECTION_STRING:
88+
raise RuntimeError(
89+
"No MongoDB connection string configured. Set MNE config 'EEGDASH_DB_URI' "
90+
"or environment variable 'DB_CONNECTION_STRING'."
91+
)
8092
self.__client, self.__db, self.__collection = MongoConnectionManager.get_client(
8193
DB_CONNECTION_STRING, is_staging
8294
)
@@ -443,8 +455,11 @@ def add(self, record: dict):
443455
except ValueError as e:
444456
logger.error("Validation error for record: %s ", record["data_name"])
445457
logger.error(e)
446-
except:
447-
logger.error("Error adding record: %s ", record["data_name"])
458+
except Exception as exc:
459+
logger.error(
460+
"Error adding record: %s ", record.get("data_name", "<unknown>")
461+
)
462+
logger.debug("Add operation failed", exc_info=exc)
448463

449464
def _update_request(self, record: dict):
450465
"""Internal helper method to create a MongoDB update request for a record."""
@@ -463,8 +478,11 @@ def update(self, record: dict):
463478
self.__collection.update_one(
464479
{"data_name": record["data_name"]}, {"$set": record}
465480
)
466-
except: # silent failure
467-
logger.error("Error updating record: %s", record["data_name"])
481+
except Exception as exc: # log and continue
482+
logger.error(
483+
"Error updating record: %s", record.get("data_name", "<unknown>")
484+
)
485+
logger.debug("Update operation failed", exc_info=exc)
468486

469487
def exists(self, query: dict[str, Any]) -> bool:
470488
"""Alias for :meth:`exist` provided for API clarity."""
@@ -617,7 +635,7 @@ def __init__(
617635
self.records = records
618636
self.download = download
619637
self.n_jobs = n_jobs
620-
self.eeg_dash_instance = eeg_dash_instance or EEGDash()
638+
self.eeg_dash_instance = eeg_dash_instance
621639

622640
# Resolve a unified cache directory across code/tests/CI
623641
self.cache_dir = Path(cache_dir or get_default_cache_dir())
@@ -748,7 +766,8 @@ def __init__(
748766
)
749767
)
750768
elif self.query:
751-
# This is the DB query path that we are improving
769+
if self.eeg_dash_instance is None:
770+
self.eeg_dash_instance = EEGDash()
752771
datasets = self._find_datasets(
753772
query=build_query_from_kwargs(**self.query),
754773
description_fields=description_fields,

0 commit comments

Comments
 (0)