Skip to content

Commit fbc4d42

Browse files
committed
✨ Refactor service key prefixes and enhance service type filtering in SQL queries
1 parent 7762849 commit fbc4d42

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

packages/models-library/src/models_library/function_services_catalog/_key_labels.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from typing import Final
22

33
from ..services import ServiceKey
4+
from ..services_regex import FRONTEND_SERVICE_KEY_PREFIX
45

56
# NOTE: due to legacy reasons, the name remains with 'frontend' in it but
67
# it now refers to a more general group: function sections that contains front-end services as well
7-
FUNCTION_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/frontend"
8+
FUNCTION_SERVICE_KEY_PREFIX: Final[str] = FRONTEND_SERVICE_KEY_PREFIX
89

910

1011
def is_function_service(service_key: ServiceKey) -> bool:

packages/models-library/src/models_library/services_regex.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
from typing import Final
33

4+
from models_library.services_enums import ServiceType
5+
46
PROPERTY_TYPE_RE = r"^(number|integer|boolean|string|ref_contentSchema|data:([^/\s,]+/[^/\s,]+|\[[^/\s,]+/[^/\s,]+(,[^/\s]+/[^/,\s]+)*\]))$"
57
PROPERTY_TYPE_TO_PYTHON_TYPE_MAP = {
68
"integer": int,
@@ -27,18 +29,32 @@
2729
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
2830
)
2931

32+
# Add key prefixes for dynamic and computational services
33+
DYNAMIC_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/dynamic"
3034
DYNAMIC_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
31-
r"^simcore/services/dynamic/"
35+
rf"^{DYNAMIC_SERVICE_KEY_PREFIX}/"
3236
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
3337
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
3438
)
35-
DYNAMIC_SERVICE_KEY_FORMAT = "simcore/services/dynamic/{service_name}"
36-
3739

38-
# Computational regex & format
40+
COMPUTATIONAL_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/comp"
3941
COMPUTATIONAL_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
40-
r"^simcore/services/comp/"
42+
rf"^{COMPUTATIONAL_SERVICE_KEY_PREFIX}/"
4143
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
4244
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
4345
)
44-
COMPUTATIONAL_SERVICE_KEY_FORMAT: Final[str] = "simcore/services/comp/{service_name}"
46+
47+
FRONTEND_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/frontend"
48+
49+
FRONTEND_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
50+
rf"^{FRONTEND_SERVICE_KEY_PREFIX}/"
51+
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
52+
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
53+
)
54+
55+
# Add service type prefixes mapping (moved from _services_sql.py)
56+
SERVICE_TYPE_PREFIXES = {
57+
ServiceType.COMPUTATIONAL: COMPUTATIONAL_SERVICE_KEY_PREFIX,
58+
ServiceType.DYNAMIC: DYNAMIC_SERVICE_KEY_PREFIX,
59+
ServiceType.FRONTEND: FRONTEND_SERVICE_KEY_PREFIX,
60+
}

services/catalog/src/simcore_service_catalog/repository/_services_sql.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import sqlalchemy as sa
44
from models_library.products import ProductName
5+
from models_library.services_regex import (
6+
SERVICE_TYPE_PREFIXES,
7+
)
58
from models_library.services_types import ServiceKey, ServiceVersion
69
from models_library.users import UserID
710
from simcore_postgres_database.models.groups import user_to_groups
@@ -118,7 +121,13 @@ def apply_services_filters(
118121
filters: ServiceFiltersDB,
119122
):
120123
if filters.service_type:
121-
stmt = stmt.where(services_meta_data.c.service_type == filters.service_type)
124+
prefix = SERVICE_TYPE_PREFIXES.get(filters.service_type)
125+
if prefix is None:
126+
msg = f"Undefined service type {filters.service_type}. Please update prefix expressions"
127+
raise ValueError(msg)
128+
prefix = prefix.rstrip("/") # safety
129+
stmt = stmt.where(services_meta_data.c.service_key.like(f"{prefix}/%"))
130+
return stmt
122131

123132

124133
def latest_services_total_count_stmt(

0 commit comments

Comments
 (0)