1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use chrono::{DateTime, Utc};
5use parking_lot::RwLock;
6use serde::{Deserialize, Serialize};
7
8pub type SharedCloudWatchState = Arc<RwLock<CloudWatchAccounts>>;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct CloudWatchSnapshot {
13 pub schema_version: u32,
14 pub accounts: CloudWatchAccounts,
15}
16
17pub const CLOUDWATCH_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
18
19#[derive(Debug, Default, Clone, Serialize, Deserialize)]
20pub struct CloudWatchAccounts {
21 pub accounts: BTreeMap<String, CloudWatchState>,
22}
23
24impl CloudWatchAccounts {
25 pub fn new() -> Self {
26 Self::default()
27 }
28
29 pub fn clone_for_snapshot(&self) -> CloudWatchAccounts {
34 CloudWatchAccounts {
35 accounts: self.accounts.clone(),
36 }
37 }
38
39 pub fn get_or_create(&mut self, account_id: &str) -> &mut CloudWatchState {
40 self.accounts
41 .entry(account_id.to_string())
42 .or_insert_with(|| CloudWatchState::new(account_id))
43 }
44
45 pub fn get(&self, account_id: &str) -> Option<&CloudWatchState> {
46 self.accounts.get(account_id)
47 }
48}
49
50#[derive(Debug, Default, Clone, Serialize, Deserialize)]
51pub struct CloudWatchState {
52 pub account_id: String,
53 pub metrics: BTreeMap<String, BTreeMap<String, Vec<MetricDatum>>>,
55 pub alarms: BTreeMap<String, BTreeMap<String, MetricAlarm>>,
57 #[serde(default)]
59 pub composite_alarms: BTreeMap<String, BTreeMap<String, CompositeAlarm>>,
60 #[serde(default)]
64 pub alarm_history: BTreeMap<String, BTreeMap<String, Vec<AlarmHistoryItem>>>,
65 #[serde(default)]
68 pub dashboards: BTreeMap<String, Dashboard>,
69 #[serde(default)]
71 pub anomaly_detectors: BTreeMap<String, BTreeMap<String, AnomalyDetector>>,
72 #[serde(default)]
74 pub insight_rules: BTreeMap<String, BTreeMap<String, InsightRule>>,
75 #[serde(default)]
77 pub managed_rules: BTreeMap<String, BTreeMap<String, Vec<ManagedRule>>>,
78 #[serde(default)]
80 pub metric_streams: BTreeMap<String, BTreeMap<String, MetricStream>>,
81 #[serde(default)]
83 pub mute_rules: BTreeMap<String, BTreeMap<String, AlarmMuteRule>>,
84 #[serde(default)]
86 pub datasets: BTreeMap<String, BTreeMap<String, Dataset>>,
87 #[serde(default)]
89 pub tags: BTreeMap<String, BTreeMap<String, String>>,
90 #[serde(default)]
92 pub otel_enrichment_running: bool,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Dashboard {
97 pub name: String,
98 pub arn: String,
99 pub body: String,
100 pub last_modified: DateTime<Utc>,
101 pub size_bytes: i64,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct Dataset {
109 pub id: String,
110 pub arn: String,
111 pub kms_key_arn: Option<String>,
112}
113
114impl CloudWatchState {
115 pub fn new(account_id: &str) -> Self {
116 Self {
117 account_id: account_id.to_string(),
118 ..Default::default()
119 }
120 }
121
122 pub fn metrics_in(&self, region: &str) -> Option<&BTreeMap<String, Vec<MetricDatum>>> {
123 self.metrics.get(region)
124 }
125
126 pub fn metrics_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, Vec<MetricDatum>> {
127 self.metrics.entry(region.to_string()).or_default()
128 }
129
130 pub fn alarms_in(&self, region: &str) -> Option<&BTreeMap<String, MetricAlarm>> {
131 self.alarms.get(region)
132 }
133
134 pub fn alarms_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, MetricAlarm> {
135 self.alarms.entry(region.to_string()).or_default()
136 }
137
138 pub fn composite_alarms_in(&self, region: &str) -> Option<&BTreeMap<String, CompositeAlarm>> {
139 self.composite_alarms.get(region)
140 }
141
142 pub fn composite_alarms_in_mut(
143 &mut self,
144 region: &str,
145 ) -> &mut BTreeMap<String, CompositeAlarm> {
146 self.composite_alarms.entry(region.to_string()).or_default()
147 }
148
149 pub fn alarm_history_in(
150 &self,
151 region: &str,
152 ) -> Option<&BTreeMap<String, Vec<AlarmHistoryItem>>> {
153 self.alarm_history.get(region)
154 }
155
156 pub fn alarm_history_in_mut(
157 &mut self,
158 region: &str,
159 ) -> &mut BTreeMap<String, Vec<AlarmHistoryItem>> {
160 self.alarm_history.entry(region.to_string()).or_default()
161 }
162
163 pub fn anomaly_detectors_in(&self, region: &str) -> Option<&BTreeMap<String, AnomalyDetector>> {
164 self.anomaly_detectors.get(region)
165 }
166
167 pub fn anomaly_detectors_in_mut(
168 &mut self,
169 region: &str,
170 ) -> &mut BTreeMap<String, AnomalyDetector> {
171 self.anomaly_detectors
172 .entry(region.to_string())
173 .or_default()
174 }
175
176 pub fn insight_rules_in(&self, region: &str) -> Option<&BTreeMap<String, InsightRule>> {
177 self.insight_rules.get(region)
178 }
179
180 pub fn insight_rules_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, InsightRule> {
181 self.insight_rules.entry(region.to_string()).or_default()
182 }
183
184 pub fn managed_rules_in(&self, region: &str) -> Option<&BTreeMap<String, Vec<ManagedRule>>> {
185 self.managed_rules.get(region)
186 }
187
188 pub fn managed_rules_in_mut(
189 &mut self,
190 region: &str,
191 ) -> &mut BTreeMap<String, Vec<ManagedRule>> {
192 self.managed_rules.entry(region.to_string()).or_default()
193 }
194
195 pub fn metric_streams_in(&self, region: &str) -> Option<&BTreeMap<String, MetricStream>> {
196 self.metric_streams.get(region)
197 }
198
199 pub fn metric_streams_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, MetricStream> {
200 self.metric_streams.entry(region.to_string()).or_default()
201 }
202
203 pub fn mute_rules_in(&self, region: &str) -> Option<&BTreeMap<String, AlarmMuteRule>> {
204 self.mute_rules.get(region)
205 }
206
207 pub fn mute_rules_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, AlarmMuteRule> {
208 self.mute_rules.entry(region.to_string()).or_default()
209 }
210
211 pub fn datasets_in(&self, region: &str) -> Option<&BTreeMap<String, Dataset>> {
212 self.datasets.get(region)
213 }
214
215 pub fn datasets_in_mut(&mut self, region: &str) -> &mut BTreeMap<String, Dataset> {
216 self.datasets.entry(region.to_string()).or_default()
217 }
218}
219
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct MetricDatum {
222 pub metric_name: String,
223 pub dimensions: BTreeMap<String, String>,
224 pub timestamp: DateTime<Utc>,
225 pub value: Option<f64>,
226 pub statistic_values: Option<StatisticSet>,
227 pub unit: Option<String>,
228 pub storage_resolution: Option<i64>,
229}
230
231#[derive(Debug, Clone, Serialize, Deserialize)]
232pub struct StatisticSet {
233 pub sample_count: f64,
234 pub sum: f64,
235 pub minimum: f64,
236 pub maximum: f64,
237}
238
239#[derive(Debug, Clone, Serialize, Deserialize)]
240pub struct MetricAlarm {
241 pub alarm_name: String,
242 pub alarm_arn: String,
243 pub alarm_description: Option<String>,
244 pub actions_enabled: bool,
245 pub ok_actions: Vec<String>,
246 pub alarm_actions: Vec<String>,
247 pub insufficient_data_actions: Vec<String>,
248 pub state_value: AlarmState,
249 pub state_reason: String,
250 pub state_updated_timestamp: DateTime<Utc>,
251 pub metric_name: Option<String>,
252 pub namespace: Option<String>,
253 pub statistic: Option<String>,
254 pub extended_statistic: Option<String>,
255 pub dimensions: BTreeMap<String, String>,
256 pub period: Option<i64>,
257 pub unit: Option<String>,
258 pub evaluation_periods: i64,
259 pub datapoints_to_alarm: Option<i64>,
260 pub threshold: Option<f64>,
261 pub comparison_operator: String,
262 pub treat_missing_data: Option<String>,
263 pub evaluate_low_sample_count_percentile: Option<String>,
264 #[serde(default)]
268 pub threshold_metric_id: Option<String>,
269 pub configuration_updated_timestamp: DateTime<Utc>,
270 pub alarm_configuration_updated_timestamp: DateTime<Utc>,
271}
272
273#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
274pub enum AlarmState {
275 Ok,
276 Alarm,
277 InsufficientData,
278}
279
280impl AlarmState {
281 pub fn as_str(&self) -> &'static str {
282 match self {
283 AlarmState::Ok => "OK",
284 AlarmState::Alarm => "ALARM",
285 AlarmState::InsufficientData => "INSUFFICIENT_DATA",
286 }
287 }
288
289 pub fn parse(s: &str) -> Option<Self> {
290 match s {
291 "OK" => Some(AlarmState::Ok),
292 "ALARM" => Some(AlarmState::Alarm),
293 "INSUFFICIENT_DATA" => Some(AlarmState::InsufficientData),
294 _ => None,
295 }
296 }
297}
298
299#[derive(Debug, Clone, Serialize, Deserialize)]
301pub struct AlarmHistoryItem {
302 pub alarm_name: String,
303 pub alarm_type: String,
305 pub timestamp: DateTime<Utc>,
306 pub history_item_type: String,
309 pub history_summary: String,
310 pub history_data: String,
312}
313
314#[derive(Debug, Clone, Serialize, Deserialize)]
316pub struct CompositeAlarm {
317 pub alarm_name: String,
318 pub alarm_arn: String,
319 pub alarm_description: Option<String>,
320 pub alarm_rule: String,
321 pub actions_enabled: bool,
322 pub ok_actions: Vec<String>,
323 pub alarm_actions: Vec<String>,
324 pub insufficient_data_actions: Vec<String>,
325 pub actions_suppressor: Option<String>,
326 pub actions_suppressor_wait_period: Option<i64>,
327 pub actions_suppressor_extension_period: Option<i64>,
328 pub state_value: AlarmState,
329 pub state_reason: String,
330 pub state_updated_timestamp: DateTime<Utc>,
331 pub alarm_configuration_updated_timestamp: DateTime<Utc>,
332}
333
334#[derive(Debug, Clone, Serialize, Deserialize)]
336pub struct AnomalyDetector {
337 pub key: String,
340 pub namespace: Option<String>,
341 pub metric_name: Option<String>,
342 pub stat: Option<String>,
343 pub dimensions: BTreeMap<String, String>,
344 pub metric_math: bool,
345 pub state_value: String,
346}
347
348#[derive(Debug, Clone, Serialize, Deserialize)]
350pub struct InsightRule {
351 pub name: String,
352 pub state: String,
353 pub schema: String,
354 pub definition: String,
355 pub managed: bool,
356 pub apply_on_transformed_logs: bool,
357}
358
359#[derive(Debug, Clone, Serialize, Deserialize)]
361pub struct ManagedRule {
362 pub template_name: String,
363 pub resource_arn: String,
364}
365
366#[derive(Debug, Clone, Serialize, Deserialize)]
368pub struct MetricStream {
369 pub name: String,
370 pub arn: String,
371 pub firehose_arn: String,
372 pub role_arn: String,
373 pub output_format: String,
374 pub state: String,
376 pub include_filters: Vec<MetricStreamFilter>,
377 pub exclude_filters: Vec<MetricStreamFilter>,
378 pub include_linked_accounts_metrics: bool,
379 pub creation_date: DateTime<Utc>,
380 pub last_update_date: DateTime<Utc>,
381}
382
383#[derive(Debug, Clone, Serialize, Deserialize)]
384pub struct MetricStreamFilter {
385 pub namespace: Option<String>,
386 pub metric_names: Vec<String>,
387}
388
389#[derive(Debug, Clone, Serialize, Deserialize)]
391pub struct AlarmMuteRule {
392 pub name: String,
393 pub arn: String,
394 pub description: Option<String>,
395 pub schedule_expression: Option<String>,
396 pub schedule_duration: Option<String>,
397 pub schedule_timezone: Option<String>,
398 pub mute_target_alarm_names: Vec<String>,
399 pub start_date: Option<DateTime<Utc>>,
400 pub expire_date: Option<DateTime<Utc>>,
401 pub last_updated_timestamp: DateTime<Utc>,
402}