Skip to content

Commit

Permalink
feat(commands): add lossy flag when creating annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-prosser committed Jul 24, 2024
1 parent 1758c3d commit 62ab5c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
23 changes: 21 additions & 2 deletions cli/src/commands/create/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::progress::{Options as ProgressOptions, Progress};
use crate::{
print_error_as_warning,
progress::{Options as ProgressOptions, Progress},
};
use anyhow::{Context, Result};
use colored::Colorize;
use log::info;
Expand Down Expand Up @@ -47,6 +50,10 @@ pub struct CreateAnnotationsArgs {
#[structopt(long = "batch-size", default_value = "128")]
/// Number of comments to batch in a single request.
batch_size: usize,

#[structopt(long)]
/// Whether to attempt to resume processing on error
lossy: bool,
}

pub fn create(client: &Client, args: &CreateAnnotationsArgs, pool: &mut Pool) -> Result<()> {
Expand Down Expand Up @@ -95,6 +102,7 @@ pub fn create(client: &Client, args: &CreateAnnotationsArgs, pool: &mut Pool) ->
args.use_moon_forms,
args.batch_size,
pool,
args.lossy,
)?;
if let Some(mut progress) = progress {
progress.done();
Expand All @@ -117,6 +125,7 @@ pub fn create(client: &Client, args: &CreateAnnotationsArgs, pool: &mut Pool) ->
args.use_moon_forms,
args.batch_size,
pool,
args.lossy,
)?;
statistics
}
Expand All @@ -133,6 +142,7 @@ pub trait AnnotationStatistic {
fn add_annotation(&self);
}

#[allow(clippy::too_many_arguments)]
pub fn upload_batch_of_annotations(
annotations_to_upload: &mut Vec<NewAnnotation>,
client: &Client,
Expand All @@ -141,6 +151,7 @@ pub fn upload_batch_of_annotations(
dataset_name: &DatasetFullName,
use_moon_forms: bool,
pool: &mut Pool,
lossy: bool,
) -> Result<()> {
let (error_sender, error_receiver) = channel();

Expand Down Expand Up @@ -190,7 +201,12 @@ pub fn upload_batch_of_annotations(
});

if let Ok(error) = error_receiver.try_recv() {
Err(error)
if lossy {
print_error_as_warning(&error);
Ok(())
} else {
Err(error)
}
} else {
annotations_to_upload.clear();
Ok(())
Expand All @@ -207,6 +223,7 @@ fn upload_annotations_from_reader(
use_moon_forms: bool,
batch_size: usize,
pool: &mut Pool,
lossy: bool,
) -> Result<()> {
let mut annotations_to_upload = Vec::new();

Expand All @@ -224,6 +241,7 @@ fn upload_annotations_from_reader(
dataset_name,
use_moon_forms,
pool,
lossy,
)?;
}
}
Expand All @@ -238,6 +256,7 @@ fn upload_annotations_from_reader(
dataset_name,
use_moon_forms,
pool,
lossy,
)?;
}

Expand Down
9 changes: 9 additions & 0 deletions cli/src/commands/create/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub struct CreateCommentsArgs {
#[structopt(short = "y", long = "yes")]
/// Consent to ai unit charge. Suppresses confirmation prompt.
yes: bool,

#[structopt(long)]
/// Whether to attempt to resume processing on error
lossy: bool,
}

pub fn create(client: &Client, args: &CreateCommentsArgs, pool: &mut Pool) -> Result<()> {
Expand Down Expand Up @@ -152,6 +156,7 @@ pub fn create(client: &Client, args: &CreateCommentsArgs, pool: &mut Pool) -> Re
args.use_moon_forms,
args.no_charge,
pool,
args.lossy,
)?;
if let Some(mut progress) = progress {
progress.done();
Expand Down Expand Up @@ -180,6 +185,7 @@ pub fn create(client: &Client, args: &CreateCommentsArgs, pool: &mut Pool) -> Re
args.use_moon_forms,
args.no_charge,
pool,
args.lossy,
)?;
statistics
}
Expand Down Expand Up @@ -330,6 +336,7 @@ fn upload_comments_from_reader(
use_moon_forms: bool,
no_charge: bool,
pool: &mut Pool,
lossy: bool,
) -> Result<()> {
assert!(batch_size > 0);

Expand Down Expand Up @@ -415,6 +422,7 @@ fn upload_comments_from_reader(
dataset_name,
use_moon_forms,
pool,
lossy,
)?;
}
}
Expand Down Expand Up @@ -442,6 +450,7 @@ fn upload_comments_from_reader(
dataset_name,
use_moon_forms,
pool,
lossy,
)?;
}
}
Expand Down
19 changes: 15 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,26 @@ fn find_configuration(args: &Args) -> Result<PathBuf> {
Ok(config_path)
}

pub fn print_error_as_warning(error: &anyhow::Error) {
warn!("An error occurred. Resuming...:");
for cause in error.chain() {
warn!(" |- {}", cause);
}
}

pub fn print_error(error: &anyhow::Error) {
error!("An error occurred:");
for cause in error.chain() {
error!(" |- {}", cause);
}
}

fn main() {
let args = Args::from_args();
utils::init_env_logger(args.verbose);

if let Err(error) = run(args) {
error!("An error occurred:");
for cause in error.chain() {
error!(" |- {}", cause);
}
print_error(&error);

#[cfg(feature = "backtrace")]
{
Expand Down

0 comments on commit 62ab5c1

Please sign in to comment.