diff --git a/app/celery/automated_tasks/csh_fair.py b/app/celery/automated_tasks/csh_fair.py index f349bd6..9421d35 100644 --- a/app/celery/automated_tasks/csh_fair.py +++ b/app/celery/automated_tasks/csh_fair.py @@ -25,12 +25,10 @@ def is_doi(identifier: str): def incoperate_results(task_dict: dict, result: 'app.models.TaskStatus', test: bool): import app.models #dynamic import - - print("incoperate results!") + session_id = task_dict["session_id"] task_id = task_dict["id"] - print(config.celery_key) status = app.models.TaskStatusIn( status= app.models.TaskStatus(result), force_update=config.celery_key ) @@ -42,12 +40,32 @@ def incoperate_results(task_dict: dict, result: 'app.models.TaskStatus', test: b return app.models.TaskStatus(result) else: url = f"http://{config.backend_url}:{config.backend_port}/session/{session_id}/tasks/{task_id}" - print(f"Patching {url}") - requests.patch( - url, - json=status.dict(), - ) + print(f"--Patching {url}", status.dict()) + try: + # Send PATCH request + response = requests.patch( + url, + json=status.dict(), + ) + response.raise_for_status() # Raise exception for non-2xx response status codes + print("---->", response.text, "<----") + print("PATCH request successful") + + except requests.RequestException as e: + # Handle request-related exceptions + print(f"Error sending PATCH request: {e}") + + # Optionally, raise the exception to propagate it further + # raise + + except Exception as e: + # Handle other types of exceptions + print(f"An unexpected error occurred: {e}") + # Optionally, raise the exception to propagate it further + # raise + + # Does not work because celery does not have access to fair_indicators # routers.update_task(session_id, task_id, status) @@ -56,7 +74,6 @@ def incoperate_results(task_dict: dict, result: 'app.models.TaskStatus', test: b @app.task def csh_f1_2_globally_unique_identifier(task_dict: dict, data: dict, test: bool = False): - print(" ") """ Representation of celery task to evaluate an assessment. These celery tasks should be in the format: @@ -79,7 +96,7 @@ def assessment_task(task_dict: dict, data: dict) -> None: :return: None """ - + print("CHECK!! globally uni ") identifier = check_route(data, ["resource", "resource_identifier"]) print("grabbed identifier: ", identifier) #could also retrive "type" from data instead of using .startswith @@ -91,9 +108,7 @@ def assessment_task(task_dict: dict, data: dict) -> None: result = "success" else: result = "failed" - - print("!!!!!!!!!!!!! ---- ", result) - + incoperate_results(task_dict, result, test) @@ -203,6 +218,7 @@ def csh_i3_03_qual_ref_other_metadata(task_dict: dict, data: dict, test: bool = result = "success" if(res.get('relationType') == None): result = "failed" + print("sollte jetzt das ergebnis in die session schreiben") incoperate_results(task_dict, result, test) @app.task @@ -224,6 +240,7 @@ def csh_i3_04_qual_ref_other_data(task_dict: dict, data: dict, test: bool = Fals ##### Reusability @app.task def csh_r1_1_01_has_reuse_license(task_dict: dict, data: dict, test: bool = False): + print("CHECK! has reuse license") resource_type = check_route(data, ["resource", "classification", "type"]) if(resource_type == False): resource_type = check_route(data, ["resource", "resource_classification", "resource_type"]) if(resource_type in ["Study", "Substudy/Data collection event"]): diff --git a/app/metrics/metrics.csv b/app/metrics/metrics.csv index 3849a16..7f31615 100644 --- a/app/metrics/metrics.csv +++ b/app/metrics/metrics.csv @@ -83,29 +83,30 @@ "CA-RDA-R1.3-02MM","Essential","Metadata of model is expressed in compliance with a machine-understandable community standard","This indicator requires that the model metadata follows a community standard that has a machine-understandable expression","This indicator can be evaluated by verifying that the community standard used f or the metadata has a machine-understandable expression" "CA-RDA-R1.3-03MA","Important","Metadata of archive is expressed in compliance with a machine-understandable cross-community standard","This indicator requires that the archive metadata follows a cross-community standard that has a machine-understandable expression","This indicator can be evaluated by verifying that the cross-community standard used f or the metadata has a machine-understandable expression" "CA-RDA-R1.3-03MM","Important","Metadata of model is expressed in compliance with a machine-understandable cross-community standard","This indicator requires that the model metadata follows a cross-community standard that has a machine-understandable expression","This indicator can be evaluated by verifying that the cross-community standard used f or the metadata has a machine-understandable expression" -"CSH-RDA-F1-01M","Essential","Metadata is identified by a persistent identifier","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-F1-02M","Essential","Metadata is identified by a globally unique identifier","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-F2-01M","Essential","Rich metadata is provided to allow discovery","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-F3-01M","Essential","Metadata includes the identifier for the data","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-F4-01M","Essential","Metadata is offered in such a way that it can be harvested and indexed","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A1-01M","Important","Metadata contains information to enable the user to get access to the data","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A1-02M","Essential","Metadata can be accessed manually","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A1-03M","Essential","Metadata identifier resolves to a metadata record","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A1-04M","Essential","Metadata is accessed through standardised protocol","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A1.1-01M","Essential","Metadata is accessible through a free access protocol","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-A2-01M","Essential","Metadata is guaranteed to remain available after data is no longer available","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I1-01M","Important","Metadata uses knowledge representation expressed in standardised format","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I1-02M","Important","Metadata uses machine-understandable knowledge representation","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I2-01M","Important","Metadata uses FAIR-compliant vocabularies","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I3-01M","Important","Metadata includes references to other metadata","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I3-02M","Useful","Metadata includes references to other data","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I3-03M","Important","Metadata includes qualified references to other metadata","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-I3-04M","Useful","Metadata includes qualified references to other data","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1-01M","Essential","Plurality of accurate and relevant attributes are provided to allow reuse","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.1-01M","Essential","Metadata includes information about the licence under which the data can be reused","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.1-02M","Important","Metadata refers to a standard reuse licence","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.1-03M","Important","Metadata refers to a machine-understandable reuse licence","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.2-01M","Important","Metadata includes provenance information according to community-specific standards","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.2-02M","Useful","Metadata includes provenance information according to a cross-community language","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.3-01M","Essential","Metadata complies with a community standard","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" -"CSH-RDA-R1.3-02M","Essential","Metadata is expressed in compliance with a machine-understandable community standard","---Add a description about the indicator (probably adapt from RDA schema)---","Add an explanation on how this indicator is technically evaluated" \ No newline at end of file + +"CSH-RDA-F1-01M","Essential","Metadata is identified by a persistent identifier","This indicator examines whether the metadata for the resource uploaded to the NFDI4Health study hub is assigned a persistent identifier. The use of a persistent identifier is crucial as it ensures the long-term findability of this specific metadata, significantly reducing the likelihood of encountering broken links and enhancing the resource's accessibility over time","The CSH demands an ID (resource.identifier) when uploading metadata. It can be generated by the study hub or entered based on a schema, such as DOI and DRKS. Thus the indicator is implicitly passed." +"CSH-RDA-F1-02M","Essential","Metadata is identified by a globally unique identifier","The identifier used for the metadata is listed in a registry service","The CSH demands an ID (resource.identifier) when uploading metadata. It can be generated by the study hub or entered based on a schema, such as DOI and DRKS. Thus the indicator is implicitly passed." +"CSH-RDA-F2-01M","Essential","Rich metadata is provided to allow discovery","The repository provides evidence that resource discovery metadata is sufficient for their designated community of users","Check if the mandatory attributes exist and have, if applicable, allowed values." +"CSH-RDA-F3-01M","Essential","Metadata includes the identifier for the data","The indicator deals with the inclusion of the reference (i.e. the identifier) of the resources in the metadata so that the resources can be accessed. The FAIR principle F3, which emphasizes that metadata should clearly and explicitly include the identifier of the data they describe, is broadly applicable to any type of resource that can be distinctly identified and referenced. This principle ensures that each piece of metadata is directly linked to its corresponding data or document, thereby enhancing the findability and accessibility of research resources.","Check if the list of resource ids contains an identifier that either has the relation “A describes B” or “A is metadata for B”. Also evaluate if the provided identifier follows an commonly accepted identifier scheme and is registered." +"CSH-RDA-F4-01M","Essential","Metadata is offered in such a way that it can be harvested and indexed","This metric pertains to the methods by which metadata is made accessible or delivered in a standardized and machine-readable format. To evaluate this metric, it is essential to grasp the functionalities provided by the data repository hosting the data. Metadata might be accessible through various channels. For instance, when resource is stored in a repository, this repository could distribute its metadata via a metadata harvesting protocol (such as OAI-PMH) and/or a web service.","This is a general indicator that has to be evaluated for the CSH as a platform. Metadata can be retrieved in a JSON format. The landing page provides a link in the footer that leads to the API. Thus, this indicator is implicitly passed." +"CSH-RDA-A1-01M","Important","Metadata contains information to enable the user to get access to the data or resources.","The indicator refers to the information that is necessary to allow the requester to gain access to the digital object. It is (i) about whether there are restrictions to access the data (i.e. access to the data may be open, restricted or closed), (ii) the actions to be taken by a person who is interested to access the data, in particular when the data has not been published on the Web and (iii) specifications that the resources are available through.","Check if the attribute chain design.dataSharingPlan.generally results in the value “Yes, there is a plan to share the ressource”. Beyond that, it is not feasible to evaluate the free text automatically. Thus, it is not possible to fully evaluate if this indicator can pass without human interaction." + +"CSH-RDA-A1-03M","Essential","Metadata identifier resolves to a metadata record","This metric pertains to the clarity of the metadata identifier. The identifier given to the metadata must be linked to a resolution service that facilitates access to the metadata document.The indicator refers to any human interactions that are needed if the requester wants to access the digital object. This FAIR principle refers mostly to automated interactions where a machine is able to access the digital object, but there may also be digital objects that require human interactions, such as clicking on a link on a landing page, sending an e-mail to the data owner, or even calling by telephone.","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable." +"CSH-RDA-A1-04M","Essential","Metadata is accessed through standardised protocol","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable.","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable." +"CSH-RDA-A1.1-01M","Essential","Metadata is accessible through a free access protocol","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable.","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable." +"CSH-RDA-A2-01M","Essential","Metadata is guaranteed to remain available after data is no longer available","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable.","In general the CSH provides the option to search for metadata based on its identifier. For sets that uploaded a check if the resource.identifier resolves via the logical URL. This indicator cannot be evaluated, if the FAIRness is evaluated during the creation of the metadata set. In this case the indicator is set as not applicable." +"CSH-RDA-I1-01M","Important","Metadata uses knowledge representation expressed in standardised format","The indicator serves to determine that an appropriate standard is used to express knowledge, for example, controlled vocabularies for subject classifications.","This is a general question. For each attribute the allowed values should be checked whether they adhere to a fitting vocabulary." +"CSH-RDA-I1-02M","Important","Metadata uses machine-understandable knowledge representation","This is a general question. For each attribute the allowed values should be checked whether they adhere to a fitting vocabulary.","Generally the CSH exports a metadata object as an JSON object upon API request. Thus, this indicator implicitly passes." +"CSH-RDA-I2-01M","Important","Metadata uses FAIR-compliant vocabularies","The indicator requires the vocabulary used for the metadata to conform to the FAIR principles, and at least be documented and resolvable using globally unique and persistent identifiers. The documentation needs to be easily findable and accessible.","Implicitly fulfilled for every resource registered in the CHSH (FHIR representation in SIMPLIFIER)" +"CSH-RDA-I3-01M","Important","Metadata includes references to other metadata","The indicator is about the way that metadata is connected to other metadata, for example through links to information about organizations, people, places, projects or time periods that are related to the digital object that the metadata describes.","Implement a check if identifier with a fitting relation type are resolvable for the registry of the specific scheme. Allowed schemes are DOI, URL, arXiv, EAN13, EISSN, Handle, ISBN, ISTC and LISSN. Most likely not all schemas can be used for metadata." +"CSH-RDA-I3-02M","Useful","Metadata includes references to other data","This indicator examines how metadata interlinks with additional data sources to enhance its context, such as connections to preceding or related research data.","Implement a check if identifiers with a fitting relation type are resolvable for the registry of the specific scheme. Allowed schemes are DOI, URL, arXiv, EAN13, EISSN, Handle, ISBN, ISTC and LISSN." +"CSH-RDA-I3-03M","Important","Metadata includes qualified references to other metadata","This measure focuses on how metadata links to additional metadata, such as descriptions of associated resources that offer further insight into the data. The connections must be explicitly defined, indicating the nature of the relationship with the related resource, such as specifying that person Y authored dataset X.","Implement a check if identifiers with a fitting relation type are resolvable for the registry of the specific scheme. Allowed schemes are DOI, URL, arXiv, EAN13, EISSN, Handle, ISBN, ISTC and LISSN." +"CSH-RDA-I3-04M","Useful","Metadata includes qualified references to other data","This indicator is about the way metadata is connected to other data, for example linking to previous or related research data that provides additional context to the data. Please note that this is not about the link from the metadata to the data it describes; that link is considered in principle F3 and in indicator RDA-F3-01M.","Implement a check if identifiers with a fitting relation type are resolvable for the registry of the specific scheme. Allowed schemes are DOI, URL, arXiv, EAN13, EISSN, Handle, ISBN, ISTC and LISSN." +"CSH-RDA-R1-01M","Essential","Plurality of accurate and relevant attributes are provided to allow reuse","The indicator concerns the quantity but also the quality of metadata provided in order to enhance data reusability.","Check the existence of all applicable attributes." +"CSH-RDA-R1.1-01M","Essential","Metadata includes information about the licence under which the data can be reused","This indicator is about the information that is provided in the metadata related to the conditions (e.g. obligations, restrictions) under which data can be reused. It about the presence of licensing information, making sure that anyone accessing the data understands the permissions and restrictions placed on the data by the data owner. Key Element: The presence of licensing information, but not necessarily in a standardized or machine-readable format.","Check the existence of all applicable attributes." +"CSH-RDA-R1.1-02M","Important","Metadata refers to a standard reuse licence","This indicator requires the reference to the conditions of reuse to be a standard license, rather than a locally defined license. This goes a step further by specifying that the license mentioned in the metadata should not be just any license, but a standard, widely recognized license. This ensures that the terms of use are well-understood and consistent, reducing ambiguity for users who want to reuse the data. Key Element: Use of a standard, widely-recognized license to ensure clarity and consistency in understanding the terms of reuse.","Check the label of the license whether it fits to the list above." +"CSH-RDA-R1.1-03M","Important","Metadata refers to a machine-understandable reuse licence","This indicator is about the way that the reuse license is expressed. Rather than being a human-readable text, the license should be expressed in such a way that it can be processed by machines, without human intervention, for example in automated searches. This is crucial for the automated processing of datasets, allowing software systems to understand and potentially enforce the licensing terms without human intervention. Key Element: The inclusion of license information in a format that can be understood and processed by machines, enabling automated systems to recognize and act on the licensing terms","Check whether the license refers to one of the following machine readable licenses: CC0 1.0, CC BY 4.0, CC BY-NC 4.0, CC BY-SA 4.0, CC BY-NC-SA 4.0" +"CSH-RDA-R1.2-01M","Important","Metadata includes provenance information according to community-specific standards","This indicator requires the metadata to include information about the provenance of the data, i.e. information about the origin, history or workflow that generated the data, in a way that is compliant with the standards that are used in the community in which the data is produced.","Check if the necessary attributes are provided in the metadata." +"CSH-RDA-R1.2-02M","Useful","Metadata includes provenance information according to a cross-community language","This indicator requires that the metadata provides provenance information according to a cross-domain language.","-" +"CSH-RDA-R1.3-01M","Essential","Metadata complies with a community standard","This indicator requires that metadata complies with community standards","The indicator addresses the general policy of the platform. Most likely it will implicitly pass." +"CSH-RDA-R1.3-02M","Essential","Metadata is expressed in compliance with a machine-understandable community standard","This indicator requires that the metadata follows a community standard that has a machine-understandable expression.","Generally, the CSH provides an export of the metadata in FHIR format. Thus, this indicator implicitly passes." \ No newline at end of file diff --git a/app/models/session.py b/app/models/session.py index d79dd06..d26b0ab 100644 --- a/app/models/session.py +++ b/app/models/session.py @@ -1,7 +1,11 @@ +import asyncio from uuid import uuid4 + +import requests from pydantic import BaseModel, HttpUrl, FileUrl, FilePath, validator, ValidationError from typing import Union, Optional from enum import Enum +#import asyncio from .tasks import ( Task, @@ -20,6 +24,9 @@ from .combine_object import CombineArchive +import time + + class SessionStatus(str, Enum): """ List of statuses for a user session: @@ -113,6 +120,8 @@ def necessary_data_provided(cls, subject_type: str, values: dict): if values.get("path") is None: raise ValueError("Url assessments need a url") elif subject_type is SubjectType.csh: + for value in values.items(): + print(value) if (values.get("metadata") is None): raise ValueError("CSH assessments need a JSON object") return subject_type @@ -205,6 +214,7 @@ def __init__(self, session: Session) -> None: self.assessed_data = self.retrieve_data(self.user_input.path) elif self.user_input.subject_type is SubjectType.csh: self.assessed_data = self.user_input.metadata + self._build_tasks_dict(list(self.session_model.tasks.values())) self.create_tasks() else: @@ -223,6 +233,22 @@ def from_user_input( :return: A SessionHandler object """ session = Session(id=session_id, session_subject=session_data) + #print(cls(session)) + """n = 0 + while session.status == "queued" and n < 2: + print("S-ID", session_id) + s_json = redis_app.json().get(f"session:{session_id}") + if s_json is not None: + subject = s_json.pop("session_subject") + session = Session(**s_json, session_subject=subject) + else: + print("ALERTA") + time.sleep(3) + + print(session.status) + print("^!!^") + n += 1 """ + return cls(session) @classmethod @@ -280,7 +306,11 @@ def from_csh(cls, session_id: str, session_data: SessionSubjectIn) -> "SessionHa :return: A SessionHandler object """ + session = Session(id=session_id, session_subject=session_data) + + print(session.id) + return cls(session) @@ -325,6 +355,13 @@ def is_running(self) -> bool: self.session_model.get_task(task_id) for task_id in self.indicator_tasks.values() ] + """for task in all_tasks: + if task.status is TaskStatus.queued: + print("QUEUED!", task.name) + elif task.status is TaskStatus.started: + print("Started!", task.name) """ + + return any( [ task.status is TaskStatus.queued or task.status is TaskStatus.started @@ -332,13 +369,18 @@ def is_running(self) -> bool: ] ) + def update_session_data(self): """ Calculate the different statistics of the session (scores, non_applicable tasks ratio, ...) :return: None. """ + print("Trying to update SCORE. Is session running? ", self.is_running()) if not self.is_running(): self.session_model.status = SessionStatus.finished + else: + print("HAE?") + all_tasks = [ self.session_model.get_task(task_key) @@ -358,6 +400,7 @@ def update_session_data(self): passed_applicable_nonessential = 0 passed_applicable_all = 0 + print("iterate through tasks...") for task in all_tasks: passed_all += task.score @@ -586,17 +629,18 @@ def update_task_children(self, task_key: str) -> None: child.status = default_status child.disabled = default_disabled - def start_automated_tasks(self): + async def start_automated_tasks(self): """Starts the assessment of automated tasks""" + print(">>>>>>>start_automated_tasks<<<<<<<<<<<") for task_id in self.indicator_tasks.values(): task = self.session_model.get_task(task_id) - print(task) + if isinstance(task, AutomatedTask): if self.user_input.subject_type is not SubjectType.csh: task.do_evaluate(self.assessed_data.dict()) else: task.do_evaluate(self.assessed_data) - + def json(self): """Returns the json representation of the session model""" return self.session_model.json() diff --git a/app/routers/router.py b/app/routers/router.py index fcfeac2..1c7498e 100644 --- a/app/routers/router.py +++ b/app/routers/router.py @@ -1,9 +1,13 @@ +import asyncio import uuid import os from shutil import copyfileobj +from app.models.session import SessionStatus from fastapi import APIRouter, HTTPException, UploadFile, Depends from typing import List, Optional from redis.exceptions import ResponseError +#import asyncio + from app.models import ( @@ -23,7 +27,7 @@ base_router = APIRouter() @base_router.post("/session", tags=["Sessions"]) -def create_session( +async def create_session( subject: SessionSubjectIn = Depends(SessionSubjectIn.as_form), uploaded_file: Optional[UploadFile] = None, metadata: Optional[object] = None @@ -74,7 +78,11 @@ def create_session( 422, "No JSON object was attached for assessment. Impossible to process query" ) try: - session_handler = SessionHandler.from_user_input(session_id, subject) + if subject.subject_type is SubjectType.csh: + session_handler = SessionHandler.from_csh(session_id, subject) + #session_handler.get(timeout=5) + else: + session_handler = SessionHandler.from_user_input(session_id, subject) except ValueError as e: raise HTTPException(422, str(e)) try: @@ -83,12 +91,33 @@ def create_session( "$", obj=session_handler.session_model.dict(), ) - session_handler.start_automated_tasks() + + if subject.subject_type is not SubjectType.csh: + session_handler.start_automated_tasks() + else: + async_tasks = session_handler.start_automated_tasks() + await async_tasks + await asyncio.sleep(2) + print("done with ASYNC TASK") + + except TypeError as e: print(session_handler.session_model) print(session_handler.session_model.dict()) raise e + if subject.subject_type is SubjectType.csh: + try: + s_json = redis_app.json().get(f"session:{session_id}") + if s_json is not None: + subject = s_json.pop("session_subject") + s = Session(**s_json, session_subject=subject) + return s + + except ResponseError as e: + print(f"An error occurred in Redis: {str(e)}") + raise HTTPException(status_code=404, detail="No session with this id was found") + return session_handler.session_model diff --git a/tests/local_request.py b/tests/local_request.py index 5601398..f327091 100644 --- a/tests/local_request.py +++ b/tests/local_request.py @@ -4,6 +4,7 @@ print("TESTING THE SERVER") + # Define the URL of the local server url = 'http://localhost:8000/session' @@ -131,18 +132,19 @@ # Check the response if response.status_code == 200: print("Request was successful.") - time.sleep(3) - print("wait 3 seconds") - #get session to look if its finished - while response.json()['status'] != 'finished': - time.sleep(3) - response = requests.get(url + '/' + response.json()['id'], ) - tasks = response.json()['tasks'] - for values in tasks.values(): - print(values['name'], ": ", values['status']) - print('status: ', response.json()['status']) + + if response.json()['status'] != 'finished': + print("not finished") + + tasks = response.json()['tasks'] + for values in tasks.values(): + print(values['name'], ": ", values['status']) + print('status: ', response.json()['status']) print(response.json()) + + #response = requests.get(url + '/' + response.json()['id'], ) + #print(response.json()) else: print("Request failed with status code:", response.status_code) print(response.text)