lomas_server package

Subpackages

Submodules

lomas_server.app module

lomas_server.app.dummy_opendp_query_handler(_request: Request, query_json: DummyOpenDPInp = Body({'dataset_name': 'PENGUIN', 'opendp_json': '{"version": "0.8.0", "ast": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "constructor", "func": "make_chain_tt", "module": "combinators", "args": [{"_type": "constructor", "func": "make_select_column", "module": "transformations", "kwargs": {"key": "bill_length_mm", "TOA": "String"}}, {"_type": "constructor", "func": "make_split_dataframe", "module": "transformations", "kwargs": {"separator": ",", "col_names": {"_type": "list", "_items": ["species", "island", "bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g", "sex"]}}}]}, "rhs": {"_type": "constructor", "func": "then_cast_default", "module": "transformations", "kwargs": {"TOA": "f64"}}}, "rhs": {"_type": "constructor", "func": "then_clamp", "module": "transformations", "kwargs": {"bounds": [30.0, 65.0]}}}, "rhs": {"_type": "constructor", "func": "then_resize", "module": "transformations", "kwargs": {"size": 346, "constant": 43.61}}}, "rhs": {"_type": "constructor", "func": "then_variance", "module": "transformations"}}, "rhs": {"_type": "constructor", "func": "then_laplace", "module": "measurements", "kwargs": {"scale": 5.0}}}}', 'fixed_delta': 1e-06, 'dummy_nb_rows': 100, 'dummy_seed': 42})) JSONResponse[source]

Handles queries on dummy datasets for the OpenDP library.

