Skip to content

Commit

Permalink
feature: list all monitor ids for a given dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
murilommen committed Dec 4, 2024
1 parent a411c51 commit 61374d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tests/helpers/test_monitor_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
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 == []
21 changes: 21 additions & 0 deletions whylabs_toolkit/helpers/monitor_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 61374d7

Please sign in to comment.