use crate::client::RestClient;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
pub uid: String,
pub name: String,
pub severity: String,
pub state: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_uid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub change_time: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub change_value: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertSettings {
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email_recipients: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BdbAlertSettingsWithThreshold {
pub enabled: bool,
pub threshold: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbAlertsSettings {
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_backup_delayed: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_crdt_src_high_syncer_lag: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_crdt_src_syncer_connection_error: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_crdt_src_syncer_general_error: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_high_latency: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_high_syncer_lag: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_high_throughput: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_long_running_action: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_low_throughput: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_ram_dataset_overhead: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_ram_values: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_replica_src_high_syncer_lag: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_replica_src_syncer_connection_error: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_replica_src_syncer_general_error: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_shard_num_ram_values: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_size: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_syncer_connection_error: Option<BdbAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bdb_syncer_general_error: Option<BdbAlertSettingsWithThreshold>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterAlertSettingsWithThreshold {
pub enabled: bool,
pub threshold: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterAlertsSettings {
#[serde(skip_serializing_if = "Option::is_none")]
pub cluster_ca_cert_about_to_expire: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cluster_certs_about_to_expire: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cluster_license_about_to_expire: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_cpu_utilization: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_ephemeral_storage: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_free_flash: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_internal_certs_about_to_expire: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_memory: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_net_throughput: Option<ClusterAlertSettingsWithThreshold>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_persistent_storage: Option<ClusterAlertSettingsWithThreshold>,
}
pub struct AlertHandler {
client: RestClient,
}
pub type AlertsHandler = AlertHandler;
impl AlertHandler {
pub fn new(client: RestClient) -> Self {
AlertHandler { client }
}
pub async fn list(&self) -> Result<Vec<Alert>> {
self.client.get("/v1/alerts").await
}
pub async fn get(&self, uid: &str) -> Result<Alert> {
self.client.get(&format!("/v1/alerts/{}", uid)).await
}
pub async fn list_by_database(&self, bdb_uid: u32) -> Result<Vec<Alert>> {
self.client
.get(&format!("/v1/bdbs/{}/alerts", bdb_uid))
.await
}
pub async fn list_by_node(&self, node_uid: u32) -> Result<Vec<Alert>> {
self.client
.get(&format!("/v1/nodes/{}/alerts", node_uid))
.await
}
pub async fn list_cluster_alerts(&self) -> Result<Vec<Alert>> {
self.client.get("/v1/cluster/alerts").await
}
pub async fn get_settings(&self, alert_name: &str) -> Result<AlertSettings> {
self.client
.get(&format!("/v1/cluster/alert_settings/{}", alert_name))
.await
}
pub async fn update_settings(
&self,
alert_name: &str,
settings: AlertSettings,
) -> Result<AlertSettings> {
self.client
.put(
&format!("/v1/cluster/alert_settings/{}", alert_name),
&settings,
)
.await
}
pub async fn get_database_alert_settings(&self, bdb_uid: u32) -> Result<DbAlertsSettings> {
self.client
.get(&format!("/v1/bdbs/{}/alert_settings", bdb_uid))
.await
}
pub async fn update_database_alert_settings(
&self,
bdb_uid: u32,
settings: &DbAlertsSettings,
) -> Result<DbAlertsSettings> {
self.client
.put(&format!("/v1/bdbs/{}/alert_settings", bdb_uid), settings)
.await
}
pub async fn get_cluster_alert_settings(&self) -> Result<ClusterAlertsSettings> {
self.client.get("/v1/cluster/alert_settings").await
}
pub async fn update_cluster_alert_settings(
&self,
settings: &ClusterAlertsSettings,
) -> Result<ClusterAlertsSettings> {
self.client
.put("/v1/cluster/alert_settings", settings)
.await
}
pub async fn clear(&self, uid: &str) -> Result<()> {
self.client.delete(&format!("/v1/alerts/{}", uid)).await
}
pub async fn clear_all(&self) -> Result<()> {
self.client.delete("/v1/alerts").await
}
}