-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathalert_structs.rs
More file actions
917 lines (826 loc) · 28.3 KB
/
alert_structs.rs
File metadata and controls
917 lines (826 loc) · 28.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use std::{collections::HashMap, time::Duration};
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::{RwLock, mpsc};
use ulid::Ulid;
use crate::{
alerts::{
AlertError, CURRENT_ALERTS_VERSION,
alert_enums::{
AlertOperator, AlertState, AlertTask, AlertType, AlertVersion, EvalConfig,
LogicalOperator, NotificationState, Severity, WhereConfigOperator,
},
alert_traits::AlertTrait,
target::{NotificationConfig, TARGETS},
},
metastore::metastore_traits::MetastoreObject,
query::resolve_stream_names,
storage::object_storage::{alert_json_path, alert_state_json_path, mttr_json_path},
};
const RESERVED_FIELDS: &[&str] = &[
"version",
"id",
"severity",
"title",
"query",
"datasets",
"alertType",
"alert_type",
"anomalyConfig",
"anomaly_config",
"forecastConfig",
"forecast_config",
"thresholdConfig",
"threshold_config",
"evalConfig",
"eval_config",
"targets",
"state",
"notificationState",
"notification_state",
"notificationConfig",
"notification_config",
"created",
"tags",
"lastTriggeredAt",
"last_triggered_at",
];
/// Helper struct for basic alert fields during migration
pub struct BasicAlertFields {
pub id: Ulid,
pub title: String,
pub severity: Severity,
}
#[derive(Debug)]
pub struct Alerts {
pub alerts: RwLock<HashMap<Ulid, Box<dyn AlertTrait>>>,
pub sender: mpsc::Sender<AlertTask>,
}
#[derive(Debug, Clone)]
pub struct Context {
pub alert_info: AlertInfo,
pub deployment_info: DeploymentInfo,
pub message: String,
pub notification_config: NotificationConfig,
}
impl Context {
pub fn new(
alert_info: AlertInfo,
deployment_info: DeploymentInfo,
notification_config: NotificationConfig,
message: String,
) -> Self {
Self {
alert_info,
deployment_info,
message,
notification_config,
}
}
pub(crate) fn default_resolved_string(&self) -> String {
format!("{} is now `not-triggered` ", self.alert_info.alert_name)
}
pub(crate) fn default_disabled_string(&self) -> String {
format!(
"{} is now `disabled`. No more evals will be run till the sate is `disabled`.",
self.alert_info.alert_name
)
}
}
#[derive(Debug, Clone)]
pub struct AlertInfo {
pub alert_id: Ulid,
pub alert_name: String,
// message: String,
// reason: String,
pub alert_state: AlertState,
pub notification_state: NotificationState,
pub severity: String,
}
impl AlertInfo {
pub fn new(
alert_id: Ulid,
alert_name: String,
alert_state: AlertState,
notification_state: NotificationState,
severity: String,
) -> Self {
Self {
alert_id,
alert_name,
alert_state,
notification_state,
severity,
}
}
}
#[derive(Debug, Clone)]
pub struct DeploymentInfo {
pub deployment_instance: String,
pub deployment_id: Ulid,
pub deployment_mode: String,
}
impl DeploymentInfo {
pub fn new(deployment_instance: String, deployment_id: Ulid, deployment_mode: String) -> Self {
Self {
deployment_instance,
deployment_id,
deployment_mode,
}
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct OperationConfig {
pub column: String,
pub operator: Option<String>,
pub value: Option<String>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FilterConfig {
pub conditions: Vec<Conditions>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct ConditionConfig {
pub column: String,
pub operator: WhereConfigOperator,
pub value: Option<String>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Conditions {
pub operator: Option<LogicalOperator>,
pub condition_config: Vec<ConditionConfig>,
}
impl Conditions {
pub fn generate_filter_message(&self) -> String {
match &self.operator {
Some(op) => match op {
LogicalOperator::And | LogicalOperator::Or => {
let expr1 = &self.condition_config[0];
let expr2 = &self.condition_config[1];
let expr1_msg = if expr1.value.as_ref().is_some_and(|v| !v.is_empty()) {
format!(
"{} {} {}",
expr1.column,
expr1.operator,
expr1.value.as_ref().unwrap()
)
} else {
format!("{} {}", expr1.column, expr1.operator)
};
let expr2_msg = if expr2.value.as_ref().is_some_and(|v| !v.is_empty()) {
format!(
"{} {} {}",
expr2.column,
expr2.operator,
expr2.value.as_ref().unwrap()
)
} else {
format!("{} {}", expr2.column, expr2.operator)
};
format!("[{expr1_msg} {op} {expr2_msg}]")
}
},
None => {
let expr = &self.condition_config[0];
if let Some(val) = &expr.value {
format!("{} {} {}", expr.column, expr.operator, val)
} else {
format!("{} {}", expr.column, expr.operator)
}
}
}
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GroupBy {
pub columns: Vec<String>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ThresholdConfig {
pub operator: AlertOperator,
pub value: f64,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RollingWindow {
// x minutes (25m)
pub eval_start: String,
// should always be "now"
pub eval_end: String,
// x minutes (5m)
pub eval_frequency: u64,
}
impl Default for RollingWindow {
fn default() -> Self {
Self {
eval_start: "10m".into(),
eval_end: "now".into(),
eval_frequency: 10,
}
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AlertRequest {
#[serde(default = "Severity::default")]
pub severity: Severity,
pub title: String,
pub query: String,
pub alert_type: String,
pub anomaly_config: Option<AnomalyConfig>,
pub forecast_config: Option<ForecastConfig>,
pub threshold_config: ThresholdConfig,
#[serde(default)]
pub notification_config: NotificationConfig,
pub eval_config: EvalConfig,
pub targets: Vec<Ulid>,
pub tags: Option<Vec<String>>,
#[serde(flatten)]
pub other_fields: Option<serde_json::Map<String, Value>>,
}
impl AlertRequest {
pub async fn into(self) -> Result<AlertConfig, AlertError> {
// Validate that other_fields doesn't contain reserved field names
let other_fields = if let Some(mut other_fields) = self.other_fields {
// Limit other_fields to maximum 10 fields
if other_fields.len() > 10 {
return Err(AlertError::ValidationFailure(format!(
"other_fields can contain at most 10 fields, found {}",
other_fields.len()
)));
}
for reserved in RESERVED_FIELDS {
if other_fields.remove(*reserved).is_some() {
tracing::warn!("Removed reserved field '{}' from other_fields", reserved);
}
}
if other_fields.is_empty() {
None
} else {
Some(other_fields)
}
} else {
None
};
// Validate that all target IDs exist
for id in &self.targets {
TARGETS.get_target_by_id(id).await?;
}
let datasets = resolve_stream_names(&self.query)?;
if datasets.len() != 1 {
return Err(AlertError::ValidationFailure(format!(
"Query should include only one dataset. Found: {datasets:?}"
)));
}
let created_timestamp = Utc::now();
let config = AlertConfig {
version: AlertVersion::from(CURRENT_ALERTS_VERSION),
id: Ulid::new(),
severity: self.severity,
title: self.title,
query: self.query,
datasets,
alert_type: {
match self.alert_type.as_str() {
"anomaly" => {
if let Some(conf) = self.anomaly_config {
AlertType::Anomaly(conf)
} else {
return Err(AlertError::Metadata(
"anomalyConfig is required for anomaly type alerts",
));
}
}
"forecast" => {
if let Some(conf) = self.forecast_config {
AlertType::Forecast(conf)
} else {
return Err(AlertError::Metadata(
"forecastConfig is required for forecast type alerts",
));
}
}
"threshold" => AlertType::Threshold,
_ => return Err(AlertError::Metadata("Invalid alert type provided")),
}
},
threshold_config: self.threshold_config,
eval_config: self.eval_config,
targets: self.targets,
state: AlertState::default(),
notification_state: NotificationState::Notify,
notification_config: self.notification_config,
created: created_timestamp,
tags: self.tags,
last_triggered_at: None,
other_fields,
};
Ok(config)
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AlertConfig {
pub version: AlertVersion,
#[serde(default)]
pub id: Ulid,
pub severity: Severity,
pub title: String,
pub query: String,
pub datasets: Vec<String>,
pub alert_type: AlertType,
pub threshold_config: ThresholdConfig,
pub eval_config: EvalConfig,
pub targets: Vec<Ulid>,
// for new alerts, state should be resolved
#[serde(default)]
pub state: AlertState,
pub notification_state: NotificationState,
pub notification_config: NotificationConfig,
pub created: DateTime<Utc>,
pub tags: Option<Vec<String>>,
pub last_triggered_at: Option<DateTime<Utc>>,
#[serde(flatten)]
pub other_fields: Option<serde_json::Map<String, Value>>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AlertConfigResponse {
pub version: AlertVersion,
#[serde(default)]
pub id: Ulid,
pub severity: Severity,
pub title: String,
pub query: String,
pub datasets: Vec<String>,
pub alert_type: &'static str,
pub anomaly_config: Option<AnomalyConfig>,
pub forecast_config: Option<ForecastConfig>,
pub threshold_config: ThresholdConfig,
pub eval_config: EvalConfig,
pub targets: Vec<Ulid>,
// for new alerts, state should be resolved
#[serde(default)]
pub state: AlertState,
pub notification_state: NotificationState,
pub notification_config: NotificationConfig,
pub created: DateTime<Utc>,
pub tags: Option<Vec<String>>,
pub last_triggered_at: Option<DateTime<Utc>>,
#[serde(flatten)]
pub other_fields: Option<serde_json::Map<String, Value>>,
}
impl AlertConfig {
/// Filters out reserved field names from other_fields
/// This prevents conflicts when flattening other_fields during serialization
pub fn sanitize_other_fields(&mut self) {
if let Some(ref mut other_fields) = self.other_fields {
for reserved in RESERVED_FIELDS {
if other_fields.remove(*reserved).is_some() {
tracing::warn!(
"Removed reserved field '{}' from other_fields during sanitization",
reserved
);
}
}
if other_fields.is_empty() {
self.other_fields = None;
}
}
}
pub fn to_response(self) -> AlertConfigResponse {
AlertConfigResponse {
version: self.version,
id: self.id,
severity: self.severity,
title: self.title,
query: self.query,
datasets: self.datasets,
alert_type: {
match self.alert_type {
AlertType::Threshold => "threshold",
AlertType::Anomaly(_) => "anomaly",
AlertType::Forecast(_) => "forecast",
}
},
anomaly_config: {
match &self.alert_type {
AlertType::Anomaly(conf) => Some(conf.clone()),
_ => None,
}
},
forecast_config: {
match self.alert_type {
AlertType::Forecast(conf) => Some(conf),
_ => None,
}
},
threshold_config: self.threshold_config,
eval_config: self.eval_config,
targets: self.targets,
state: self.state,
notification_state: self.notification_state,
notification_config: self.notification_config,
created: self.created,
tags: self.tags,
last_triggered_at: self.last_triggered_at,
other_fields: self.other_fields,
}
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AlertsSummary {
pub total: u64,
pub triggered: AlertsInfoByState,
pub disabled: AlertsInfoByState,
pub not_triggered: AlertsInfoByState,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AlertsInfoByState {
pub total: u64,
pub alert_info: Vec<AlertsInfo>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AlertsInfo {
pub title: String,
pub id: Ulid,
pub severity: Severity,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ForecastConfig {
pub historic_duration: String,
pub forecast_duration: String,
}
impl ForecastConfig {
pub fn calculate_eval_window(&self) -> Result<String, AlertError> {
let parsed_historic_duration =
if let Ok(historic_duration) = humantime::parse_duration(&self.historic_duration) {
historic_duration
} else {
return Err(AlertError::Metadata(
"historicDuration should be of type humantime",
));
};
let eval_window = if parsed_historic_duration.lt(&Duration::from_secs(60 * 60 * 24 * 3)) {
// less than 3 days = 10 mins
"10m"
} else {
"30m"
};
Ok(eval_window.into())
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AnomalyConfig {
pub historic_duration: String,
}
impl AnomalyConfig {
pub fn calculate_eval_window(&self) -> Result<String, AlertError> {
let parsed_historic_duration =
if let Ok(historic_duration) = humantime::parse_duration(&self.historic_duration) {
historic_duration
} else {
return Err(AlertError::Metadata(
"historicDuration should be of type humantime",
));
};
let eval_window = if parsed_historic_duration.lt(&Duration::from_secs(60 * 60 * 24 * 3)) {
// less than 3 days = 10 mins
"10m"
} else {
"30m"
};
Ok(eval_window.into())
}
}
/// Result structure for alert query execution with group support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertQueryResult {
/// List of group results, each containing group values and the aggregate value
pub groups: Vec<GroupResult>,
/// True if this is a simple query without GROUP BY (single group with empty group_values)
pub is_simple_query: bool,
}
/// Result for a single group in a GROUP BY query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupResult {
/// The group-by column values (empty for non-GROUP BY queries)
pub group_values: HashMap<String, String>,
/// The aggregate function value for this group
pub aggregate_value: f64,
}
impl AlertQueryResult {
/// Get the single aggregate value for simple queries (backward compatibility)
pub fn get_single_value(&self) -> f64 {
if self.is_simple_query && !self.groups.is_empty() {
self.groups[0].aggregate_value
} else {
0.0
}
}
}
#[derive(Deserialize)]
pub struct NotificationStateRequest {
pub state: String,
}
/// MTTR (Mean Time To Recovery) statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MTTRStats {
/// Total number of incidents (triggered -> not-triggered cycles)
pub total_incidents: usize,
/// Mean recovery time in seconds
pub mean_seconds: f64,
/// Median recovery time in seconds
pub median_seconds: f64,
/// Minimum recovery time in seconds
pub min_seconds: f64,
/// Maximum recovery time in seconds
pub max_seconds: f64,
/// All individual recovery times in seconds
pub recovery_times_seconds: Vec<i64>,
}
impl MTTRStats {
/// Check if there are no incidents recorded
pub fn is_empty(&self) -> bool {
self.total_incidents == 0
}
/// Create MTTRStats from a list of recovery times
pub fn from_recovery_times(recovery_times: Vec<i64>) -> MTTRStats {
if recovery_times.is_empty() {
return MTTRStats::default();
}
let total_incidents = recovery_times.len();
let total_recovery_time: i64 = recovery_times.iter().sum();
let mean_seconds = total_recovery_time as f64 / total_incidents as f64;
let min_seconds = *recovery_times.iter().min().unwrap() as f64;
let max_seconds = *recovery_times.iter().max().unwrap() as f64;
// Calculate median
let median_seconds = if total_incidents == 1 {
recovery_times[0] as f64
} else {
let mut sorted_times = recovery_times.clone();
sorted_times.sort_unstable();
if total_incidents.is_multiple_of(2) {
let mid = total_incidents / 2;
(sorted_times[mid - 1] + sorted_times[mid]) as f64 / 2.0
} else {
sorted_times[total_incidents / 2] as f64
}
};
MTTRStats {
total_incidents,
mean_seconds,
median_seconds,
min_seconds,
max_seconds,
recovery_times_seconds: recovery_times,
}
}
}
impl Default for MTTRStats {
fn default() -> Self {
Self {
total_incidents: 0,
mean_seconds: 0.0,
median_seconds: 0.0,
min_seconds: 0.0,
max_seconds: 0.0,
recovery_times_seconds: Vec::new(),
}
}
}
/// Aggregated MTTR statistics across multiple alerts
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AggregatedMTTRStats {
/// Overall MTTR statistics
pub overall: MTTRStats,
/// Number of alerts included in the calculation
pub total_alerts: usize,
/// Number of alerts that had incidents
pub alerts_with_incidents: usize,
/// Per-alert breakdown (optional, for detailed analysis)
pub per_alert_stats: HashMap<String, MTTRStats>,
}
/// Daily MTTR statistics for a specific date
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DailyMTTRStats {
/// Date in YYYY-MM-DD format
pub date: NaiveDate,
/// Aggregated MTTR statistics for this date
pub stats: AggregatedMTTRStats,
}
/// MTTR history containing array of daily MTTR objects
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MTTRHistory {
/// Array of daily MTTR statistics
pub daily_stats: Vec<DailyMTTRStats>,
}
/// Query parameters for MTTR API endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MTTRQueryParams {
pub start_date: Option<String>,
pub end_date: Option<String>,
}
impl AggregatedMTTRStats {
/// Calculate aggregated MTTR stats from multiple alert state entries
pub fn from_alert_states(alert_states: Vec<AlertStateEntry>) -> Self {
let mut all_recovery_times = Vec::new();
let mut per_alert_stats = HashMap::new();
let mut alerts_with_incidents = 0;
for alert_state in &alert_states {
let alert_stats = alert_state.get_mttr_stats();
if !alert_stats.is_empty() {
alerts_with_incidents += 1;
all_recovery_times.extend(alert_stats.recovery_times_seconds.iter());
per_alert_stats.insert(alert_state.alert_id.to_string(), alert_stats);
}
}
let overall = MTTRStats::from_recovery_times(all_recovery_times);
Self {
overall,
total_alerts: alert_states.len(),
alerts_with_incidents,
per_alert_stats,
}
}
}
/// Represents a single state transition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateTransition {
/// The alert state
pub state: AlertState,
/// Timestamp when this state was set/updated
pub last_updated_at: DateTime<Utc>,
/// The previous alert state before this transition, if any
pub previous_alert_state: Option<AlertState>,
/// Duration in seconds
pub previous_state_duration: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertStateEntry {
/// The unique identifier for the alert
pub alert_id: Ulid,
pub states: Vec<StateTransition>,
}
impl StateTransition {
/// Creates a new state transition with the current timestamp
pub fn new(
state: AlertState,
previous_alert_state: Option<AlertState>,
previous_alert_time: Option<DateTime<Utc>>,
) -> Self {
let now = Utc::now();
// calculate duration if previous alert time is provided
let previous_state_duration =
previous_alert_time.map(|alert_time| (now - alert_time).num_seconds());
Self {
state,
last_updated_at: now,
previous_alert_state,
previous_state_duration,
}
}
}
impl AlertStateEntry {
/// Creates a new alert state entry with an initial state
pub fn new(alert_id: Ulid, initial_state: AlertState) -> Self {
Self {
alert_id,
states: vec![StateTransition::new(initial_state, None, None)],
}
}
/// Updates the state (only adds new entry if state has changed)
/// Returns true if the state was changed, false if it remained the same
pub fn update_state(&mut self, new_state: AlertState) -> bool {
match self.states.last() {
Some(last_transition) => {
if last_transition.state != new_state {
// State changed - add new transition
self.states.push(StateTransition::new(
new_state,
Some(last_transition.state),
Some(last_transition.last_updated_at),
));
true
} else {
// If state hasn't changed, do nothing - preserve the original timestamp
false
}
}
None => {
// No previous states - add the first one
self.states
.push(StateTransition::new(new_state, None, None));
true
}
}
}
/// Gets the current (latest) state
pub fn current_state(&self) -> Option<&StateTransition> {
self.states.last()
}
/// Get all recovery times (in seconds) from triggered to not-triggered
/// Returns recovery times in chronological order
pub fn get_recovery_times(&self) -> Vec<i64> {
let mut recovery_times = Vec::new();
let mut trigger_time: Option<DateTime<Utc>> = None;
// Create a sorted view without mutating the original
let mut sorted_states = self.states.clone();
sorted_states.sort_by(|a, b| a.last_updated_at.cmp(&b.last_updated_at));
for transition in &sorted_states {
match transition.state {
AlertState::Triggered => {
// Record when alert was triggered
trigger_time = Some(transition.last_updated_at);
}
AlertState::NotTriggered => {
// If we have a trigger time, calculate recovery time
if let Some(triggered_at) = trigger_time {
let recovery_duration = transition
.last_updated_at
.signed_duration_since(triggered_at);
let recovery_seconds = recovery_duration.num_seconds();
// Only include positive durations (validation against clock issues)
if recovery_seconds > 0 {
recovery_times.push(recovery_seconds);
} else {
tracing::warn!(
"Negative or zero recovery time detected: {} seconds. Triggered at: {}, Recovered at: {}",
recovery_seconds,
triggered_at,
transition.last_updated_at
);
}
trigger_time = None; // Reset for next cycle
}
}
AlertState::Disabled => {
// Ignore disabled state - it doesn't affect MTTR calculation
// until it's explicitly resolved (moves to not-triggered)
}
}
}
recovery_times
}
/// This is the method that is used for MTTR statistics
pub fn get_mttr_stats(&self) -> MTTRStats {
let recovery_times = self.get_recovery_times();
MTTRStats::from_recovery_times(recovery_times)
}
}
impl MetastoreObject for AlertStateEntry {
fn get_object_id(&self) -> String {
self.alert_id.to_string()
}
fn get_object_path(&self) -> String {
alert_state_json_path(self.alert_id).to_string()
}
}
impl MetastoreObject for AlertConfig {
fn get_object_id(&self) -> String {
self.id.to_string()
}
fn get_object_path(&self) -> String {
alert_json_path(self.id).to_string()
}
}
impl MetastoreObject for MTTRHistory {
fn get_object_id(&self) -> String {
"mttr".to_string()
}
fn get_object_path(&self) -> String {
mttr_json_path().to_string()
}
}