From 05a9ebb7a62608268e8b90fd79efda25571cc64c Mon Sep 17 00:00:00 2001 From: SergiAguilo Date: Fri, 31 Jan 2025 14:59:15 +0100 Subject: [PATCH] Change request dataset from omop (it was kind of hardcoded, to regular mongo) --- beacon/connections/omopcdm/datasets.py | 133 +++++++++++++++++++------ 1 file changed, 103 insertions(+), 30 deletions(-) diff --git a/beacon/connections/omopcdm/datasets.py b/beacon/connections/omopcdm/datasets.py index a214809..6885640 100644 --- a/beacon/connections/omopcdm/datasets.py +++ b/beacon/connections/omopcdm/datasets.py @@ -1,4 +1,12 @@ -from beacon.logs.logs import log_with_args +from beacon.connections.mongo.__init__ import client +from beacon.exceptions.exceptions import raise_exception +from beacon.logs.logs import log_with_args, log_with_args_mongo +from beacon.logs.logs import LOG + +from beacon.connections.mongo.utils import get_count, get_documents +from beacon.connections.mongo.request_parameters import apply_request_parameters +from beacon.connections.mongo.utils import get_docs_by_response_type, query_id + from beacon.conf.conf import level from typing import Optional from beacon.response.schemas import DefaultSchemas @@ -6,39 +14,104 @@ from beacon.connections.omopcdm.biosamples import get_biosamples from beacon.connections.omopcdm.individuals import get_individuals +@log_with_args_mongo(level) +def get_datasets(self): + try: + collection = client.beacon.datasets + query = {} + query = collection.find(query) + return query + except Exception as e:# pragma: no cover + err = str(e) + errcode=500 + raise_exception(err, errcode) -# There are no different datasets in OMOPCDM -@log_with_args(level) +@log_with_args_mongo(level) def get_full_datasets(self, entry_id: Optional[str], qparams: RequestParams): - schema = DefaultSchemas.DATASETS - response = [{'id':'cdm', 'name':'OMOP CDM dataset'}] - count = 1 - return response, count, schema + try: + collection = client.beacon.datasets + if entry_id == None: + query = {} + else:# pragma: no cover + query = {'id': entry_id} + count = get_count(self, client.beacon.datasets, query) + query = collection.find(query) + entity_schema = DefaultSchemas.DATASETS + response_converted = ( + [r for r in query] if query else [] + ) + return response_converted, count, entity_schema + except Exception as e:# pragma: no cover + err = str(e) + errcode=500 + raise_exception(err, errcode) -@log_with_args(level) +@log_with_args_mongo(level) def get_list_of_datasets(self): - datasets, count, schema = get_full_datasets(self, None, None) - return datasets + try: + datasets = get_datasets(self) + beacon_datasets = [ r for r in datasets ] + LOG.debug(beacon_datasets) + return beacon_datasets + except Exception as e:# pragma: no cover + err = str(e) + errcode=500 + raise_exception(err, errcode) -@log_with_args(level) +@log_with_args_mongo(level) def get_dataset_with_id(self, entry_id: Optional[str], qparams: RequestParams): + limit = qparams.query.pagination.limit + query_parameters, parameters_as_filters = apply_request_parameters(self, {}, qparams, entry_id) + if parameters_as_filters == True: + query, parameters_as_filters = apply_request_parameters(self, {}, qparams, entry_id)# pragma: no cover + else: + query={} + query = query_id(self, query, entry_id) schema = DefaultSchemas.DATASETS - if entry_id=='cdm': - response = [{'id':'cdm', 'name':'OMOP CDM dataset'}] - count = 1 - return response, count, schema - return {}, 0, schema - -@log_with_args(level) -def get_biosamples_of_dataset(self, entry_id: Optional[str], qparams: RequestParams, dataset: str): - schema = DefaultSchemas.BIOSAMPLES - if entry_id=='cdm': - return get_biosamples(self, None, qparams, dataset) - return schema, 0, 0, {}, dataset - -@log_with_args(level) -def get_individuals_of_dataset(self, entry_id: Optional[str], qparams: RequestParams, dataset: str): - schema = DefaultSchemas.INDIVIDUALS - if entry_id=='cdm': - return get_individuals(self, None, qparams, dataset) - return schema, 0, 0, {}, dataset \ No newline at end of file + count = get_count(self, client.beacon.datasets, query) + docs = get_documents(self, + client.beacon.datasets, + query, + qparams.query.pagination.skip, + qparams.query.pagination.skip*limit + ) + response_converted = ( + [r for r in docs] if docs else [] + ) + return response_converted, count, schema + +# # There are no different datasets in OMOPCDM +# @log_with_args(level) +# def get_full_datasets(self, entry_id: Optional[str], qparams: RequestParams): +# schema = DefaultSchemas.DATASETS +# response = [{'id':'cdm', 'name':'OMOP CDM dataset'}] +# count = 1 +# return response, count, schema + +# @log_with_args(level) +# def get_list_of_datasets(self): +# datasets, count, schema = get_full_datasets(self, None, None) +# return datasets + +# @log_with_args(level) +# def get_dataset_with_id(self, entry_id: Optional[str], qparams: RequestParams): +# schema = DefaultSchemas.DATASETS +# if entry_id=='cdm': +# response = [{'id':'cdm', 'name':'OMOP CDM dataset'}] +# count = 1 +# return response, count, schema +# return {}, 0, schema + +# @log_with_args(level) +# def get_biosamples_of_dataset(self, entry_id: Optional[str], qparams: RequestParams, dataset: str): +# schema = DefaultSchemas.BIOSAMPLES +# if entry_id=='cdm': +# return get_biosamples(self, None, qparams, dataset) +# return schema, 0, 0, {}, dataset + +# @log_with_args(level) +# def get_individuals_of_dataset(self, entry_id: Optional[str], qparams: RequestParams, dataset: str): +# schema = DefaultSchemas.INDIVIDUALS +# if entry_id=='cdm': +# return get_individuals(self, None, qparams, dataset) +# return schema, 0, 0, {}, dataset \ No newline at end of file