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): add only with attachments filter on get comments #298

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,4 +1,5 @@
# Unreleased
- Add `only-with-attachment` filter on get comments
- Add ability to upload attachment content for comments

# v0.29.0
Expand Down
9 changes: 8 additions & 1 deletion api/src/resources/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,19 @@ pub enum TimeResolution {
pub enum Attribute {
Labels,
AttachmentPropertyTypes,
AttachmentPropertyNumAttachments,
}

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

#[derive(Debug, Clone, Serialize)]
Expand Down
25 changes: 24 additions & 1 deletion cli/src/commands/get/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub struct GetManyCommentsArgs {
#[structopt(long = "attachments")]
/// Save attachment content for each comment
include_attachment_content: Option<bool>,

#[structopt(long = "--only-with-attachments")]
/// Save attachment content for each comment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect docstring

only_with_attachments: Option<bool>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -304,6 +308,7 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
recipients,
senders,
include_attachment_content,
only_with_attachments,
} = args;

let by_timerange = from_timestamp.is_some() || to_timestamp.is_some();
Expand All @@ -328,7 +333,8 @@ 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() {
if (!attachment_type_filters.is_empty() | only_with_attachments.is_some()) && dataset.is_none()
{
bail!("Cannot use a attachment type filter when `dataset` is not provided.")
}

Expand Down Expand Up @@ -387,6 +393,17 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
});
}

let mut only_with_attachments_filter: Option<AttributeFilter> = None;
if only_with_attachments.unwrap_or_default() {
only_with_attachments_filter = Some(AttributeFilter {
attribute: Attribute::AttachmentPropertyNumAttachments,
filter: AttributeFilterEnum::NumberRange {
minimum: Some(1),
maximum: None,
},
});
}

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

if let Some(file) = jsonl_file {
Expand Down Expand Up @@ -490,6 +508,7 @@ struct CommentDownloadOptions {
user_properties_filter: Option<UserPropertiesFilter>,
messages_filter: Option<MessagesFilter>,
attachments_dir: Option<PathBuf>,
only_with_attachments_filter: Option<AttributeFilter>,
}

impl CommentDownloadOptions {
Expand All @@ -504,6 +523,10 @@ impl CommentDownloadOptions {
filters.push(attachment_types_attribute_filter.clone())
}

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

filters
}
}
Expand Down
Loading