Parameters:
  • request (Request) – Raw request object.

  • query_json (DummyOpenDPInp, optional) –

    A JSON object containing the following: - opendp_pipeline: The OpenDP pipeline for the query. - fixed_delta: If the pipeline measurement is of type “ZeroConcentratedDivergence” (e.g. with “make_gaussian”) then

    it is converted to “SmoothedMaxDivergence” with “make_zCDP_to_approxDP” (see opendp measurements documentation at https://docs.opendp.org/en/stable/api/python/opendp.combinators.html#opendp.combinators.make_zCDP_to_approxDP). # noqa # pylint: disable=C0301 In that case a “fixed_delta” must be provided by the user.

    • dummy (bool, optional): Whether to use a dummy dataset (default: False).

    • nb_rows (int, optional): The number of rows in the dummy dataset (default: 100).

    • seed (int, optional): The random seed for generating the dummy dataset (default: 42).

    Defaults to Body(example_dummy_opendp).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – If there is not enough budget or the dataset does not exist.

Returns:

A JSON object containing:
  • query_response (pd.DataFrame): a DataFrame containing the query response.

Return type:

JSONResponse

lomas_server.app.dummy_smartnoise_sql_handler(_request: Request, query_json: DummySNSQLInp = Body({'query_str': 'SELECT COUNT(*) AS NB_ROW FROM df', 'dataset_name': 'PENGUIN', 'epsilon': 100.0, 'delta': 0.99, 'mechanisms': {'count': 'gaussian'}, 'postprocess': False, 'dummy_nb_rows': 100, 'dummy_seed': 42})) JSONResponse[source]

Handles queries on dummy datasets for the SmartNoiseSQL library.

Parameters:
  • request (Request) – Raw request object

  • query_json (DummySNSQLInp, optional) –

    A JSON object containing: - query: The SQL query to execute. NOTE: the table name is “df”,

    the query must end with “FROM df”.

    • epsilon (float): Privacy parameter (e.g., 0.1).

    • delta (float): Privacy parameter (e.g., 1e-5).

    • mechanisms (dict, optional): Dictionary of mechanisms for the query (default: {}). See Smartnoise-SQL mechanisms documentation https://docs.smartnoise.org/sql/advanced.html#overriding-mechanisms.

    • postprocess (bool, optional): Whether to postprocess the query results (default: True). See Smartnoise-SQL postprocessing documentation https://docs.smartnoise.org/sql/advanced.html#postprocess.

    • dummy (bool, optional): Whether to use a dummy dataset (default: False).

    • nb_rows (int, optional): The number of rows in the dummy dataset (default: 100).

    • seed (int, optional): The random seed for generating the dummy dataset (default: 42).

    Defaults to Body(example_dummy_smartnoise_sql).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – If there is not enough budget or the dataset does not exist.

Returns:

A JSON object containing:
  • query_response (pd.DataFrame): a DataFrame containing the query response.

Return type:

JSONResponse

lomas_server.app.estimate_opendp_cost(_request: Request, query_json: OpenDPInp = Body({'dataset_name': 'PENGUIN', 'opendp_json': '{"version": "0.8.0", "ast": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "constructor", "func": "make_chain_tt", "module": "combinators", "args": [{"_type": "constructor", "func": "make_select_column", "module": "transformations", "kwargs": {"key": "bill_length_mm", "TOA": "String"}}, {"_type": "constructor", "func": "make_split_dataframe", "module": "transformations", "kwargs": {"separator": ",", "col_names": {"_type": "list", "_items": ["species", "island", "bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g", "sex"]}}}]}, "rhs": {"_type": "constructor", "func": "then_cast_default", "module": "transformations", "kwargs": {"TOA": "f64"}}}, "rhs": {"_type": "constructor", "func": "then_clamp", "module": "transformations", "kwargs": {"bounds": [30.0, 65.0]}}}, "rhs": {"_type": "constructor", "func": "then_resize", "module": "transformations", "kwargs": {"size": 346, "constant": 43.61}}}, "rhs": {"_type": "constructor", "func": "then_variance", "module": "transformations"}}, "rhs": {"_type": "constructor", "func": "then_laplace", "module": "measurements", "kwargs": {"scale": 5.0}}}}', 'fixed_delta': 1e-06})) JSONResponse[source]

Estimates the privacy loss budget cost of an OpenDP query.

Parameters:
  • request (Request) – Raw request object

  • query_json (OpenDPInp, optional) –

    A JSON object containing the following: - “opendp_pipeline”: The OpenDP pipeline for the query.

    Defaults to Body(example_opendp).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist or the pipeline does not contain a measurement.

Returns:

A JSON object containing:
  • epsilon_cost (float): The estimated epsilon cost.

  • delta_cost (float): The estimated delta cost.

Return type:

JSONResponse

lomas_server.app.estimate_smartnoise_cost(_request: Request, query_json: SNSQLInpCost = Body({'query_str': 'SELECT COUNT(*) AS NB_ROW FROM df', 'dataset_name': 'PENGUIN', 'epsilon': 0.1, 'delta': 1e-05, 'mechanisms': {'count': 'gaussian'}})) JSONResponse[source]

Estimates the privacy loss budget cost of a SmartNoiseSQL query.

Parameters:
  • request (Request) – Raw request object

  • query_json (SNSQLInpCost, optional) –

    A JSON object containing the following: - query: The SQL query to estimate the cost for.

    NOTE: the table name is “df”, the query must end with “FROM df”.

    Defaults to Body(example_smartnoise_sql_cost).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist.

Returns:

A JSON object containing:
  • epsilon_cost (float): The estimated epsilon cost.

  • delta_cost (float): The estimated delta cost.

Return type:

JSONResponse

lomas_server.app.get_dataset_metadata(_request: Request, query_json: GetDbData = Body({'dataset_name': 'PENGUIN'})) JSONResponse[source]

Retrieves metadata for a given dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDbData, optional) – A JSON object containing the dataset_name key for indicating the dataset. Defaults to Body(example_get_admin_db_data).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

Returns:

The metadata dictionary for the specified

dataset_name.

Return type:

JSONResponse

