summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_measurement_filter/src
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@ifm.com>2022-05-13 09:35:37 +0200
committerMatthias Beyer <matthias.beyer@ifm.com>2022-05-13 09:35:37 +0200
commitd1b953a27576c370f756112f62783e9801a0b6b3 (patch)
tree60099113d432760985cb361f94c6e8b6447a2b3e /plugins/plugin_measurement_filter/src
parent92607ccb7ad9984f841e4d13d10c978e44341d7e (diff)
parentc2cf5c4964d2298d2031b49728aadf8dd2f830ab (diff)
Merge branch 'feature/add_tedge_api/self-describing-configuration-types' into feature/add_tedge_api_impl
This merge brings in self-describing configuration type functionality, so that we can `tedge-cli doc <plugin_name>` for getting the documentation for the configuration of a plugin named `<plugin_name>`. 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));
}
-
}