Skip to main content

stele/
service_checks.rs

1use serde::{Deserialize, Serialize};
2
3/// A simplified service check representation.
4#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
5pub struct ServiceCheck {
6    name: String,
7    status: u8,
8    hostname: String,
9    message: String,
10    tags: Vec<String>,
11    /// Unix timestamp in seconds from the `d:` field, or None if not present.
12    ///
13    /// NOTE: Do not compare this field directly without first normalizing pipeline-generated
14    /// fill-in values — see `ServiceChecksAnalyzer` for details.
15    pub timestamp: Option<u64>,
16}
17
18impl ServiceCheck {
19    /// Returns the name of the service check.
20    pub fn name(&self) -> &str {
21        &self.name
22    }
23
24    /// Returns the status of the service check (0=OK, 1=Warning, 2=Critical, 3=Unknown).
25    pub fn status(&self) -> u8 {
26        self.status
27    }
28
29    /// Returns the hostname of the service check.
30    pub fn hostname(&self) -> &str {
31        &self.hostname
32    }
33
34    /// Returns the message associated with the service check.
35    pub fn message(&self) -> &str {
36        &self.message
37    }
38
39    /// Returns the tags of the service check.
40    pub fn tags(&self) -> &[String] {
41        &self.tags
42    }
43
44    /// Returns the timestamp of the service check (Unix seconds), or None if not set.
45    pub fn timestamp(&self) -> Option<u64> {
46        self.timestamp
47    }
48
49    /// Builds a `ServiceCheck` from the fields sent to `/api/v1/check_run`.
50    pub fn from_check_run(
51        name: String, status: u8, hostname: String, message: String, mut tags: Vec<String>, timestamp: Option<u64>,
52    ) -> Self {
53        tags.sort_unstable();
54        Self {
55            name,
56            status,
57            hostname,
58            message,
59            tags,
60            timestamp,
61        }
62    }
63}