commonpower.benchmark.storage.BenchmarkMongoStorage

class BenchmarkMongoStorage[source]

Bases: BenchmarkStorage

A class that provides storage functionality for benchmarks using MongoDB. MongoDB connection information (host, port, username, password) must be provided as environment variables. Specifically the following environment variables must be set: - MONGO_HOST: The MongoDB host. - MONGO_PORT: The MongoDB port. - MONGO_USERNAME: The MongoDB username. - MONGO_PASSWORD: The MongoDB password.

Optionally, the following environment variables can be set: - MONGO_DB_NAME: The name of the MongoDB database. Default is “commonpower”. - MONGO_COLLECTION_NAME: The name of the MongoDB collection. Default is “benchmarks”.

save(benchmark_identifier

str, serialized_scenario: str, uri: str) -> None: Saves a benchmark to the MongoDB collection.

load(uri

str) -> str: Loads a benchmark from the MongoDB collection.

handle_data_sources(system_node

System, uri) -> None: Handles data sources for a system node and stores them in MongoDB.

delete(benchmark_hash

str) -> None: Deletes a benchmark from the MongoDB collection.

Methods

delete

Deletes a benchmark from the MongoDB collection.

handle_data_sources

Finds all data sources used in the system node and stores them in MongoDB with GridFS.

load

Loads a benchmark from the MongoDB collection.

save

Saves a benchmark to the MongoDB collection.

delete(benchmark_hash: str) None[source]

Deletes a benchmark from the MongoDB collection.

Parameters:

benchmark_hash (str) – The hash of the benchmark.

Raises:

FileNotFoundError – If the benchmark with the given hash is not found in MongoDB.

handle_data_sources(system_node: System, uri=None) None[source]

Finds all data sources used in the system node and stores them in MongoDB with GridFS.

Parameters:
  • system_node (System) – The system node object.

  • uri – Not used in this implementation but kept for compatibility with the base class.

Raises:

FileNotFoundError – If a data source for the benchmark is not found in MongoDB.

load(uri: str) str[source]

Loads a benchmark from the MongoDB collection.

Parameters:

uri (str) – The scenario hash of the benchmark to load.

Returns:

str – The serialized benchmark document.

Raises:

FileNotFoundError – If the benchmark with the given hash is not found in MongoDB or required data sources are missing.

save(benchmark_identifier: str, serialized_scenario: str, uri: str) None[source]

Saves a benchmark to the MongoDB collection.

Parameters:
  • benchmark_identifier (str) – Not used in this implementation but kept for compatibility with the base class.

  • serialized_scenario (str) – The serialized scenario data returned by jsonpickle.

  • uri (str) – Not used in this implementation but kept for compatibility with the base class.

Raises:

ValueError – If a benchmark with the same hash already exists in MongoDB.