|
| 1 | +use std::fmt::{self, Display}; |
| 2 | + |
| 3 | +use derive_more::derive::FromStr; |
| 4 | +use ulid::Ulid; |
| 5 | + |
| 6 | +use crate::alerts::{alert_structs::RollingWindow, alert_traits::AlertTrait}; |
| 7 | + |
| 8 | +pub enum AlertTask { |
| 9 | + Create(Box<dyn AlertTrait>), |
| 10 | + Delete(Ulid), |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 14 | +#[serde(rename_all = "lowercase")] |
| 15 | +pub enum AlertVersion { |
| 16 | + V1, |
| 17 | + #[default] |
| 18 | + V2, |
| 19 | +} |
| 20 | + |
| 21 | +impl From<&str> for AlertVersion { |
| 22 | + fn from(value: &str) -> Self { |
| 23 | + match value { |
| 24 | + "v1" => Self::V1, |
| 25 | + "v2" => Self::V2, |
| 26 | + _ => Self::V2, // default to v2 |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Default)] |
| 32 | +#[serde(rename_all = "camelCase")] |
| 33 | +pub enum Severity { |
| 34 | + Critical, |
| 35 | + High, |
| 36 | + #[default] |
| 37 | + Medium, |
| 38 | + Low, |
| 39 | +} |
| 40 | + |
| 41 | +impl Display for Severity { |
| 42 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 | + match self { |
| 44 | + Severity::Critical => write!(f, "Critical"), |
| 45 | + Severity::High => write!(f, "High"), |
| 46 | + Severity::Medium => write!(f, "Medium"), |
| 47 | + Severity::Low => write!(f, "Low"), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 53 | +#[serde(rename_all = "camelCase")] |
| 54 | +pub enum LogicalOperator { |
| 55 | + And, |
| 56 | + Or, |
| 57 | +} |
| 58 | + |
| 59 | +impl Display for LogicalOperator { |
| 60 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 61 | + match self { |
| 62 | + LogicalOperator::And => write!(f, "AND"), |
| 63 | + LogicalOperator::Or => write!(f, "OR"), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)] |
| 69 | +#[serde(rename_all = "camelCase")] |
| 70 | +pub enum AlertType { |
| 71 | + Threshold, |
| 72 | + Anomaly, |
| 73 | + Forecast, |
| 74 | +} |
| 75 | + |
| 76 | +impl Display for AlertType { |
| 77 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 78 | + match self { |
| 79 | + AlertType::Threshold => write!(f, "threshold"), |
| 80 | + AlertType::Anomaly => write!(f, "anomaly"), |
| 81 | + AlertType::Forecast => write!(f, "forecast"), |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 87 | +#[serde(rename_all = "camelCase")] |
| 88 | +pub enum AlertOperator { |
| 89 | + #[serde(rename = ">")] |
| 90 | + GreaterThan, |
| 91 | + #[serde(rename = "<")] |
| 92 | + LessThan, |
| 93 | + #[serde(rename = "=")] |
| 94 | + Equal, |
| 95 | + #[serde(rename = "!=")] |
| 96 | + NotEqual, |
| 97 | + #[serde(rename = ">=")] |
| 98 | + GreaterThanOrEqual, |
| 99 | + #[serde(rename = "<=")] |
| 100 | + LessThanOrEqual, |
| 101 | +} |
| 102 | + |
| 103 | +impl Display for AlertOperator { |
| 104 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 105 | + match self { |
| 106 | + AlertOperator::GreaterThan => write!(f, ">"), |
| 107 | + AlertOperator::LessThan => write!(f, "<"), |
| 108 | + AlertOperator::Equal => write!(f, "="), |
| 109 | + AlertOperator::NotEqual => write!(f, "!="), |
| 110 | + AlertOperator::GreaterThanOrEqual => write!(f, ">="), |
| 111 | + AlertOperator::LessThanOrEqual => write!(f, "<="), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, FromStr, PartialEq, Eq)] |
| 117 | +#[serde(rename_all = "camelCase")] |
| 118 | +pub enum WhereConfigOperator { |
| 119 | + #[serde(rename = "=")] |
| 120 | + Equal, |
| 121 | + #[serde(rename = "!=")] |
| 122 | + NotEqual, |
| 123 | + #[serde(rename = "<")] |
| 124 | + LessThan, |
| 125 | + #[serde(rename = ">")] |
| 126 | + GreaterThan, |
| 127 | + #[serde(rename = "<=")] |
| 128 | + LessThanOrEqual, |
| 129 | + #[serde(rename = ">=")] |
| 130 | + GreaterThanOrEqual, |
| 131 | + #[serde(rename = "is null")] |
| 132 | + IsNull, |
| 133 | + #[serde(rename = "is not null")] |
| 134 | + IsNotNull, |
| 135 | + #[serde(rename = "ilike")] |
| 136 | + ILike, |
| 137 | + #[serde(rename = "contains")] |
| 138 | + Contains, |
| 139 | + #[serde(rename = "begins with")] |
| 140 | + BeginsWith, |
| 141 | + #[serde(rename = "ends with")] |
| 142 | + EndsWith, |
| 143 | + #[serde(rename = "does not contain")] |
| 144 | + DoesNotContain, |
| 145 | + #[serde(rename = "does not begin with")] |
| 146 | + DoesNotBeginWith, |
| 147 | + #[serde(rename = "does not end with")] |
| 148 | + DoesNotEndWith, |
| 149 | +} |
| 150 | + |
| 151 | +impl WhereConfigOperator { |
| 152 | + /// Convert the enum value to its string representation |
| 153 | + pub fn as_str(&self) -> &'static str { |
| 154 | + match self { |
| 155 | + Self::Equal => "=", |
| 156 | + Self::NotEqual => "!=", |
| 157 | + Self::LessThan => "<", |
| 158 | + Self::GreaterThan => ">", |
| 159 | + Self::LessThanOrEqual => "<=", |
| 160 | + Self::GreaterThanOrEqual => ">=", |
| 161 | + Self::IsNull => "is null", |
| 162 | + Self::IsNotNull => "is not null", |
| 163 | + Self::ILike => "ilike", |
| 164 | + Self::Contains => "contains", |
| 165 | + Self::BeginsWith => "begins with", |
| 166 | + Self::EndsWith => "ends with", |
| 167 | + Self::DoesNotContain => "does not contain", |
| 168 | + Self::DoesNotBeginWith => "does not begin with", |
| 169 | + Self::DoesNotEndWith => "does not end with", |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl Display for WhereConfigOperator { |
| 175 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 176 | + // We can reuse our as_str method to get the string representation |
| 177 | + write!(f, "{}", self.as_str()) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 182 | +#[serde(rename_all = "camelCase")] |
| 183 | +pub enum AggregateFunction { |
| 184 | + Avg, |
| 185 | + Count, |
| 186 | + CountDistinct, |
| 187 | + Min, |
| 188 | + Max, |
| 189 | + Sum, |
| 190 | +} |
| 191 | + |
| 192 | +impl Display for AggregateFunction { |
| 193 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 194 | + match self { |
| 195 | + AggregateFunction::Avg => write!(f, "Avg"), |
| 196 | + AggregateFunction::Count => write!(f, "Count"), |
| 197 | + AggregateFunction::CountDistinct => write!(f, "CountDistinct"), |
| 198 | + AggregateFunction::Min => write!(f, "Min"), |
| 199 | + AggregateFunction::Max => write!(f, "Max"), |
| 200 | + AggregateFunction::Sum => write!(f, "Sum"), |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 206 | +#[serde(rename_all = "camelCase")] |
| 207 | +pub enum EvalConfig { |
| 208 | + RollingWindow(RollingWindow), |
| 209 | +} |
| 210 | + |
| 211 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Default, FromStr)] |
| 212 | +#[serde(rename_all = "camelCase")] |
| 213 | +pub enum AlertState { |
| 214 | + Triggered, |
| 215 | + #[default] |
| 216 | + NotTriggered, |
| 217 | + Paused, |
| 218 | +} |
| 219 | + |
| 220 | +impl Display for AlertState { |
| 221 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 222 | + match self { |
| 223 | + AlertState::Triggered => write!(f, "triggered"), |
| 224 | + AlertState::Paused => write!(f, "paused"), |
| 225 | + AlertState::NotTriggered => write!(f, "not-triggered"), |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Default)] |
| 231 | +#[serde(rename_all = "camelCase")] |
| 232 | +pub enum NotificationState { |
| 233 | + #[default] |
| 234 | + Notify, |
| 235 | + /// Snoozed means the alert will evaluate but no notifications will be sent out |
| 236 | + /// |
| 237 | + /// It is a state which can only be set manually |
| 238 | + /// |
| 239 | + /// user needs to pass the timestamp or the duration (in human time) till which the alert is silenced |
| 240 | + Snoozed(String), |
| 241 | +} |
| 242 | + |
| 243 | +impl Display for NotificationState { |
| 244 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 245 | + match self { |
| 246 | + NotificationState::Notify => write!(f, "notify"), |
| 247 | + NotificationState::Snoozed(end_time) => write!(f, "snoozed till {end_time}"), |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments