From 5cd9f8e7d7d3b7f87cb0e03152e2c8883b1beb9c Mon Sep 17 00:00:00 2001 From: Johnny-Boi-013 Date: Sun, 15 Feb 2026 12:33:20 -0500 Subject: [PATCH 1/3] #496 added check_rule statement --- scylla-server/src/controllers/rule_controller.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scylla-server/src/controllers/rule_controller.rs b/scylla-server/src/controllers/rule_controller.rs index b1d2fa90..9e1c78e6 100644 --- a/scylla-server/src/controllers/rule_controller.rs +++ b/scylla-server/src/controllers/rule_controller.rs @@ -72,3 +72,17 @@ pub async fn get_all_rules_with_client_info( .await, )) } + +#[debug_handler] +pub async fn check_rule( + Extension(rules_manager): Extension>, + Json(rule): Json, +) -> Result, ScyllaError> { + debug!("Checking if rule exists: {} - {}", rule.topic, rule.expression); + let exists = rules_manager + .get_all_rules() + .await + .iter() + .any(|r| r.topic == rule.topic && r.expression == rule.expression); + Ok(Json(exists)) +} \ No newline at end of file From caf58e635d022427d8d1477633123b6b339c33ff Mon Sep 17 00:00:00 2001 From: Johnny-Boi-013 Date: Sun, 15 Feb 2026 12:49:57 -0500 Subject: [PATCH 2/3] #496 added check_rule function --- .../src/controllers/rule_controller.rs | 11 +--- scylla-server/src/rule_structs.rs | 12 +++- scylla-server/tests/rule_structs_test.rs | 60 +++++++++++++++++++ 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/scylla-server/src/controllers/rule_controller.rs b/scylla-server/src/controllers/rule_controller.rs index 9e1c78e6..61e16fbe 100644 --- a/scylla-server/src/controllers/rule_controller.rs +++ b/scylla-server/src/controllers/rule_controller.rs @@ -77,12 +77,7 @@ pub async fn get_all_rules_with_client_info( pub async fn check_rule( Extension(rules_manager): Extension>, Json(rule): Json, -) -> Result, ScyllaError> { - debug!("Checking if rule exists: {} - {}", rule.topic, rule.expression); - let exists = rules_manager - .get_all_rules() - .await - .iter() - .any(|r| r.topic == rule.topic && r.expression == rule.expression); - Ok(Json(exists)) +) -> Json { + debug!("Checking if rule exists: {} - {}", rule.topic, rule.expr()); + Json(rules_manager.check_rule(&rule.topic, &rule.expr).await) } \ No newline at end of file diff --git a/scylla-server/src/rule_structs.rs b/scylla-server/src/rule_structs.rs index 7625f159..3a40e2f1 100644 --- a/scylla-server/src/rule_structs.rs +++ b/scylla-server/src/rule_structs.rs @@ -247,6 +247,10 @@ impl Rule { } } + pub fn expr(&self) -> &str { + &self.expr + } + /// process an event of seeing this topic, with the given values fn process_seen(&self, values: &[f32]) -> Option { let mut context: HashMapContext = @@ -485,7 +489,13 @@ impl RuleManager { } } } - + pub async fn check_rule(&self, topic: &Topic, expr: &str) -> bool { + self.rules + .read() + .await + .values() + .any(|rule| rule.topic == *topic && rule.expr() == expr) + } pub async fn get_all_rules(&self) -> Vec { self.rules .read() diff --git a/scylla-server/tests/rule_structs_test.rs b/scylla-server/tests/rule_structs_test.rs index 0bbd3083..b219ee2c 100644 --- a/scylla-server/tests/rule_structs_test.rs +++ b/scylla-server/tests/rule_structs_test.rs @@ -535,6 +535,66 @@ async fn test_concurrent_high_frequency_messages() -> Result<(), RuleManagerErro } } } + #[tokio::test] + async fn test_check_rule_exists() -> Result<(), RuleManagerError> { + let rule_manager = RuleManager::new(); + let client = ClientId("test_client".to_string()); + + let rule = Rule::new( + RuleId("rule_1".to_string()), + Topic("test/topic".to_string()), + core::time::Duration::from_secs(60), + "a > 10".to_owned(), + ); + + rule_manager.add_rule(client, rule).await?; + + assert!(rule_manager.check_rule(&Topic("test/topic".to_string()), "a > 10").await); + Ok(()) + } + + #[tokio::test] + async fn test_check_rule_wrong_expr() -> Result<(), RuleManagerError> { + let rule_manager = RuleManager::new(); + let client = ClientId("test_client".to_string()); + + let rule = Rule::new( + RuleId("rule_1".to_string()), + Topic("test/topic".to_string()), + core::time::Duration::from_secs(60), + "a > 10".to_owned(), + ); + + rule_manager.add_rule(client, rule).await?; + + assert!(!rule_manager.check_rule(&Topic("test/topic".to_string()), "a > 20").await); + Ok(()) + } + + #[tokio::test] + async fn test_check_rule_wrong_topic() -> Result<(), RuleManagerError> { + let rule_manager = RuleManager::new(); + let client = ClientId("test_client".to_string()); + + let rule = Rule::new( + RuleId("rule_1".to_string()), + Topic("test/topic".to_string()), + core::time::Duration::from_secs(60), + "a > 10".to_owned(), + ); + + rule_manager.add_rule(client, rule).await?; + + assert!(!rule_manager.check_rule(&Topic("wrong/topic".to_string()), "a > 10").await); + Ok(()) + } + + #[tokio::test] + async fn test_check_rule_empty_manager() { + let rule_manager = RuleManager::new(); + + assert!(!rule_manager.check_rule(&Topic("test/topic".to_string()), "a > 10").await); + } // Verify system state is unchanged assert_eq!(rule_manager.get_all_rules().await.len(), num_rules); From cbb8862c4096ae86a97eb27b1046031656c10b8d Mon Sep 17 00:00:00 2001 From: Johnny-Boi-013 Date: Tue, 17 Feb 2026 22:14:32 -0500 Subject: [PATCH 3/3] #496 finished check_rule statement --- scylla-server/src/controllers/rule_controller.rs | 2 +- scylla-server/src/rule_structs.rs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/scylla-server/src/controllers/rule_controller.rs b/scylla-server/src/controllers/rule_controller.rs index 61e16fbe..43183095 100644 --- a/scylla-server/src/controllers/rule_controller.rs +++ b/scylla-server/src/controllers/rule_controller.rs @@ -78,6 +78,6 @@ pub async fn check_rule( Extension(rules_manager): Extension>, Json(rule): Json, ) -> Json { - debug!("Checking if rule exists: {} - {}", rule.topic, rule.expr()); + debug!("Checking if rule exists: {} - {}", rule.topic, rule.expr); Json(rules_manager.check_rule(&rule.topic, &rule.expr).await) } \ No newline at end of file diff --git a/scylla-server/src/rule_structs.rs b/scylla-server/src/rule_structs.rs index 3a40e2f1..e1aba021 100644 --- a/scylla-server/src/rule_structs.rs +++ b/scylla-server/src/rule_structs.rs @@ -247,10 +247,6 @@ impl Rule { } } - pub fn expr(&self) -> &str { - &self.expr - } - /// process an event of seeing this topic, with the given values fn process_seen(&self, values: &[f32]) -> Option { let mut context: HashMapContext = @@ -494,7 +490,7 @@ impl RuleManager { .read() .await .values() - .any(|rule| rule.topic == *topic && rule.expr() == expr) + .any(|rule| rule.topic == *topic && rule.expr == expr) } pub async fn get_all_rules(&self) -> Vec { self.rules