Skip to content
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

feat(commands): filter get comments on attachment type #272

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Fixes issue when getting streams that have multiple filters on single user property
- Fixes issue where upper case file names would not be matched in `parse`
- Reduce batch size when deleting comment batches
- Support attachment type filters

# v0.24.0
- BREAKING: the `--context` option is now required. Users need to opt
Expand Down
11 changes: 6 additions & 5 deletions api/src/resources/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
source::Id as SourceId,
user::Username,
},
AnnotatedComment, CommentFilter, Continuation, LabelName,
AnnotatedComment, CommentFilter, Continuation,
};
use std::{
fmt::{Display, Formatter, Result as FmtResult},
Expand Down Expand Up @@ -87,12 +87,13 @@ pub enum TimeResolution {
#[serde(rename_all = "snake_case")]
pub enum Attribute {
Labels,
AttachmentPropertyTypes,
}

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AttributeFilterEnum {
StringAnyOf { any_of: Vec<LabelName> },
StringAnyOf { any_of: Vec<String> },
}

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -326,7 +327,7 @@ mod tests {
attribute_filters: vec![AttributeFilter {
attribute: Attribute::Labels,
filter: AttributeFilterEnum::StringAnyOf {
any_of: vec![LabelName("Access Management".to_string())],
any_of: vec!["Access Management".to_string()],
},
}],
continuation: Some(Continuation(
Expand Down Expand Up @@ -361,7 +362,7 @@ mod tests {
attribute_filters: vec![AttributeFilter {
attribute: Attribute::Labels,
filter: AttributeFilterEnum::StringAnyOf {
any_of: vec![LabelName("Access Management".to_string())],
any_of: vec!["Access Management".to_string()],
},
}],
continuation: Some(Continuation(
Expand Down Expand Up @@ -394,7 +395,7 @@ mod tests {
attribute_filters: vec![AttributeFilter {
attribute: Attribute::Labels,
filter: AttributeFilterEnum::StringAnyOf {
any_of: vec![LabelName("label Name".to_string())],
any_of: vec!["label Name".to_string()],
},
}],
label_property_timeseries: true,
Expand Down
39 changes: 29 additions & 10 deletions cli/src/commands/get/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ pub struct GetManyCommentsArgs {
#[structopt(long = "interactive-user-property-filter")]
/// Open a dialog to interactively construct the user property filter to use
interactive_property_filter: bool,

#[structopt(long = "attachment-types")]
/// The list of attachment types to filter to
attachment_type_filters: Vec<String>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -246,6 +250,7 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
to_timestamp,
path,
label_filter,
attachment_type_filters,
property_filter: user_property_filter,
interactive_property_filter: interative_property_filter,
recipients,
Expand Down Expand Up @@ -274,6 +279,10 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
bail!("Cannot use a label filter when `dataset` is not provided.")
}

if !attachment_type_filters.is_empty() && dataset.is_none() {
bail!("Cannot use a attachment type filter when `dataset` is not provided.")
}

if label_filter.is_some() && reviewed_only {
bail!("The `reviewed_only` and `label_filter` options are mutually exclusive.")
}
Expand Down Expand Up @@ -318,6 +327,17 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
}
}

let mut attachment_property_types_filter: Option<AttributeFilter> = None;

if !attachment_type_filters.is_empty() {
attachment_property_types_filter = Some(AttributeFilter {
attribute: Attribute::AttachmentPropertyTypes,
filter: AttributeFilterEnum::StringAnyOf {
any_of: attachment_type_filters.to_vec(),
},
});
}

let user_properties_filter = if let Some(filter) = user_property_filter {
Some(filter.0.clone())
} else if *interative_property_filter {
Expand Down Expand Up @@ -364,6 +384,7 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
show_progress: !no_progress,
label_attribute_filter,
user_properties_filter,
attachment_property_types_filter,
messages_filter: Some(messages_filter),
};

Expand All @@ -386,25 +407,18 @@ fn get_label_attribute_filter(
) -> Result<Option<AttributeFilter>> {
let dataset = client.get_dataset(dataset_id)?;

let label_names: Vec<LabelName> = dataset
let label_names: Vec<String> = dataset
.label_defs
.into_iter()
.filter(|label_def| filter.is_match(&label_def.name.0))
.map(|label_def| label_def.name)
.map(|label_def| label_def.name.0)
.collect();

if label_names.is_empty() {
info!("No label names matching the filter '{}'", filter);
Ok(None)
} else {
info!(
"Filtering on label(s):\n- {}",
label_names
.iter()
.map(|label_name| label_name.0.as_str())
.collect::<Vec<_>>()
.join("\n- ")
);
info!("Filtering on label(s):\n- {}", label_names.join("\n- "));
Ok(Some(AttributeFilter {
attribute: Attribute::Labels,
filter: AttributeFilterEnum::StringAnyOf {
Expand All @@ -422,6 +436,7 @@ struct CommentDownloadOptions {
timerange: CommentsIterTimerange,
show_progress: bool,
label_attribute_filter: Option<AttributeFilter>,
attachment_property_types_filter: Option<AttributeFilter>,
user_properties_filter: Option<UserPropertiesFilter>,
messages_filter: Option<MessagesFilter>,
}
Expand All @@ -434,6 +449,10 @@ impl CommentDownloadOptions {
filters.push(label_attribute_filter.clone());
}

if let Some(attachment_types_attribute_filter) = &self.attachment_property_types_filter {
filters.push(attachment_types_attribute_filter.clone())
}

filters
}
}
Expand Down
Loading