From 61374d7e7f087f36d9439b183b77868692b8a02d Mon Sep 17 00:00:00 2001 From: murilommen Date: Wed, 4 Dec 2024 14:05:07 -0300 Subject: [PATCH] feature: list all monitor ids for a given dataset --- tests/helpers/test_monitor_helpers.py | 15 +++++++++++++-- whylabs_toolkit/helpers/monitor_helpers.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/helpers/test_monitor_helpers.py b/tests/helpers/test_monitor_helpers.py index 93a8e81..1d6dff7 100644 --- a/tests/helpers/test_monitor_helpers.py +++ b/tests/helpers/test_monitor_helpers.py @@ -6,7 +6,8 @@ get_model_granularity, get_monitor_config, get_analyzer_ids, - get_monitor + get_monitor, + list_monitors ) from whylabs_toolkit.helpers.utils import get_monitor_api from whylabs_toolkit.utils.granularity import Granularity @@ -182,4 +183,14 @@ def test_delete_monitor(self) -> None: assert MONITOR_ID not in monitor["id"] for analyzer in monitor_config["analyzers"]: - assert ANALYZER_ID not in analyzer["id"] \ No newline at end of file + assert ANALYZER_ID not in analyzer["id"] + + def test_list_monitors(self) -> None: + monitors = list_monitors(org_id=ORG_ID, dataset_id=DATASET_ID) + + assert monitors == [MONITOR_ID] + + def test_list_monitors_with_wrong_configs(self) -> None: + monitors = list_monitors(org_id=ORG_ID, dataset_id="model-doesnt-exist") + + assert monitors == [] \ No newline at end of file diff --git a/whylabs_toolkit/helpers/monitor_helpers.py b/whylabs_toolkit/helpers/monitor_helpers.py index 46a9cf8..0e5b18f 100644 --- a/whylabs_toolkit/helpers/monitor_helpers.py +++ b/whylabs_toolkit/helpers/monitor_helpers.py @@ -138,3 +138,24 @@ def delete_monitor( except ApiValueError as e: logger.error(f"Error deleting monitor {monitor_id}: {e.msg}") # type: ignore raise e + + +def list_monitors(org_id: Optional[str], dataset_id: Optional[str], config: Config = Config()) -> List[str]: + org_id = org_id or config.get_default_org_id() + dataset_id = dataset_id or config.get_default_dataset_id() + + try: + monitors = get_monitor_config(org_id=org_id, dataset_id=dataset_id) + if monitors is not None: + return [monitor["id"] for monitor in monitors.get("monitors")] + else: + logger.info(f"No monitors found for {dataset_id}") + return [] + except ForbiddenException as e: + logger.warning( + f"You don't have access to monitor list for {dataset_id}. Did you set a correct WHYLABS_API_KEY?" + ) + raise e + except Exception as e: + logger.error(f"Error listing monitors for {dataset_id}: {e}") + raise e \ No newline at end of file