-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): add get emails (#262)
* feat(commands): add get emails --------- Co-authored-by: Irina Gossmann <[email protected]>
- Loading branch information
1 parent
54136d5
commit 0c22d50
Showing
8 changed files
with
261 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] | ||
pub struct GetBucketStatisticsResponse { | ||
pub statistics: Statistics, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] | ||
pub struct Statistics { | ||
pub count: usize, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod audit; | ||
pub mod bucket; | ||
pub mod bucket_statistics; | ||
pub mod comment; | ||
pub mod dataset; | ||
pub mod documents; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use anyhow::{Context, Result}; | ||
|
||
use colored::Colorize; | ||
use reinfer_client::{BucketIdentifier, Client}; | ||
use std::{ | ||
fs::File, | ||
io::{self, BufWriter, Write}, | ||
path::PathBuf, | ||
sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
use structopt::StructOpt; | ||
|
||
use crate::{ | ||
printer::print_resources_as_json, | ||
progress::{Options as ProgressOptions, Progress}, | ||
}; | ||
|
||
#[derive(Debug, StructOpt)] | ||
pub struct GetManyEmailsArgs { | ||
#[structopt(name = "bucket")] | ||
/// Bucket name or id | ||
bucket: BucketIdentifier, | ||
|
||
#[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>, | ||
} | ||
|
||
pub fn get_many(client: &Client, args: &GetManyEmailsArgs) -> Result<()> { | ||
let GetManyEmailsArgs { bucket, path } = args; | ||
|
||
let file = match path { | ||
Some(path) => Some( | ||
File::create(path) | ||
.with_context(|| format!("Could not open file for writing `{}`", path.display())) | ||
.map(BufWriter::new)?, | ||
), | ||
None => None, | ||
}; | ||
|
||
if let Some(file) = file { | ||
download_emails(client, bucket.clone(), file) | ||
} else { | ||
download_emails(client, bucket.clone(), io::stdout().lock()) | ||
} | ||
} | ||
|
||
fn download_emails( | ||
client: &Client, | ||
bucket_identifier: BucketIdentifier, | ||
mut writer: impl Write, | ||
) -> Result<()> { | ||
let bucket = client | ||
.get_bucket(bucket_identifier) | ||
.context("Operation to get bucket has failed.")?; | ||
|
||
let bucket_statistics = client | ||
.get_bucket_statistics(&bucket.full_name()) | ||
.context("Could not get bucket statistics")?; | ||
|
||
let statistics = Arc::new(Statistics::new()); | ||
|
||
let _progress = get_emails_progress_bar(bucket_statistics.count as u64, &statistics); | ||
|
||
client | ||
.get_emails_iter(&bucket.full_name(), None) | ||
.try_for_each(|page| { | ||
let page = page.context("Operation to get emails has failed.")?; | ||
statistics.add_emails(page.len()); | ||
print_resources_as_json(page.into_iter(), &mut writer) | ||
})?; | ||
log::info!( | ||
"Successfully downloaded {} emails.", | ||
statistics.num_downloaded(), | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Statistics { | ||
downloaded: AtomicUsize, | ||
} | ||
|
||
impl Statistics { | ||
fn new() -> Self { | ||
Self { | ||
downloaded: AtomicUsize::new(0), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn add_emails(&self, num_downloaded: usize) { | ||
self.downloaded.fetch_add(num_downloaded, Ordering::SeqCst); | ||
} | ||
|
||
#[inline] | ||
fn num_downloaded(&self) -> usize { | ||
self.downloaded.load(Ordering::SeqCst) | ||
} | ||
} | ||
|
||
fn get_emails_progress_bar(total_bytes: u64, statistics: &Arc<Statistics>) -> Progress { | ||
Progress::new( | ||
move |statistics| { | ||
let num_downloaded = statistics.num_downloaded(); | ||
( | ||
num_downloaded as u64, | ||
format!( | ||
"{} {}", | ||
num_downloaded.to_string().bold(), | ||
"emails".dimmed(), | ||
), | ||
) | ||
}, | ||
statistics, | ||
Some(total_bytes), | ||
ProgressOptions { bytes_units: false }, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.