Skip to content

Commit d786c64

Browse files
review @pcrespov
1 parent fb2a8d1 commit d786c64

File tree

6 files changed

+93
-63
lines changed

6 files changed

+93
-63
lines changed

packages/models-library/src/models_library/api_schemas_webserver/computations.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
from datetime import datetime
2-
from typing import Annotated, Any, NamedTuple
2+
from typing import Annotated, Any
33

44
from common_library.basic_types import DEFAULT_FACTORY
55
from pydantic import (
66
BaseModel,
7+
ConfigDict,
78
Field,
8-
PositiveInt,
99
)
1010

1111
from ..api_schemas_directorv2.computations import (
1212
ComputationGet as _DirectorV2ComputationGet,
1313
)
14+
from ..basic_types import IDStr
1415
from ..projects import CommitID, ProjectID
1516
from ..projects_nodes_io import NodeID
1617
from ..projects_state import RunningState
17-
from ._base import InputSchemaWithoutCamelCase, OutputSchemaWithoutCamelCase
18+
from ..rest_ordering import OrderBy, create_ordering_query_model_class
19+
from ..rest_pagination import PageQueryParameters
20+
from ._base import (
21+
InputSchemaWithoutCamelCase,
22+
OutputSchema,
23+
OutputSchemaWithoutCamelCase,
24+
)
1825

1926

2027
class ComputationPathParams(BaseModel):
@@ -50,7 +57,32 @@ class ComputationStarted(OutputSchemaWithoutCamelCase):
5057
] = DEFAULT_FACTORY
5158

5259

53-
class ComputationRunRestGet(BaseModel):
60+
### Computation Run
61+
62+
63+
ComputationRunListOrderParams = create_ordering_query_model_class(
64+
ordering_fields={
65+
"submitted_at",
66+
"started_at",
67+
"ended_at",
68+
"state",
69+
},
70+
default=OrderBy(field=IDStr("submitted_at")),
71+
ordering_fields_api_to_column_map={
72+
"submitted_at": "created",
73+
"started_at": "started",
74+
"ended_at": "ended",
75+
},
76+
)
77+
78+
79+
class ComputationRunListQueryParams(
80+
PageQueryParameters,
81+
ComputationRunListOrderParams, # type: ignore[misc, valid-type]
82+
): ...
83+
84+
85+
class ComputationRunRestGet(OutputSchema):
5486
project_uuid: ProjectID
5587
iteration: int
5688
state: RunningState
@@ -60,21 +92,34 @@ class ComputationRunRestGet(BaseModel):
6092
ended_at: datetime | None
6193

6294

63-
class ComputationRunRestGetPage(NamedTuple):
64-
items: list[ComputationRunRestGet]
65-
total: PositiveInt
95+
### Computation Task
96+
97+
98+
class ComputationTaskPathParams(BaseModel):
99+
project_id: ProjectID
100+
model_config = ConfigDict(populate_by_name=True, extra="forbid")
101+
102+
103+
ComputationTaskListOrderParams = create_ordering_query_model_class(
104+
ordering_fields={
105+
"started_at",
106+
},
107+
default=OrderBy(field=IDStr("started_at")),
108+
ordering_fields_api_to_column_map={"started_at": "start"},
109+
)
66110

67111

68-
class ComputationTaskRestGet(BaseModel):
112+
class ComputationTaskListQueryParams(
113+
PageQueryParameters,
114+
ComputationTaskListOrderParams, # type: ignore[misc, valid-type]
115+
): ...
116+
117+
118+
class ComputationTaskRestGet(OutputSchema):
69119
project_uuid: ProjectID
70120
node_id: NodeID
71121
state: RunningState
72122
progress: float
73123
image: dict[str, Any]
74124
started_at: datetime | None
75125
ended_at: datetime | None
76-
77-
78-
class ComputationTaskRestGetPage(NamedTuple):
79-
items: list[ComputationTaskRestGet]
80-
total: PositiveInt

services/web/server/src/simcore_service_webserver/director_v2/_controller/_computations_rest_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Computation Run
88

