use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use crate::block::Block;
use crate::message::Tone;
use crate::style::paint;
use crate::style::palette::{AMBER, MUTED, ROSE, SAGE};
use crate::table::{Align, Table};
use crate::{format, sparkline};
const KEEP: usize = 10;
pub(crate) struct History {
path: PathBuf,
runs: BTreeMap<String, Vec<f64>>,
dirty: bool,
}
impl History {
pub(crate) fn open(app: &str) -> Option<History> {
if std::env::var_os("TINSEL_NO_HISTORY").is_some() {
return None;
}
let path = cache_dir()?.join(format!("{}.tsv", sanitize(app)));
let runs = std::fs::read_to_string(&path).map(|s| parse(&s)).unwrap_or_default();
Some(History {
path,
runs,
dirty: false,
})
}
pub(crate) fn default_app() -> String {
std::env::current_exe()
.ok()
.and_then(|p| p.file_stem().map(|s| s.to_string_lossy().into_owned()))
.unwrap_or_else(|| "tinsel".into())
}
pub(crate) fn estimate(&self, key: &str) -> Option<f64> {
History::median(self.runs.get(key)?)
}
fn median(runs: &[f64]) -> Option<f64> {
let mut runs = runs.to_vec();
if runs.is_empty() {
return None;
}
runs.sort_by(|a, b| a.total_cmp(b));
let mid = runs.len() / 2;
Some(if runs.len() % 2 == 0 {
(runs[mid - 1] + runs[mid]) / 2.0
} else {
runs[mid]
})
}
pub(crate) fn record(&mut self, key: &str, secs: f64) {
let runs = self.runs.entry(clean(key)).or_default();
runs.push(secs);
if runs.len() > KEEP {
runs.drain(..runs.len() - KEEP);
}
self.dirty = true;
}
pub(crate) fn save(&self) {
if !self.dirty {
return;
}
let Some(dir) = self.path.parent() else { return };
if std::fs::create_dir_all(dir).is_err() {
return;
}
let body: String = self
.runs
.iter()
.map(|(key, runs)| {
let runs: Vec<String> = runs.iter().map(|r| format!("{r:.2}")).collect();
format!("{key}\t{}\n", runs.join(" "))
})
.collect();
let tmp = self.path.with_extension(format!("tsv.{}", std::process::id()));
if std::fs::write(&tmp, body).is_ok() {
let _ = std::fs::rename(&tmp, &self.path);
}
}
}
pub(crate) fn report(app: Option<&str>) -> Block {
let apps: Vec<String> = match app {
Some(a) => vec![a.to_string()],
None => cache_dir()
.and_then(|d| std::fs::read_dir(d).ok())
.map(|entries| {
let mut names: Vec<String> = entries
.filter_map(|e| e.ok()?.file_name().to_str()?.strip_suffix(".tsv").map(String::from))
.collect();
names.sort();
names
})
.unwrap_or_default(),
};
let mut out = Block::new();
for app in &apps {
let Some(history) = History::open(app).filter(|h| !h.runs.is_empty()) else {
continue;
};
if !out.is_empty() {
out.push(String::new());
}
out.push(format!(" {}", paint(ROSE, app)));
let mut table = Table::new(["step", "recent", "usual", "last", "trend"]).align(4, Align::Left);
for (key, runs) in &history.runs {
let (Some(&last), Some(usual)) = (runs.last(), history.estimate(key)) else {
continue;
};
let earlier = History::median(&runs[..runs.len() - 1]);
let trend = match earlier {
Some(before) if last > before * 1.25 => {
paint(AMBER, &format!("{:.0}% slower", (last / before - 1.0) * 100.0))
}
Some(before) if last < before * 0.8 => {
paint(SAGE, &format!("{:.0}% faster", (1.0 - last / before) * 100.0))
}
Some(_) => paint(MUTED, "steady"),
None => paint(MUTED, "first run"),
};
let secs = |s: f64| format::duration(Duration::from_secs_f64(s));
table = table.row([
key.split_once(" › ").map_or(key.as_str(), |(_, step)| step).to_string(),
sparkline(runs),
secs(usual),
secs(last),
trend,
]);
}
out.extend(table.lines());
}
if out.is_empty() {
out.push(Tone::Hint.line("no step timings recorded yet: they're kept after a Live run on a terminal"));
}
out
}
pub(crate) fn key(parent: &str, label: &str) -> String {
clean(&format!("{parent} › {label}"))
}
fn cache_dir() -> Option<PathBuf> {
let base = std::env::var_os("XDG_CACHE_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cache")))?;
Some(base.join("tinsel"))
}
fn clean(key: &str) -> String {
key.replace(['\t', '\n', '\r'], " ")
}
fn sanitize(app: &str) -> String {
let s: String = app
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || "-_.".contains(c) {
c
} else {
'_'
}
})
.collect();
if s.is_empty() || s.starts_with('.') {
format!("app{s}")
} else {
s
}
}
fn parse(body: &str) -> BTreeMap<String, Vec<f64>> {
body.lines()
.filter_map(|line| {
let (key, runs) = line.split_once('\t')?;
let runs: Vec<f64> = runs.split_whitespace().filter_map(|r| r.parse().ok()).collect();
Some((key.to_string(), runs))
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn history() -> History {
History {
path: PathBuf::from("/nonexistent/x.tsv"),
runs: BTreeMap::new(),
dirty: false,
}
}
#[test]
fn estimates_with_the_median() {
let mut h = history();
for secs in [40.0, 42.0, 300.0] {
h.record("deploy › Build", secs);
}
assert_eq!(h.estimate("deploy › Build"), Some(42.0));
h.record("deploy › Build", 44.0);
assert_eq!(h.estimate("deploy › Build"), Some(43.0));
assert_eq!(h.estimate("deploy › Upload"), None);
}
#[test]
fn keeps_only_recent_runs() {
let mut h = history();
for i in 0..25 {
h.record("k", i as f64);
}
assert_eq!(h.runs["k"].len(), KEEP);
assert_eq!(h.runs["k"][0], 15.0);
}
#[test]
fn reads_what_it_writes() {
let parsed = parse("deploy › Build\t41.20 39.80\nbroken line\n");
assert_eq!(parsed["deploy › Build"], vec![41.2, 39.8]);
assert_eq!(parsed.len(), 1);
}
#[test]
fn file_names_are_safe() {
assert_eq!(sanitize("my app/../x"), "my_app_.._x");
assert_eq!(sanitize(".hidden"), "app.hidden");
}
}