lomas_server.app.get_dummy_dataset(_request: Request, query_json: GetDummyDataset = Body({'dataset_name': 'PENGUIN', 'dummy_nb_rows': 100, 'dummy_seed': 42})) StreamingResponse[source]

Generates and returns a dummy dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDummyDataset, optional) –

    A JSON object containing the following:
    • nb_rows (int, optional): The number of rows in the dummy dataset (default: 100).

    • seed (int, optional): The random seed for generating the dummy dataset (default: 42).

    Defaults to Body(example_get_dummy_dataset).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

Returns:

a pd.DataFrame representing the dummy dataset.

Return type:

StreamingResponse

lomas_server.app.get_initial_budget(_request: Request, query_json: GetDbData = Body({'dataset_name': 'PENGUIN'}), user_name: str = Header(None)) JSONResponse[source]

Returns the initial budget for a user and dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDbData, optional) –

    A JSON object containing: - dataset_name (str): The name of the dataset.

    Defaults to Body(example_get_admin_db_data).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist.

  • UnauthorizedAccessException – The user does not exist or the user does not have access to the dataset.

Returns:

a JSON object with:
  • initial_epsilon (float): initial epsilon budget.

  • initial_delta (float): initial delta budget.

Return type:

JSONResponse

async lomas_server.app.get_memory_usage() JSONResponse[source]

Return the dataset store object memory usage :param user_name: The user name. Defaults to Header(None). :type user_name: str, optional

Returns:

with DatasetStore object memory usage

Return type:

JSONResponse

lomas_server.app.get_remaining_budget(_request: Request, query_json: GetDbData = Body({'dataset_name': 'PENGUIN'}), user_name: str = Header(None)) JSONResponse[source]

Returns the remaining budget for a user and dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDbData, optional) –

    A JSON object containing: - dataset_name (str): The name of the dataset.

    Defaults to Body(example_get_admin_db_data).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist.

  • UnauthorizedAccessException – The user does not exist or the user does not have access to the dataset.

Returns:

a JSON object with:
  • remaining_epsilon (float): remaining epsilon budget.

  • remaining_delta (float): remaining delta budget.

Return type:

JSONResponse

async lomas_server.app.get_state(user_name: str = Header(None)) JSONResponse[source]

Returns the current state dict of this server instance.

Parameters:

user_name (str, optional) – The user name. Defaults to Header(None).

Returns:

The state of the server instance.

Return type:

JSONResponse

lomas_server.app.get_total_spent_budget(_request: Request, query_json: GetDbData = Body({'dataset_name': 'PENGUIN'}), user_name: str = Header(None)) JSONResponse[source]

Returns the spent budget for a user and dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDbData, optional) –

    A JSON object containing: - dataset_name (str): The name of the dataset.

    Defaults to Body(example_get_admin_db_data).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist.

  • UnauthorizedAccessException – The user does not exist or the user does not have access to the dataset.

Returns:

a JSON object with:
  • total_spent_epsilon (float): total spent epsilon budget.

  • total_spent_delta (float): total spent delta budget.

Return type:

JSONResponse

lomas_server.app.get_user_previous_queries(_request: Request, query_json: GetDbData = Body({'dataset_name': 'PENGUIN'}), user_name: str = Header(None)) JSONResponse[source]

Returns the query history of a user on a specific dataset.

Parameters:
  • request (Request) – Raw request object

  • query_json (GetDbData, optional) –

    A JSON object containing: - dataset_name (str): The name of the dataset.

    Defaults to Body(example_get_admin_db_data).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The dataset does not exist.

  • UnauthorizedAccessException – The user does not exist or the user does not have access to the dataset.

Returns:

A JSON object containing:
  • previous_queries (list[dict]): a list of dictionaries containing the previous queries.

Return type:

JSONResponse

lomas_server.app.lifespan(app: FastAPI) AsyncGenerator[source]

Lifespan function for the server.

