use super::event::{Event, EventPriority, ImpactScope};
use super::plugin::EventResponsePlugin;
use mofa_kernel::plugin::PluginResult;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Clone)]
pub enum RuleAdjustmentStrategy {
PriorityBased,
ImpactScopeBased,
Combined,
}
pub struct RuleManager {
plugins: Arc<RwLock<HashMap<String, Box<dyn EventResponsePlugin + Send + Sync>>>>,
strategy: RwLock<RuleAdjustmentStrategy>,
default_rules: HashMap<String, HashMap<String, serde_json::Value>>,
}
impl RuleManager {
pub fn new() -> Self {
Self {
plugins: Arc::new(RwLock::new(HashMap::new())),
strategy: RwLock::new(RuleAdjustmentStrategy::Combined),
default_rules: HashMap::new(),
}
}
pub async fn add_plugin(&self, plugin: Box<dyn EventResponsePlugin + Send + Sync>) {
let plugin_id = plugin.metadata().id.to_string();
let mut plugins = self.plugins.write().await;
plugins.insert(plugin_id, plugin);
}
pub async fn set_strategy(&self, strategy: RuleAdjustmentStrategy) {
let mut current_strategy = self.strategy.write().await;
*current_strategy = strategy;
}
pub async fn adjust_rules(&self, _event: &Event) -> PluginResult<()> {
Ok(())
}
async fn adjust_rules_by_priority(
&self,
plugin: &dyn EventResponsePlugin,
event: &Event,
) -> PluginResult<()> {
println!(
"Adjusting rules for plugin {} based on priority: {:?}",
plugin.metadata().name,
event.priority
);
if event.priority == EventPriority::Emergency {
println!(" - Emergency event: skipping non-critical validation steps");
}
Ok(())
}
async fn adjust_rules_by_scope(
&self,
plugin: &dyn EventResponsePlugin,
event: &Event,
) -> PluginResult<()> {
println!(
"Adjusting rules for plugin {} based on scope: {:?}",
plugin.metadata().name,
event.scope
);
if let ImpactScope::System = event.scope {
println!(" - System-wide event: triggering automatic failover");
}
Ok(())
}
pub async fn get_strategy(&self) -> RuleAdjustmentStrategy {
self.strategy.read().await.clone()
}
pub fn update_default_rules(
&mut self,
plugin_id: &str,
rules: HashMap<String, serde_json::Value>,
) {
self.default_rules.insert(plugin_id.to_string(), rules);
}
}