-
Notifications
You must be signed in to change notification settings - Fork 8
rust(feature): support calculated channels in cli #501
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
Open
tsift
wants to merge
5
commits into
main
Choose a base branch
from
rust/support-calculated-channels-in-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+291
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86a2634
rust(feat): Support calculated channels in sift-cli
tsift d84f976
Merge branch 'main' into rust/support-calculated-channels-in-cli
tsift 8c41829
rust(feat): Add calculated-channel export support
tsift 9c36445
Merge branch 'main' into rust/support-calculated-channels-in-cli
tsift 240cb2d
PR feedback
tsift 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| use anyhow::{Context, Result, anyhow}; | ||
| use sift_rs::{ | ||
| SiftChannel, | ||
| calculated_channels::v2::{ | ||
| CalculatedChannel, CalculatedChannelAbstractChannelReference, | ||
| ListCalculatedChannelsRequest, ListCalculatedChannelsResponse, | ||
| ResolveCalculatedChannelRequest, calculated_channel_asset_configuration::AssetScope, | ||
| calculated_channel_service_client::CalculatedChannelServiceClient, | ||
| resolve_calculated_channel_request::CalculatedChannel as RequestCalculatedChannel, | ||
| }, | ||
| common::r#type::v1::{ | ||
| Ids, NamedResources, ResourceIdentifier, named_resources::Resources, | ||
| resource_identifier::Identifier, | ||
| }, | ||
| exports::v1::CalculatedChannelConfig, | ||
| }; | ||
|
|
||
| pub enum ResolveScope<'a> { | ||
| Run(&'a str), | ||
| Assets(&'a [String]), | ||
| } | ||
|
|
||
| pub async fn filter_calculated_channels( | ||
| grpc_channel: SiftChannel, | ||
| filter: &str, | ||
| ) -> Result<Vec<CalculatedChannel>> { | ||
| let mut service = CalculatedChannelServiceClient::new(grpc_channel); | ||
| let mut page_token = String::new(); | ||
| let mut query_result = Vec::new(); | ||
|
|
||
| loop { | ||
| let ListCalculatedChannelsResponse { | ||
| calculated_channels, | ||
| next_page_token, | ||
| .. | ||
| } = service | ||
| .list_calculated_channels(ListCalculatedChannelsRequest { | ||
| page_token, | ||
| filter: filter.to_string(), | ||
| page_size: 1000, | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .context("failed to query calculated channels")? | ||
| .into_inner(); | ||
|
|
||
| query_result.extend(calculated_channels.into_iter()); | ||
|
|
||
| if next_page_token.is_empty() { | ||
| break; | ||
| } | ||
| page_token = next_page_token; | ||
| } | ||
| Ok(query_result) | ||
| } | ||
|
|
||
| pub fn channel_applies_to_assets(channel: &CalculatedChannel, asset_ids: &[String]) -> bool { | ||
| let Some(config) = &channel.calculated_channel_configuration else { | ||
| return true; | ||
| }; | ||
| let Some(asset_config) = &config.asset_configuration else { | ||
| return true; | ||
| }; | ||
| match &asset_config.asset_scope { | ||
| None => true, | ||
| Some(AssetScope::AllAssets(_)) => true, | ||
| Some(AssetScope::Selection(selection)) => { | ||
| selection.asset_ids.iter().any(|id| asset_ids.contains(id)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub async fn resolve_to_calculated_channel_configs( | ||
| grpc_channel: SiftChannel, | ||
| channel: &CalculatedChannel, | ||
| scope: &ResolveScope<'_>, | ||
| ) -> Result<Vec<CalculatedChannelConfig>> { | ||
| let mut service = CalculatedChannelServiceClient::new(grpc_channel); | ||
|
|
||
| let (assets, run) = match scope { | ||
| ResolveScope::Run(run_id) => ( | ||
| None, | ||
| Some(ResourceIdentifier { | ||
| identifier: Some(Identifier::Id(run_id.to_string())), | ||
| }), | ||
| ), | ||
| ResolveScope::Assets(asset_ids) => ( | ||
| Some(NamedResources { | ||
| resources: Some(Resources::Ids(Ids { | ||
| ids: asset_ids.to_vec(), | ||
| })), | ||
| }), | ||
| None, | ||
| ), | ||
| }; | ||
|
|
||
| let response = service | ||
| .resolve_calculated_channel(ResolveCalculatedChannelRequest { | ||
| assets, | ||
| run, | ||
| calculated_channel: Some(RequestCalculatedChannel::Identifier(ResourceIdentifier { | ||
| identifier: Some(Identifier::Id(channel.calculated_channel_id.clone())), | ||
| })), | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .with_context(|| format!("failed to resolve calculated channel '{}'", channel.name))? | ||
| .into_inner(); | ||
|
|
||
| if !response.unresolved.is_empty() { | ||
| let assets: Vec<_> = response | ||
| .unresolved | ||
| .iter() | ||
| .map(|u| format!("'{}': {}", u.asset_name, u.error_message)) | ||
| .collect(); | ||
| return Err(anyhow!( | ||
| "calculated channel '{}' could not be resolved for the following assets:\n{}", | ||
| channel.name, | ||
| assets.join("\n") | ||
| )); | ||
| } | ||
|
|
||
| response | ||
| .resolved | ||
| .into_iter() | ||
| .map(|resolved| { | ||
| let expr = resolved.expression_request.ok_or_else(|| { | ||
| anyhow!( | ||
| "resolved calculated channel '{}' has no expression request", | ||
| channel.name | ||
| ) | ||
| })?; | ||
|
|
||
| let channel_references = expr | ||
| .expression_channel_references | ||
| .into_iter() | ||
| .map(|r| CalculatedChannelAbstractChannelReference { | ||
| channel_reference: r.channel_reference, | ||
| channel_identifier: r.channel_id, | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(CalculatedChannelConfig { | ||
| name: channel.name.clone(), | ||
| expression: expr.expression, | ||
| channel_references, | ||
| units: channel.units.clone(), | ||
| }) | ||
| }) | ||
| .collect() | ||
| } |
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,4 +1,5 @@ | ||
| pub mod api; | ||
| pub mod calculated_channel; | ||
| pub mod channel; | ||
| pub mod job; | ||
| pub mod progress; | ||
|
|
||
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.
If a user provides only invalid channels or calculated channels, it looks like we revert back to exporting the whole run if I understand this correctly. Should we in that case just return an error instead? Otherwise we're kicking off a huge job the user probably doesn't want.