Skip to content

Commit

Permalink
Switching to humantime
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Jul 6, 2023
1 parent ae768bf commit 405b1e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 58 deletions.
56 changes: 4 additions & 52 deletions quickwit/quickwit-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@

use std::collections::HashSet;
use std::str::FromStr;
use std::time::Duration;

use anyhow::{bail, Context};
use anyhow::Context;
use clap::{arg, Arg, ArgMatches};
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
use once_cell::sync::Lazy;
use quickwit_common::runtimes::RuntimesConfig;
use quickwit_common::uri::Uri;
use quickwit_config::service::QuickwitService;
Expand All @@ -40,7 +38,6 @@ use quickwit_metastore::{Metastore, MetastoreResolver};
use quickwit_rest_client::models::Timeout;
use quickwit_rest_client::rest_client::{QuickwitClient, QuickwitClientBuilder, DEFAULT_BASE_URL};
use quickwit_storage::{load_file, StorageResolver};
use regex::Regex;
use reqwest::Url;
use tabled::object::Rows;
use tabled::{Alignment, Header, Modify, Style, Table, Tabled};
Expand Down Expand Up @@ -70,9 +67,6 @@ pub const QW_ENABLE_TOKIO_CONSOLE_ENV_KEY: &str = "QW_ENABLE_TOKIO_CONSOLE";
pub const QW_ENABLE_OPENTELEMETRY_OTLP_EXPORTER_ENV_KEY: &str =
"QW_ENABLE_OPENTELEMETRY_OTLP_EXPORTER";

/// Regular expression representing a valid duration with unit.
pub const DURATION_WITH_UNIT_PATTERN: &str = r#"^(\d{1,3})(s|m|h|d)$"#;

fn config_cli_arg() -> Arg {
Arg::new("config")
.long("config")
Expand Down Expand Up @@ -204,32 +198,13 @@ impl ClientArgs {
}
}

/// Parse duration with unit like `1s`, `2m`, `3h`, `5d`.
pub fn parse_duration_with_unit(duration_with_unit_str: &str) -> anyhow::Result<Duration> {
static DURATION_WITH_UNIT_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(DURATION_WITH_UNIT_PATTERN).unwrap());
let captures = DURATION_WITH_UNIT_RE
.captures(duration_with_unit_str)
.ok_or_else(|| anyhow::anyhow!("Invalid duration format: `[0-9]+[smhd]`"))?;
let value = captures.get(1).unwrap().as_str().parse::<u64>().unwrap();
let unit = captures.get(2).unwrap().as_str();

match unit {
"s" => Ok(Duration::from_secs(value)),
"m" => Ok(Duration::from_secs(value * 60)),
"h" => Ok(Duration::from_secs(value * 60 * 60)),
"d" => Ok(Duration::from_secs(value * 60 * 60 * 24)),
_ => bail!("Invalid duration format: `[0-9]+[smhd]`"),
}
}

pub fn parse_duration_or_none(duration_with_unit_str: &str) -> anyhow::Result<Timeout> {
if duration_with_unit_str == "none" {
Ok(Timeout::none())
} else {
parse_duration_with_unit(duration_with_unit_str)
humantime::parse_duration(duration_with_unit_str)
.map(Timeout::new)
.map_err(|_| anyhow::anyhow!("Invalid duration format: `[0-9]+[smhd]|none`"))
.context("Failed to parse timeout.")
}
}

Expand Down Expand Up @@ -442,33 +417,10 @@ pub mod busy_detector {

#[cfg(test)]
mod tests {
use std::time::Duration;

use quickwit_config::S3StorageConfig;
use quickwit_rest_client::models::Timeout;

use super::*;

#[test]
fn test_parse_duration_with_unit() -> anyhow::Result<()> {
assert_eq!(parse_duration_with_unit("8s")?, Duration::from_secs(8));
assert_eq!(parse_duration_with_unit("5m")?, Duration::from_secs(5 * 60));
assert_eq!(
parse_duration_with_unit("2h")?,
Duration::from_secs(2 * 60 * 60)
);
assert_eq!(
parse_duration_with_unit("3d")?,
Duration::from_secs(3 * 60 * 60 * 24)
);

assert!(parse_duration_with_unit("").is_err());
assert!(parse_duration_with_unit("a2d").is_err());
assert!(parse_duration_with_unit("3 d").is_err());
assert!(parse_duration_with_unit("3").is_err());
assert!(parse_duration_with_unit("1h30").is_err());
Ok(())
}
use crate::parse_duration_or_none;

#[test]
fn test_parse_duration_or_none() -> anyhow::Result<()> {
Expand Down
12 changes: 6 additions & 6 deletions quickwit/quickwit-cli/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ use tracing::{debug, info};

use crate::checklist::{GREEN_COLOR, RED_COLOR};
use crate::{
config_cli_arg, get_resolvers, load_node_config, parse_duration_with_unit, run_index_checklist,
start_actor_runtimes, THROUGHPUT_WINDOW_SIZE,
config_cli_arg, get_resolvers, load_node_config, run_index_checklist, start_actor_runtimes,
THROUGHPUT_WINDOW_SIZE,
};

pub fn build_tool_command() -> Command {
Expand Down Expand Up @@ -238,15 +238,15 @@ impl ToolCliCommand {

fn parse_garbage_collect_args(mut matches: ArgMatches) -> anyhow::Result<Self> {
let config_uri = matches
.remove_one::<String>("config")
.map(|uri_str| Uri::from_str(&uri_str))
.get_one("config")
.map(|uri_str: &String| Uri::from_str(&uri_str))
.expect("`config` should be a required arg.")?;
let index_id = matches
.remove_one::<String>("index")
.expect("`index` should be a required arg.");
let grace_period = matches
.remove_one::<String>("grace-period")
.map(|duration| parse_duration_with_unit(&duration))
.get_one("grace-period")
.map(|duration_str: &String| humantime::parse_duration(duration_str))
.expect("`grace-period` should have a default value.")?;
let dry_run = matches.get_flag("dry-run");
Ok(Self::GarbageCollect(GarbageCollectIndexArgs {
Expand Down

0 comments on commit 405b1e0

Please sign in to comment.