Skip to content

Commit 01ff07d

Browse files
committed
Implement CoalesceFormula
And introduce support for voltage and frequency metrics using `CoalesceFormula`s. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 0dc990e commit 01ff07d

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/logical_meter/formula.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
//! Formula module for the logical meter.
55
66
mod aggregation_formula;
7+
mod coalesce_formula;
78
pub(crate) mod graph_formula_provider;
89
pub use aggregation_formula::AggregationFormula;
10+
pub use coalesce_formula::CoalesceFormula;
911

1012
use crate::{Error, Sample};
1113
use tokio::sync::broadcast;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// License: MIT
2+
// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
//! An coalesce formula.
5+
6+
use super::Formula;
7+
use crate::{
8+
Error, Sample, logical_meter::logical_meter_actor, proto::common::v1::metrics::Metric,
9+
};
10+
use tokio::sync::{broadcast, mpsc, oneshot};
11+
12+
#[derive(Clone)]
13+
pub struct CoalesceFormula {
14+
formula: frequenz_microgrid_component_graph::CoalesceFormula,
15+
metric: Metric,
16+
instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
17+
}
18+
19+
impl std::fmt::Display for CoalesceFormula {
20+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
self.formula.fmt(f)
22+
}
23+
}
24+
25+
impl CoalesceFormula {
26+
pub(crate) fn new(
27+
formula: frequenz_microgrid_component_graph::CoalesceFormula,
28+
metric: Metric,
29+
instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
30+
) -> Self {
31+
Self {
32+
formula,
33+
metric,
34+
instructions_tx,
35+
}
36+
}
37+
}
38+
39+
impl Formula for CoalesceFormula {
40+
async fn subscribe(&self) -> Result<broadcast::Receiver<Sample>, Error> {
41+
let (tx, rx) = oneshot::channel();
42+
43+
self.instructions_tx
44+
.send(logical_meter_actor::Instruction::SubscribeFormula {
45+
formula: self.formula.to_string(),
46+
metric: self.metric,
47+
response_tx: tx,
48+
})
49+
.await
50+
.map_err(|e| Error::connection_failure(format!("Could not send instruction: {e}")))?;
51+
let receiver = rx.await.map_err(|e| {
52+
Error::connection_failure(format!("Could not receive instruction: {e}"))
53+
})?;
54+
55+
Ok(receiver)
56+
}
57+
}

src/logical_meter/formula/graph_formula_provider.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use frequenz_microgrid_component_graph::ComponentGraph;
1111
use std::collections::BTreeSet;
1212
use tokio::sync::mpsc;
1313

14-
use super::AggregationFormula;
14+
use super::{AggregationFormula, CoalesceFormula};
1515

1616
macro_rules! graph_formula_provider {
1717
($(($fnname:ident $(, $idsparam:ident)?)),+ $(,)?) => {$(
@@ -82,3 +82,11 @@ impl GraphFormulaProvider for AggregationFormula {
8282
(ev_charger, ev_charger_formula, ev_charger_ids),
8383
);
8484
}
85+
86+
impl GraphFormulaProvider for CoalesceFormula {
87+
impl_graph_formula_provider!(
88+
(grid, grid_coalesce_formula),
89+
(battery, battery_ac_coalesce_formula, battery_ids),
90+
(pv, pv_ac_coalesce_formula, pv_inverter_ids),
91+
);
92+
}

src/logical_meter/metric.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ define_metric! {
4141
{name: AcCurrentPhase1, formula: AggregationFormula},
4242
{name: AcCurrentPhase2, formula: AggregationFormula},
4343
{name: AcCurrentPhase3, formula: AggregationFormula},
44+
45+
{name: AcVoltage, formula: CoalesceFormula},
46+
{name: AcVoltagePhase1N, formula: CoalesceFormula},
47+
{name: AcVoltagePhase2N, formula: CoalesceFormula},
48+
{name: AcVoltagePhase3N, formula: CoalesceFormula},
49+
{name: AcVoltagePhase1Phase2, formula: CoalesceFormula},
50+
{name: AcVoltagePhase2Phase3, formula: CoalesceFormula},
51+
{name: AcVoltagePhase3Phase1, formula: CoalesceFormula},
52+
53+
{name: AcFrequency, formula: CoalesceFormula},
4454
}

0 commit comments

Comments
 (0)