Adds stats summary (#384)
* stat command parsing is handled without subcommands * Updates match clause based on PR review Co-authored-by: Conrad Ludgate <oon@conradludgate.com> * updates value returned by match based on PR review * adds vscode to gitignore * use an if statement instead of match Co-authored-by: Satyarth <satyarth.sampath@gojek.com> Co-authored-by: Conrad Ludgate <oon@conradludgate.com>
This commit is contained in:
parent
7a394b0115
commit
4a839dab44
3 changed files with 18 additions and 35 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@
|
||||||
*/target
|
*/target
|
||||||
.env
|
.env
|
||||||
.idea/
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
|
|
@ -29,7 +29,6 @@ pub enum Cmd {
|
||||||
Import(import::Cmd),
|
Import(import::Cmd),
|
||||||
|
|
||||||
/// Calculate statistics for your history
|
/// Calculate statistics for your history
|
||||||
#[clap(subcommand)]
|
|
||||||
Stats(stats::Cmd),
|
Stats(stats::Cmd),
|
||||||
|
|
||||||
/// Output shell setup
|
/// Output shell setup
|
||||||
|
|
|
@ -14,12 +14,9 @@ use atuin_client::{
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[clap(infer_subcommands = true)]
|
#[clap(infer_subcommands = true)]
|
||||||
pub enum Cmd {
|
pub struct Cmd {
|
||||||
/// Compute statistics for all of time
|
/// compute statistics for the specified period, leave blank for statistics since the beginning
|
||||||
All,
|
period: Vec<String>,
|
||||||
|
|
||||||
/// Compute statistics for a single day
|
|
||||||
Day { words: Vec<String> },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_stats(history: &[History]) -> Result<()> {
|
fn compute_stats(history: &[History]) -> Result<()> {
|
||||||
|
@ -28,7 +25,6 @@ fn compute_stats(history: &[History]) -> Result<()> {
|
||||||
for i in history {
|
for i in history {
|
||||||
*commands.entry(i.command.clone()).or_default() += 1;
|
*commands.entry(i.command.clone()).or_default() += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1));
|
let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1));
|
||||||
|
|
||||||
if most_common_command.is_none() {
|
if most_common_command.is_none() {
|
||||||
|
@ -72,32 +68,19 @@ impl Cmd {
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let context = current_context();
|
let context = current_context();
|
||||||
|
let words = if self.period.is_empty() {
|
||||||
match self {
|
String::from("all")
|
||||||
Self::Day { words } => {
|
|
||||||
let words = if words.is_empty() {
|
|
||||||
String::from("yesterday")
|
|
||||||
} else {
|
} else {
|
||||||
words.join(" ")
|
self.period.join(" ")
|
||||||
};
|
};
|
||||||
|
let history = if words.as_str() == "all" {
|
||||||
|
db.list(FilterMode::Global, &context, None, false).await?
|
||||||
|
} else {
|
||||||
let start = parse_date_string(&words, Local::now(), settings.dialect.into())?;
|
let start = parse_date_string(&words, Local::now(), settings.dialect.into())?;
|
||||||
let end = start + Duration::days(1);
|
let end = start + Duration::days(1);
|
||||||
|
db.range(start.into(), end.into()).await?
|
||||||
let history = db.range(start.into(), end.into()).await?;
|
};
|
||||||
|
|
||||||
compute_stats(&history)?;
|
compute_stats(&history)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::All => {
|
|
||||||
let history = db.list(FilterMode::Global, &context, None, false).await?;
|
|
||||||
|
|
||||||
compute_stats(&history)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue