Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
murilommen committed Jul 27, 2023
1 parent 68a0f1e commit f678945
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
14 changes: 9 additions & 5 deletions tests/monitor/manager/test_monitor_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ def test_exclude_target_columns(monitor_setup):

monitor_setup.apply()

assert isinstance(monitor_setup.config.target_matrix, ColumnMatrix)
assert monitor_setup.config.target_matrix.exclude == ["prediction_temperature"]
assert isinstance(monitor_setup.target_matrix, ColumnMatrix)
assert monitor_setup.target_matrix.exclude == ["prediction_temperature"]

assert isinstance(monitor_setup.analyzer.targetMatrix, ColumnMatrix)
assert monitor_setup.analyzer.targetMatrix.exclude == ["prediction_temperature"]


def test_set_target_columns(monitor_setup):
Expand All @@ -55,9 +58,10 @@ def test_set_target_columns(monitor_setup):

monitor_setup.apply()

assert isinstance(monitor_setup.config.target_matrix, ColumnMatrix)
assert monitor_setup.config.target_matrix.include == ["prediction_temperature"]

assert isinstance(monitor_setup.target_matrix, ColumnMatrix)
assert monitor_setup.target_matrix.include == ["prediction_temperature"]
assert isinstance(monitor_setup.analyzer.targetMatrix, ColumnMatrix)
assert monitor_setup.analyzer.targetMatrix.include == ["prediction_temperature"]

def test_setup_apply(monitor_setup):
assert not monitor_setup.monitor
Expand Down
27 changes: 15 additions & 12 deletions whylabs_toolkit/helpers/monitor_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_monitor(
try:
monitor = api.get_monitor(org_id=org_id, dataset_id=dataset_id, monitor_id=monitor_id)
return monitor
except (NotFoundException, ForbiddenException):
except (ForbiddenException, NotFoundException):
logger.warning(
f"Could not find a monitor with id {monitor_id} for {dataset_id}." "Did you set a correct WHYLABS_API_KEY?"
)
Expand All @@ -51,18 +51,20 @@ def get_analyzer_ids(
dataset_id = dataset_id or config.get_default_dataset_id()
try:
monitor_config = get_monitor_config(org_id=org_id, dataset_id=dataset_id, config=config)
for item in monitor_config["monitors"]:
if item["id"] == monitor_id:
resp = item["analyzerIds"]
return resp
except (ForbiddenException, NotFoundException):

if monitor_config:
for item in monitor_config.get("monitors"):
if item["id"] == monitor_id:
resp = item["analyzerIds"]
return resp
except ForbiddenException:
logger.warning(f"Could not find analyzer IDs for {org_id}, {dataset_id}, {monitor_id}")
return None


def get_analyzers(
monitor_id: str, org_id: Optional[str] = None, dataset_id: Optional[str] = None, config: Config = Config()
) -> List[Any]:
) -> Optional[List[Any]]:
org_id = org_id or config.get_default_org_id()
dataset_id = dataset_id or config.get_default_dataset_id()
api = get_monitor_api(config=config)
Expand All @@ -73,7 +75,7 @@ def get_analyzers(
analyzers.append(api.get_analyzer(org_id=org_id, dataset_id=dataset_id, analyzer_id=analyzer))
return analyzers
else:
raise NotFoundException
return None


def get_model_granularity(
Expand All @@ -91,10 +93,10 @@ def get_model_granularity(
"W": Granularity.weekly,
"M": Granularity.monthly,
}

for key, value in time_period_to_gran.items():
if key in model_meta["time_period"]:
return value
if model_meta:
for key, value in time_period_to_gran.items():
if key in model_meta["time_period"]:
return value
return None


Expand All @@ -115,4 +117,5 @@ def delete_monitor(
resp_monitor = api.delete_monitor(org_id=org_id, dataset_id=dataset_id, monitor_id=monitor_id)
logger.debug(f"Deleted monitor with Resp:{resp_monitor}")
except ApiValueError as e:
logger.error(f"Error deleting monitor {monitor_id}: {e.msg}")
raise e
32 changes: 16 additions & 16 deletions whylabs_toolkit/monitor/manager/monitor_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ def __init__(self, monitor_id: str, dataset_id: Optional[str] = None, config: Co
self._prefill_properties()

def _check_if_monitor_exists(self) -> Any:
try:
existing_monitor = get_monitor(
org_id=self.credentials.org_id,
dataset_id=self.credentials.dataset_id,
monitor_id=self.credentials.monitor_id,
config=self._config,
)
existing_monitor = get_monitor(
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!")
except NotFoundException:
else:
logger.info(f"Did not find a monitor with {self.credentials.monitor_id}, creating a new one.")
existing_monitor = None
return existing_monitor

def _check_if_analyzer_exists(self) -> Any:
try:
existing_analyzers = get_analyzers(
org_id=self.credentials.org_id,
dataset_id=self.credentials.dataset_id,
monitor_id=self.credentials.monitor_id,
config=self._config,
)
existing_analyzers = get_analyzers(
org_id=self.credentials.org_id,
dataset_id=self.credentials.dataset_id,
monitor_id=self.credentials.monitor_id,
config=self._config,
)
if existing_analyzers:
existing_analyzer = Analyzer.parse_obj(existing_analyzers[0]) # enforcing 1:1 relationship

except NotFoundException:
else:
existing_analyzer = None
return existing_analyzer

Expand Down

0 comments on commit f678945

Please sign in to comment.