This function is executed once on server startup, yields and finishes running at server shutdown.

Server initialization is performed (config loading, etc.) and the server state is updated accordingly. This can have potential side effects on the return values of the “depends” functions, which check the server state.

async lomas_server.app.middleware(request: Request, call_next: Callable[[Request], Response]) Response[source]

Adds delays to requests response to protect against timing attack

lomas_server.app.opendp_query_handler(_request: Request, query_json: OpenDPInp = Body({'dataset_name': 'PENGUIN', 'opendp_json': '{"version": "0.8.0", "ast": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "partial_chain", "lhs": {"_type": "constructor", "func": "make_chain_tt", "module": "combinators", "args": [{"_type": "constructor", "func": "make_select_column", "module": "transformations", "kwargs": {"key": "bill_length_mm", "TOA": "String"}}, {"_type": "constructor", "func": "make_split_dataframe", "module": "transformations", "kwargs": {"separator": ",", "col_names": {"_type": "list", "_items": ["species", "island", "bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g", "sex"]}}}]}, "rhs": {"_type": "constructor", "func": "then_cast_default", "module": "transformations", "kwargs": {"TOA": "f64"}}}, "rhs": {"_type": "constructor", "func": "then_clamp", "module": "transformations", "kwargs": {"bounds": [30.0, 65.0]}}}, "rhs": {"_type": "constructor", "func": "then_resize", "module": "transformations", "kwargs": {"size": 346, "constant": 43.61}}}, "rhs": {"_type": "constructor", "func": "then_variance", "module": "transformations"}}, "rhs": {"_type": "constructor", "func": "then_laplace", "module": "measurements", "kwargs": {"scale": 5.0}}}}', 'fixed_delta': 1e-06}), user_name: str = Header(None)) JSONResponse[source]

Handles queries for the OpenDP Library.

