From e9e8653aefad7d93e177d621b463c69991fb13e4 Mon Sep 17 00:00:00 2001 From: murilommen Date: Thu, 4 Jul 2024 14:18:37 -0300 Subject: [PATCH] rephrase confusing message on monitor creation --- whylabs_toolkit/helpers/monitor_helpers.py | 20 ++++++++----- .../monitor/manager/monitor_setup.py | 28 ++++++++----------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/whylabs_toolkit/helpers/monitor_helpers.py b/whylabs_toolkit/helpers/monitor_helpers.py index 7fb0e59..7eee236 100644 --- a/whylabs_toolkit/helpers/monitor_helpers.py +++ b/whylabs_toolkit/helpers/monitor_helpers.py @@ -14,17 +14,20 @@ logger = logging.getLogger(__name__) -# TODO create deactivate_monitor - - def get_monitor_config(org_id: str, dataset_id: str, config: Config = Config()) -> Any: api = get_monitor_api(config=config) try: monitor_config = api.get_monitor_config_v3(org_id=org_id, dataset_id=dataset_id) + logger.info(f"Found monitor config for {dataset_id}") return monitor_config except NotFoundException: - logger.warning(f"Could not find a monitor config for {dataset_id}") + logger.info(f"Could not find a monitor config for {dataset_id}") return None + except ForbiddenException as e: + logger.warning( + f"You don't have access to monitor config for {dataset_id}. Did you set a correct WHYLABS_API_KEY?" + ) + raise e def get_monitor( @@ -37,11 +40,14 @@ def get_monitor( try: monitor = api.get_monitor(org_id=org_id, dataset_id=dataset_id, monitor_id=monitor_id) return monitor - except (ForbiddenException, NotFoundException): + except (NotFoundException): + logger.info(f"Didn't find a monitor with id {monitor_id} for {dataset_id}. Creating a new one...") + return None + except ForbiddenException as e: logger.warning( - f"Could not find a monitor with id {monitor_id} for {dataset_id}." "Did you set a correct WHYLABS_API_KEY?" + f"You don't have access to monitor {monitor_id} for {dataset_id}. Did you set a correct WHYLABS_API_KEY?" ) - return None + raise e def get_analyzer_ids( diff --git a/whylabs_toolkit/monitor/manager/monitor_setup.py b/whylabs_toolkit/monitor/manager/monitor_setup.py index 0c55e40..41cbc6c 100644 --- a/whylabs_toolkit/monitor/manager/monitor_setup.py +++ b/whylabs_toolkit/monitor/manager/monitor_setup.py @@ -7,7 +7,12 @@ from whylabs_toolkit.monitor.models import * from whylabs_toolkit.monitor.models.analyzer.targets import ColumnGroups from whylabs_toolkit.monitor.manager.credentials import MonitorCredentials -from whylabs_toolkit.helpers.monitor_helpers import get_analyzers, get_monitor, get_model_granularity +from whylabs_toolkit.helpers.monitor_helpers import ( + get_analyzers, + get_monitor, + get_model_granularity, + get_monitor_config, +) from whylabs_toolkit.helpers.config import Config @@ -54,20 +59,15 @@ def __init__(self, monitor_id: str, dataset_id: Optional[str] = None, config: Co self._prefill_properties() - def _check_if_monitor_exists(self) -> Any: - existing_monitor = get_monitor( + def _check_if_monitor_exists(self) -> Optional[Monitor]: + existing_monitor = get_monitor_config( org_id=self.credentials.org_id, dataset_id=self.credentials.dataset_id, monitor_id=self.credentials.monitor_id, config=self._config, ) - if existing_monitor: - existing_monitor = Monitor.parse_obj(existing_monitor) - logger.info(f"Got existing {self.credentials.monitor_id} from WhyLabs!") - else: - logger.info(f"Did not find a monitor with {self.credentials.monitor_id}, creating a new one.") - existing_monitor = None - return existing_monitor + if existing_monitor is not None: + return Monitor.parse_obj(existing_monitor) def _check_if_analyzer_exists(self) -> Any: existing_analyzers = get_analyzers( @@ -76,12 +76,8 @@ def _check_if_analyzer_exists(self) -> Any: monitor_id=self.credentials.monitor_id, config=self._config, ) - if existing_analyzers: - existing_analyzer = Analyzer.parse_obj(existing_analyzers[0]) # enforcing 1:1 relationship - - else: - existing_analyzer = None - return existing_analyzer + if existing_analyzers is not None: + return Analyzer.parse_obj(existing_analyzers[0]) # enforcing 1:1 relationship def _prefill_properties(self) -> None: if self.monitor: