frequenz_microgrid/logical_meter/config.rs
1// License: MIT
2// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
4//! This module defines the configuration for the logical meter.
5
6use crate::Sample;
7use crate::client::proto::common::metrics::Metric;
8use chrono::TimeDelta;
9use frequenz_microgrid_component_graph::ComponentGraphConfig;
10use frequenz_resampling::ResamplingFunction;
11use std::collections::HashMap;
12
13pub struct LogicalMeterConfig {
14 /// The resampling interval for the logical meter.
15 pub(crate) resampling_interval: TimeDelta,
16 /// Resampler function.
17 pub(crate) resampling_function: Option<ResamplingFunction<f32, Sample<f32>>>,
18 /// Resampler overrides.
19 pub(crate) resampling_overrides: HashMap<Metric, ResamplingFunction<f32, Sample<f32>>>,
20 /// The maximum age of samples to be considered for resampling, in number of
21 /// intervals.
22 pub(crate) max_age_in_intervals: u32,
23 /// Configuration forwarded to the underlying [`ComponentGraph`][cg]. Defaults
24 /// to [`ComponentGraphConfig::default()`].
25 ///
26 /// [cg]: frequenz_microgrid_component_graph::ComponentGraph
27 pub(crate) component_graph_config: ComponentGraphConfig,
28}
29
30impl LogicalMeterConfig {
31 /// Creates a new `LogicalMeterConfig` with the given resampling interval.
32 pub fn new(resampling_interval: TimeDelta) -> Self {
33 Self {
34 resampling_interval,
35 resampling_function: None,
36 resampling_overrides: HashMap::new(),
37 max_age_in_intervals: 3,
38 component_graph_config: ComponentGraphConfig::default(),
39 }
40 }
41
42 /// Sets the default resampling function.
43 ///
44 /// This function will be used for all metrics that do not have a specific
45 /// override set.
46 ///
47 /// If no default resampling function is set, the logical meter will default
48 /// to using the `Average` resampling function.
49 pub fn with_default_resampling_function(
50 mut self,
51 function: ResamplingFunction<f32, Sample<f32>>,
52 ) -> Self {
53 self.resampling_function = Some(function);
54 self
55 }
56
57 /// Sets a resampling function override for a specific metric.
58 ///
59 /// If this function is called multiple times for the same metric, the last
60 /// function provided will be used.
61 pub fn override_resampling_function<M: crate::metric::Metric>(
62 mut self,
63 function: ResamplingFunction<f32, Sample<f32>>,
64 ) -> Self {
65 self.resampling_overrides.insert(M::METRIC, function);
66
67 self
68 }
69
70 /// Sets the maximum age of samples to be considered for resampling, in
71 /// number of intervals.
72 ///
73 /// Must be at least 1. If a smaller value is provided, it will be clamped
74 /// to 1.
75 ///
76 /// If not set, the default value is 3.
77 pub fn with_max_age_in_intervals(mut self, max_age_in_intervals: u32) -> Self {
78 // Ensure that the maximum age is at least 1 interval.
79 self.max_age_in_intervals = max_age_in_intervals.max(1);
80 self
81 }
82
83 /// Sets the [`ComponentGraphConfig`] forwarded to the underlying graph
84 /// when [`LogicalMeterHandle::try_new`][lm] (and therefore
85 /// [`Microgrid::try_new`][mg]) builds it. If not set, the graph crate's
86 /// `Default::default()` is used.
87 ///
88 /// [lm]: crate::LogicalMeterHandle::try_new
89 /// [mg]: crate::Microgrid::try_new
90 pub fn with_component_graph_config(mut self, config: ComponentGraphConfig) -> Self {
91 self.component_graph_config = config;
92 self
93 }
94}