Skip to content

Commit

Permalink
stats (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate authored Feb 10, 2023
1 parent 2672f78 commit deb7c19
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ itertools = "0.10.5"
tokio = { version = "1", features = ["full"] }
async-trait = "0.1.58"
interim = { version = "0.1.0", features = ["chrono"] }
cli-table = { version = "0.4", default-features = false }
base64 = "0.20.0"
crossbeam-channel = "0.5.1"
clap = { version = "4.0.18", features = ["derive"] }
Expand Down
76 changes: 42 additions & 34 deletions src/command/client/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use chrono::{prelude::*, Duration};
use clap::Parser;
use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
use crossterm::style::{Color, ResetColor, SetAttribute, SetForegroundColor};
use eyre::{bail, Result};
use interim::parse_date_string;

Expand All @@ -17,46 +17,54 @@ use atuin_client::{
pub struct Cmd {
/// compute statistics for the specified period, leave blank for statistics since the beginning
period: Vec<String>,
}

fn compute_stats(history: &[History]) -> Result<()> {
let mut commands = HashMap::<String, i64>::new();
/// How many top commands to list
#[arg(long, short, default_value = "10")]
count: usize,
}

fn compute_stats(history: &[History], count: usize) -> Result<()> {
let mut commands = HashMap::<&str, usize>::new();
for i in history {
*commands.entry(i.command.clone()).or_default() += 1;
*commands
.entry(i.command.split_ascii_whitespace().next().unwrap())
.or_default() += 1;
}
let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1));

if most_common_command.is_none() {
let unique = commands.len();
let mut top = commands.into_iter().collect::<Vec<_>>();
top.sort_unstable_by_key(|x| std::cmp::Reverse(x.1));
top.truncate(count);
if top.is_empty() {
bail!("No commands found");
}

let table = vec![
vec![
"Most used command".cell(),
most_common_command
.unwrap()
.0
.cell()
.justify(Justify::Right),
],
vec![
"Commands ran".cell(),
history.len().to_string().cell().justify(Justify::Right),
],
vec![
"Unique commands ran".cell(),
commands.len().to_string().cell().justify(Justify::Right),
],
]
.table()
.title(vec![
"Statistic".cell().bold(true),
"Value".cell().bold(true),
])
.bold(true);
let max = top.iter().map(|x| x.1).max().unwrap();
let num_pad = max.ilog10() as usize + 1;

print_stdout(table)?;
for (command, count) in top {
let gray = SetForegroundColor(Color::Grey);
let bold = SetAttribute(crossterm::style::Attribute::Bold);

let in_ten = 10 * count / max;
print!("[");
print!("{}", SetForegroundColor(Color::Red));
for i in 0..in_ten {
if i == 2 {
print!("{}", SetForegroundColor(Color::Yellow));
}
if i == 5 {
print!("{}", SetForegroundColor(Color::Green));
}
print!("▮");
}
for _ in in_ten..10 {
print!(" ");
}

println!("{ResetColor}] {gray}{count:num_pad$}{ResetColor} {bold}{command}{ResetColor}");
}
println!("Total commands: {}", history.len());
println!("Unique commands: {unique}");

Ok(())
}
Expand All @@ -76,7 +84,7 @@ impl Cmd {
let end = start + Duration::days(1);
db.range(start.into(), end.into()).await?
};
compute_stats(&history)?;
compute_stats(&history, self.count)?;
Ok(())
}
}

0 comments on commit deb7c19

Please sign in to comment.