scouter_types/alert/
dispatch.rs

1use crate::drift::DriftArgs;
2use pyo3::{prelude::*, IntoPyObjectExt};
3use serde::{Deserialize, Serialize};
4use std::fmt::Display;
5
6#[pyclass]
7#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
8pub struct ConsoleDispatchConfig {
9    #[pyo3(get, set)]
10    pub enabled: bool,
11}
12
13#[pyclass]
14#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
15pub struct SlackDispatchConfig {
16    #[pyo3(get, set)]
17    pub channel: String,
18}
19
20#[pymethods]
21impl SlackDispatchConfig {
22    #[new]
23    pub fn new(channel: String) -> PyResult<Self> {
24        Ok(SlackDispatchConfig { channel })
25    }
26}
27
28#[pyclass]
29#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
30pub struct OpsGenieDispatchConfig {
31    #[pyo3(get, set)]
32    pub team: String,
33
34    #[pyo3(get, set)]
35    pub priority: String,
36}
37
38#[pymethods]
39impl OpsGenieDispatchConfig {
40    #[new]
41    #[pyo3(signature = (team, priority="P5"))]
42    pub fn new(team: &str, priority: &str) -> PyResult<Self> {
43        Ok(OpsGenieDispatchConfig {
44            team: team.to_string(),
45            priority: priority.to_string(),
46        })
47    }
48}
49
50#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
51pub enum AlertDispatchConfig {
52    Slack(SlackDispatchConfig),
53    OpsGenie(OpsGenieDispatchConfig),
54    Console(ConsoleDispatchConfig),
55}
56
57impl AlertDispatchConfig {
58    pub fn dispatch_type(&self) -> AlertDispatchType {
59        match self {
60            AlertDispatchConfig::Slack(_) => AlertDispatchType::Slack,
61            AlertDispatchConfig::OpsGenie(_) => AlertDispatchType::OpsGenie,
62            AlertDispatchConfig::Console(_) => AlertDispatchType::Console,
63        }
64    }
65
66    pub fn config<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
67        match self {
68            AlertDispatchConfig::Slack(config) => config.clone().into_bound_py_any(py),
69            AlertDispatchConfig::OpsGenie(config) => config.clone().into_bound_py_any(py),
70            AlertDispatchConfig::Console(config) => config.clone().into_bound_py_any(py),
71        }
72    }
73}
74
75impl Default for AlertDispatchConfig {
76    fn default() -> Self {
77        AlertDispatchConfig::Console(ConsoleDispatchConfig { enabled: true })
78    }
79}
80
81#[pyclass(eq)]
82#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)]
83pub enum AlertDispatchType {
84    Slack,
85    #[default]
86    Console,
87    OpsGenie,
88}
89
90#[pymethods]
91impl AlertDispatchType {
92    pub fn to_string(&self) -> &str {
93        match self {
94            AlertDispatchType::Slack => "Slack",
95            AlertDispatchType::Console => "Console",
96            AlertDispatchType::OpsGenie => "OpsGenie",
97        }
98    }
99}
100
101impl Display for AlertDispatchType {
102    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103        match self {
104            AlertDispatchType::Slack => write!(f, "Slack"),
105            AlertDispatchType::Console => write!(f, "Console"),
106            AlertDispatchType::OpsGenie => write!(f, "OpsGenie"),
107        }
108    }
109}
110
111pub trait DispatchAlertDescription {
112    fn create_alert_description(&self, dispatch_type: AlertDispatchType) -> String;
113}
114
115pub trait DispatchDriftConfig {
116    fn get_drift_args(&self) -> DriftArgs;
117}
118
119#[pyclass]
120#[derive(Debug, PartialEq, Clone, Serialize)]
121pub enum TransportType {
122    Kafka,
123    RabbitMQ,
124    Http,
125    Redis,
126    Mock,
127}