-
Notifications
You must be signed in to change notification settings - Fork 3
Support voltage formulas #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
da5db09
Upgrade to latest component-graph
shsms 473f4a4
Rename `Formula` → `AggregationFormula`
shsms ebb439c
Move `AggregationFormula` into its own module
shsms 76ed7dd
Add new Metric representation bound to a formula type
shsms 472eb2e
Defines a `Formula` trait and implement it for `AggregationFormula`
shsms d077cd9
Implement a generic way to create formulas from the component graph
shsms 9704904
Upgrade the logical meter to use the new metric types
shsms cb32b8e
Remove old Metric definitions
shsms f50a1c3
Implement `CoalesceFormula`
shsms 2a8ddfb
Start a resampler for each requested metric for a component
shsms 5de5aa2
Update example to also stream grid voltage
shsms adac1a1
Fix doc-comment typo
shsms bcf19c2
Rename stream status channel for clarity
shsms 9aa1c78
Group resampled data by metrics and then by component ID
shsms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,129 +1,18 @@ | ||
| // License: MIT | ||
| // Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| //! A composable formula type, that can be subscribed to. | ||
| //! Formula module for the logical meter. | ||
|
|
||
| use tokio::sync::{broadcast, mpsc, oneshot}; | ||
| mod aggregation_formula; | ||
| mod coalesce_formula; | ||
| pub(crate) mod graph_formula_provider; | ||
| pub use aggregation_formula::AggregationFormula; | ||
| pub use coalesce_formula::CoalesceFormula; | ||
|
|
||
| use crate::{Error, Metric, Sample}; | ||
| use crate::{Error, Sample}; | ||
| use tokio::sync::broadcast; | ||
|
|
||
| use super::logical_meter_actor; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct Formula { | ||
| formula: frequenz_microgrid_component_graph::Formula, | ||
| metric: Metric, | ||
| instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>, | ||
| } | ||
|
|
||
| impl std::fmt::Display for Formula { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| self.formula.fmt(f) | ||
| } | ||
| } | ||
|
|
||
| impl Formula { | ||
| pub(super) fn new( | ||
| formula: frequenz_microgrid_component_graph::Formula, | ||
| metric: Metric, | ||
| instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>, | ||
| ) -> Self { | ||
| Self { | ||
| formula, | ||
| metric, | ||
| instructions_tx, | ||
| } | ||
| } | ||
|
|
||
| pub async fn subscribe(&self) -> Result<broadcast::Receiver<Sample>, Error> { | ||
| let (tx, rx) = oneshot::channel(); | ||
|
|
||
| self.instructions_tx | ||
| .send(logical_meter_actor::Instruction::SubscribeFormula { | ||
| formula: self.formula.to_string(), | ||
| metric: self.metric, | ||
| response_tx: tx, | ||
| }) | ||
| .await | ||
| .map_err(|e| Error::connection_failure(format!("Could not send instruction: {e}")))?; | ||
| let receiver = rx.await.map_err(|e| { | ||
| Error::connection_failure(format!("Could not receive instruction: {e}")) | ||
| })?; | ||
|
|
||
| Ok(receiver) | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Add for Formula { | ||
| type Output = Result<Self, Error>; | ||
|
|
||
| fn add(self, other: Self) -> Self::Output { | ||
| if self.metric != other.metric { | ||
| return Err(Error::invalid_metric(format!( | ||
| "Cannot add formulas with different metrics: {} and {}", | ||
| self.metric as isize, other.metric as isize | ||
| ))); | ||
| } | ||
| let new_formula = self.formula + other.formula; | ||
| Ok(Self::new(new_formula, self.metric, self.instructions_tx)) | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Sub for Formula { | ||
| type Output = Result<Self, Error>; | ||
|
|
||
| fn sub(self, other: Self) -> Self::Output { | ||
| if self.metric != other.metric { | ||
| return Err(Error::invalid_metric(format!( | ||
| "Cannot subtract formulas with different metrics: {} and {}", | ||
| self.metric as isize, other.metric as isize | ||
| ))); | ||
| } | ||
| let new_formula = self.formula - other.formula; | ||
| Ok(Self::new(new_formula, self.metric, self.instructions_tx)) | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Add<Formula> for Result<Formula, Error> { | ||
| type Output = Result<Formula, Error>; | ||
|
|
||
| fn add(self, other: Formula) -> Self::Output { | ||
| match self { | ||
| Ok(left) => left + other, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Sub<Formula> for Result<Formula, Error> { | ||
| type Output = Result<Formula, Error>; | ||
|
|
||
| fn sub(self, other: Formula) -> Self::Output { | ||
| match self { | ||
| Ok(left) => left - other, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Add<Result<Formula, Error>> for Formula { | ||
| type Output = Result<Formula, Error>; | ||
|
|
||
| fn add(self, other: Result<Formula, Error>) -> Self::Output { | ||
| match other { | ||
| Ok(right) => self + right, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Sub<Result<Formula, Error>> for Formula { | ||
| type Output = Result<Formula, Error>; | ||
|
|
||
| fn sub(self, other: Result<Formula, Error>) -> Self::Output { | ||
| match other { | ||
| Ok(right) => self - right, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| /// Defines a formula that can be subscribed to for receiving samples. | ||
| pub trait Formula: std::fmt::Display { | ||
| fn subscribe(&self) -> impl Future<Output = Result<broadcast::Receiver<Sample>, Error>> + Send; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trait method uses
impl Futurereturn type which requires importingstd::future::Future. This import is missing and will cause compilation errors.