saluki_core/data_model/event/eventd/
mod.rs1use std::{fmt, num::NonZeroU64};
4
5use serde::{Serialize, Serializer};
6use stringtheory::MetaString;
7
8use crate::data_model::tags::TagSet;
9
10pub const PRIORITY_LOW: &str = "low";
12
13pub const ALERT_TYPE_ERROR: &str = "error";
15
16pub const ALERT_TYPE_WARNING: &str = "warning";
18
19pub const ALERT_TYPE_SUCCESS: &str = "success";
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum AlertType {
25 Info,
27
28 Error,
30
31 Warning,
33
34 Success,
36}
37
38impl fmt::Display for AlertType {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.write_str(self.as_str())
41 }
42}
43
44impl Serialize for AlertType {
45 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
46 where
47 S: Serializer,
48 {
49 serializer.serialize_str(self.as_str())
50 }
51}
52
53impl AlertType {
54 pub fn try_from_string(alert_type: &str) -> Option<Self> {
58 match alert_type {
59 ALERT_TYPE_ERROR => Some(AlertType::Error),
60 ALERT_TYPE_WARNING => Some(AlertType::Warning),
61 ALERT_TYPE_SUCCESS => Some(AlertType::Success),
62 _ => Some(AlertType::Info),
63 }
64 }
65
66 pub const fn as_str(self) -> &'static str {
68 match self {
69 AlertType::Info => "info",
70 AlertType::Error => "error",
71 AlertType::Warning => "warning",
72 AlertType::Success => "success",
73 }
74 }
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub enum Priority {
80 Normal,
82
83 Low,
85}
86
87impl fmt::Display for Priority {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 f.write_str(self.as_str())
90 }
91}
92
93impl Serialize for Priority {
94 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
95 where
96 S: Serializer,
97 {
98 serializer.serialize_str(self.as_str())
99 }
100}
101
102impl Priority {
103 pub fn try_from_string(priority: &str) -> Option<Self> {
107 match priority {
108 PRIORITY_LOW => Some(Priority::Low),
109 _ => Some(Priority::Normal),
110 }
111 }
112
113 pub const fn as_str(self) -> &'static str {
115 match self {
116 Priority::Normal => "normal",
117 Priority::Low => "low",
118 }
119 }
120}
121
122#[derive(Clone, Debug, PartialEq, Serialize)]
124pub struct EventD {
125 title: MetaString,
126 text: MetaString,
127 timestamp: Option<NonZeroU64>,
128 #[serde(skip_serializing_if = "MetaString::is_empty")]
129 hostname: MetaString,
130 #[serde(skip_serializing_if = "MetaString::is_empty")]
131 aggregation_key: MetaString,
132 priority: Option<Priority>,
133 #[serde(skip_serializing_if = "MetaString::is_empty")]
134 source_type_name: MetaString,
135 alert_type: Option<AlertType>,
136 tags: TagSet,
137 origin_tags: TagSet,
138}
139
140impl EventD {
141 pub fn title(&self) -> &str {
143 &self.title
144 }
145
146 pub fn text(&self) -> &str {
148 &self.text
149 }
150
151 pub fn hostname(&self) -> Option<&str> {
153 if self.hostname.is_empty() {
154 None
155 } else {
156 Some(&self.hostname)
157 }
158 }
159
160 pub fn aggregation_key(&self) -> Option<&str> {
162 if self.aggregation_key.is_empty() {
163 None
164 } else {
165 Some(&self.aggregation_key)
166 }
167 }
168
169 pub fn priority(&self) -> Option<Priority> {
171 self.priority
172 }
173
174 pub fn source_type_name(&self) -> Option<&str> {
176 if self.source_type_name.is_empty() {
177 None
178 } else {
179 Some(&self.source_type_name)
180 }
181 }
182
183 pub fn alert_type(&self) -> Option<AlertType> {
185 self.alert_type
186 }
187
188 pub fn timestamp(&self) -> Option<u64> {
192 self.timestamp.map(|ts| ts.get())
193 }
194
195 pub fn tags(&self) -> &TagSet {
197 &self.tags
198 }
199
200 pub fn origin_tags(&self) -> &TagSet {
202 &self.origin_tags
203 }
204
205 pub fn with_timestamp(mut self, timestamp: impl Into<Option<u64>>) -> Self {
211 self.timestamp = timestamp.into().and_then(NonZeroU64::new);
212 self
213 }
214
215 pub fn set_timestamp(&mut self, timestamp: impl Into<Option<u64>>) {
219 self.timestamp = timestamp.into().and_then(NonZeroU64::new);
220 }
221
222 pub fn with_hostname(mut self, hostname: impl Into<Option<MetaString>>) -> Self {
226 self.hostname = match hostname.into() {
227 Some(s) => s,
228 None => MetaString::empty(),
229 };
230 self
231 }
232
233 pub fn set_hostname(&mut self, hostname: impl Into<Option<MetaString>>) {
235 self.hostname = match hostname.into() {
236 Some(s) => s,
237 None => MetaString::empty(),
238 };
239 }
240
241 pub fn with_aggregation_key(mut self, aggregation_key: impl Into<Option<MetaString>>) -> Self {
247 self.aggregation_key = match aggregation_key.into() {
248 Some(s) => s,
249 None => MetaString::empty(),
250 };
251 self
252 }
253
254 pub fn set_aggregation_key(&mut self, aggregation_key: impl Into<Option<MetaString>>) {
258 self.aggregation_key = match aggregation_key.into() {
259 Some(s) => s,
260 None => MetaString::empty(),
261 };
262 }
263
264 pub fn with_priority(mut self, priority: impl Into<Option<Priority>>) -> Self {
268 self.priority = priority.into();
269 self
270 }
271
272 pub fn set_priority(&mut self, priority: impl Into<Option<Priority>>) {
274 self.priority = priority.into();
275 }
276
277 pub fn with_source_type_name(mut self, source_type_name: impl Into<Option<MetaString>>) -> Self {
281 self.source_type_name = match source_type_name.into() {
282 Some(s) => s,
283 None => MetaString::empty(),
284 };
285 self
286 }
287
288 pub fn set_source_type_name(&mut self, source_type_name: impl Into<Option<MetaString>>) {
290 self.source_type_name = match source_type_name.into() {
291 Some(s) => s,
292 None => MetaString::empty(),
293 };
294 }
295
296 pub fn with_alert_type(mut self, alert_type: impl Into<Option<AlertType>>) -> Self {
300 self.alert_type = alert_type.into();
301 self
302 }
303
304 pub fn set_alert_type(&mut self, alert_type: impl Into<Option<AlertType>>) {
306 self.alert_type = alert_type.into();
307 }
308
309 pub fn with_tags(mut self, tags: impl Into<TagSet>) -> Self {
313 self.tags = tags.into();
314 self
315 }
316
317 pub fn set_tags(&mut self, tags: impl Into<Option<TagSet>>) {
319 self.tags = tags.into().unwrap_or_default();
320 }
321
322 pub fn with_origin_tags(mut self, origin_tags: impl Into<TagSet>) -> Self {
326 self.origin_tags = origin_tags.into();
327 self
328 }
329
330 pub fn new(title: impl Into<MetaString>, text: impl Into<MetaString>) -> Self {
334 Self {
335 title: title.into(),
336 text: text.into(),
337 timestamp: None,
338 hostname: MetaString::empty(),
339 aggregation_key: MetaString::empty(),
340 priority: Some(Priority::Normal),
341 source_type_name: MetaString::empty(),
342 alert_type: Some(AlertType::Info),
343 tags: TagSet::default(),
344 origin_tags: TagSet::default(),
345 }
346 }
347}