Skip to content

Commit d757d71

Browse files
committed
šŸ› refactor: replace get_services_for_user_in_product with list_user_services_with_versions for improved clarity and caching
1 parent afe36d5 commit d757d71

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

ā€Žservices/web/server/src/simcore_service_webserver/catalog/_catalog_rest_client_service.pyā€Ž

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44
import urllib.parse
5-
from collections.abc import Callable, Iterator
5+
from collections.abc import Iterator
66
from contextlib import contextmanager
77
from typing import Any, Final
88

@@ -30,20 +30,11 @@
3030
from yarl import URL
3131

3232
from .._meta import api_version_prefix
33+
from ._models import ServiceKeyVersionDict
3334
from .settings import CatalogSettings, get_plugin_settings
3435

3536
_logger = logging.getLogger(__name__)
3637

37-
# Cache settings
38-
_SECOND = 1 # in seconds
39-
_MINUTE = 60 * _SECOND
40-
_CACHE_TTL: Final = 1 * _MINUTE
41-
42-
43-
def _create_service_cache_key(_f: Callable[..., Any], *_args, **kw):
44-
assert len(_args) == 1, f"Expected only app, got {_args}" # nosec
45-
return f"get_service_{kw['user_id']}_{kw['service_key']}_{kw['service_version']}_{kw['product_name']}"
46-
4738

4839
@contextmanager
4940
def _handle_client_exceptions(app: web.Application) -> Iterator[ClientSession]:
@@ -96,10 +87,24 @@ def to_backend_service(rel_url: URL, origin: URL, version_prefix: str) -> URL:
9687
return origin.with_path(new_path).with_query(rel_url.query)
9788

9889

99-
async def get_services_for_user_in_product(
100-
app: web.Application, user_id: UserID, product_name: str, *, only_key_versions: bool
101-
) -> list[dict]:
90+
# Cache settings for services rest API
91+
_SECOND = 1 # in seconds
92+
_MINUTE = 60 * _SECOND
93+
_CACHE_TTL: Final = 1 * _MINUTE
94+
95+
96+
@cached(
97+
ttl=_CACHE_TTL,
98+
key_builder=lambda _f, *_args, **kw: f"get_services_for_user_in_product_{kw['user_id']}_{kw['product_name']}",
99+
cache=Cache.MEMORY,
100+
)
101+
async def list_user_services_with_versions(
102+
app: web.Application, *, user_id: UserID, product_name: str
103+
) -> list[ServiceKeyVersionDict]:
104+
102105
settings: CatalogSettings = get_plugin_settings(app)
106+
only_key_versions = True
107+
103108
url = (URL(settings.api_base_url) / "services").with_query(
104109
{"user_id": user_id, "details": f"{not only_key_versions}"}
105110
)
@@ -115,13 +120,18 @@ async def get_services_for_user_in_product(
115120
user_id,
116121
)
117122
return []
118-
body: list[dict] = await response.json()
119-
return body
123+
services: list[dict] = await response.json()
124+
125+
# This reduces the size cached in the memory
126+
return [
127+
ServiceKeyVersionDict(key=service["key"], version=service["version"])
128+
for service in services
129+
]
120130

121131

122132
@cached(
123133
ttl=_CACHE_TTL,
124-
key_builder=_create_service_cache_key,
134+
key_builder=lambda _f, *_args, **kw: f"get_service_{kw['user_id']}_{kw['service_key']}_{kw['service_version']}_{kw['product_name']}",
125135
cache=Cache.MEMORY,
126136
# SEE https://github.com/ITISFoundation/osparc-simcore/pull/7802
127137
)
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# NOTE: missing. @bisgaard-itis will follow up here
1+
from typing import TypedDict
2+
3+
4+
class ServiceKeyVersionDict(TypedDict):
5+
key: str
6+
version: str

ā€Žservices/web/server/src/simcore_service_webserver/catalog/catalog_service.pyā€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
get_service,
33
get_service_access_rights,
44
get_service_resources,
5-
get_services_for_user_in_product,
65
is_catalog_service_responsive,
6+
list_user_services_with_versions,
77
to_backend_service,
88
)
9+
from ._models import ServiceKeyVersionDict
910
from ._service import batch_get_my_services
1011

1112
__all__: tuple[str, ...] = (
1213
"batch_get_my_services",
1314
"get_service",
1415
"get_service_access_rights",
1516
"get_service_resources",
16-
"get_services_for_user_in_product",
17+
"list_user_services_with_versions",
1718
"is_catalog_service_responsive",
1819
"to_backend_service",
20+
"ServiceKeyVersionDict",
1921
)
2022
# nopycln: file

ā€Žservices/web/server/src/simcore_service_webserver/projects/_crud_api_read.pyā€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ async def list_projects( # pylint: disable=too-many-arguments
120120
) -> tuple[list[ProjectDict], int]:
121121
db = ProjectDBAPI.get_from_app_context(app)
122122

123-
user_available_services: list[dict] = (
124-
await catalog_service.get_services_for_user_in_product(
125-
app, user_id, product_name, only_key_versions=True
123+
user_available_services: list[ServiceKeyVersionDict] = (
124+
await catalog_service.list_user_services_with_versions(
125+
app, user_id=user_id, product_name=product_name
126126
)
127127
)
128128

@@ -204,9 +204,9 @@ async def list_projects_full_depth(
204204
) -> tuple[list[ProjectDict], int]:
205205
db = ProjectDBAPI.get_from_app_context(app)
206206

207-
user_available_services: list[dict] = (
208-
await catalog_service.get_services_for_user_in_product(
209-
app, user_id, product_name, only_key_versions=True
207+
user_available_services: list[ServiceKeyVersionDict] = (
208+
await catalog_service.list_user_services_with_versions(
209+
app, user_id=user_id, product_name=product_name
210210
)
211211
)
212212

ā€Žservices/web/server/src/simcore_service_webserver/projects/utils.pyā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def is_graph_equal(
190190

191191

192192
def are_project_services_available(
193-
project: dict[str, Any], available_services: list[dict[str, Any]]
193+
project: dict[str, Any], available_services: list[dict[str, str]]
194194
) -> bool:
195195
if not project["workbench"]:
196196
# empty project

ā€Žservices/web/server/tests/unit/with_dbs/01/test_catalog_rest_client.pyā€Ž

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
)
1616
from simcore_service_webserver.catalog.catalog_service import (
1717
get_service_access_rights,
18-
get_services_for_user_in_product,
1918
is_catalog_service_responsive,
19+
list_user_services_with_versions,
2020
)
2121

2222

@@ -62,11 +62,10 @@ async def test_get_services_for_user_in_product(
6262
status=backend_status_code,
6363
)
6464
assert client.app
65-
_ = await get_services_for_user_in_product(
65+
await list_user_services_with_versions(
6666
app=client.app,
6767
user_id=logged_user["id"],
6868
product_name="osparc",
69-
only_key_versions=False,
7069
)
7170

7271

0 commit comments

Comments
Ā (0)