pub struct SimpleMetricManager<H> { /* private fields */ }Expand description
A MetricManager implementation that provides simple metric registration and handling.
SimpleMetricManager provides a simple way to manage metrics with support for
command callbacks and metric publishing.
§Example
use srad_eon::SimpleMetricManager;
//Create a new simple metric manager
let manager = SimpleMetricManager::new();
// Assume we successfully create a device with a SimpleMetricManager as it's metrics manager
create_device_with_manager(&manager);
// Register a simple metric
let counter = manager.register_metric("Counter", 0 as i32).unwrap();
// Register a metric with a command handler
let temperature = manager.register_metric_with_cmd_handler(
"temperature",
25.5,
|mgr, metric, new_value| async move {
if let Some(value) = new_value {
mgr.publish_metric(metric.update(|x|{ *x = value })).await;
}
}
);Implementations§
Source§impl<H> SimpleMetricManager<H>
impl<H> SimpleMetricManager<H>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty SimpleMetricManager.
This initialises a new metric manager with no registered metrics.
Sourcepub fn register_metric<S, T>(
&self,
name: S,
value: T,
) -> Option<SimpleManagerMetric<T, H>>
pub fn register_metric<S, T>( &self, name: S, value: T, ) -> Option<SimpleManagerMetric<T, H>>
Registers a new metric with the given name and initial value.
Returns None if a metric with the same name already exists, otherwise
returns the newly created metric.
Sourcepub fn register_metric_with_cmd_handler<S, T, F, Fut>(
&self,
name: S,
value: T,
cmd_handler: F,
) -> Option<SimpleManagerMetric<T, H>>
pub fn register_metric_with_cmd_handler<S, T, F, Fut>( &self, name: S, value: T, cmd_handler: F, ) -> Option<SimpleManagerMetric<T, H>>
Registers a new metric with a command handler that will be called when commands for this metric are received.
The command handler is an async function that receives the manager, the metric, and an optional new value for the metric. If the value is “None” that indicates the CMD metric ‘is_null’ field was true
Returns None if a metric with the same name already exists, otherwise
returns the newly created metric.
Sourcepub async fn publish_metric(
&self,
metric: SimpleManagerPublishMetric,
) -> Result<(), PublishError>
pub async fn publish_metric( &self, metric: SimpleManagerPublishMetric, ) -> Result<(), PublishError>
Publishes a single metric.
Returns an error if the metric was not published.
Sourcepub async fn publish_metrics(
&self,
metrics: Vec<SimpleManagerPublishMetric>,
) -> Result<(), PublishError>
pub async fn publish_metrics( &self, metrics: Vec<SimpleManagerPublishMetric>, ) -> Result<(), PublishError>
Publishes a multiple metric in a single batch.
Returns an error if the metrics were not published.
Trait Implementations§
Source§impl<H: Clone> Clone for SimpleMetricManager<H>
impl<H: Clone> Clone for SimpleMetricManager<H>
Source§fn clone(&self) -> SimpleMetricManager<H>
fn clone(&self) -> SimpleMetricManager<H>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more