-
Notifications
You must be signed in to change notification settings - Fork 8
rust(feat): add report tooling for external rules #718
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c9d353
initial framework
Brandon-Shippy d0a663a
Merge main into rust/mcp-external-rules-reports
Brandon-Shippy 492b24e
polish
Brandon-Shippy 85bdba5
add deafult archived false guidance
Brandon-Shippy f59625c
update skill file
Brandon-Shippy 6571624
cargo fmt
Brandon-Shippy e9effd8
update the skill file
Brandon-Shippy 6638bb4
Address feedback
Brandon-Shippy 7fb6205
change from always to default
Brandon-Shippy 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
254 changes: 254 additions & 0 deletions
254
rust/crates/sift_mcp/src/service/report_templates/mod.rs
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,254 @@ | ||
| use crate::policy::{RetryPolicy, with_retry}; | ||
| use crate::service::common; | ||
| use anyhow::{Context, Result, anyhow}; | ||
| use pbjson_types::FieldMask; | ||
| use sift_rs::{ | ||
| SiftChannel, | ||
| metadata::v1::MetadataValue, | ||
| report_templates::v1::{ | ||
| CreateReportTemplateRequest, CreateReportTemplateRequestClientKeys, | ||
| CreateReportTemplateRequestRuleIds, ListReportTemplatesRequest, | ||
| ListReportTemplatesResponse, ReportTemplate, ReportTemplateRule, ReportTemplateTag, | ||
| UpdateReportTemplateRequest, create_report_template_request, | ||
| report_template_service_client::ReportTemplateServiceClient, | ||
| }, | ||
| }; | ||
|
|
||
| #[cfg(test)] | ||
| mod test; | ||
|
|
||
| #[allow(clippy::enum_variant_names)] | ||
| pub enum TemplateRuleIdentifier { | ||
| RuleIds(Vec<String>), | ||
| RuleClientKeys(Vec<String>), | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub struct ReportTemplateUpdate { | ||
| pub name: Option<String>, | ||
| pub description: Option<String>, | ||
| pub tag_names: Option<Vec<String>>, | ||
| pub rules: Option<TemplateRuleIdentifier>, | ||
| pub metadata: Option<Vec<MetadataValue>>, | ||
| pub is_archived: Option<bool>, | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct ReportTemplateService { | ||
| channel: SiftChannel, | ||
| policy: RetryPolicy, | ||
| } | ||
|
|
||
| impl ReportTemplateService { | ||
| pub fn new(channel: SiftChannel, policy: RetryPolicy) -> Self { | ||
| Self { channel, policy } | ||
| } | ||
|
|
||
| pub async fn list_report_templates( | ||
| &self, | ||
| filter: String, | ||
| order_by: Option<String>, | ||
| limit: Option<u32>, | ||
| organization_id: Option<String>, | ||
| ) -> Result<Vec<ReportTemplate>> { | ||
| let (page_size, record_limit) = common::paging(limit); | ||
|
|
||
| let mut page_token = String::new(); | ||
| let mut results = Vec::new(); | ||
|
|
||
| let order_by = order_by.unwrap_or_default(); | ||
| let organization_id = organization_id.unwrap_or_default(); | ||
|
|
||
| loop { | ||
| let channel = self.channel.clone(); | ||
| let filter = filter.clone(); | ||
| let order_by = order_by.clone(); | ||
| let organization_id = organization_id.clone(); | ||
| let token = page_token.clone(); | ||
|
|
||
| let resp = with_retry(&self.policy, move || { | ||
| let channel = channel.clone(); | ||
| let filter = filter.clone(); | ||
| let order_by = order_by.clone(); | ||
| let organization_id = organization_id.clone(); | ||
| let token = token.clone(); | ||
| async move { | ||
| let mut client = ReportTemplateServiceClient::new(channel); | ||
| client | ||
| .list_report_templates(ListReportTemplatesRequest { | ||
| page_size, | ||
| page_token: token, | ||
| filter, | ||
| organization_id, | ||
| order_by, | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .map(|resp| resp.into_inner()) | ||
| } | ||
| }) | ||
| .await | ||
| .context("failed to query report templates")?; | ||
|
|
||
| let ListReportTemplatesResponse { | ||
| report_templates, | ||
| next_page_token, | ||
| } = resp; | ||
| if report_templates.is_empty() { | ||
| break; | ||
| } | ||
| results.extend(report_templates); | ||
|
|
||
| if results.len() >= record_limit || next_page_token.is_empty() { | ||
| break; | ||
| } | ||
| page_token = next_page_token; | ||
| } | ||
|
|
||
| results.truncate(record_limit); | ||
|
|
||
| Ok(results) | ||
| } | ||
|
|
||
| pub async fn create_report_template( | ||
| &self, | ||
| organization_id: Option<String>, | ||
| name: String, | ||
| client_key: Option<String>, | ||
| description: Option<String>, | ||
| tag_names: Vec<String>, | ||
| rules: TemplateRuleIdentifier, | ||
| metadata: Vec<MetadataValue>, | ||
| ) -> Result<ReportTemplate> { | ||
| let rule_identifiers = match rules { | ||
| TemplateRuleIdentifier::RuleIds(rule_ids) => { | ||
| create_report_template_request::RuleIdentifiers::RuleIds( | ||
| CreateReportTemplateRequestRuleIds { rule_ids }, | ||
| ) | ||
| } | ||
| TemplateRuleIdentifier::RuleClientKeys(rule_client_keys) => { | ||
| create_report_template_request::RuleIdentifiers::RuleClientKeys( | ||
| CreateReportTemplateRequestClientKeys { rule_client_keys }, | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| let request = CreateReportTemplateRequest { | ||
| name, | ||
| client_key, | ||
| description, | ||
| tag_names, | ||
| organization_id: organization_id.unwrap_or_default(), | ||
| rule_identifiers: Some(rule_identifiers), | ||
| metadata, | ||
| }; | ||
|
|
||
| let channel = self.channel.clone(); | ||
| let resp = with_retry(&self.policy, move || { | ||
| let channel = channel.clone(); | ||
| let request = request.clone(); | ||
| async move { | ||
| let mut client = ReportTemplateServiceClient::new(channel); | ||
| client | ||
| .create_report_template(request) | ||
| .await | ||
| .map(|resp| resp.into_inner()) | ||
| } | ||
| }) | ||
| .await | ||
| .context("failed to create report template")?; | ||
|
|
||
| resp.report_template | ||
| .ok_or_else(|| anyhow!("create_report_template response missing report_template")) | ||
| } | ||
|
|
||
| pub async fn update_report_template( | ||
| &self, | ||
| report_template_id: String, | ||
| changes: ReportTemplateUpdate, | ||
| ) -> Result<ReportTemplate> { | ||
| let ReportTemplateUpdate { | ||
| name, | ||
| description, | ||
| tag_names, | ||
| rules, | ||
| metadata, | ||
| is_archived, | ||
| } = changes; | ||
|
|
||
| let mut template = ReportTemplate { | ||
| report_template_id, | ||
| ..Default::default() | ||
| }; | ||
| let mut paths = Vec::new(); | ||
|
|
||
| if let Some(name) = name { | ||
| template.name = name; | ||
| paths.push("name".to_string()); | ||
| } | ||
| if let Some(description) = description { | ||
| template.description = Some(description); | ||
| paths.push("description".to_string()); | ||
| } | ||
| if let Some(tag_names) = tag_names { | ||
| template.tags = tag_names | ||
| .into_iter() | ||
| .map(|tag_name| ReportTemplateTag { tag_name }) | ||
| .collect(); | ||
| paths.push("tags".to_string()); | ||
| } | ||
| if let Some(rules) = rules { | ||
| template.rules = template_rules_from_identifier(rules); | ||
| paths.push("rules".to_string()); | ||
| } | ||
| if let Some(metadata) = metadata { | ||
| template.metadata = metadata; | ||
| paths.push("metadata".to_string()); | ||
| } | ||
| if let Some(is_archived) = is_archived { | ||
| template.is_archived = is_archived; | ||
| paths.push("is_archived".to_string()); | ||
| } | ||
|
|
||
| let channel = self.channel.clone(); | ||
| let resp = with_retry(&self.policy, move || { | ||
| let channel = channel.clone(); | ||
| let template = template.clone(); | ||
| let paths = paths.clone(); | ||
| async move { | ||
| let mut client = ReportTemplateServiceClient::new(channel); | ||
| client | ||
| .update_report_template(UpdateReportTemplateRequest { | ||
| report_template: Some(template), | ||
| update_mask: Some(FieldMask { paths }), | ||
| }) | ||
| .await | ||
| .map(|resp| resp.into_inner()) | ||
| } | ||
| }) | ||
| .await | ||
| .context("failed to update report template")?; | ||
|
|
||
| resp.report_template | ||
| .ok_or_else(|| anyhow!("update_report_template response missing report_template")) | ||
| } | ||
| } | ||
|
|
||
| fn template_rules_from_identifier(rules: TemplateRuleIdentifier) -> Vec<ReportTemplateRule> { | ||
| match rules { | ||
| TemplateRuleIdentifier::RuleIds(rule_ids) => rule_ids | ||
| .into_iter() | ||
| .map(|rule_id| ReportTemplateRule { | ||
| rule_id, | ||
| ..Default::default() | ||
| }) | ||
| .collect(), | ||
| TemplateRuleIdentifier::RuleClientKeys(client_keys) => client_keys | ||
| .into_iter() | ||
| .map(|client_key| ReportTemplateRule { | ||
| client_key, | ||
| ..Default::default() | ||
| }) | ||
| .collect(), | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.