9+
910
ComputationRunListOrderParams = create_ordering_query_model_class(
1011
ordering_fields={
1112
"submitted_at",

services/web/server/src/simcore_service_webserver/director_v2/_controller/computations_rest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ async def list_computations_latest_iteration(request: web.Request) -> web.Respon
6767

6868
page = Page[ComputationRunRestGet].model_validate(
6969
paginate_data(
70-
chunk=[ComputationRunRestGet(**task.dict()) for task in _get.items],
70+
chunk=[
71+
ComputationRunRestGet.model_validate(task, from_attributes=True)
72+
for task in _get.items
73+
],
7174
total=_get.total,
7275
limit=query_params.limit,
7376
offset=query_params.offset,
7477
request_url=request.url,
7578
)
7679
)
80+
7781
return web.Response(
7882
text=page.model_dump_json(**RESPONSE_MODEL_POLICY),
7983
content_type=MIMETYPE_APPLICATION_JSON,
@@ -111,7 +115,10 @@ async def list_computations_latest_iteration_tasks(
111115

112116
page = Page[ComputationTaskRestGet].model_validate(
113117
paginate_data(
114-
chunk=[ComputationTaskRestGet(**task.dict()) for task in _get.items],
118+
chunk=[
119+
ComputationTaskRestGet.model_validate(task, from_attributes=True)
120+
for task in _get.items
121+
],
115122
total=_get.total,
116123
limit=query_params.limit,
117124
offset=query_params.offset,

services/web/server/tests/unit/conftest.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77

88
import json
99
import sys
10-
from collections.abc import Callable, Iterable
10+
from collections.abc import AsyncIterator, Callable, Iterable
1111
from pathlib import Path
1212
from typing import Any
1313

1414
import pytest
1515
import yaml
16+
from aiohttp.test_utils import TestClient
1617
from models_library.products import ProductName
1718
from pytest_mock import MockFixture, MockType
18-
from pytest_simcore.helpers.webserver_projects import empty_project_data
19+
from pytest_simcore.helpers.webserver_login import UserInfoDict
20+
from pytest_simcore.helpers.webserver_projects import NewProject, empty_project_data
1921
from simcore_service_webserver.application_settings_utils import AppConfigDict
2022
from simcore_service_webserver.constants import FRONTEND_APP_DEFAULT
23+
from simcore_service_webserver.projects.models import ProjectDict
2124

2225
CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
2326

@@ -93,3 +96,23 @@ def disabled_setup_garbage_collector(mocker: MockFixture) -> MockType:
9396
@pytest.fixture(scope="session")
9497
def product_name() -> ProductName:
9598
return ProductName(FRONTEND_APP_DEFAULT)
99+
100+
101+
@pytest.fixture
102+
async def user_project(
103+
client: TestClient,
104+
fake_project: ProjectDict,
105+
logged_user: UserInfoDict,
106+
tests_data_dir: Path,
107+
osparc_product_name: str,
108+
) -> AsyncIterator[ProjectDict]:
109+
async with NewProject(
110+
fake_project,
111+
client.app,
112+
user_id=logged_user["id"],
113+
product_name=osparc_product_name,
114+
tests_data_dir=tests_data_dir,
115+
) as project:
116+
print("-----> added project", project["name"])
117+
yield project
118+
print("<----- removed project", project["name"])
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
# pylint: disable=redefined-outer-name
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
4-
from collections.abc import AsyncIterator
5-
from pathlib import Path
64

75
import pytest
8-
from aiohttp.test_utils import TestClient
9-
from pytest_simcore.helpers.webserver_login import UserInfoDict
10-
from pytest_simcore.helpers.webserver_projects import NewProject
11-
from simcore_service_webserver.projects.models import ProjectDict
126

137

148
@pytest.fixture
@@ -18,23 +12,3 @@ def app_environment(
1812
# NOTE: overrides app_environment
1913
monkeypatch.setenv("WEBSERVER_GARBAGE_COLLECTOR", "null")
2014
return app_environment | {"WEBSERVER_GARBAGE_COLLECTOR": "null"}
21-
22-
23-
@pytest.fixture
24-
async def user_project(
25-
client: TestClient,
26-
fake_project: ProjectDict,
27-
logged_user: UserInfoDict,
28-
tests_data_dir: Path,
29-
osparc_product_name: str,
30-
) -> AsyncIterator[ProjectDict]:
31-
async with NewProject(
32-
fake_project,
33-
client.app,
34-
user_id=logged_user["id"],
35-
product_name=osparc_product_name,
36-
tests_data_dir=tests_data_dir,
37-
) as project:
38-
print("-----> added project", project["name"])
39-
yield project
40-
print("<----- removed project", project["name"])

services/web/server/tests/unit/with_dbs/02/conftest.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,6 @@ def mock_catalog_api(
104104
}
105105

106106

107-
@pytest.fixture
108-
async def user_project(
109-
client: TestClient,
110-
fake_project: ProjectDict,
111-
logged_user: UserInfoDict,
112-
tests_data_dir: Path,
113-
osparc_product_name: str,
114-
) -> AsyncIterator[ProjectDict]:
115-
async with NewProject(
116-
fake_project,
117-
client.app,
118-
user_id=logged_user["id"],
119-
product_name=osparc_product_name,
120-
tests_data_dir=tests_data_dir,
121-
) as project:
122-
print("-----> added project", project["name"])
123-
yield project
124-
print("<----- removed project", project["name"])
125-
126-
127107
@pytest.fixture
128108
async def shared_project(
129109
client,

0 commit comments

Comments
 (0)