sacred icon indicating copy to clipboard operation
sacred copied to clipboard

Store url and database as part of MongoObserver

Open vnmabus opened this issue 2 years ago • 5 comments

I am trying to implement some code that creates the appropriate ExperimentLoader from incense given a particular observer. Unfortunately it seems that it is not possible to access the url and database name provided to MongoObserver, as they are not stored in the object itself. Would it be possible to keep that information around?

vnmabus avatar Apr 04 '22 18:04 vnmabus

Hey @vnmabus , are you still looking for this to be added?

At some point, you must be giving the URL to the MongoObserver. You would be free to add the URL there if that works for you.

url = "mongodb://sample:password@localhost:27017/?authMechanism=SCRAM-SHA-1"
ex = Experiment("add_url")
mongo_obs = MongoObserver.create(url=url, db_name="db")
mongo_obs.url = url
ex.observers.append(mongo_obs)

If that doesn't work for your use case, I'd be happy to put a PR up that stores the pymongo client as an instance attribute.

Gracecr avatar Jul 28 '22 14:07 Gracecr

Unfortunately the part of the code responsible from creating the observer is not under my control, so that is not possible.

vnmabus avatar Jul 28 '22 14:07 vnmabus

Interesting. I put up a PR to allow you to get the MongoClient with mongo_obs._client.

How is the observer getting created? Are you using another library to handle that part?

Gracecr avatar Jul 28 '22 16:07 Gracecr

No, my code IS a library. Given an observer and some ids, it needs to recover the results.

vnmabus avatar Jul 28 '22 16:07 vnmabus

Just to let you know, my actual, totally a hack solution to this problem is here:

    elif isinstance(storage, MongoObserver):
        database = storage.runs.database
        client = database.client
        url, port = list(
            client.topology_description.server_descriptions().keys(),
        )[0]

        return ExperimentLoader(
            mongo_uri=f"mongodb://{url}:{port}/",
            db_name=database.name,
            unpickle=False,
        )

I would rather write:

    elif isinstance(storage, MongoObserver):
        return ExperimentLoader(
            mongo_uri=f"mongodb://{storage.url}:{storage.port}/",
            db_name=database.name,
            unpickle=False,
        )

vnmabus avatar Aug 10 '22 19:08 vnmabus