diff --git a/models/io/registry.py b/models/io/registry.py index 72bd4b9..4a29b21 100644 --- a/models/io/registry.py +++ b/models/io/registry.py @@ -125,6 +125,14 @@ class ModelRegistry: def status(self, domain: str, scenario: str, version: str) -> str: return self._status(self.get(domain, scenario, version)) + def in_production(self, domain: str) -> list[str]: + """The scenarios of `domain` with a version holding the `produccion` alias, by name.""" + if domain not in DOMAINS: + raise ValueError(f"dominio desconocido: {domain!r}") + prefix = registered_name(domain, "") + models = self.client.search_registered_models(filter_string=f"name LIKE '{prefix}%'") + return sorted(m.name.removeprefix(prefix) for m in models if PRODUCTION_ALIAS in (m.aliases or {})) + def resolve(self, domain: str, scenario: str, version: str | None = None) -> ResolvedVersion: """With no `version`, the one holding the `produccion` alias; with a version, that one, whatever its status (to test or compare a candidate). The status travels with it: confirming risks only diff --git a/tests/io/test_registry.py b/tests/io/test_registry.py index 535490c..d870f37 100644 --- a/tests/io/test_registry.py +++ b/tests/io/test_registry.py @@ -1,4 +1,4 @@ -"""What this module covers: the registry's promotion checks, promotion and retirement, the lineage of a version, and +"""What this module covers: the registry's promotion checks, promotion and retirement, the scenarios in production, the lineage of a version, and what its run stored (`registry.artifacts`, `registry.artifact`, `registry.routines`). Fixtures: `production_ready` (from `conftest.py`), a PROD version whose run recorded every routine and its model; @@ -108,6 +108,19 @@ def test_retire_and_lineage(production_ready): registry.retire(domain, scenario, second) +def test_in_production_lists_the_scenarios_holding_the_alias(production_ready): + """Only scenarios of the domain with a version in production are listed; a candidate or a retired one is not. + Fails if `in_production` stops reading the `produccion` alias or leaks another domain's models.""" + domain, scenario, version = production_ready("v1") + production_ready("v1-dx", scenario="diagnostico") + assert registry.in_production(domain) == [] + registry.promote(domain, scenario, version) + assert registry.in_production(domain) == [scenario] + assert registry.in_production("cama") == [] + registry.retire(domain, scenario, version) + assert registry.in_production(domain) == [] + + @pytest.mark.parametrize("domain, scenario", [("cacu' OR '1'='1", None), ("cacu", "x' OR name LIKE '%")]) def test_version_filters_reject_anything_but_known_names(mlflow_tracking, domain, scenario): with pytest.raises(ValueError):