|
| 1 | +from models_library.products import ProductName |
| 2 | +from models_library.projects_nodes import Node |
| 3 | +from models_library.rest_pagination import ( |
| 4 | + MAXIMUM_NUMBER_OF_ITEMS_PER_PAGE, |
| 5 | + PageMetaInfoLimitOffset, |
| 6 | + PageOffsetInt, |
| 7 | +) |
| 8 | +from models_library.rpc_pagination import PageLimitInt |
| 9 | +from models_library.users import UserID |
| 10 | +from simcore_service_api_server.models.schemas.studies import StudyID |
| 11 | + |
| 12 | +from .models.api_resources import compose_resource_name |
| 13 | +from .models.schemas.jobs import Job, JobInputs |
| 14 | +from .services_http.solver_job_models_converters import ( |
| 15 | + create_job_inputs_from_node_inputs, |
| 16 | +) |
| 17 | +from .services_rpc.wb_api_server import WbApiRpcClient |
| 18 | + |
| 19 | +DEFAULT_PAGINATION_LIMIT = MAXIMUM_NUMBER_OF_ITEMS_PER_PAGE - 1 |
| 20 | + |
| 21 | + |
| 22 | +class StudiesService: |
| 23 | + _webserver_client: WbApiRpcClient |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + webserver_client: WbApiRpcClient, |
| 28 | + ): |
| 29 | + self._webserver_client = webserver_client |
| 30 | + |
| 31 | + async def list_jobs( |
| 32 | + self, |
| 33 | + *, |
| 34 | + product_name: ProductName, |
| 35 | + user_id: UserID, |
| 36 | + # filters |
| 37 | + study_id: StudyID | None = None, |
| 38 | + # pagination |
| 39 | + offset: PageOffsetInt = 0, |
| 40 | + limit: PageLimitInt = DEFAULT_PAGINATION_LIMIT, |
| 41 | + ) -> tuple[list[Job], PageMetaInfoLimitOffset]: |
| 42 | + """Lists all solver jobs for a user with pagination""" |
| 43 | + |
| 44 | + # 1. Compose job parent resource name prefix |
| 45 | + collection_or_resource_ids: list[str] = [ |
| 46 | + "study", # study_id, "jobs", |
| 47 | + ] |
| 48 | + if study_id: |
| 49 | + collection_or_resource_ids.append(f"{study_id}") |
| 50 | + |
| 51 | + job_parent_resource_name_prefix = compose_resource_name( |
| 52 | + *collection_or_resource_ids |
| 53 | + ) |
| 54 | + |
| 55 | + # 2. List projects marked as jobs |
| 56 | + projects_page = await self._webserver_client.list_projects_marked_as_jobs( |
| 57 | + product_name=product_name, |
| 58 | + user_id=user_id, |
| 59 | + offset=offset, |
| 60 | + limit=limit, |
| 61 | + job_parent_resource_name_prefix=job_parent_resource_name_prefix, |
| 62 | + ) |
| 63 | + |
| 64 | + # 3. Convert projects to jobs |
| 65 | + jobs: list[Job] = [] |
| 66 | + for project_job in projects_page.data: |
| 67 | + |
| 68 | + assert ( # nosec |
| 69 | + len(project_job.workbench) == 1 |
| 70 | + ), "Expected only one solver node in workbench" |
| 71 | + |
| 72 | + solver_node: Node = next(iter(project_job.workbench.values())) |
| 73 | + job_inputs: JobInputs = create_job_inputs_from_node_inputs( |
| 74 | + inputs=solver_node.inputs or {} |
| 75 | + ) |
| 76 | + assert project_job.job_parent_resource_name # nosec |
| 77 | + |
| 78 | + jobs.append( |
| 79 | + Job( |
| 80 | + id=project_job.uuid, |
| 81 | + name=Job.compose_resource_name( |
| 82 | + project_job.job_parent_resource_name, project_job.uuid |
| 83 | + ), |
| 84 | + inputs_checksum=job_inputs.compute_checksum(), |
| 85 | + created_at=project_job.created_at, |
| 86 | + runner_name=project_job.job_parent_resource_name, |
| 87 | + url=None, |
| 88 | + runner_url=None, |
| 89 | + outputs_url=None, |
| 90 | + ) |
| 91 | + ) |
| 92 | + |
| 93 | + return jobs, projects_page.meta |
0 commit comments