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(comments): add messages filters #248

Merged
merged 1 commit into from
Jan 9, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased
- Add messages filters

## v0.21.3
- Reduce batch size for parse emls

Expand Down
16 changes: 14 additions & 2 deletions api/src/resources/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ pub enum ReviewedFilterEnum {
type UserPropertyName = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserPropertiesFilter(pub HashMap<UserPropertyName, UserPropertyFilterKind>);
pub struct UserPropertiesFilter(pub HashMap<UserPropertyName, PropertyFilterKind>);

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UserPropertyFilterKind {
pub enum PropertyFilterKind {
OneOf(Vec<PropertyValue>),
}

Expand All @@ -89,6 +89,18 @@ pub struct CommentFilter {
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub sources: Vec<SourceId>,

#[serde(skip_serializing_if = "Option::is_none")]
pub messages: Option<MessagesFilter>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MessagesFilter {
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<PropertyFilterKind>,

#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<PropertyFilterKind>,
}

#[derive(Debug, Clone, Serialize)]
Expand Down
4 changes: 2 additions & 2 deletions api/src/resources/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub(crate) struct UpdateResponse {
#[cfg(test)]
mod tests {
use crate::resources::comment::{
CommentTimestampFilter, UserPropertiesFilter, UserPropertyFilterKind,
CommentTimestampFilter, PropertyFilterKind, UserPropertiesFilter,
};

use super::*;
Expand Down Expand Up @@ -431,7 +431,7 @@ mod tests {
pub fn test_serialize_user_properties_request_params() {
let user_property_filter = UserPropertiesFilter(HashMap::from([(
"string:Generation Tag".to_string(),
UserPropertyFilterKind::OneOf(vec![PropertyValue::String(
PropertyFilterKind::OneOf(vec![PropertyValue::String(
"72b01fe7-ef2e-481e-934d-bc2fe0ca9b06".to_string(),
)]),
)]));
Expand Down
46 changes: 40 additions & 6 deletions cli/src/commands/get/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use regex::Regex;
use reinfer_client::{
resources::{
comment::{
CommentTimestampFilter, ReviewedFilterEnum, UserPropertiesFilter,
UserPropertyFilterKind,
CommentTimestampFilter, MessagesFilter, PropertyFilterKind, ReviewedFilterEnum,
UserPropertiesFilter,
},
dataset::{
Attribute, AttributeFilter, AttributeFilterEnum, OrderEnum, QueryRequestParams,
Expand Down Expand Up @@ -89,6 +89,14 @@ pub struct GetManyCommentsArgs {
/// Ending timestamp for comments to retrieve (inclusive).
to_timestamp: Option<DateTime<Utc>>,

#[structopt(long = "senders")]
/// Filter to comments only from these senders
senders: Option<Vec<String>>,

#[structopt(long = "recipients")]
/// Filter to emails only to these recipients
recipients: Option<Vec<String>>,

#[structopt(short = "f", long = "file", parse(from_os_str))]
/// Path where to write comments as JSON. If not specified, stdout will be used.
path: Option<PathBuf>,
Expand Down Expand Up @@ -211,10 +219,7 @@ fn get_user_properties_filter_interactively(
}
}

filters.insert(
selected_property_name,
UserPropertyFilterKind::OneOf(values),
);
filters.insert(selected_property_name, PropertyFilterKind::OneOf(values));

if !Confirm::new()
.with_prompt("Do you want to filter additional user properties?")
Expand All @@ -241,6 +246,8 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
label_filter,
property_filter: user_property_filter,
interactive_property_filter: interative_property_filter,
recipients,
senders,
} = args;

let by_timerange = from_timestamp.is_some() || to_timestamp.is_some();
Expand Down Expand Up @@ -287,6 +294,10 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
bail!("The `interative_property_filter` and `property_filter` options are mutually exclusive.")
}

if (senders.is_some() || recipients.is_some()) && dataset.is_none() {
bail!("Cannot filter on `senders` or `recipients` when `dataset` is not provided")
}

let file = match path {
Some(path) => Some(
File::create(path)
Expand Down Expand Up @@ -316,6 +327,25 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
None
};

let messages_filter = MessagesFilter {
from: senders.as_ref().map(|senders| {
PropertyFilterKind::OneOf(
senders
.iter()
.map(|sender| PropertyValue::String(sender.to_owned()))
.collect(),
)
}),
to: recipients.as_ref().map(|recipients| {
PropertyFilterKind::OneOf(
recipients
.iter()
.map(|recipient| PropertyValue::String(recipient.to_owned()))
.collect(),
)
}),
};

let download_options = CommentDownloadOptions {
dataset_identifier: dataset.clone(),
include_predictions: include_predictions.unwrap_or(false),
Expand All @@ -328,6 +358,7 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
show_progress: !no_progress,
label_attribute_filter,
user_properties_filter,
messages_filter: Some(messages_filter),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be None if both senders and recipients are None? Or does it not really matter api/perf wise

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It doesn't matter api wise :)

};

if let Some(file) = file {
Expand Down Expand Up @@ -386,6 +417,7 @@ struct CommentDownloadOptions {
show_progress: bool,
label_attribute_filter: Option<AttributeFilter>,
user_properties_filter: Option<UserPropertiesFilter>,
messages_filter: Option<MessagesFilter>,
}

impl CommentDownloadOptions {
Expand Down Expand Up @@ -424,6 +456,7 @@ fn download_comments(
None
},
user_properties: options.user_properties_filter.clone(),
messages: options.messages_filter.clone(),
};

Ok(get_comments_progress_bar(
Expand Down Expand Up @@ -541,6 +574,7 @@ fn get_comments_from_uids(
}),
user_properties: options.user_properties_filter.clone(),
sources: vec![source.id],
messages: options.messages_filter.clone(),
},
limit: DEFAULT_QUERY_PAGE_SIZE,
order: OrderEnum::Recent,
Expand Down