Skip to content

Commit 864aed4

Browse files
committed
drafts service layer
1 parent 9d62136 commit 864aed4

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

services/api-server/src/simcore_service_api_server/api/dependencies/services.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from fastapi import Depends, HTTPException, Request, status
77
from servicelib.rabbitmq import RabbitMQRPCClient
8+
from simcore_service_api_server._service_studies import StudiesService
89

910
from ..._service_solvers import SolverService
1011
from ...services_rpc.catalog import CatalogService
@@ -62,3 +63,11 @@ def get_solver_service(
6263
catalog_service=catalog_service,
6364
webserver_client=webserver_client,
6465
)
66+
67+
68+
def get_studies_service(
69+
webserver_client: Annotated[WbApiRpcClient, Depends(get_wb_api_rpc_client)],
70+
) -> StudiesService:
71+
return StudiesService(
72+
webserver_client=webserver_client,
73+
)

services/api-server/src/simcore_service_api_server/api/routes/studies_jobs.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
from models_library.projects_nodes import InputID, InputTypes
1515
from models_library.projects_nodes_io import NodeID
1616
from pydantic import PositiveInt
17+
from pydantic.types import PositiveInt
1718
from servicelib.logging_utils import log_context
19+
from simcore_service_api_server._service_studies import StudiesService
1820

1921
from ...exceptions.backend_errors import ProjectAlreadyStartedError
2022
from ...models.pagination import Page, PaginationParams
@@ -47,8 +49,8 @@
4749
from ...services_rpc.wb_api_server import WbApiRpcClient
4850
from ..dependencies.application import get_reverse_url_mapper
4951
from ..dependencies.authentication import get_current_user_id, get_product_name
50-
from ..dependencies.services import get_api_client
51-
from ..dependencies.webserver_http import get_webserver_session
52+
from ..dependencies.services import get_api_client, get_studies_service
53+
from ..dependencies.webserver_http import AuthSession, get_webserver_session
5254
from ..dependencies.webserver_rpc import (
5355
get_wb_api_rpc_client,
5456
)
@@ -57,9 +59,17 @@
5759
FMSG_CHANGELOG_NEW_IN_VERSION,
5860
create_route_description,
5961
)
60-
from .solvers_jobs import JOBS_STATUS_CODES
62+
from .solvers_jobs import (
63+
JOBS_STATUS_CODES,
64+
_compose_job_resource_name,
65+
)
66+
67+
# pylint: disable=too-many-arguments
68+
6169

6270
_logger = logging.getLogger(__name__)
71+
72+
6373
router = APIRouter()
6474

6575

@@ -85,6 +95,9 @@ def _compose_job_resource_name(study_key, job_id) -> str:
8595
async def list_study_jobs(
8696
study_id: StudyID,
8797
page_params: Annotated[PaginationParams, Depends()],
98+
studies_service: Annotated[StudiesService, Depends(get_studies_service)],
99+
url_for: Annotated[Callable, Depends(get_reverse_url_mapper)],
100+
product_name: Annotated[str, Depends(get_product_name)],
88101
):
89102
msg = f"list study jobs study_id={study_id!r} with pagination={page_params!r}. SEE https://github.com/ITISFoundation/osparc-simcore/issues/4177"
90103
raise NotImplementedError(msg)

0 commit comments

Comments
 (0)