|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from functools import cached_property |
| 4 | +from typing import Any, Dict, List, Literal, Optional |
| 5 | + |
| 6 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 7 | + |
| 8 | + |
| 9 | +class CloudWatchAlarmState(DictWrapper): |
| 10 | + @property |
| 11 | + def value(self) -> Literal["OK", "ALARM", "INSUFFICIENT_DATA"]: |
| 12 | + """ |
| 13 | + Overall state of the alarm. |
| 14 | + """ |
| 15 | + return self["value"] |
| 16 | + |
| 17 | + @property |
| 18 | + def reason(self) -> str: |
| 19 | + """ |
| 20 | + Reason why alarm was changed to this state. |
| 21 | + """ |
| 22 | + return self["reason"] |
| 23 | + |
| 24 | + @property |
| 25 | + def reason_data(self) -> str: |
| 26 | + """ |
| 27 | + Additional data to back up the reason, usually contains the evaluated data points, |
| 28 | + the calculated threshold and timestamps. |
| 29 | + """ |
| 30 | + return self["reasonData"] |
| 31 | + |
| 32 | + @cached_property |
| 33 | + def reason_data_decoded(self) -> Optional[Any]: |
| 34 | + """ |
| 35 | + Deserialized version of reason_data. |
| 36 | + """ |
| 37 | + |
| 38 | + return self._json_deserializer(self.reason_data) if self.reason_data else None |
| 39 | + |
| 40 | + @property |
| 41 | + def actions_suppressed_by(self) -> Optional[Literal["Alarm", "ExtensionPeriod", "WaitPeriod"]]: |
| 42 | + """ |
| 43 | + Describes why the actions when the value is `ALARM` are suppressed in a composite |
| 44 | + alarm. |
| 45 | + """ |
| 46 | + return self.get("actionsSuppressedBy", None) |
| 47 | + |
| 48 | + @property |
| 49 | + def actions_suppressed_reason(self) -> Optional[str]: |
| 50 | + """ |
| 51 | + Captures the reason for action suppression. |
| 52 | + """ |
| 53 | + return self.get("actionsSuppressedReason", None) |
| 54 | + |
| 55 | + @property |
| 56 | + def timestamp(self) -> str: |
| 57 | + """ |
| 58 | + Timestamp of this state change in ISO-8601 format. |
| 59 | + """ |
| 60 | + return self["timestamp"] |
| 61 | + |
| 62 | + |
| 63 | +class CloudWatchAlarmMetric(DictWrapper): |
| 64 | + @property |
| 65 | + def metric_id(self) -> str: |
| 66 | + """ |
| 67 | + Unique ID of the alarm metric. |
| 68 | + """ |
| 69 | + return self["id"] |
| 70 | + |
| 71 | + @property |
| 72 | + def expression(self) -> Optional[str]: |
| 73 | + """ |
| 74 | + Optional expression of the alarm metric. |
| 75 | + """ |
| 76 | + return self.get("expression", None) |
| 77 | + |
| 78 | + @property |
| 79 | + def label(self) -> Optional[str]: |
| 80 | + """ |
| 81 | + Optional label of the alarm metric. |
| 82 | + """ |
| 83 | + return self.get("label", None) |
| 84 | + |
| 85 | + @property |
| 86 | + def return_data(self) -> bool: |
| 87 | + """ |
| 88 | + Whether this metric data is used to determine the state of the alarm or not. |
| 89 | + """ |
| 90 | + return self["returnData"] |
| 91 | + |
| 92 | + @property |
| 93 | + def metric_stat(self) -> CloudWatchAlarmMetricStat: |
| 94 | + return CloudWatchAlarmMetricStat(self["metricStat"]) |
| 95 | + |
| 96 | + |
| 97 | +class CloudWatchAlarmMetricStat(DictWrapper): |
| 98 | + @property |
| 99 | + def period(self) -> Optional[int]: |
| 100 | + """ |
| 101 | + Metric evaluation period, in seconds. |
| 102 | + """ |
| 103 | + return self.get("period", None) |
| 104 | + |
| 105 | + @property |
| 106 | + def stat(self) -> Optional[str]: |
| 107 | + """ |
| 108 | + Statistical aggregation of metric points, e.g. Average, SampleCount, etc. |
| 109 | + """ |
| 110 | + return self.get("stat", None) |
| 111 | + |
| 112 | + @property |
| 113 | + def unit(self) -> Optional[str]: |
| 114 | + """ |
| 115 | + Unit for metric. |
| 116 | + """ |
| 117 | + return self.get("unit", None) |
| 118 | + |
| 119 | + @property |
| 120 | + def metric(self) -> Optional[Dict]: |
| 121 | + """ |
| 122 | + Metric details |
| 123 | + """ |
| 124 | + return self.get("metric", {}) |
| 125 | + |
| 126 | + |
| 127 | +class CloudWatchAlarmData(DictWrapper): |
| 128 | + @property |
| 129 | + def alarm_name(self) -> str: |
| 130 | + """ |
| 131 | + Alarm name. |
| 132 | + """ |
| 133 | + return self["alarmName"] |
| 134 | + |
| 135 | + @property |
| 136 | + def state(self) -> CloudWatchAlarmState: |
| 137 | + """ |
| 138 | + The current state of the Alarm. |
| 139 | + """ |
| 140 | + return CloudWatchAlarmState(self["state"]) |
| 141 | + |
| 142 | + @property |
| 143 | + def previous_state(self) -> CloudWatchAlarmState: |
| 144 | + """ |
| 145 | + The previous state of the Alarm. |
| 146 | + """ |
| 147 | + return CloudWatchAlarmState(self["previousState"]) |
| 148 | + |
| 149 | + @property |
| 150 | + def configuration(self) -> CloudWatchAlarmConfiguration: |
| 151 | + """ |
| 152 | + The configuration of the Alarm. |
| 153 | + """ |
| 154 | + return CloudWatchAlarmConfiguration(self["configuration"]) |
| 155 | + |
| 156 | + |
| 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) |
| 164 | + |
| 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 |
| 200 | + |
| 201 | + |
| 202 | +class CloudWatchAlarmEvent(DictWrapper): |
| 203 | + @property |
| 204 | + def source(self) -> Literal["aws.cloudwatch"]: |
| 205 | + """ |
| 206 | + Source of the triggered event. |
| 207 | + """ |
| 208 | + return self["source"] |
| 209 | + |
| 210 | + @property |
| 211 | + def alarm_arn(self) -> str: |
| 212 | + """ |
| 213 | + The ARN of the CloudWatch Alarm. |
| 214 | + """ |
| 215 | + return self["alarmArn"] |
| 216 | + |
| 217 | + @property |
| 218 | + def region(self) -> str: |
| 219 | + """ |
| 220 | + The AWS region in which the Alarm is active. |
| 221 | + """ |
| 222 | + return self["region"] |
| 223 | + |
| 224 | + @property |
| 225 | + def source_account_id(self) -> str: |
| 226 | + """ |
| 227 | + The AWS Account ID that the Alarm is deployed to. |
| 228 | + """ |
| 229 | + return self["accountId"] |
| 230 | + |
| 231 | + @property |
| 232 | + def timestamp(self) -> str: |
| 233 | + """ |
| 234 | + Alarm state change event timestamp in ISO-8601 format. |
| 235 | + """ |
| 236 | + return self["time"] |
| 237 | + |
| 238 | + @property |
| 239 | + def alarm_data(self) -> CloudWatchAlarmData: |
| 240 | + """ |
| 241 | + Contains basic data about the Alarm and its current and previous states. |
| 242 | + """ |
| 243 | + return CloudWatchAlarmData(self["alarmData"]) |
0 commit comments