-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevents.py
More file actions
163 lines (137 loc) · 4.88 KB
/
events.py
File metadata and controls
163 lines (137 loc) · 4.88 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
"""
Simvue Event Alerts
===================
Interface to event-based Simvue alerts.
"""
import typing
import pydantic
try:
from typing import Self, override
except ImportError:
from typing_extensions import Self, override
from simvue.api.objects.base import write_only
from .base import AlertBase, staging_check
from simvue.models import NAME_REGEX
class EventsAlert(AlertBase):
"""
Simvue Events Alert
===================
This class is used to connect to/create event-based alert objects on the Simvue server,
any modification of EventsAlert instance attributes is mirrored on the remote object.
"""
def __init__(self, identifier: str | None = None, **kwargs) -> None:
"""Initialise an Events Alert
If an identifier is provided a connection will be made to the
object matching the identifier on the target server.
Else a new EventsAlert instance will be created using arguments provided in kwargs.
Parameters
----------
identifier : str, optional
the remote server unique id for the target folder
**kwargs : dict
any additional arguments to be passed to the object initialiser
"""
self.alert = EventAlertDefinition(self)
super().__init__(identifier, **kwargs)
@classmethod
def get(
cls, count: int | None = None, offset: int | None = None
) -> dict[str, typing.Any]:
"""Retrieve only alerts of the event alert type"""
raise NotImplementedError("Retrieval of only event alerts is not yet supported")
@classmethod
@pydantic.validate_call
def new(
cls,
*,
name: typing.Annotated[str, pydantic.Field(pattern=NAME_REGEX)],
description: str | None,
notification: typing.Literal["none", "email"],
pattern: str,
frequency: pydantic.PositiveInt,
enabled: bool = True,
offline: bool = False,
**_,
) -> Self:
"""Create a new event-based alert
Note parameters are keyword arguments only.
Parameters
----------
name : str
name of the alert
description : str | None
description for this alert
notification : "none" | "email"
configure notifications sent by this alert
pattern : str
pattern to monitor in event logs
frequency : int
how often to check for updates
enabled : bool, optional
enable this alert upon creation, default is True
offline : bool, optional
create alert locally, default is False
Returns
-------
EventAlert
a new event alert with changes staged
"""
_alert_definition = {"pattern": pattern, "frequency": frequency}
_alert = EventsAlert(
name=name,
description=description,
notification=notification,
source="events",
alert=_alert_definition,
enabled=enabled,
_read_only=False,
_offline=offline,
)
_alert._staging |= _alert_definition
_alert._params = {"deduplicate": True}
return _alert
@override
def _compare_objects(self, other: "AlertBase") -> bool:
"""Compare Events Alerts."""
if not isinstance(other, EventsAlert):
return False
return super()._compare_objects(other) and self.alert == other.alert
class EventAlertDefinition:
"""Event alert definition sub-class"""
def __init__(self, alert: EventsAlert) -> None:
"""Initialise an alert definition with its parent alert"""
self._sv_obj = alert
def __eq__(self, other: "EventAlertDefinition") -> bool:
"""Compare this definition with that of another EventAlert"""
return all(
[
self.frequency == other.frequency,
self.pattern == other.pattern,
]
)
@property
def pattern(self) -> str:
"""Retrieve the event log pattern monitored by this alert"""
try:
return self._sv_obj.get_alert()["pattern"]
except KeyError as e:
raise RuntimeError(
"Expected key 'pattern' in alert definition retrieval"
) from e
@property
@staging_check
def frequency(self) -> int:
"""Retrieve the update 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 update frequency for this alert"""
_alert = self._sv_obj.get_alert() | {"frequency": frequency}
self._sv_obj._staging["alert"] = _alert