use chrono::Local;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertMessage {
pub time: String,
pub level: AlertLevel,
pub title: String,
pub content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlertLevel {
Info,
Warn,
Error,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct AlertConfig {
pub dingding: DingdingConfig,
pub wecom: WecomConfig,
pub feishu: FeishuConfig,
pub custom: CustomConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeishuConfig {
pub enable: bool,
pub webhook: String,
#[serde(default = "default_keyword")]
pub keyword: String,
}
fn default_keyword() -> String {
"aiway".to_string()
}
impl Default for FeishuConfig {
fn default() -> Self {
Self {
enable: false,
webhook: "".to_string(),
keyword: "aiway".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DingdingConfig {
pub enable: bool,
pub webhook: String,
#[serde(default = "default_keyword")]
pub keyword: String,
}
impl Default for DingdingConfig {
fn default() -> Self {
Self {
enable: false,
webhook: "".to_string(),
keyword: "aiway".to_string(),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WecomConfig {
pub enable: bool,
pub webhook: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CustomConfig {
pub enable: bool,
pub webhook: String,
}
impl AlertMessage {
pub fn info(title: &str, content: &str) -> Self {
AlertMessage {
time: Self::now(),
level: AlertLevel::Info,
title: title.into(),
content: content.into(),
}
}
pub fn warn(title: &str, content: &str) -> Self {
AlertMessage {
time: Self::now(),
level: AlertLevel::Warn,
title: title.into(),
content: content.into(),
}
}
pub fn error(title: &str, content: &str) -> Self {
AlertMessage {
time: Self::now(),
level: AlertLevel::Error,
title: title.into(),
content: content.into(),
}
}
#[inline]
fn now() -> String {
let local_time = Local::now();
local_time.format("%Y-%m-%d %H:%M:%S.%3f").to_string()
}
}