frequenz_microgrid/logical_meter/
formula.rs

1// License: MIT
2// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
4//! Formula module for the logical meter.
5
6use async_trait::async_trait;
7pub(crate) mod aggregation_formula;
8mod async_formula;
9pub(crate) mod coalesce_formula;
10pub(crate) mod graph_formula_provider;
11pub use async_formula::Formula;
12
13use crate::{
14    Error,
15    Sample,
16    metric::Metric,
17    quantity::Quantity, //
18};
19use tokio::sync::{
20    broadcast,
21    mpsc, //
22};
23
24use super::logical_meter_actor;
25
26/// Connects logical meter formulas to the component graph formulas.
27pub(crate) trait GraphFormulaConnector: std::fmt::Display {
28    type GraphFormulaType: frequenz_microgrid_component_graph::Formula;
29}
30
31#[async_trait]
32pub trait FormulaSubscriber: std::fmt::Display + Sync + Send {
33    type QuantityType: Quantity;
34    async fn subscribe(&self) -> Result<broadcast::Receiver<Sample<Self::QuantityType>>, Error>;
35}
36
37/// Parameters for creating a logical meter formula.
38pub(super) struct FormulaParams<F: GraphFormulaConnector, M: Metric> {
39    pub(super) formula: F::GraphFormulaType,
40    pub(super) metric: M,
41    pub(super) instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
42}
43
44impl<F: GraphFormulaConnector, M: Metric> FormulaParams<F, M> {
45    pub(super) fn new(
46        formula: F::GraphFormulaType,
47        metric: M,
48        instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
49    ) -> Self {
50        Self {
51            formula,
52            metric,
53            instructions_tx,
54        }
55    }
56}