-
Notifications
You must be signed in to change notification settings - Fork 1
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 lossy flag when creating annotations #289
Changes from 4 commits
62ab5c1
4345cef
4198ccf
9c5e67a
9bb5df2
8acf879
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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<()> { | ||
|
@@ -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(); | ||
|
@@ -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 | ||
} | ||
|
@@ -131,8 +140,10 @@ pub fn create(client: &Client, args: &CreateAnnotationsArgs, pool: &mut Pool) -> | |
|
||
pub trait AnnotationStatistic { | ||
fn add_annotation(&self); | ||
fn add_failed_annotation(&self); | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub fn upload_batch_of_annotations( | ||
annotations_to_upload: &mut Vec<NewAnnotation>, | ||
client: &Client, | ||
|
@@ -141,6 +152,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(); | ||
|
||
|
@@ -182,15 +194,21 @@ pub fn upload_batch_of_annotations( | |
|
||
if let Err(error) = result { | ||
error_sender.send(error).expect("Could not send error"); | ||
statistics.add_failed_annotation(); | ||
} else { | ||
statistics.add_annotation(); | ||
} | ||
|
||
statistics.add_annotation(); | ||
}); | ||
}) | ||
}); | ||
|
||
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(()) | ||
|
@@ -207,6 +225,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(); | ||
|
||
|
@@ -224,6 +243,7 @@ fn upload_annotations_from_reader( | |
dataset_name, | ||
use_moon_forms, | ||
pool, | ||
lossy, | ||
)?; | ||
} | ||
} | ||
|
@@ -238,6 +258,7 @@ fn upload_annotations_from_reader( | |
dataset_name, | ||
use_moon_forms, | ||
pool, | ||
lossy, | ||
)?; | ||
} | ||
|
||
|
@@ -307,19 +328,24 @@ fn read_annotations_iter<'a>( | |
pub struct Statistics { | ||
bytes_read: AtomicUsize, | ||
annotations: AtomicUsize, | ||
failed_annotations: AtomicUsize, | ||
} | ||
|
||
impl AnnotationStatistic for Statistics { | ||
fn add_annotation(&self) { | ||
self.annotations.fetch_add(1, Ordering::SeqCst); | ||
} | ||
fn add_failed_annotation(&self) { | ||
self.failed_annotations.fetch_add(1, Ordering::SeqCst); | ||
} | ||
} | ||
|
||
impl Statistics { | ||
fn new() -> Self { | ||
Self { | ||
bytes_read: AtomicUsize::new(0), | ||
annotations: AtomicUsize::new(0), | ||
failed_annotations: AtomicUsize::new(0), | ||
} | ||
} | ||
|
||
|
@@ -337,14 +363,29 @@ impl Statistics { | |
pub fn num_annotations(&self) -> usize { | ||
self.annotations.load(Ordering::SeqCst) | ||
} | ||
|
||
#[inline] | ||
pub fn num_failed_annotations(&self) -> usize { | ||
self.failed_annotations.load(Ordering::SeqCst) | ||
} | ||
} | ||
|
||
fn basic_statistics(statistics: &Statistics) -> (u64, String) { | ||
let bytes_read = statistics.bytes_read(); | ||
let num_annotations = statistics.num_annotations(); | ||
let num_failed_annotations = statistics.num_failed_annotations(); | ||
( | ||
bytes_read as u64, | ||
format!("{} {}", num_annotations, "annotations".dimmed(),), | ||
format!( | ||
"{} {}{}", | ||
num_annotations, | ||
"annotations".dimmed(), | ||
if num_failed_annotations > 0 { | ||
format!(" {} {}", num_failed_annotations, "skipped".dimmed()) | ||
} else { | ||
"".to_string() | ||
} | ||
), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nested let failed_annotations_string = if num_failed_annotations > 0 {
format!(" {num_failed_annotations} {}", "skipped".dimmed())
} else {
String::new()
}; Similarly below. |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need two functions. How about just pub fn print_error(error: &anyhow::Error) {
for cause in error.chain() {
error!(" |- {}", cause);
}
} and leave the call to |
||
|
||
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")] | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lossy
isn't very descriptive. How aboutresume-on-error
?