Skip to content

Commit 21bf4d0

Browse files
author
Sushanth Sathish Kumar
committed
test: add unit tests for dashboard generation and error handling
1 parent c002c45 commit 21bf4d0

File tree

3 files changed

+255
-70
lines changed

3 files changed

+255
-70
lines changed

src/sagemaker/model_monitor/model_monitoring.py

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,6 @@ def _check_dashboard_validity_without_checking_in_use(
15431543
self,
15441544
monitor_schedule_name,
15451545
enable_cloudwatch_metrics=True,
1546-
enable_automatic_dashboard=False,
15471546
dashboard_name=None,
15481547
):
15491548
"""Checks if the parameters are valid, without checking if dashboard name is taken
@@ -1552,44 +1551,33 @@ def _check_dashboard_validity_without_checking_in_use(
15521551
monitor_schedule_name (str): Monitoring schedule name.
15531552
enable_cloudwatch_metrics (bool): Whether to publish cloudwatch metrics as part of
15541553
the baselining or monitoring jobs.
1555-
enable_automatic_dashboard (bool): Whether to publish dashboard as part of
1556-
the monitoring job.
15571554
dashboard_name (str): The name to use when publishing dashboard
15581555
"""
1559-
if (not enable_cloudwatch_metrics) and enable_automatic_dashboard:
1556+
if not enable_cloudwatch_metrics:
15601557
message = (
15611558
"Could not create automatic dashboard. "
15621559
"Please set enable_cloudwatch_metrics to True."
15631560
)
15641561
_LOGGER.error(message)
15651562
raise ValueError(message)
15661563

1567-
if (not enable_automatic_dashboard) and dashboard_name is not None:
1564+
if dashboard_name is None:
1565+
dashboard_name = monitor_schedule_name
1566+
dashboard_name_validation = bool(re.match(r"^[0-9A-Za-z\-_]{1,255}$", dashboard_name))
1567+
if not dashboard_name_validation:
15681568
message = (
1569-
"Parameter dashboard_name was provided, but enable_automatic_dashboard "
1570-
"parameter was False. Dashboard will not be generated."
1569+
f"Dashboard name {dashboard_name} is not a valid dashboard name. "
1570+
"Dashboard name can be at most 255 characters long "
1571+
"and valid characters in dashboard names include '0-9A-Za-z-_'."
15711572
)
1572-
_LOGGER.warning(message)
1573-
1574-
if enable_automatic_dashboard:
1575-
# verify that the provided dashboard name is not taken
1576-
dashboard_name = monitor_schedule_name if dashboard_name is None else dashboard_name
1577-
1578-
dashboard_name_validation = bool(re.match(r"^[0-9A-Za-z\-_]{1,255}$", dashboard_name))
1579-
if not dashboard_name_validation:
1580-
message = (
1581-
f"Dashboard name {dashboard_name} is not a valid dashboard name. "
1582-
"Dashboard name can be at most 255 characters long "
1583-
"and valid characters in dashboard names include '0-9A-Za-z-_'."
1584-
)
1585-
_LOGGER.error(message)
1586-
raise ValueError(message)
1573+
_LOGGER.error(message)
1574+
raise ValueError(message)
15871575

15881576
def _check_automatic_dashboard_validity(
15891577
self,
1578+
cw_client,
15901579
monitor_schedule_name,
15911580
enable_cloudwatch_metrics=True,
1592-
enable_automatic_dashboard=False,
15931581
dashboard_name=None,
15941582
):
15951583
"""Checks if the parameters provided to generate an automatic dashboard are valid
@@ -1598,35 +1586,30 @@ def _check_automatic_dashboard_validity(
15981586
monitor_schedule_name (str): Monitoring schedule name.
15991587
enable_cloudwatch_metrics (bool): Whether to publish cloudwatch metrics as part of
16001588
the baselining or monitoring jobs.
1601-
enable_automatic_dashboard (bool): Whether to publish dashboard as part of
1602-
the monitoring job.
16031589
dashboard_name (str): The name to use when publishing dashboard
16041590
"""
16051591

16061592
self._check_dashboard_validity_without_checking_in_use(
16071593
monitor_schedule_name=monitor_schedule_name,
16081594
enable_cloudwatch_metrics=enable_cloudwatch_metrics,
1609-
enable_automatic_dashboard=enable_automatic_dashboard,
16101595
dashboard_name=dashboard_name,
16111596
)
1612-
cw_client = self.sagemaker_session.boto_session.client("cloudwatch")
16131597

