1
+ from __future__ import annotations
2
+
1
3
import json
2
4
from enum import Enum , auto
3
5
from typing import List , Optional
@@ -32,7 +34,10 @@ def reason_data(self) -> Optional[dict]:
32
34
Additional data to back up the reason, usually contains the evaluated data points,
33
35
the calculated threshold and timestamps.
34
36
"""
35
- return json .loads (self .get ("reasonData" )) if self .get ("reasonData" ) is not None else None
37
+ if self .get ("reasonData" ) is None :
38
+ return None
39
+
40
+ return json .loads (str (self .get ("reasonData" )))
36
41
37
42
@property
38
43
def timestamp (self ) -> str :
@@ -43,6 +48,11 @@ def timestamp(self) -> str:
43
48
44
49
45
50
class CloudWatchAlarmMetric (DictWrapper ):
51
+ def __init__ (self , data : dict ):
52
+ super ().__init__ (data )
53
+
54
+ self ._metric_stat : dict | None = self .get ("metricStat" )
55
+
46
56
@property
47
57
def metric_id (self ) -> str :
48
58
"""
@@ -55,35 +65,50 @@ def namespace(self) -> Optional[str]:
55
65
"""
56
66
Namespace of the correspondent CloudWatch Metric.
57
67
"""
58
- return self .get ("metricStat" , {}).get ("metric" , {}).get ("namespace" , None )
68
+ if self ._metric_stat is not None :
69
+ return self ._metric_stat .get ("metric" , {}).get ("namespace" , None )
70
+
71
+ return None
59
72
60
73
@property
61
74
def name (self ) -> Optional [str ]:
62
75
"""
63
76
Name of the correspondent CloudWatch Metric.
64
77
"""
65
- return self .get ("metricStat" , {}).get ("metric" , {}).get ("name" , None )
78
+ if self ._metric_stat is not None :
79
+ return self ._metric_stat .get ("metric" , {}).get ("name" , None )
80
+
81
+ return None
66
82
67
83
@property
68
84
def dimensions (self ) -> Optional [dict ]:
69
85
"""
70
86
Additional dimensions of the correspondent CloudWatch Metric, if available.
71
87
"""
72
- return self .get ("metricStat" , {}).get ("metric" , {}).get ("dimensions" , None )
88
+ if self ._metric_stat is not None :
89
+ return self ._metric_stat .get ("metric" , {}).get ("dimensions" , None )
90
+
91
+ return None
73
92
74
93
@property
75
94
def period (self ) -> Optional [int ]:
76
95
"""
77
96
Metric evaluation period, in seconds.
78
97
"""
79
- return self .get ("metricStat" , {}).get ("period" , None )
98
+ if self ._metric_stat is not None :
99
+ return self ._metric_stat .get ("period" , None )
100
+
101
+ return None
80
102
81
103
@property
82
104
def stat (self ) -> Optional [str ]:
83
105
"""
84
106
Statistical aggregation of metric points, e.g. Average, SampleCount, etc.
85
107
"""
86
- return self .get ("metricStat" , {}).get ("stat" , None )
108
+ if self ._metric_stat is not None :
109
+ return self ._metric_stat .get ("stat" , None )
110
+
111
+ return None
87
112
88
113
@property
89
114
def return_data (self ) -> bool :
@@ -131,14 +156,14 @@ def alarm_name(self) -> str:
131
156
"""
132
157
Alarm name.
133
158
"""
134
- return self . get ( "alarmData" ). get ( "alarmName" )
159
+ return self [ "alarmData" ][ "alarmName" ]
135
160
136
161
@property
137
162
def alarm_description (self ) -> Optional [str ]:
138
163
"""
139
164
Optional description for the Alarm.
140
165
"""
141
- return self . get ( "alarmData" ) .get ("configuration" , {}).get ("description" , None )
166
+ return self [ "alarmData" ] .get ("configuration" , {}).get ("description" , None )
142
167
143
168
@property
144
169
def state (self ):
@@ -156,7 +181,7 @@ def previous_state(self):
156
181
157
182
@property
158
183
def alarm_metrics (self ) -> Optional [List [CloudWatchAlarmMetric ]]:
159
- maybe_metrics = self . get ( "alarmData" , {}) .get ("configuration" , {}).get ("metrics" , None )
184
+ maybe_metrics = self [ "alarmData" ] .get ("configuration" , {}).get ("metrics" , None )
160
185
161
186
if maybe_metrics is not None :
162
187
return [CloudWatchAlarmMetric (i ) for i in maybe_metrics ]
0 commit comments