-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
312 lines (268 loc) · 10.5 KB
/
metrics.py
File metadata and controls
312 lines (268 loc) · 10.5 KB
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
"""
Simvue Metric Alerts
====================
Classes for interacting with metric-based alerts either defined
locally or on a Simvue server
"""
import pydantic
import typing
try:
from typing import Self
except ImportError:
from typing_extensions import Self
from simvue.api.objects.base import write_only
from .base import AlertBase, staging_check
from simvue.models import NAME_REGEX
Aggregate = typing.Literal["average", "sum", "at least one", "all"]
Rule = typing.Literal["is above", "is below", "is inside range", "is outside range"]
class MetricsThresholdAlert(AlertBase):
"""Class for connecting to/creating a local or remotely defined metric threshold alert"""
def __init__(self, identifier: str | None = None, **kwargs) -> None:
"""Connect to a local or remote threshold alert by identifier"""
self.alert = MetricThresholdAlertDefinition(self)
super().__init__(identifier, **kwargs)
@classmethod
def get(
cls, count: int | None = None, offset: int | None = None
) -> dict[str, typing.Any]:
"""Retrieve only MetricsThresholdAlerts"""
raise NotImplementedError("Retrieve of only metric alerts is not yet supported")
@classmethod
@pydantic.validate_call
def new(
cls,
*,
name: typing.Annotated[str, pydantic.Field(pattern=NAME_REGEX)],
metric: str,
description: str | None,
notification: typing.Literal["none", "email"],
aggregation: Aggregate,
rule: typing.Literal["is above", "is below"],
window: pydantic.PositiveInt,
threshold: float | int,
frequency: pydantic.PositiveInt,
enabled: bool = True,
offline: bool = False,
**_,
) -> Self:
"""Create a new metric threshold alert either locally or on the server
Note all arguments are keyword arguments.
Parameters
----------
name : str
name to assign to this alert
description : str | None
description for this alert
metric : str
the metric to monitor, or a globular expression to match multiple metrics
notification : "none" | "email"
the notification settings for this alert
aggregation : "average" | "sum" | "at least one" | "all"
how to aggregate metric values to deduce if alert is triggered
rule : "is above" | "is below"
threshold condition
window : int
window over which to calculate aggregation
threshold : float | int
the value defining the threshold
frequency : int
how often to monitor the metric
enabled : bool, optional
whether this alert is enabled upon creation, default is True
offline : bool, optional
whether to create the alert locally, default is False
"""
_alert_definition = {
"rule": rule,
"frequency": frequency,
"window": window,
"metric": metric,
"aggregation": aggregation,
"threshold": threshold,
}
_alert = MetricsThresholdAlert(
name=name,
description=description,
notification=notification,
source="metrics",
alert=_alert_definition,
enabled=enabled,
_read_only=False,
_offline=offline,
)
_alert._staging |= _alert_definition
_alert._params = {"deduplicate": True}
return _alert
class MetricsRangeAlert(AlertBase):
"""Class for connecting to/creating a local or remotely defined metric range alert"""
def __init__(self, identifier: str | None = None, **kwargs) -> None:
"""Connect to a local or remote threshold alert by identifier"""
self.alert = MetricRangeAlertDefinition(self)
super().__init__(identifier, **kwargs)
def compare(self, other: "MetricsRangeAlert") -> bool:
"""Compare two MetricRangeAlerts"""
return self.alert.compare(other) if super().compare(other) else False
@classmethod
@pydantic.validate_call
def new(
cls,
*,
name: typing.Annotated[str, pydantic.Field(pattern=NAME_REGEX)],
metric: str,
description: str | None,
notification: typing.Literal["none", "email"],
aggregation: Aggregate,
rule: typing.Literal["is inside range", "is outside range"],
window: pydantic.PositiveInt,
range_high: float,
range_low: float,
frequency: pydantic.PositiveInt,
enabled: bool = True,
offline: bool = False,
**_,
) -> Self:
"""Create a new metric range alert either locally or on the server
Note all arguments are keyword arguments.
Parameters
----------
name : str
name to assign to this alert
metric : str
the metric to monitor
description : str | None
description for this alert
notification : "none" | "email"
the notification settings for this alert
aggregation : "average" | "sum" | "at least one" | "all"
how to aggregate metric values to deduce if alert is triggered
rule : "is inside range" | "is outside range"
threshold condition
window : int
window over which to calculate aggregation
range_high : float | int
the value defining the upper limit
range_low : float | int
the value defining the lower limit
frequency : int | None
how often to monitor the metric
enabled : bool, optional
whether this alert is enabled upon creation, default is True
offline : bool, optional
whether to create the alert locally, default is False
"""
if range_low >= range_high:
raise ValueError(f"Invalid arguments for range [{range_low}, {range_high}]")
_alert_definition = {
"rule": rule,
"frequency": frequency,
"window": window,
"metric": metric,
"aggregation": aggregation,
"range_low": range_low,
"range_high": range_high,
}
_alert = MetricsRangeAlert(
name=name,
description=description,
notification=notification,
source="metrics",
enabled=enabled,
alert=_alert_definition,
_read_only=False,
_offline=offline,
)
_alert._staging |= _alert_definition
_alert._params = {"deduplicate": True}
return _alert
class MetricsAlertDefinition:
"""General alert definition for a metric alert"""
def __init__(self, alert: MetricsRangeAlert) -> None:
"""Initialise definition with target alert"""
self._sv_obj = alert
def compare(self, other: "MetricsAlertDefinition") -> bool:
"""Compare a MetricsAlertDefinition with another"""
return all(
[
self.aggregation == other.aggregation,
self.frequency == other.frequency,
self.rule == other.rule,
self.window == other.window,
]
)
@property
def aggregation(self) -> Aggregate:
"""Retrieve the aggregation strategy for this alert"""
if not (_aggregation := self._sv_obj.get_alert().get("aggregation")):
raise RuntimeError(
"Expected key 'aggregation' in alert definition retrieval"
)
return _aggregation
@property
def rule(self) -> Rule:
"""Retrieve the rule for this alert"""
if not (_rule := self._sv_obj.get_alert().get("rule")):
raise RuntimeError("Expected key 'rule' in alert definition retrieval")
return _rule
@property
def window(self) -> int:
"""Retrieve the aggregation window for this alert"""
if not (_window := self._sv_obj.get_alert().get("window")):
raise RuntimeError("Expected key 'window' in alert definition retrieval")
return _window
@property
@staging_check
def frequency(self) -> int:
"""Retrieve the monitor frequency for this alert"""
try:
return self._sv_obj.get_alert()["frequency"]
except KeyError as e:
raise RuntimeError(
"Expected key 'frequency' in alert definition retrieval"
) from e
@frequency.setter
@write_only
@pydantic.validate_call
def frequency(self, frequency: int) -> None:
"""Set the monitor frequency for this alert"""
_alert = self._sv_obj.get_alert() | {"frequency": frequency}
self._sv_obj._staging["alert"] = _alert
class MetricThresholdAlertDefinition(MetricsAlertDefinition):
"""Alert definition for metric threshold alerts"""
def compare(self, other: "MetricThresholdAlertDefinition") -> bool:
"""Compare this MetricThresholdAlertDefinition with another"""
if not isinstance(other, MetricThresholdAlertDefinition):
return False
return all([super().compare(other), self.threshold == other.threshold])
@property
def threshold(self) -> float:
"""Retrieve the threshold value for this alert"""
if not (threshold_l := self._sv_obj.get_alert().get("threshold")):
raise RuntimeError("Expected key 'threshold' in alert definition retrieval")
return threshold_l
class MetricRangeAlertDefinition(MetricsAlertDefinition):
"""Alert definition for metric range alerts"""
def compare(self, other: "MetricRangeAlertDefinition") -> bool:
"""Compare a MetricRangeAlertDefinition with another"""
if not isinstance(other, MetricRangeAlertDefinition):
return False
return all(
[
super().compare(other),
self.range_high == other.range_high,
self.range_low == other.range_low,
]
)
@property
def range_low(self) -> float:
"""Retrieve the lower limit for metric range"""
if not (range_l := self._sv_obj.get_alert().get("range_low")):
raise RuntimeError("Expected key 'range_low' in alert definition retrieval")
return range_l
@property
def range_high(self) -> float:
"""Retrieve upper limit for metric range"""
if not (range_u := self._sv_obj.get_alert().get("range_high")):
raise RuntimeError(
"Expected key 'range_high' in alert definition retrieval"
)
return range_u