Parameters:
  • request (Request) – Raw request object.

  • query_json (OpenDPInp, optional) –

    A JSON object containing the following: - opendp_pipeline: The OpenDP pipeline for the query. - fixed_delta: If the pipeline measurement is of type

    ”ZeroConcentratedDivergence” (e.g. with “make_gaussian”) then it is converted to “SmoothedMaxDivergence” with “make_zCDP_to_approxDP” (see “opendp measurements documentation at https://docs.opendp.org/en/stable/api/python/opendp.combinators.html#opendp.combinators.make_zCDP_to_approxDP). # noqa # pylint: disable=C0301 In that case a “fixed_delta” must be provided by the user.

    Defaults to Body(example_opendp).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – The pipeline does not contain a “measurement”, there is not enough budget or the dataset does not exist.

  • UnauthorizedAccessException – A query is already ongoing for this user, the user does not exist or does not have access to the dataset.

Returns:

A JSON object containing the following:
  • requested_by (str): The user name.

  • query_response (pd.DataFrame): A DataFrame containing the query response.

  • spent_epsilon (float): The amount of epsilon budget spent for the query.

  • spent_delta (float): The amount of delta budget spent for the query.

Return type:

JSONResponse

async lomas_server.app.root()[source]

Redirect root endpoint to the state endpoint :returns: The state of the server instance. :rtype: JSONResponse

lomas_server.app.smartnoise_sql_handler(_request: Request, query_json: SNSQLInp = Body({'query_str': 'SELECT COUNT(*) AS NB_ROW FROM df', 'dataset_name': 'PENGUIN', 'epsilon': 0.1, 'delta': 1e-05, 'mechanisms': {'count': 'gaussian'}, 'postprocess': True}), user_name: str = Header(None)) JSONResponse[source]

Handles queries for the SmartNoiseSQL library.

Parameters:
  • request (Request) – Raw request object

  • query_json (SNSQLInp) –

    A JSON object containing: - query: The SQL query to execute. NOTE: the table name is “df”,

    the query must end with “FROM df”.

    Defaults to Body(example_smartnoise_sql).

  • user_name (str, optional) – The user name. Defaults to Header(None).

Raises:
  • ExternalLibraryException – For exceptions from libraries external to this package.

  • InternalServerException – For any other unforseen exceptions.

  • InvalidQueryException – If there is not enough budget or the dataset does not exist.

  • UnauthorizedAccessException – A query is already ongoing for this user, the user does not exist or does not have access to the dataset.

Returns:

A JSON object containing the following:
  • requested_by (str): The user name.

  • query_response (pd.DataFrame): A DataFrame containing the query response.

  • spent_epsilon (float): The amount of epsilon budget spent for the query.

  • spent_delta (float): The amount of delta budget spent for the query.

Return type:

JSONResponse

lomas_server.constants module

class lomas_server.constants.AdminDBType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Types of administration databases

MONGODB: str = 'mongodb'
YAML: str = 'yaml'
class lomas_server.constants.ConfigKeys(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Keys of the configuration file

DATASET_STORE: str = 'dataset_store'
DATASET_STORE_TYPE: str = 'ds_store_type'
DB: str = 'admin_database'
DB_TYPE: str = 'db_type'
DB_TYPE_MONGODB: str = 'mongodb'
DEVELOP_MODE: str = 'develop_mode'
DP_LIBRARY: str = 'dp_libraries'
LRU_DATASET_STORE_MAX_SIZE: str = 'max_memory_usage'
MONGODB_ADDR: str = 'address'
MONGODB_PORT: str = 'port'
RUNTIME_ARGS: str = 'runtime_args'
SERVER: str = 'server'
SETTINGS: str = 'settings'
SUBMIT_LIMIT: str = 'submit_limit'
TIME_ATTACK: str = 'time_attack'
class lomas_server.constants.DPLibraries(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Name of DP Library used in the query

OPENDP = 'opendp'
SMARTNOISE_SQL = 'smartnoise_sql'
class lomas_server.constants.DatasetStoreType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Types of classes to handle datasets in memory

BASIC: str = 'basic'
LRU: str = 'LRU_cache'
class lomas_server.constants.OpenDPDatasetInputMetric(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of opendp input metric for datasets see https://docs.opendp.org/en/stable/api/python/opendp.metrics.html see https://github.com/opendp/opendp/blob/main/rust/src/metrics/mod.rs

CHANGE_ONE_DISTANCE = 'ChangeOneDistance'
HAMMING_DISTANCE = 'HammingDistance'
INSERT_DELETE_DISTANCE = 'InsertDeleteDistance'
INT_DISTANCE = 'u32'
SYMMETRIC_DISTANCE = 'SymmetricDistance'
class lomas_server.constants.OpenDPMeasurement(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of divergence for opendp measurement see https://docs.opendp.org/en/stable/api/python/opendp.measurements.html

FIXED_SMOOTHED_MAX_DIVERGENCE = 'fixed_smoothed_max_divergence'
MAX_DIVERGENCE = 'max_divergence'
SMOOTHED_MAX_DIVERGENCE = 'smoothed_max_divergence'
ZERO_CONCENTRATED_DIVERGENCE = 'zero_concentrated_divergence'
class lomas_server.constants.PrivateDatabaseType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of Private Database for the private data

PATH = 'PATH_DB'
S3 = 'S3_DB'
class lomas_server.constants.TimeAttackMethod(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Possible methods against timing attacks

JITTER = 'jitter'
STALL = 'stall'

lomas_server.mongodb_admin module

lomas_server.mongodb_admin.add_dataset(db: Database, dataset_name: str, database_type: str, metadata_database_type: str, dataset_path: str | None = '', metadata_path: str | None = '', s3_bucket: str | None = '', s3_key: str | None = '', endpoint_url: str | None = '', aws_access_key_id: str | None = '', aws_secret_access_key: str | None = '', metadata_s3_bucket: str | None = '', metadata_s3_key: str | None = '', metadata_endpoint_url: str | None = '', metadata_aws_access_key_id: str | None = '', metadata_aws_secret_access_key: str | None = '') None[source]

Set a database type to a dataset in dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset_name (str) – Dataset name

  • database_type (str) – Type of the database

  • metadata_database_type (str) – Metadata database type

  • dataset_path (str) – Path to the dataset (for local db type)

  • metadata_path (str) – Path to metadata (for local db type)

  • s3_bucket (str) – S3 bucket name

  • s3_key (str) – S3 key

  • endpoint_url (str) – S3 endpoint URL

  • aws_access_key_id (str) – AWS access key ID

  • aws_secret_access_key (str) – AWS secret access key

  • metadata_s3_bucket (str) – Metadata S3 bucket name

  • metadata_s3_key (str) – Metadata S3 key

  • metadata_endpoint_url (str) – Metadata S3 endpoint URL

  • metadata_aws_access_key_id (str) – Metadata AWS access key ID

  • metadata_aws_secret_access_key (str) – Metadata AWS secret access key

Raises:

ValueError – If the dataset already exists or if the database type is unknown.

Returns:

None

lomas_server.mongodb_admin.add_dataset_to_user(db: Database, user: str, dataset: str, epsilon: float, delta: float) None[source]

Add dataset with initialized budget values to list of datasets that the user has access to. Will not add if already added (no error will be raised in that case).

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to check

  • dataset (str) – name of the dataset to add to user

  • epsilon (float) – epsilon value for initial budget of user

  • delta (float) – delta value for initial budget of user

Raises:

ValueError – _description_

Returns:

None

lomas_server.mongodb_admin.add_datasets_via_yaml(db: Database, yaml_file: str | Dict, clean: bool, overwrite_datasets: bool, overwrite_metadata: bool) None[source]

Set all database types to datasets in dataset collection based on yaml file.

Parameters:
  • db (Database) – mongo database object

  • yaml_file (Union[str, Dict]) – if str: a path to the YAML file location if Dict: a dictionnary containing the collection data

  • clean (bool) – Whether to clean the collection before adding.

  • overwrite_datasets (bool) – Whether to overwrite existing datasets.

  • overwrite_metadata (bool) – Whether to overwrite existing metadata.

Raises:

ValueError – If there are errors in the YAML file format.

Returns:

None

lomas_server.mongodb_admin.add_user(db: Database, user: str) None[source]

Add new user in users collection with initial values for all fields set by default.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be added

Raises:
  • ValueError – If the user already exists.

  • WriteConcernError – If the result is not acknowledged.

Returns:

None

lomas_server.mongodb_admin.add_user_with_budget(db: Database, user: str, dataset: str, epsilon: float, delta: float) None[source]

Add new user in users collection with initial values for all fields set by default.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be added

  • dataset (str) – name of the dataset to add to user

  • epsilon (float) – epsilon value for initial budget of user

  • delta (float) – delta value for initial budget of user

Raises:

ValueError – _description_

Returns:

None

lomas_server.mongodb_admin.add_users_via_yaml(db: Database, yaml_file: str | Dict, clean: bool, overwrite: bool) None[source]

Add all users from yaml file to the user collection

Parameters:
  • db (Database) – mongo database object

  • yaml_file (Union[str, Dict]) – if str: a path to the YAML file location if Dict: a dictionnary containing the collection data

  • clean (bool) – boolean flag True if drop current user collection False if keep current user collection

  • overwrite (bool) – boolean flag True if overwrite already existing users False errors if new values for already existing users

Returns:

None

lomas_server.mongodb_admin.check_dataset_and_metadata_exist(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user does not already exist in the user collection.

lomas_server.mongodb_admin.check_user_exists(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user does (not) exist in the user collection depending on the enforce_true parameter.

Parameters:

enforce_true (bool) – If set to True, the wrapper will enforce the user is already in the database. If set to False, it will enforce the user is NOT in the database.

Returns:

The wrapper function that enforces user presence

(or absence) before calling the suplied function.

Return type:

Callable

lomas_server.mongodb_admin.check_user_has_dataset(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user has access (or not) to the supplied dataset depending on the enforce_true parameter.

Parameters:

enforce_true (bool) – If set to true, the wrapper function will enforce the user has access to the dataset. If set to False, the wrapper function will enforce the user has NOT access to the specified dataset.

Returns:

The wrapper function that asserts user access (or not)

to the provided dataset.

Return type:

Callable

lomas_server.mongodb_admin.del_dataset(db: Database, dataset: str) None[source]

Delete dataset from dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – Dataset name to be deleted

Returns:

None

lomas_server.mongodb_admin.del_dataset_to_user(db: Database, user: str, dataset: str) None[source]

Remove if exists the dataset (and all related budget info) from list of datasets that user has access to.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to which to delete a dataset

  • dataset (str) – name of the dataset to remove from user

Returns:

None

lomas_server.mongodb_admin.del_user(db: Database, user: str) None[source]

Delete all related information for user from the users collection.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be deleted

Returns:

None

lomas_server.mongodb_admin.drop_collection(db: Database, collection: str) None[source]

Delete collection.

Parameters:
  • db (Database) – mongo database object

  • collection (str) – Collection name to be deleted.

Returns:

None

lomas_server.mongodb_admin.get_list_of_datasets(db: Database) list[source]

Get the list of all dataset is ‘datasets’ collection

Parameters:

db (Database) – mongo database object

Returns:

list of names of all datasets

Return type:

dataset_names (list)

lomas_server.mongodb_admin.get_list_of_datasets_from_user(db: Database, user: str) list[source]

Get the list of all datasets from the user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show archives

Returns:

list of names of all users

Return type:

user_datasets (list)

lomas_server.mongodb_admin.get_list_of_users(db: Database) list[source]

Get the list of all users is ‘users’ collection

Parameters:

db (Database) – mongo database object

Returns:

list of names of all users

Return type:

user_names (list)

lomas_server.mongodb_admin.set_budget_field(db: Database, user: str, dataset: str, field: str, value: float) None[source]

Set (for some reason) a budget field to a given value if given user exists and has access to given dataset.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to set budget to

  • dataset (str) – name of the dataset to set budget to

  • field (str) – one of ‘epsilon’ or ‘delta’

  • value (float) – value to set as epsilon or delta

Returns:

None

lomas_server.mongodb_admin.set_may_query(db: Database, user: str, value: bool) None[source]

Set (for some reason) the ‘may query’ field to a given value if given user exists.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to enable/disable

  • value (bool) – may query value (True or False)

Returns:

None

lomas_server.mongodb_admin.show_archives_of_user(db: Database, user: str) List[dict][source]

Show all previous queries from a user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show archives

Returns:

list of previous queries from the user

Return type:

archives (List)

lomas_server.mongodb_admin.show_collection(db: Database, collection: str) list[source]

Show a collection

Parameters:
  • db (Database) – mongo database object

  • collection (str) – Collection name to be shown.

Returns:

None

lomas_server.mongodb_admin.show_dataset(db: Database, dataset: str) dict[source]

Show a dataset from dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – name of the dataset to show

Returns:

informations about the dataset

Return type:

dataset_info (dict)

lomas_server.mongodb_admin.show_metadata_of_dataset(db: Database, dataset: str) dict[source]

Show a metadata from metadata collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – name of the dataset of the metadata to show

Returns:

informations about the metadata

Return type:

metadata (dict)

lomas_server.mongodb_admin.show_user(db: Database, user: str) dict[source]

Show a user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show

Returns:

all information of user from ‘users’ collection

Return type:

user (dict)

lomas_server.mongodb_admin_cli module

lomas_server.uvicorn_serve module

Module contents