55
66use super :: { FormulaParams , FormulaSubscriber , GraphFormulaProvider } ;
77use crate :: {
8- Error , Sample , logical_meter:: logical_meter_actor, proto :: common :: v1alpha8 :: metrics :: Metric ,
8+ Error , Sample , logical_meter:: logical_meter_actor, metric :: Metric , quantity :: Quantity ,
99} ;
1010use tokio:: sync:: { broadcast, mpsc, oneshot} ;
1111
1212#[ derive( Clone ) ]
13- pub struct AggregationFormula {
13+ pub struct AggregationFormula < M : Metric > {
1414 formula : frequenz_microgrid_component_graph:: AggregationFormula ,
15- metric : Metric ,
15+ metric : M ,
1616 instructions_tx : mpsc:: Sender < logical_meter_actor:: Instruction > ,
1717}
1818
19- impl std:: fmt:: Display for AggregationFormula {
19+ impl < M : Metric > std:: fmt:: Display for AggregationFormula < M > {
2020 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
2121 self . formula . fmt ( f)
2222 }
2323}
2424
25- impl GraphFormulaProvider for AggregationFormula {
25+ impl < M : Metric > GraphFormulaProvider for AggregationFormula < M > {
2626 type GraphFormulaType = frequenz_microgrid_component_graph:: AggregationFormula ;
2727}
2828
29- impl FormulaSubscriber for AggregationFormula {
30- async fn subscribe ( & self ) -> Result < broadcast:: Receiver < Sample > , Error > {
29+ impl < Q : Quantity + ' static , M : Metric < QuantityType = Q > + Sync > FormulaSubscriber
30+ for AggregationFormula < M >
31+ {
32+ type MetricType = M ;
33+
34+ async fn subscribe ( & self ) -> Result < broadcast:: Receiver < Sample < Q > > , Error > {
3135 let ( tx, rx) = oneshot:: channel ( ) ;
3236
3337 self . instructions_tx
3438 . send ( logical_meter_actor:: Instruction :: SubscribeFormula {
3539 formula : self . formula . to_string ( ) ,
36- metric : self . metric ,
37- response_tx : tx,
40+ metric : M :: METRIC ,
41+ response_tx : tx. try_into ( ) ? ,
3842 } )
3943 . await
4044 . map_err ( |e| Error :: connection_failure ( format ! ( "Could not send instruction: {e}" ) ) ) ?;
@@ -46,8 +50,8 @@ impl FormulaSubscriber for AggregationFormula {
4650 }
4751}
4852
49- impl From < FormulaParams < AggregationFormula > > for AggregationFormula {
50- fn from ( params : FormulaParams < AggregationFormula > ) -> Self {
53+ impl < M : Metric > From < FormulaParams < AggregationFormula < M > , M > > for AggregationFormula < M > {
54+ fn from ( params : FormulaParams < AggregationFormula < M > , M > ) -> Self {
5155 Self {
5256 formula : params. formula ,
5357 metric : params. metric ,
@@ -56,8 +60,8 @@ impl From<FormulaParams<AggregationFormula>> for AggregationFormula {
5660 }
5761}
5862
59- impl From < AggregationFormula > for FormulaParams < AggregationFormula > {
60- fn from ( formula : AggregationFormula ) -> Self {
63+ impl < M : Metric > From < AggregationFormula < M > > for FormulaParams < AggregationFormula < M > , M > {
64+ fn from ( formula : AggregationFormula < M > ) -> Self {
6165 FormulaParams {
6266 formula : formula. formula ,
6367 metric : formula. metric ,
@@ -66,73 +70,61 @@ impl From<AggregationFormula> for FormulaParams<AggregationFormula> {
6670 }
6771}
6872
69- impl std:: ops:: Add for AggregationFormula {
73+ impl < M : Metric > std:: ops:: Add for AggregationFormula < M > {
7074 type Output = Result < Self , Error > ;
7175
7276 fn add ( self , other : Self ) -> Self :: Output {
73- if self . metric != other. metric {
74- return Err ( Error :: invalid_metric ( format ! (
75- "Cannot add formulas with different metrics: {} and {}" ,
76- self . metric as isize , other. metric as isize
77- ) ) ) ;
78- }
7977 let new_formula = self . formula + other. formula ;
8078 Ok ( FormulaParams :: new ( new_formula, self . metric , self . instructions_tx ) . into ( ) )
8179 }
8280}
8381
84- impl std:: ops:: Sub for AggregationFormula {
82+ impl < M : Metric > std:: ops:: Sub for AggregationFormula < M > {
8583 type Output = Result < Self , Error > ;
8684
8785 fn sub ( self , other : Self ) -> Self :: Output {
88- if self . metric != other. metric {
89- return Err ( Error :: invalid_metric ( format ! (
90- "Cannot subtract formulas with different metrics: {} and {}" ,
91- self . metric as isize , other. metric as isize
92- ) ) ) ;
93- }
9486 let new_formula = self . formula - other. formula ;
9587 Ok ( FormulaParams :: new ( new_formula, self . metric , self . instructions_tx ) . into ( ) )
9688 }
9789}
9890
99- impl std:: ops:: Add < AggregationFormula > for Result < AggregationFormula , Error > {
100- type Output = Result < AggregationFormula , Error > ;
91+ impl < M : Metric > std:: ops:: Add < AggregationFormula < M > > for Result < AggregationFormula < M > , Error > {
92+ type Output = Result < AggregationFormula < M > , Error > ;
10193
102- fn add ( self , other : AggregationFormula ) -> Self :: Output {
94+ fn add ( self , other : AggregationFormula < M > ) -> Self :: Output {
10395 match self {
10496 Ok ( left) => left + other,
10597 Err ( e) => Err ( e) ,
10698 }
10799 }
108100}
109101
110- impl std:: ops:: Sub < AggregationFormula > for Result < AggregationFormula , Error > {
111- type Output = Result < AggregationFormula , Error > ;
102+ impl < M : Metric > std:: ops:: Sub < AggregationFormula < M > > for Result < AggregationFormula < M > , Error > {
103+ type Output = Result < AggregationFormula < M > , Error > ;
112104
113- fn sub ( self , other : AggregationFormula ) -> Self :: Output {
105+ fn sub ( self , other : AggregationFormula < M > ) -> Self :: Output {
114106 match self {
115107 Ok ( left) => left - other,
116108 Err ( e) => Err ( e) ,
117109 }
118110 }
119111}
120112
121- impl std:: ops:: Add < Result < AggregationFormula , Error > > for AggregationFormula {
122- type Output = Result < AggregationFormula , Error > ;
113+ impl < M : Metric > std:: ops:: Add < Result < AggregationFormula < M > , Error > > for AggregationFormula < M > {
114+ type Output = Result < AggregationFormula < M > , Error > ;
123115
124- fn add ( self , other : Result < AggregationFormula , Error > ) -> Self :: Output {
116+ fn add ( self , other : Result < AggregationFormula < M > , Error > ) -> Self :: Output {
125117 match other {
126118 Ok ( right) => self + right,
127119 Err ( e) => Err ( e) ,
128120 }
129121 }
130122}
131123
132- impl std:: ops:: Sub < Result < AggregationFormula , Error > > for AggregationFormula {
133- type Output = Result < AggregationFormula , Error > ;
124+ impl < M : Metric > std:: ops:: Sub < Result < AggregationFormula < M > , Error > > for AggregationFormula < M > {
125+ type Output = Result < AggregationFormula < M > , Error > ;
134126
135- fn sub ( self , other : Result < AggregationFormula , Error > ) -> Self :: Output {
127+ fn sub ( self , other : Result < AggregationFormula < M > , Error > ) -> Self :: Output {
136128 match other {
137129 Ok ( right) => self - right,
138130 Err ( e) => Err ( e) ,
0 commit comments