1614-
if enable_automatic_dashboard:
1615-
try:
1616-
# try to access the dashboard to see if it exists already
1617-
cw_client.get_dashboard(DashboardName=dashboard_name)
1618-
message = (
1619-
f"Dashboard name {dashboard_name} is already in use. "
1620-
"Please provide a different dashboard name, or delete the already "
1621-
"existing dashboard."
1622-
)
1623-
_LOGGER.error(message)
1624-
raise ValueError(message)
1625-
except Exception as e:
1626-
_LOGGER.log(f"Correctly received error {e}.")
1627-
# in this case, the dashboard name is not in use
1628-
# and we are free to write to it without overwriting any
1629-
# customer data.
1598+
# flag to check if dashboard with name dashboard_name exists already
1599+
dashboard_exists = True
1600+
try:
1601+
cw_client.get_dashboard(DashboardName=dashboard_name)
1602+
except Exception as e:
1603+
dashboard_exists = False
1604+
1605+
if dashboard_exists:
1606+
message = (
1607+
f"Dashboard name {dashboard_name} is already in use. "
1608+
"Please provide a different dashboard name, or delete the already "
1609+
"existing dashboard."
1610+
)
1611+
_LOGGER.error(message)
1612+
raise ValueError(message)
16301613

16311614
def _create_monitoring_schedule_from_job_definition(
16321615
self,
@@ -2113,10 +2096,11 @@ def create_monitoring_schedule(
21132096
)
21142097

21152098
if enable_automatic_dashboard:
2099+
cw_client = self.sagemaker_session.boto_session.client("cloudwatch")
21162100
self._check_automatic_dashboard_validity(
2101+
cw_client=cw_client,
21172102
monitor_schedule_name=monitor_schedule_name,
21182103
enable_cloudwatch_metrics=enable_cloudwatch_metrics,
2119-
enable_automatic_dashboard=enable_automatic_dashboard,
21202104
dashboard_name=dashboard_name,
21212105
)
21222106

@@ -2214,8 +2198,8 @@ def update_monitoring_schedule(
22142198
env=None,
22152199
network_config=None,
22162200
enable_cloudwatch_metrics=None,
2217-
# enable_automatic_dashboard=False,
2218-
# dashboard_name=None,
2201+
enable_automatic_dashboard=False,
2202+
dashboard_name=None,
22192203
role=None,
22202204
batch_transform_input=None,
22212205
data_analysis_start_time=None,
@@ -2302,8 +2286,6 @@ def update_monitoring_schedule(
23022286
env=env,
23032287
network_config=network_config,
23042288
enable_cloudwatch_metrics=enable_cloudwatch_metrics,
2305-
# enable_automatic_dashboard=enable_automatic_dashboard,
2306-
# dashboard_name=dashboard_name,
23072289
role=role,
23082290
batch_transform_input=batch_transform_input,
23092291
data_analysis_start_time=data_analysis_start_time,
@@ -2425,8 +2407,6 @@ def _update_data_quality_monitoring_schedule(
24252407
statistics=None,
24262408
schedule_cron_expression=None,
24272409
enable_cloudwatch_metrics=None,
2428-
# enable_automatic_dashboard=False,
2429-
# dashboard_name=None,
24302410
role=None,
24312411
instance_count=None,
24322412
instance_type=None,
@@ -3307,10 +3287,11 @@ def create_monitoring_schedule(
33073287
)
33083288

33093289
if enable_automatic_dashboard:
3290+
cw_client = self.sagemaker_session.boto_session.client("cloudwatch")
33103291
self._check_automatic_dashboard_validity(
3292+
cw_client=cw_client,
33113293
monitor_schedule_name=monitor_schedule_name,
33123294
enable_cloudwatch_metrics=enable_cloudwatch_metrics,
3313-
enable_automatic_dashboard=enable_automatic_dashboard,
33143295
dashboard_name=dashboard_name,
33153296
)
33163297

@@ -3373,8 +3354,6 @@ def create_monitoring_schedule(
33733354
raise
33743355

33753356
if enable_automatic_dashboard:
3376-
if dashboard_name is None:
3377-
dashboard_name = monitor_schedule_name
33783357
if isinstance(endpoint_input, EndpointInput):
33793358
endpoint_name = endpoint_input.endpoint_name
33803359
else:
@@ -3402,8 +3381,6 @@ def update_monitoring_schedule(
34023381
constraints=None,
34033382
schedule_cron_expression=None,
34043383
enable_cloudwatch_metrics=None,
3405-
# enable_automatic_dashboard=False,
3406-
# dashboard_name=None,
34073384
role=None,
34083385
instance_count=None,
34093386
instance_type=None,

0 commit comments

Comments
 (0)