Skip to content

Commit

Permalink
more flexible job list filters.
Browse files Browse the repository at this point in the history
- job list filtering documentation
- case-insenstitive jobtype and jobstate filters
- use comma as delimeter for jobtype and jobstate filters

needed to use comma, as clap doesn't handle multiple args that
take multiple values well.
  • Loading branch information
curtin_devn committed Aug 2, 2022
1 parent 87c249e commit f22aa25
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ giant-squid l

### List ASVO jobs in JSON

the following commands are equivalent:

```bash
# Any of the following work.
giant-squid list --json
giant-squid list -j
giant-squid l -j
Expand Down Expand Up @@ -114,6 +115,30 @@ In [3]: q.keys()
Out[3]: dict_keys(['216087', '216241', '217628'])
```

### Filter ASVO job listing

`giant-squid list` takes an optional list of identifiers that can be used to filter the job listing,
these identifiers can either be a list of jobIDs or a list of obsIDs, but not both.

Additionally, the `--states` and `--types` options can be used to further filter the output.

These both taks a comma-separated, case-insensitive list of values from the `jobType` and
`jobState` lists above. These can be provided in `TitleCase`, `UPPERCASE`, `lowercase`,
`kebab-case`, `snake_case`, or even `SPoNgeBOb-CAse`

example: show only jobs that match both of the following conditions:

- obsid is `1234567890` or `1234567891`
- jobType is `DownloadVisibilities`, `DownloadMetadata` or `CancelJob`
- jobState is `Processing` or `Queued`

```bash
giant-squid list \
--types dOwNlOaD__vIsIbIlItIeS,download-metadata,CANCELJOB \
--states pRoCeSsInG,__Q_u_e_u_e_D__ \
1234567890 1234567891
```

### Download ASVO jobs

To download job ID 12345:
Expand Down
11 changes: 4 additions & 7 deletions src/asvo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ impl AsvoClient {
Delivery::Acacia => {
match f.url.as_deref() {
Some(url) => {
debug!("Downloading file {:?}", f.url);
let url = url;

debug!("Downloading file {:?}", &url);

// parse out path from url
Expand All @@ -205,14 +202,14 @@ impl AsvoClient {
if keep_tar {
// Simply dump the response to the appropriate file name. Use a
// buffer to avoid doing frequent writes.

let mut out_file = File::create(out_path)?;
let mut file_buf = BufReader::with_capacity(buffer_size, tee.by_ref());

loop {
let buffer = file_buf.fill_buf()?;
out_file.write_all(buffer)?;

let length = buffer.len();
file_buf.consume(length);
if length == 0 {
Expand All @@ -225,7 +222,7 @@ impl AsvoClient {
let mut tar = Archive::new(&mut tee);
tar.unpack(".")?;
}

// If we were told to hash the download, compare our hash against
// the upstream hash. Stream untarring may not read all of the
// bytes; read the tee to the end.
Expand Down
40 changes: 34 additions & 6 deletions src/asvo/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ use serde::Serialize;

use crate::{obsid::Obsid, AsvoError};

/// Sanitize a string to lowercase, and ascii 'a'-'z' only.
///
/// Used to sanitize user input for ASVO identifiers.
fn _sanitize_identifier(s: &str) -> String {
let mut sanitized = s.to_lowercase();
sanitized.retain(|c| 'a' <= c && c <= 'z');
sanitized
}

/// All of the available types of ASVO jobs.
#[derive(Serialize, PartialEq, Debug, Clone)]
pub enum AsvoJobType {
Expand All @@ -26,12 +35,12 @@ impl FromStr for AsvoJobType {
type Err = AsvoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
match _sanitize_identifier(s).as_str() {
"conversion" => Ok(AsvoJobType::Conversion),
"download_visibilities" => Ok(AsvoJobType::DownloadVisibilities),
"download_metadata" => Ok(AsvoJobType::DownloadMetadata),
"download_voltage" => Ok(AsvoJobType::DownloadVoltage),
"cancel_job" => Ok(AsvoJobType::CancelJob),
"downloadvisibilities" => Ok(AsvoJobType::DownloadVisibilities),
"downloadmetadata" => Ok(AsvoJobType::DownloadMetadata),
"downloadvoltage" => Ok(AsvoJobType::DownloadVoltage),
"canceljob" => Ok(AsvoJobType::CancelJob),
_ => Err(AsvoError::InvalidJobType { str: s.to_string() }),
}
}
Expand All @@ -52,7 +61,7 @@ impl FromStr for AsvoJobState {
type Err = AsvoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
match _sanitize_identifier(s).as_str() {
"queued" => Ok(AsvoJobState::Queued),
"processing" => Ok(AsvoJobState::Processing),
"ready" => Ok(AsvoJobState::Ready),
Expand Down Expand Up @@ -293,3 +302,22 @@ impl std::fmt::Display for Delivery {
)
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_asvo_job_state_fromstr() {
assert!(matches!(AsvoJobState::from_str("__Q_u_e_u_e_D__"), Ok(AsvoJobState::Queued)));
assert!(matches!(AsvoJobState::from_str("invalid job state"), Err(AsvoError::InvalidJobState{..})));
}

#[test]
fn test_asvo_job_type_fromstr() {
assert!(matches!(AsvoJobType::from_str("DownloadVisibilities"), Ok(AsvoJobType::DownloadVisibilities)));
assert!(matches!(AsvoJobType::from_str("download_visibilities"), Ok(AsvoJobType::DownloadVisibilities)));
assert!(matches!(AsvoJobType::from_str("invalid job type"), Err(AsvoError::InvalidJobType{..})));
}
}
4 changes: 2 additions & 2 deletions src/bin/giant-squid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ enum Args {

/// show only jobs matching the provided states, case insensitive.
/// Options: queued, processing, ready, error, expired, cancelled.
#[clap(long, name = "STATE")]
#[clap(long, name = "STATE", value_delimiter = ',')]
states: Vec<AsvoJobState>,

/// filter job list by type, case insensitive with underscores. Options:
/// conversion, download_visibilities, download_metadata,
/// download_voltage or cancel_job
#[clap(long, name = "TYPE")]
#[clap(long, name = "TYPE", value_delimiter = ',')]
types: Vec<AsvoJobType>,

/// job IDs or obsids to filter by. Files containing job IDs or
Expand Down

0 comments on commit f22aa25

Please sign in to comment.