improve code

This commit is contained in:
houseme
2025-05-30 15:39:21 +08:00
parent 7f983e72e7
commit 703c465bd0
7 changed files with 18 additions and 25 deletions
+3 -3
View File
@@ -424,21 +424,21 @@ impl EventNotifierConfig {
let mut adapters = Vec::new();
// Add all enabled webhook configurations
for (_, webhook) in &self.webhook {
for webhook in self.webhook.values() {
if webhook.common.enable {
adapters.push(AdapterConfig::Webhook(webhook.clone()));
}
}
// Add all enabled Kafka configurations
for (_, kafka) in &self.kafka {
for kafka in self.kafka.values() {
if kafka.common.enable {
adapters.push(AdapterConfig::Kafka(kafka.clone()));
}
}
// Add all enabled MQTT configurations
for (_, mqtt) in &self.mqtt {
for mqtt in self.mqtt.values() {
if mqtt.common.enable {
adapters.push(AdapterConfig::Mqtt(mqtt.clone()));
}
-1
View File
@@ -6,7 +6,6 @@ mod event;
mod global;
mod notifier;
mod store;
mod target;
pub use adapter::create_adapters;
#[cfg(all(feature = "kafka", target_os = "linux"))]
+10 -14
View File
@@ -25,18 +25,17 @@ pub static GLOBAL_EVENT_CONFIG: Lazy<Mutex<Option<EventNotifierConfig>>> = Lazy:
/// EventManager Responsible for managing all operations of the event system
#[derive(Debug)]
pub struct EventManager<S: StorageAPI> {
api: Arc<S>,
pub struct EventManager {
api: Arc<ECStore>,
}
impl<S: StorageAPI> EventManager {
impl EventManager {
/// Create a new Event Manager
pub async fn new(api: Arc<S>) -> Self {
// Update the global access point at the same time
if let Ok(mut global_api) = GLOBAL_STORE_API.lock() {
if let Some(store) = api.as_any().downcast_ref::<ECStore>() {
*global_api = Some(Arc::new(store.clone()));
}
pub async fn new(api: Arc<ECStore>) -> Self {
// Set the global storage API
{
let mut global_api = GLOBAL_STORE_API.lock().await;
*global_api = Some(api.clone());
}
Self { api }
@@ -82,10 +81,7 @@ impl<S: StorageAPI> EventManager {
}
save_event_config(self.api.clone(), cfg).await?;
*GLOBAL_EVENT_CONFIG
.lock()
.await
.map_err(|e| Error::msg(format!("Failed to acquire global config lock: {}", e)))? = Some(cfg.clone());
*GLOBAL_EVENT_CONFIG.lock().await = Some(cfg.clone());
Ok(())
}
@@ -199,7 +195,7 @@ async fn save_event_config<S: StorageAPI>(api: Arc<S>, config: &EventNotifierCon
let config_file = get_event_config_file();
let data = config.marshal()?;
save_config(api, &config_file, data).await;
save_config(api, &config_file, data).await
}
/// Get the event profile path
+3 -1
View File
@@ -15,7 +15,7 @@ use uuid::Uuid;
pub struct Key {
/// Key name
pub name: String,
/// Whether or not to compress
/// Whether to compress
pub compress: bool,
/// filename extension
pub extension: String,
@@ -35,6 +35,7 @@ impl Key {
}
/// Convert to string form
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
let mut key_str = self.name.clone();
if self.item_count > 1 {
@@ -47,6 +48,7 @@ impl Key {
}
/// Parse key from file name
#[allow(clippy::redundant_closure)]
pub fn parse_key(filename: &str) -> Key {
let compress = filename.ends_with(".snappy");
let filename = if compress {