1
1
from __future__ import annotations
2
2
3
3
from functools import cached_property
4
- from typing import Any , List , Literal , Optional
4
+ from typing import Any , Dict , List , Literal , Optional
5
5
6
6
from aws_lambda_powertools .utilities .data_classes .common import DictWrapper
7
7
8
- CloudWatchAlarmStateValue = Literal ["OK" , "ALARM" , "INSUFFICIENT_DATA" ]
9
- CloudWatchAlarmActionSuppressor = Literal ["Alarm" , "ExtensionPeriod" , "WaitPeriod" ]
10
-
11
8
12
9
class CloudWatchAlarmState (DictWrapper ):
13
10
@property
14
- def value (self ) -> CloudWatchAlarmStateValue :
11
+ def value (self ) -> Literal [ "OK" , "ALARM" , "INSUFFICIENT_DATA" ] :
15
12
"""
16
13
Overall state of the alarm.
17
14
"""
18
15
return self ["value" ]
19
16
20
17
@property
21
- def reason (self ) -> Optional [ str ] :
18
+ def reason (self ) -> str :
22
19
"""
23
20
Reason why alarm was changed to this state.
24
21
"""
25
22
return self .get ("reason" )
26
23
27
24
@property
28
- def reason_data (self ) -> Optional [ str ] :
25
+ def reason_data (self ) -> str :
29
26
"""
30
27
Additional data to back up the reason, usually contains the evaluated data points,
31
28
the calculated threshold and timestamps.
@@ -37,13 +34,11 @@ def reason_data_decoded(self) -> Optional[Any]:
37
34
"""
38
35
Deserialized version of reason_data.
39
36
"""
40
- if self .reason_data is None :
41
- return None
42
37
43
- return self ._json_deserializer (self .reason_data )
38
+ return self ._json_deserializer (self .reason_data ) if self . reason_data else None
44
39
45
40
@property
46
- def actions_suppressed_by (self ) -> Optional [CloudWatchAlarmActionSuppressor ]:
41
+ def actions_suppressed_by (self ) -> Optional [Literal [ "Alarm" , "ExtensionPeriod" , "WaitPeriod" ] ]:
47
42
"""
48
43
Describes why the actions when the value is `ALARM` are suppressed in a composite
49
44
alarm.
@@ -66,11 +61,6 @@ def timestamp(self) -> str:
66
61
67
62
68
63
class CloudWatchAlarmMetric (DictWrapper ):
69
- def __init__ (self , data : dict ):
70
- super ().__init__ (data )
71
-
72
- self ._metric_stat : dict | None = self .get ("metricStat" )
73
-
74
64
@property
75
65
def metric_id (self ) -> str :
76
66
"""
@@ -81,98 +71,67 @@ def metric_id(self) -> str:
81
71
@property
82
72
def expression (self ) -> Optional [str ]:
83
73
"""
84
- The mathematical expression for calculating the metric, if applicable .
74
+ Optional expression of the alarm metric .
85
75
"""
86
76
return self .get ("expression" , None )
87
77
88
78
@property
89
- def label (self ) -> Optional [ str ] :
79
+ def label (self ) -> str :
90
80
"""
91
- Optional label of the metric.
81
+ Optional label of the alarm metric.
92
82
"""
93
83
return self .get ("label" , None )
94
84
95
85
@property
96
- def namespace (self ) -> Optional [str ]:
97
- """
98
- Namespace of the correspondent CloudWatch Metric.
99
- """
100
- if self ._metric_stat is not None :
101
- return self ._metric_stat .get ("metric" , {}).get ("namespace" , None )
102
-
103
- return None
104
-
105
- @property
106
- def name (self ) -> Optional [str ]:
86
+ def return_data (self ) -> bool :
107
87
"""
108
- Name of the correspondent CloudWatch Metric .
88
+ Whether this metric data is used to determine the state of the alarm or not .
109
89
"""
110
- if self ._metric_stat is not None :
111
- return self ._metric_stat .get ("metric" , {}).get ("name" , None )
112
-
113
- return None
90
+ return self ["returnData" ]
114
91
115
92
@property
116
- def dimensions (self ) -> Optional [dict ]:
117
- """
118
- Additional dimensions of the correspondent CloudWatch Metric, if available.
119
- """
120
- if self ._metric_stat is not None :
121
- return self ._metric_stat .get ("metric" , {}).get ("dimensions" , None )
93
+ def metric_stat (self ) -> CloudWatchAlarmMetricStat :
94
+ return CloudWatchAlarmMetricStat (self ["metricStat" ])
122
95
123
- return None
124
96
97
+ class CloudWatchAlarmMetricStat (DictWrapper ):
125
98
@property
126
99
def period (self ) -> Optional [int ]:
127
100
"""
128
101
Metric evaluation period, in seconds.
129
102
"""
130
- if self ._metric_stat is not None :
131
- return self ._metric_stat .get ("period" , None )
132
-
133
- return None
103
+ return self .get ("period" , None )
134
104
135
105
@property
136
106
def stat (self ) -> Optional [str ]:
137
107
"""
138
108
Statistical aggregation of metric points, e.g. Average, SampleCount, etc.
139
109
"""
140
- if self ._metric_stat is not None :
141
- return self ._metric_stat .get ("stat" , None )
110
+ return self .get ("stat" , None )
142
111
143
- return None
112
+ @property
113
+ def unit (self ) -> Optional [str ]:
114
+ """
115
+ Unit for metric.
116
+ """
117
+ return self .get ("unit" , None )
144
118
145
119
@property
146
- def return_data (self ) -> bool :
120
+ def metric (self ) -> Dict :
147
121
"""
148
- Whether this metric data is used to determine the state of the alarm or not.
122
+ Metric details
149
123
"""
150
- return self [ "returnData" ]
124
+ return self . get ( "metric" , {})
151
125
152
126
153
127
class CloudWatchAlarmData (DictWrapper ):
154
- def __init__ (self , data : dict ):
155
- super ().__init__ (data )
156
-
157
- self ._configuration = self .get ("configuration" , None )
158
-
159
128
@property
160
- def name (self ) -> str :
129
+ def alarm_name (self ) -> str :
161
130
"""
162
131
Alarm name.
163
132
"""
164
133
return self ["alarmName" ]
165
134
166
- @property
167
- def description (self ) -> Optional [str ]:
168
- """
169
- Optional description for the Alarm.
170
- """
171
- if self ._configuration is not None :
172
- return self ._configuration .get ("description" , None )
173
-
174
- return None
175
-
176
135
@property
177
136
def state (self ) -> CloudWatchAlarmState :
178
137
"""
@@ -188,19 +147,56 @@ def previous_state(self) -> CloudWatchAlarmState:
188
147
return CloudWatchAlarmState (self ["previousState" ])
189
148
190
149
@property
191
- def metrics (self ) -> Optional [ List [ CloudWatchAlarmMetric ]] :
150
+ def configuration (self ) -> CloudWatchAlarmConfiguration :
192
151
"""
193
- The metrics evaluated for the Alarm.
152
+ The configuration of the Alarm.
194
153
"""
195
- if self ._configuration is None :
196
- return None
154
+ return CloudWatchAlarmConfiguration (self ["configuration" ])
197
155
198
- maybe_metrics = self ._configuration .get ("metrics" , None )
199
156
200
- if maybe_metrics is not None :
201
- return [CloudWatchAlarmMetric (i ) for i in maybe_metrics ]
157
+ class CloudWatchAlarmConfiguration (DictWrapper ):
158
+ @property
159
+ def description (self ) -> Optional [str ]:
160
+ """
161
+ Optional description for the Alarm.
162
+ """
163
+ return self .get ("description" , None )
202
164
203
- return None
165
+ @property
166
+ def alarm_rule (self ) -> Optional [str ]:
167
+ """
168
+ Optional description for the Alarm rule in case of composite alarm.
169
+ """
170
+ return self .get ("alarmRule" , None )
171
+
172
+ @property
173
+ def alarm_actions_suppressor (self ) -> Optional [str ]:
174
+ """
175
+ Optional action suppression for the Alarm rule in case of composite alarm.
176
+ """
177
+ return self .get ("actionsSuppressor" , None )
178
+
179
+ @property
180
+ def alarm_actions_suppressor_wait_period (self ) -> Optional [str ]:
181
+ """
182
+ Optional action suppression wait period for the Alarm rule in case of composite alarm.
183
+ """
184
+ return self .get ("actionsSuppressorWaitPeriod" , None )
185
+
186
+ @property
187
+ def alarm_actions_suppressor_extension_period (self ) -> Optional [str ]:
188
+ """
189
+ Optional action suppression extension period for the Alarm rule in case of composite alarm.
190
+ """
191
+ return self .get ("actionsSuppressorExtensionPeriod" , None )
192
+
193
+ @property
194
+ def metrics (self ) -> Optional [List [CloudWatchAlarmMetric ]]:
195
+ """
196
+ The metrics evaluated for the Alarm.
197
+ """
198
+ metrics = self .get ("metrics" )
199
+ return [CloudWatchAlarmMetric (i ) for i in metrics ] if metrics else None
204
200
205
201
206
202
class CloudWatchAlarmEvent (DictWrapper ):
0 commit comments