summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_measurement_filter/src
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@ifm.com>2022-05-12 12:23:34 +0200
committerMatthias Beyer <matthias.beyer@ifm.com>2022-05-12 14:52:34 +0200
commit105d830e67e4b8519a4041535d4bb3ce8bf50363 (patch)
tree3ad38c33ffaf4416954f3aa28e81dded7d410ef4 /plugins/plugin_measurement_filter/src
parentbaf77605896e8a9a1e17e1c1a1f23e37bb58e162 (diff)
plugin_measurement_filter: Make config type self-describing
This patch rewrites the configuration type to be self-describing and adapts the `PluginBuilder` implementation to implement the `PluginBuilder::kind_configuration()` interface. Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
Diffstat (limited to 'plugins/plugin_measurement_filter/src')
-rw-r--r--plugins/plugin_measurement_filter/src/builder.rs4
-rw-r--r--plugins/plugin_measurement_filter/src/config.rs7
-rw-r--r--plugins/plugin_measurement_filter/src/extractor.rs22
-rw-r--r--plugins/plugin_measurement_filter/src/filter.rs111
4 files changed, 142 insertions, 2 deletions
diff --git a/plugins/plugin_measurement_filter/src/builder.rs b/plugins/plugin_measurement_filter/src/builder.rs
index 24f55759..6f3b2105 100644
--- a/plugins/plugin_measurement_filter/src/builder.rs
+++ b/plugins/plugin_measurement_filter/src/builder.rs
@@ -26,6 +26,10 @@ where
"measurement_filter"
}
+ fn kind_configuration() -> Option<tedge_api::ConfigDescription> {
+ Some(<MeasurementFilterConfig as tedge_api::AsConfig>::as_config())
+ }
+
fn kind_message_types() -> HandleTypes
where
Self: Sized,
diff --git a/plugins/plugin_measurement_filter/src/config.rs b/plugins/plugin_measurement_filter/src/config.rs
index 46536bd3..f6e973de 100644
--- a/plugins/plugin_measurement_filter/src/config.rs
+++ b/plugins/plugin_measurement_filter/src/config.rs
@@ -1,12 +1,17 @@
-#[derive(Debug, serde::Deserialize)]
+#[derive(Debug, serde::Deserialize, tedge_api::Config)]
#[serde_with::serde_as]
pub struct MeasurementFilterConfig {
+ /// The name of the plugin to send the measurements to if the filter did not match its value
pub(crate) target: String,
+
+ /// The name of the plugin to send measurements to if the filter matched its value
pub(crate) filtered_target: Option<String>,
+ /// A path to find the value inside a measurement
#[serde_as(as = "TryFromInto<String>")]
pub(crate) extractor: crate::extractor::Extractor,
+ /// The filter to filter the measurements with
#[serde(flatten)]
pub(crate) filter: crate::filter::Filter,
}
diff --git a/plugins/plugin_measurement_filter/src/extractor.rs b/plugins/plugin_measurement_filter/src/extractor.rs
index 08a533ba..a584b6c5 100644
--- a/plugins/plugin_measurement_filter/src/extractor.rs
+++ b/plugins/plugin_measurement_filter/src/extractor.rs
@@ -1,8 +1,30 @@
+use tedge_api::AsConfig;
+use tedge_api::ConfigKind;
use tracing::trace;
#[derive(Debug, serde_with::DeserializeFromStr)]
pub struct Extractor(pub(crate) Vec<Token>);
+impl AsConfig for Extractor {
+ fn as_config() -> tedge_api::ConfigDescription {
+ tedge_api::ConfigDescription::new(
+ "Extractor".to_string(),
+ ConfigKind::String,
+ Some(indoc::indoc!{r#"
+ A path to extract a value from a measurement
+
+ # Examples
+
+ To extract the member "celcius" from a measurement named "temperature":
+
+ ```
+ temperature.celcius
+ ```
+ "#})
+ )
+ }
+}
+
impl From<Extractor> for String {
fn from(e: Extractor) -> String {
e.0.iter().map(Token::to_string).collect()
diff --git a/plugins/plugin_measurement_filter/src/filter.rs b/plugins/plugin_measurement_filter/src/filter.rs
index 7ccf7341..3b81204e 100644
--- a/plugins/plugin_measurement_filter/src/filter.rs
+++ b/plugins/plugin_measurement_filter/src/filter.rs
@@ -1,3 +1,4 @@
+use tedge_api::AsConfig;
use tedge_lib::measurement::MeasurementValue;
use tracing::trace;
@@ -20,6 +21,115 @@ pub enum Filter {
Excludes(String),
}
+impl AsConfig for Filter {
+ fn as_config() -> tedge_api::ConfigDescription {
+ tedge_api::ConfigDescription::new(
+ "Filter".to_string(),
+ tedge_api::ConfigKind::Enum(
+ tedge_api::config::ConfigEnumKind::Untagged,
+ vec![
+ (
+ "is",
+ Some(indoc::indoc!(
+ r#"
+ Filter with a boolean comparison
+
+ ## Example
+
+ To check whether the measurement value is true:
+
+ ```
+ is = true
+ ```
+ "#
+ )),
+ tedge_api::config::EnumVariantRepresentation::Wrapped(Box::new(
+ bool::as_config(),
+ )),
+ ),
+ (
+ "less_than",
+ Some(indoc::indoc!(
+ r#"
+ Filter with a less-than comparison
+
+ ## Example
+
+ To check whether the measurement value is less than 5.0:
+
+ ```
+ less_than = 5.0
+ ```
+ "#
+ )),
+ tedge_api::config::EnumVariantRepresentation::Wrapped(Box::new(
+ f64::as_config(),
+ )),
+ ),
+ (
+ "more_than",
+ Some(indoc::indoc!(
+ r#"
+ Filter with a more-than comparison
+
+ ## Example
+
+ To check whether the measurement value is more than 12.0:
+
+ ```
+ more_than = 12.0
+ ```
+ "#
+ )),
+ tedge_api::config::EnumVariantRepresentation::Wrapped(Box::new(
+ f64::as_config(),
+ )),
+ ),
+ (
+ "contains",
+ Some(indoc::indoc!(
+ r#"
+ Filter by checking whether a substring is present in the measurement
+
+ ## Example
+
+ To check whether the measurement contains "foo"
+
+ ```
+ contains = "foo"
+ ```
+ "#
+ )),
+ tedge_api::config::EnumVariantRepresentation::Wrapped(Box::new(
+ String::as_config(),
+ )),
+ ),
+ (
+ "excludes",
+ Some(indoc::indoc!(
+ r#"
+ Filter by checking whether a substring is absent in the measurement
+
+ ## Example
+
+ To check whether the measurement does not contain "bar"
+
+ ```
+ excludes = "bar"
+ ```
+ "#
+ )),
+ tedge_api::config::EnumVariantRepresentation::Wrapped(Box::new(
+ String::as_config(),
+ )),
+ ),
+ ],
+ ),
+ None,
+ )
+ }
+}
+
pub trait Filterable {
fn apply_filter(&self, filter: &Filter) -> bool;
}
@@ -118,5 +228,4 @@ mod tests {
let filt = Filter::Is(true);
assert!(!msmt.apply_filter(&filt));
}
-
}