1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
# coding=utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import uuid
import datetime
from devtools_testutils import AzureRecordedTestCase, is_live
from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential
from azure.ai.metricsadvisor.models import (
SqlServerDataFeedSource,
DataFeedSchema,
DataFeedMetric,
DataFeedDimension,
DataFeedGranularity,
DataFeedIngestionSettings,
DataFeedMissingDataPointFillSettings,
DataFeedRollupSettings,
MetricAlertConfiguration,
MetricAnomalyAlertScope,
MetricAnomalyAlertConditions,
MetricBoundaryCondition,
TopNGroupScope,
SeverityCondition,
MetricDetectionCondition,
MetricSeriesGroupDetectionCondition,
MetricSingleSeriesDetectionCondition,
SmartDetectionCondition,
SuppressCondition,
ChangeThresholdCondition,
HardThresholdCondition,
EmailNotificationHook,
WebNotificationHook,
)
# for pytest.parametrize
subscription_key = os.getenv("METRICS_ADVISOR_SUBSCRIPTION_KEY", "metrics_advisor_subscription_key")
api_key = os.getenv("METRICS_ADVISOR_API_KEY", "metrics_advisor_api_key")
API_KEY = [MetricsAdvisorKeyCredential(subscription_key, api_key)]
AAD = ["AAD"]
CREDENTIALS = [MetricsAdvisorKeyCredential(subscription_key, api_key), "AAD"]
def ids(val):
if isinstance(val, MetricsAdvisorKeyCredential):
return "APIKey"
else:
return "AAD"
class MetricsAdvisorClientPreparer(object):
def __init__(self, client_cls, client_kwargs={}, **kwargs):
self.client_cls = client_cls
self.client_kwargs = client_kwargs
self.service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT", "https://fakeendpoint.cognitiveservices.azure.com")
self.data_feed = kwargs.pop("data_feed", False)
self.detection_config = kwargs.pop("detection_config", False)
self.alert_config = kwargs.pop("alert_config", False)
self.email_hook = kwargs.pop("email_hook", False)
self.web_hook = kwargs.pop("web_hook", False)
self.variables = kwargs.pop("variables", {})
def __call__(self, fn):
def _preparer_wrapper(test_class, credential, **kwargs):
self.create_test_client(credential)
self.create_resources(**kwargs)
if is_live():
fn(test_class, self.client, variables=self.variables)
else:
fn(test_class, self.client)
return _preparer_wrapper
def create_test_client(self, credential):
if credential == "AAD":
credential = AzureRecordedTestCase().get_credential(self.client_cls)
self.client = self.client_cls(self.service_endpoint, credential, **self.client_kwargs)
def create_resources(self, **kwargs):
if not is_live():
return kwargs
try:
if self.data_feed:
self.data_feed = self.create_data_feed("datafeed")
if self.detection_config:
self.detection_config = self.create_detection_config("detectionconfig")
if self.alert_config:
self.alert_config = self.create_alert_config("alertconfig")
if self.email_hook:
self.email_hook = self.create_email_hook("emailhook")
if self.web_hook:
self.web_hook = self.create_web_hook("web_hook")
except Exception as e:
try:
self.client.delete_data_feed(self.variables["data_feed_id"])
except KeyError:
pass
raise e
def create_random_name(self, name):
return name + str(uuid.uuid4())
def create_data_feed(self, name):
name = self.create_random_name(name)
if is_live():
self.variables["data_feed_name"] = name
data_feed = self.client.create_data_feed(
name=self.variables["data_feed_name"],
source=SqlServerDataFeedSource(
connection_string=os.getenv("METRICS_ADVISOR_SQL_SERVER_CONNECTION_STRING", "metrics_advisor_sql_server_connection_string"),
query="select * from adsample2 where Timestamp = @StartTime"
),
granularity=DataFeedGranularity(
granularity_type="Daily",
),
schema=DataFeedSchema(
metrics=[
DataFeedMetric(name="cost", description="the cost"),
DataFeedMetric(name="revenue", description="the revenue")
],
dimensions=[
DataFeedDimension(name="category"),
DataFeedDimension(name="region")
],
timestamp_column="Timestamp"
),
ingestion_settings=DataFeedIngestionSettings(
ingestion_begin_time=datetime.datetime(2019, 10, 1),
data_source_request_concurrency=0,
ingestion_retry_delay=-1,
ingestion_start_offset=-1,
stop_retry_after=-1,
),
admins=["yournamehere@microsoft.com"],
data_feed_description="my first data feed",
missing_data_point_fill_settings=DataFeedMissingDataPointFillSettings(
fill_type="SmartFilling"
),
rollup_settings=DataFeedRollupSettings(
rollup_type="NoRollup",
rollup_method="None",
),
viewers=["viewers"],
access_mode="Private",
action_link_template="action link template"
)
if is_live():
self.variables["data_feed_id"] = data_feed.id
self.variables["data_feed_metric_id"] = data_feed.metric_ids['cost']
return data_feed
def create_detection_config(self, name):
detection_config_name = self.create_random_name(name)
if is_live():
self.variables["detection_config_name"] = detection_config_name
detection_config = self.client.create_detection_configuration(
name=self.variables["detection_config_name"],
metric_id=self.variables["data_feed_metric_id"],
description="My test metric anomaly detection configuration",
whole_series_detection_condition=MetricDetectionCondition(
condition_operator="AND",
smart_detection_condition=SmartDetectionCondition(
sensitivity=50,
anomaly_detector_direction="Both",
suppress_condition=SuppressCondition(
min_number=5,
min_ratio=5
)
),
hard_threshold_condition=HardThresholdCondition(
anomaly_detector_direction="Both",
suppress_condition=SuppressCondition(
min_number=5,
min_ratio=5
),
lower_bound=0,
upper_bound=100
),
change_threshold_condition=ChangeThresholdCondition(
change_percentage=50,
shift_point=30,
within_range=True,
anomaly_detector_direction="Both",
suppress_condition=SuppressCondition(
min_number=2,
min_ratio=2
)
)
),
series_detection_conditions=[MetricSingleSeriesDetectionCondition(
series_key={"region": "Beijing", "category": "Shoes Handbags & Sunglasses"},
smart_detection_condition=SmartDetectionCondition(
anomaly_detector_direction="Both",
sensitivity=63,
suppress_condition=SuppressCondition(
min_number=1,
min_ratio=100
)
)
)],
series_group_detection_conditions=[MetricSeriesGroupDetectionCondition(
series_group_key={"region": "Beijing"},
smart_detection_condition=SmartDetectionCondition(
anomaly_detector_direction="Both",
sensitivity=63,
suppress_condition=SuppressCondition(
min_number=1,
min_ratio=100
)
)
)]
)
if is_live():
self.variables["detection_config_id"] = detection_config.id
return detection_config
def create_alert_config(self, name):
alert_config_name = self.create_random_name(name)
if is_live():
self.variables["alert_config_name"] = alert_config_name
alert_config = self.client.create_alert_configuration(
name=self.variables["alert_config_name"],
cross_metrics_operator="AND",
metric_alert_configurations=[
MetricAlertConfiguration(
detection_configuration_id=self.variables["detection_config_id"],
alert_scope=MetricAnomalyAlertScope(
scope_type="TopN",
top_n_group_in_scope=TopNGroupScope(
top=5,
period=10,
min_top_count=9
)
),
alert_conditions=MetricAnomalyAlertConditions(
metric_boundary_condition=MetricBoundaryCondition(
direction="Both",
companion_metric_id=self.variables["data_feed_metric_id"],
lower=1.0,
upper=5.0
)
)
),
MetricAlertConfiguration(
detection_configuration_id=self.variables["detection_config_id"],
alert_scope=MetricAnomalyAlertScope(
scope_type="SeriesGroup",
series_group_in_scope={'region': 'Beijing'}
),
alert_conditions=MetricAnomalyAlertConditions(
severity_condition=SeverityCondition(
min_alert_severity="Low",
max_alert_severity="High"
)
)
),
MetricAlertConfiguration(
detection_configuration_id=self.variables["detection_config_id"],
alert_scope=MetricAnomalyAlertScope(
scope_type="WholeSeries"
),
alert_conditions=MetricAnomalyAlertConditions(
severity_condition=SeverityCondition(
min_alert_severity="Low",
max_alert_severity="High"
)
)
)
],
hook_ids=[]
)
if is_live():
self.variables["alert_config_id"] = alert_config.id
return alert_config
def create_email_hook(self, name):
email_hook_name = self.create_random_name(name)
if is_live():
self.variables["email_hook_name"] = email_hook_name
email_hook = self.client.create_hook(
hook=EmailNotificationHook(
name=self.variables["email_hook_name"],
emails_to_alert=["yournamehere@microsoft.com"],
description="my email hook",
external_link="external link"
)
)
if is_live():
self.variables["email_hook_id"] = email_hook.id
return email_hook
def create_web_hook(self, name):
web_hook_name = self.create_random_name(name)
if is_live():
self.variables["web_hook_name"] = web_hook_name
web_hook = self.client.create_hook(
hook=WebNotificationHook(
name=self.variables["web_hook_name"],
endpoint="https://httpbin.org/post",
description="my web hook",
external_link="external link",
username="krista",
password="123"
)
)
if is_live():
self.variables["web_hook_id"] = web_hook.id
return web_hook
class TestMetricsAdvisorClientBase(AzureRecordedTestCase):
@property
def service_endpoint(self):
return os.getenv("METRICS_ADVISOR_ENDPOINT", "https://fakeendpoint.cognitiveservices.azure.com")
@property
def subscription_key(self):
return os.getenv("METRICS_ADVISOR_SUBSCRIPTION_KEY", "metrics_advisor_subscription_key")
@property
def api_key(self):
return os.getenv("METRICS_ADVISOR_API_KEY", "metrics_advisor_api_key")
@property
def sql_server_connection_string(self):
return os.getenv("METRICS_ADVISOR_SQL_SERVER_CONNECTION_STRING", "metrics_advisor_sql_server_connection_string")
@property
def data_feed_id(self):
return os.getenv("METRICS_ADVISOR_DATA_FEED_ID", "metrics_advisor_data_feed_id")
@property
def anomaly_detection_configuration_id(self):
return os.getenv("METRICS_ADVISOR_ANOMALY_DETECTION_CONFIGURATION_ID", "metrics_advisor_anomaly_detection_configuration_id")
@property
def anomaly_alert_configuration_id(self):
return os.getenv("METRICS_ADVISOR_ANOMALY_ALERT_CONFIGURATION_ID", "metrics_advisor_anomaly_alert_configuration_id")
@property
def metric_id(self):
return os.getenv("METRICS_ADVISOR_METRIC_ID", "metrics_advisor_metric_id")
@property
def incident_id(self):
return os.getenv("METRICS_ADVISOR_INCIDENT_ID", "metrics_advisor_incident_id")
@property
def feedback_id(self):
return os.getenv("METRICS_ADVISOR_FEEDBACK_ID", "metrics_advisor_feedback_id")
@property
def alert_id(self):
return os.getenv("METRICS_ADVISOR_ALERT_ID", "metrics_advisor_alert_id")
def clean_up(self, delete_func, variables, key=None):
try:
id_to_delete = variables[key] if key else variables["data_feed_id"]
delete_func(id_to_delete)
except KeyError:
pass
def create_random_name(self, name):
return name + str(uuid.uuid4())
|