saluki_core/data_model/event/eventd/
mod.rs1use std::{fmt, num::NonZeroU64};
4
5use saluki_context::tags::SharedTagSet;
6use serde::{Serialize, Serializer};
7use stringtheory::MetaString;
8
9pub const PRIORITY_LOW: &str = "low";
11
12pub const ALERT_TYPE_ERROR: &str = "error";
14
15pub const ALERT_TYPE_WARNING: &str = "warning";
17
18pub const ALERT_TYPE_SUCCESS: &str = "success";
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum AlertType {
24 Info,
26
27 Error,
29
30 Warning,
32
33 Success,
35}
36
37impl fmt::Display for AlertType {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str(self.as_str())
40 }
41}
42
43impl Serialize for AlertType {
44 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45 where
46 S: Serializer,
47 {
48 serializer.serialize_str(self.as_str())
49 }
50}
51
52impl AlertType {
53 pub fn try_from_string(alert_type: &str) -> Option<Self> {
57 match alert_type {
58 ALERT_TYPE_ERROR => Some(AlertType::Error),
59 ALERT_TYPE_WARNING => Some(AlertType::Warning),
60 ALERT_TYPE_SUCCESS => Some(AlertType::Success),
61 _ => Some(AlertType::Info),
62 }
63 }
64
65 pub const fn as_str(self) -> &'static str {
67 match self {
68 AlertType::Info => "info",
69 AlertType::Error => "error",
70 AlertType::Warning => "warning",
71 AlertType::Success => "success",
72 }
73 }
74}
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum Priority {
79 Normal,
81
82 Low,
84}
85
86impl fmt::Display for Priority {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 f.write_str(self.as_str())
89 }
90}
91
92impl Serialize for Priority {
93 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94 where
95 S: Serializer,
96 {
97 serializer.serialize_str(self.as_str())
98 }
99}
100
101impl Priority {
102 pub fn try_from_string(priority: &str) -> Option<Self> {
106 match priority {
107 PRIORITY_LOW => Some(Priority::Low),
108 _ => Some(Priority::Normal),
109 }
110 }
111
112 pub const fn as_str(self) -> &'static str {
114 match self {
115 Priority::Normal => "normal",
116 Priority::Low => "low",
117 }
118 }
119}
120
121#[derive(Clone, Debug, PartialEq, Serialize)]
123pub struct EventD {
124 title: MetaString,
125 text: MetaString,
126 timestamp: Option<NonZeroU64>,
127 #[serde(skip_serializing_if = "MetaString::is_empty")]
128 hostname: MetaString,
129 #[serde(skip_serializing_if = "MetaString::is_empty")]
130 aggregation_key: MetaString,
131 priority: Option<Priority>,
132 #[serde(skip_serializing_if = "MetaString::is_empty")]
133 source_type_name: MetaString,
134 alert_type: Option<AlertType>,
135 tags: SharedTagSet,
136 origin_tags: SharedTagSet,
137}
138
139impl EventD {
140 pub fn title(&self) -> &str {
142 &self.title
143 }
144
145 pub fn text(&self) -> &str {
147 &self.text
148 }
149
150 pub fn hostname(&self) -> Option<&str> {
152 if self.hostname.is_empty() {
153 None
154 } else {
155 Some(&self.hostname)
156 }
157 }
158
159 pub fn aggregation_key(&self) -> Option<&str> {
161 if self.aggregation_key.is_empty() {
162 None
163 } else {
164 Some(&self.aggregation_key)
165 }
166 }
167
168 pub fn priority(&self) -> Option<Priority> {
170 self.priority
171 }
172
173 pub fn source_type_name(&self) -> Option<&str> {
175 if self.source_type_name.is_empty() {
176 None
177 } else {
178 Some(&self.source_type_name)
179 }
180 }
181
182 pub fn alert_type(&self) -> Option<AlertType> {
184 self.alert_type
185 }
186
187 pub fn timestamp(&self) -> Option<u64> {
191 self.timestamp.map(|ts| ts.get())
192 }
193
194 pub fn tags(&self) -> &SharedTagSet {
196 &self.tags
197 }
198
199 pub fn origin_tags(&self) -> &SharedTagSet {
201 &self.origin_tags
202 }
203
204 pub fn with_timestamp(mut self, timestamp: impl Into<Option<u64>>) -> Self {
210 self.timestamp = timestamp.into().and_then(NonZeroU64::new);
211 self
212 }
213
214 pub fn set_timestamp(&mut self, timestamp: impl Into<Option<u64>>) {
218 self.timestamp = timestamp.into().and_then(NonZeroU64::new);
219 }
220
221 pub fn with_hostname(mut self, hostname: impl Into<Option<MetaString>>) -> Self {
225 self.hostname = match hostname.into() {
226 Some(s) => s,
227 None => MetaString::empty(),
228 };
229 self
230 }
231
232 pub fn set_hostname(&mut self, hostname: impl Into<Option<MetaString>>) {
234 self.hostname = match hostname.into() {
235 Some(s) => s,
236 None => MetaString::empty(),
237 };
238 }
239
240 pub fn with_aggregation_key(mut self, aggregation_key: impl Into<Option<MetaString>>) -> Self {
246 self.aggregation_key = match aggregation_key.into() {
247 Some(s) => s,
248 None => MetaString::empty(),
249 };
250 self
251 }
252
253 pub fn set_aggregation_key(&mut self, aggregation_key: impl Into<Option<MetaString>>) {
257 self.aggregation_key = match aggregation_key.into() {
258 Some(s) => s,
259 None => MetaString::empty(),
260 };
261 }
262
263 pub fn with_priority(mut self, priority: impl Into<Option<Priority>>) -> Self {
267 self.priority = priority.into();
268 self
269 }
270
271 pub fn set_priority(&mut self, priority: impl Into<Option<Priority>>) {
273 self.priority = priority.into();
274 }
275
276 pub fn with_source_type_name(mut self, source_type_name: impl Into<Option<MetaString>>) -> Self {
280 self.source_type_name = match source_type_name.into() {
281 Some(s) => s,
282 None => MetaString::empty(),
283 };
284 self
285 }
286
287 pub fn set_source_type_name(&mut self, source_type_name: impl Into<Option<MetaString>>) {
289 self.source_type_name = match source_type_name.into() {
290 Some(s) => s,
291 None => MetaString::empty(),
292 };
293 }
294
295 pub fn with_alert_type(mut self, alert_type: impl Into<Option<AlertType>>) -> Self {
299 self.alert_type = alert_type.into();
300 self
301 }
302
303 pub fn set_alert_type(&mut self, alert_type: impl Into<Option<AlertType>>) {
305 self.alert_type = alert_type.into();
306 }
307
308 pub fn with_tags(mut self, tags: impl Into<SharedTagSet>) -> Self {
312 self.tags = tags.into();
313 self
314 }
315
316 pub fn set_tags(&mut self, tags: impl Into<Option<SharedTagSet>>) {
318 self.tags = tags.into().unwrap_or_default();
319 }
320
321 pub fn with_origin_tags(mut self, origin_tags: SharedTagSet) -> Self {
325 self.origin_tags = origin_tags;
326 self
327 }
328
329 pub fn new(title: impl Into<MetaString>, text: impl Into<MetaString>) -> Self {
333 Self {
334 title: title.into(),
335 text: text.into(),
336 timestamp: None,
337 hostname: MetaString::empty(),
338 aggregation_key: MetaString::empty(),
339 priority: Some(Priority::Normal),
340 source_type_name: MetaString::empty(),
341 alert_type: Some(AlertType::Info),
342 tags: SharedTagSet::default(),
343 origin_tags: SharedTagSet::default(),
344 }
345 }
346}