Skip to content

Commit 3a25b02

Browse files
fix: use workbench subquery
1 parent f4fd950 commit 3a25b02

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_projects_nodes.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from typing import Annotated, Any
55

66
import asyncpg.exceptions # type: ignore[import-untyped]
7-
import sqlalchemy
7+
import sqlalchemy as sa
88
import sqlalchemy.exc
99
from common_library.async_tools import maybe_await
1010
from common_library.basic_types import DEFAULT_FACTORY
1111
from common_library.errors_classes import OsparcErrorMixin
1212
from pydantic import BaseModel, ConfigDict, Field
1313
from simcore_postgres_database.utils_aiosqlalchemy import map_db_exception
1414
from sqlalchemy.dialects.postgresql import insert as pg_insert
15+
from sqlalchemy.sql.selectable import Subquery
1516

1617
from ._protocols import DBConnection
1718
from .aiopg_errors import ForeignKeyViolation, UniqueViolation
@@ -81,6 +82,53 @@ class ProjectNode(ProjectNodeCreate):
8182
model_config = ConfigDict(from_attributes=True)
8283

8384

85+
def make_workbench_subquery() -> Subquery:
86+
return (
87+
sa.select(
88+
projects_nodes.c.project_uuid,
89+
sa.func.json_object_agg(
90+
projects_nodes.c.node_id,
91+
sa.func.json_build_object(
92+
"key",
93+
projects_nodes.c.key,
94+
"version",
95+
projects_nodes.c.version,
96+
"label",
97+
projects_nodes.c.label,
98+
"progress",
99+
projects_nodes.c.progress,
100+
"thumbnail",
101+
projects_nodes.c.thumbnail,
102+
"inputAccess",
103+
projects_nodes.c.input_access,
104+
"inputNodes",
105+
projects_nodes.c.input_nodes,
106+
"inputs",
107+
projects_nodes.c.inputs,
108+
"inputsRequired",
109+
projects_nodes.c.inputs_required,
110+
"inputsUnits",
111+
projects_nodes.c.inputs_units,
112+
"outputNodes",
113+
projects_nodes.c.output_nodes,
114+
"outputs",
115+
projects_nodes.c.outputs,
116+
"runHash",
117+
projects_nodes.c.run_hash,
118+
"state",
119+
projects_nodes.c.state,
120+
"parent",
121+
projects_nodes.c.parent,
122+
"bootOptions",
123+
projects_nodes.c.boot_options,
124+
),
125+
).label("workbench"),
126+
)
127+
.group_by(projects_nodes.c.project_uuid)
128+
.subquery()
129+
)
130+
131+
84132
@dataclass(frozen=True, kw_only=True)
85133
class ProjectNodesRepo:
86134
project_uuid: uuid.UUID

services/web/server/src/simcore_service_webserver/projects/_project_document_service.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..resource_manager.registry import get_registry
3131
from ..resource_manager.service import list_opened_project_ids
3232
from ..socketio._utils import get_socket_server
33-
from . import _projects_nodes_repository, _projects_repository
33+
from . import _projects_repository
3434

3535
_logger = logging.getLogger(__name__)
3636

@@ -64,13 +64,9 @@ async def _create_project_document_and_increment_version() -> (
6464
- the project document and its version must be kept in sync
6565
"""
6666
# Get the full project with workbench for document creation
67-
project = await _projects_repository.get_project(
67+
project = await _projects_repository.get_project_with_workbench(
6868
app=app, project_uuid=project_uuid
6969
)
70-
project_nodes = await _projects_nodes_repository.get_by_project(
71-
app=app, project_id=project_uuid
72-
)
73-
workbench = {f"{node_id}": node for node_id, node in project_nodes}
7470

7571
# Create project document
7672
project_document = ProjectDocument(
@@ -83,7 +79,7 @@ async def _create_project_document_and_increment_version() -> (
8379
classifiers=project.classifiers,
8480
dev=project.dev,
8581
quality=project.quality,
86-
workbench=workbench,
82+
workbench=project.workbench,
8783
ui=project.ui,
8884
type=cast(ProjectTypeAPI, project.type),
8985
template_type=cast(ProjectTemplateType, project.template_type),

services/web/server/src/simcore_service_webserver/projects/_projects_repository.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pydantic import NonNegativeInt, PositiveInt
1616
from simcore_postgres_database.models.projects import projects
1717
from simcore_postgres_database.models.users import users
18+
from simcore_postgres_database.utils_projects_nodes import make_workbench_subquery
1819
from simcore_postgres_database.utils_repos import (
1920
get_columns_from_db_model,
2021
pass_or_acquire_connection,
@@ -122,7 +123,12 @@ async def get_project_with_workbench(
122123
project_uuid: ProjectID,
123124
) -> ProjectWithWorkbenchDBGet:
124125
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
125-
query = sql.select(*PROJECT_DB_COLS).where(projects.c.uuid == f"{project_uuid}")
126+
query = sql.select(
127+
*PROJECT_DB_COLS,
128+
sa.func.coalesce(
129+
make_workbench_subquery().c.workbench, sa.text("'{}'::json")
130+
).label("workbench"),
131+
).where(projects.c.uuid == f"{project_uuid}")
126132
result = await conn.execute(query)
127133
row = result.one_or_none()
128134
if row is None:

0 commit comments

Comments
 (0)