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 = "Option::is_none")]
130 hostname: Option<MetaString>,
131 #[serde(skip_serializing_if = "MetaString::is_empty")]
132 aggregation_key: MetaString,
133 priority: Option<Priority>,
134 #[serde(skip_serializing_if = "MetaString::is_empty")]
135 source_type_name: MetaString,
136 alert_type: Option<AlertType>,
137 tags: TagSet,
138 origin_tags: TagSet,
139}
140
141impl EventD {
142 pub fn title(&self) -> &str {
144 &self.title
145 }
146
147 pub fn text(&self) -> &str {
149 &self.text
150 }
151
152 pub fn hostname(&self) -> Option<&str> {
157 self.hostname.as_deref()
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 {
228 self.hostname = hostname.into();
229 self
230 }
231
232 pub fn set_hostname(&mut self, hostname: impl Into<Option<MetaString>>) {
236 self.hostname = hostname.into();
237 }
238
239 pub fn with_aggregation_key(mut self, aggregation_key: impl Into<Option<MetaString>>) -> Self {
245 self.aggregation_key = match aggregation_key.into() {
246 Some(s) => s,
247 None => MetaString::empty(),
248 };
249 self
250 }
251
252 pub fn set_aggregation_key(&mut self, aggregation_key: impl Into<Option<MetaString>>) {
256 self.aggregation_key = match aggregation_key.into() {
257 Some(s) => s,
258 None => MetaString::empty(),
259 };
260 }
261
262 pub fn with_priority(mut self, priority: impl Into<Option<Priority>>) -> Self {
266 self.priority = priority.into();
267 self
268 }
269
270 pub fn set_priority(&mut self, priority: impl Into<Option<Priority>>) {
272 self.priority = priority.into();
273 }
274
275 pub fn with_source_type_name(mut self, source_type_name: impl Into<Option<MetaString>>) -> Self {
279 self.source_type_name = match source_type_name.into() {
280 Some(s) => s,
281 None => MetaString::empty(),
282 };
283 self
284 }
285
286 pub fn set_source_type_name(&mut self, source_type_name: impl Into<Option<MetaString>>) {
288 self.source_type_name = match source_type_name.into() {
289 Some(s) => s,
290 None => MetaString::empty(),
291 };
292 }
293
294 pub fn with_alert_type(mut self, alert_type: impl Into<Option<AlertType>>) -> Self {
298 self.alert_type = alert_type.into();
299 self
300 }
301
302 pub fn set_alert_type(&mut self, alert_type: impl Into<Option<AlertType>>) {
304 self.alert_type = alert_type.into();
305 }
306
307 pub fn with_tags(mut self, tags: impl Into<TagSet>) -> Self {
311 self.tags = tags.into();
312 self
313 }
314
315 pub fn set_tags(&mut self, tags: impl Into<Option<TagSet>>) {
317 self.tags = tags.into().unwrap_or_default();
318 }
319
320 pub fn with_origin_tags(mut self, origin_tags: impl Into<TagSet>) -> Self {
324 self.origin_tags = origin_tags.into();
325 self
326 }
327
328 pub fn new(title: impl Into<MetaString>, text: impl Into<MetaString>) -> Self {
332 Self {
333 title: title.into(),
334 text: text.into(),
335 timestamp: None,
336 hostname: None,
337 aggregation_key: MetaString::empty(),
338 priority: Some(Priority::Normal),
339 source_type_name: MetaString::empty(),
340 alert_type: Some(AlertType::Info),
341 tags: TagSet::default(),
342 origin_tags: TagSet::default(),
343 }
344 }
345}
346
347#[cfg(test)]
348mod tests {
349 use stringtheory::MetaString;
350
351 use super::EventD;
352
353 #[test]
354 fn unset_and_explicitly_empty_hostnames_are_distinct() {
355 let unset = EventD::new("title", "text");
356 assert_eq!(unset.hostname(), None);
357
358 let empty = EventD::new("title", "text").with_hostname(MetaString::empty());
359 assert_eq!(empty.hostname(), Some(""));
360
361 let cleared = empty.with_hostname(None);
362 assert_eq!(cleared.hostname(), None);
363 }
364}