Skip to content

Commit f762de7

Browse files
committed
adapting catalog
1 parent 6a9b289 commit f762de7

File tree

5 files changed

+58
-27
lines changed

5 files changed

+58
-27
lines changed

services/catalog/src/simcore_service_catalog/api/rpc/_services.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,24 @@ async def get_my_service_history(
237237
) -> PageRpcServiceRelease:
238238
assert app.state.engine # nosec
239239

240-
history = await services_api.get_service_history(
240+
total_count, items = await services_api.get_service_history(
241241
repo=ServicesRepository(app.state.engine),
242242
product_name=product_name,
243243
user_id=user_id,
244244
service_key=service_key,
245+
limit=limit,
246+
offset=offset,
247+
)
248+
249+
assert len(items) <= total_count # nosec
250+
assert len(items) <= limit # nosec
251+
252+
return cast(
253+
PageRpcServiceRelease,
254+
PageRpcServiceRelease.create(
255+
items,
256+
total=total_count,
257+
limit=limit,
258+
offset=offset,
259+
),
245260
)

services/catalog/src/simcore_service_catalog/db/repositories/_services_sql.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _has_access_rights(
111111
)
112112

113113

114-
def total_count_stmt(
114+
def latest_services_total_count_stmt(
115115
*,
116116
product_name: ProductName,
117117
user_id: UserID,
@@ -312,7 +312,11 @@ def get_service_history_stmt(
312312
user_id: UserID,
313313
access_rights: sa.sql.ClauseElement,
314314
service_key: ServiceKey,
315+
limit: int | None,
316+
offset: int | None,
315317
):
318+
assert offset is None, "UNDER DEV"
319+
assert limit is None, "UNDER DEV"
316320

317321
_sq = (
318322
sa.select(

services/catalog/src/simcore_service_catalog/db/repositories/services.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
can_get_service_stmt,
3939
get_service_history_stmt,
4040
get_service_stmt,
41+
latest_services_total_count_stmt,
4142
list_latest_services_stmt,
4243
list_services_stmt,
43-
total_count_stmt,
4444
)
4545

4646
_logger = logging.getLogger(__name__)
@@ -340,6 +340,8 @@ async def get_service_with_history(
340340
user_id=user_id,
341341
access_rights=AccessRightsClauses.can_read,
342342
service_key=key,
343+
limit=None,
344+
offset=None,
343345
)
344346
async with self.db_engine.begin() as conn:
345347
result = await conn.execute(stmt_history)
@@ -381,7 +383,7 @@ async def list_latest_services(
381383
) -> tuple[PositiveInt, list[ServiceWithHistoryDBGet]]:
382384

383385
# get page
384-
stmt_total = total_count_stmt(
386+
stmt_total = latest_services_total_count_stmt(
385387
product_name=product_name,
386388
user_id=user_id,
387389
access_rights=AccessRightsClauses.can_read,
@@ -438,24 +440,32 @@ async def get_service_history(
438440
user_id: UserID,
439441
# get args
440442
key: ServiceKey,
441-
) -> list[ReleaseDBGet] | None:
443+
# list args: pagination
444+
limit: int | None = None,
445+
offset: int | None = None,
446+
) -> tuple[PositiveInt, list[ReleaseDBGet]]:
442447

443-
stmt_history = get_service_history_stmt(
448+
stmt_total, stmt_history = get_service_history_stmt(
444449
product_name=product_name,
445450
user_id=user_id,
446451
access_rights=AccessRightsClauses.can_read,
447452
service_key=key,
453+
offset=offset,
454+
limit=limit,
448455
)
449456
async with self.db_engine.connect() as conn:
457+
result = await conn.execute(stmt_total)
458+
total_count = result.scalar() or 0
459+
450460
result = await conn.execute(stmt_history)
451461
row = result.one_or_none()
452462

453-
return (
454-
TypeAdapter(list[ReleaseDBGet]).validate_python(row.history)
455-
if row
456-
else None
463+
items = (
464+
TypeAdapter(list[ReleaseDBGet]).validate_python(row.history) if row else []
457465
)
458466

467+
return total_count, items
468+
459469
# Service Access Rights ----
460470

461471
async def get_service_access_rights(

services/catalog/src/simcore_service_catalog/services/services_api.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010
from models_library.groups import GroupID
1111
from models_library.products import ProductName
12-
from models_library.rest_pagination import PageLimitInt
12+
from models_library.rest_pagination import PageLimitInt, PageTotalCount
1313
from models_library.services_access import ServiceGroupAccessRightsV2
1414
from models_library.services_history import Compatibility, ServiceRelease
1515
from models_library.services_metadata_published import ServiceMetaDataPublished
@@ -125,7 +125,7 @@ async def list_latest_services(
125125
user_id: UserID,
126126
limit: PageLimitInt | None,
127127
offset: NonNegativeInt = 0,
128-
) -> tuple[NonNegativeInt, list[LatestServiceGet]]:
128+
) -> tuple[PageTotalCount, list[LatestServiceGet]]:
129129

130130
# defines the order
131131
total_count, services = await repo.list_latest_services(
@@ -472,26 +472,26 @@ async def get_service_history(
472472
product_name: ProductName,
473473
user_id: UserID,
474474
service_key: ServiceKey,
475+
limit: PageLimitInt | None = None,
476+
offset: NonNegativeInt | None = None,
475477
include_compatibility: bool = False,
476-
) -> list[ServiceRelease]:
477-
478-
history = (
479-
await repo.get_service_history(
480-
# NOTE: that the service history might be different for each user
481-
# since access-rights are defined on a k:v basis
482-
product_name=product_name,
483-
user_id=user_id,
484-
key=service_key,
485-
)
486-
or []
487-
)
478+
) -> tuple[PageTotalCount, list[ServiceRelease]]:
479+
total_count, history = await repo.get_service_history(
480+
# NOTE: that the service history might be different for each user
481+
# since access-rights are defined on a k:v basis
482+
product_name=product_name,
483+
user_id=user_id,
484+
key=service_key,
485+
limit=limit,
486+
offset=offset,
487+
) or (0, [])
488488

489489
compatibility_map = {}
490490
if include_compatibility:
491491
msg = "This operation is heavy and for the moment is not necessary"
492492
raise NotImplementedError(msg)
493493

494-
return [
494+
items = [
495495
# domain -> domain
496496
ServiceRelease.model_construct(
497497
version=h.version,
@@ -502,3 +502,5 @@ async def get_service_history(
502502
)
503503
for h in history
504504
]
505+
506+
return total_count, items

services/catalog/tests/unit/test_db_repositories_services_sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
can_get_service_stmt,
1010
get_service_history_stmt,
1111
get_service_stmt,
12+
latest_services_total_count_stmt,
1213
list_latest_services_stmt,
13-
total_count_stmt,
1414
)
1515

1616

@@ -70,7 +70,7 @@ def _check(func_smt, **kwargs):
7070
)
7171

7272
_check(
73-
total_count_stmt,
73+
latest_services_total_count_stmt,
7474
product_name=product_name,
7575
user_id=user_id,
7676
access_rights=AccessRightsClauses.can_read,

0 commit comments

Comments
 (0)