use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use kuizer::detect::{Category, Safety};
use kuizer::output::display_path;
use crate::model::Source;
pub const MIN_SHARE: f64 = 0.004;
pub const MAX_TILES: usize = 80;
const CACHES: &str = "\0caches";
const ELSEWHERE: &str = "\0elsewhere";
const REST: &str = "\0rest";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Item {
pub source: Source,
pub id: u64,
pub category: Category,
pub safety: Safety,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Leaf {
pub path: PathBuf,
pub bytes: u64,
pub item: Item,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
Group,
Folder,
Item(Item),
Rest(usize),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node {
pub key: String,
pub name: String,
pub path: Option<PathBuf>,
pub bytes: u64,
pub items: usize,
pub kind: Kind,
pub children: Vec<Node>,
}
impl Node {
pub fn opens(&self) -> bool {
matches!(self.kind, Kind::Group | Kind::Folder) && !self.children.is_empty()
}
pub fn item(&self) -> Option<Item> {
match self.kind {
Kind::Item(item) => Some(item),
_ => None,
}
}
pub fn child(&self, key: &str) -> Option<&Node> {
self.children.iter().find(|child| child.key == key)
}
pub fn leaves(&self) -> Vec<(Item, u64)> {
let mut found = Vec::new();
self.collect(&mut found);
found
}
fn collect(&self, found: &mut Vec<(Item, u64)>) {
match self.kind {
Kind::Item(item) => found.push((item, self.bytes)),
_ => {
for child in &self.children {
child.collect(found);
}
}
}
}
pub fn trail(&self, path: &[String]) -> Vec<&Node> {
let mut trail = vec![self];
for key in path {
let Some(next) = trail.last().and_then(|node| node.child(key)) else {
break;
};
if !next.opens() {
break;
}
trail.push(next);
}
trail
}
}
#[derive(Default)]
struct Trie<'a> {
leaf: Option<usize>,
children: BTreeMap<&'a OsStr, Trie<'a>>,
}
impl<'a> Trie<'a> {
fn insert(&mut self, relative: &'a Path, index: usize) {
let mut node = self;
for part in relative.components() {
node = node.children.entry(part.as_os_str()).or_default();
}
if node.leaf.is_none() {
node.leaf = Some(index);
}
}
}
struct Builder<'a> {
base: &'a Path,
leaves: &'a [Leaf],
}
impl<'a> Builder<'a> {
fn tile(&self, mut trie: Trie<'a>, parts: &mut Vec<&'a OsStr>) -> Option<Node> {
let start = parts.len() - 1;
while trie.leaf.is_none() && trie.children.len() == 1 {
let (part, only) = trie.children.pop_first()?;
parts.push(part);
trie = only;
}
let name = chain_name(&parts[start..]);
let depth = parts.len();
let node = if let Some(index) = trie.leaf {
self.item(index, name)
} else if trie.children.is_empty() {
None
} else {
let mut path = self.base.to_path_buf();
path.extend(parts.iter().copied());
let children = trie
.children
.into_iter()
.filter_map(|(part, child)| {
parts.push(part);
let node = self.tile(child, parts);
parts.truncate(depth);
node
})
.collect();
Some(container(
path.to_string_lossy().into_owned(),
name,
Some(path),
Kind::Folder,
children,
))
};
parts.truncate(start + 1);
node
}
fn item(&self, index: usize, name: String) -> Option<Node> {
let leaf = self.leaves.get(index)?;
Some(Node {
key: leaf.path.to_string_lossy().into_owned(),
name,
path: Some(leaf.path.clone()),
bytes: leaf.bytes,
items: 1,
kind: Kind::Item(leaf.item),
children: Vec::new(),
})
}
}
fn chain_name(parts: &[&OsStr]) -> String {
let mut name = String::new();
for (index, part) in parts.iter().enumerate() {
if index > 0 {
name.push('/');
}
name.push_str(&part.to_string_lossy());
}
name
}
fn container(
key: String,
name: String,
path: Option<PathBuf>,
kind: Kind,
children: Vec<Node>,
) -> Node {
Node {
key,
name,
path,
bytes: children.iter().map(|child| child.bytes).sum(),
items: children.iter().map(|child| child.items).sum(),
kind,
children,
}
}
fn group(key: String, name: String, base: &Path, leaves: &[Leaf]) -> Option<Node> {
if leaves.is_empty() {
return None;
}
let mut trie = Trie::default();
for (index, leaf) in leaves.iter().enumerate() {
let relative = leaf.path.strip_prefix(base).unwrap_or(Path::new(""));
trie.insert(relative, index);
}
let builder = Builder { base, leaves };
let children = if let Some(index) = trie.leaf.take() {
let path = &leaves.get(index)?.path;
let name = path.file_name().map_or_else(
|| display_path(path),
|name| name.to_string_lossy().into_owned(),
);
vec![builder.item(index, name)?]
} else {
let mut parts = Vec::new();
trie.children
.into_iter()
.filter_map(|(part, child)| {
parts.push(part);
let node = builder.tile(child, &mut parts);
parts.pop();
node
})
.collect()
};
Some(container(
key,
name,
Some(base.to_path_buf()),
Kind::Group,
children,
))
}
fn common_ancestor<'a>(paths: impl IntoIterator<Item = &'a Path>) -> PathBuf {
let mut paths = paths.into_iter();
let Some(first) = paths.next() else {
return PathBuf::new();
};
let mut base = first.parent().unwrap_or(first).to_path_buf();
for path in paths {
while !path.starts_with(&base) {
if !base.pop() {
return PathBuf::new();
}
}
}
base
}
fn arrange(node: &mut Node) {
for child in &mut node.children {
arrange(child);
}
node.children
.sort_by(|a, b| b.bytes.cmp(&a.bytes).then_with(|| a.name.cmp(&b.name)));
#[allow(
clippy::cast_precision_loss,
reason = "a share only needs a few digits"
)]
let floor = node.bytes as f64 * MIN_SHARE;
#[allow(
clippy::cast_precision_loss,
reason = "a share only needs a few digits"
)]
let cut = node
.children
.iter()
.position(|child| (child.bytes as f64) < floor)
.unwrap_or(node.children.len())
.min(MAX_TILES - 1);
if node.children.len() - cut >= 2 {
let rest: Vec<Node> = node.children.drain(cut..).collect();
let count = rest.len();
node.children.push(Node {
key: format!("{}{REST}", node.key),
name: format!("{count} smaller"),
path: None,
bytes: rest.iter().map(|child| child.bytes).sum(),
items: rest.iter().map(|child| child.items).sum(),
kind: Kind::Rest(count),
children: rest,
});
}
}
pub fn build(roots: &[PathBuf], artifacts: Vec<Leaf>, caches: Vec<Leaf>) -> Node {
let mut by_root: Vec<(&PathBuf, Vec<Leaf>)> =
roots.iter().map(|root| (root, Vec::new())).collect();
by_root.sort_by_key(|(root, _)| std::cmp::Reverse(root.components().count()));
let mut elsewhere = Vec::new();
for leaf in artifacts {
match by_root
.iter_mut()
.find(|(root, _)| leaf.path.starts_with(root))
{
Some((_, leaves)) => leaves.push(leaf),
None => elsewhere.push(leaf),
}
}
let mut groups: Vec<Node> = by_root
.into_iter()
.filter_map(|(root, leaves)| {
group(
root.to_string_lossy().into_owned(),
display_path(root),
root,
&leaves,
)
})
.collect();
let base = common_ancestor(elsewhere.iter().map(|leaf| leaf.path.as_path()));
groups.extend(group(
ELSEWHERE.to_owned(),
"Outside the scanned folders".to_owned(),
&base,
&elsewhere,
));
let base = common_ancestor(caches.iter().map(|leaf| leaf.path.as_path()));
groups.extend(group(
CACHES.to_owned(),
"Global caches".to_owned(),
&base,
&caches,
));
let mut top = if groups.len() == 1 {
groups.remove(0)
} else {
let name = match roots {
[root] => display_path(root),
_ => "Everything".to_owned(),
};
container(String::new(), name, None, Kind::Group, groups)
};
arrange(&mut top);
top
}
#[cfg(test)]
mod tests {
use super::*;
fn leaf(id: u64, path: &str, bytes: u64) -> Leaf {
Leaf {
path: PathBuf::from(path),
bytes,
item: Item {
source: Source::Artifact,
id,
category: Category::Dependencies,
safety: Safety::Safe,
},
}
}
fn cache(id: u64, path: &str, bytes: u64) -> Leaf {
let mut leaf = leaf(id, path, bytes);
leaf.item.source = Source::Cache;
leaf.item.category = Category::Cache;
leaf
}
fn names(node: &Node) -> Vec<&str> {
node.children
.iter()
.map(|child| child.name.as_str())
.collect()
}
#[test]
fn sizes_add_up_the_path() {
let roots = [PathBuf::from("/p")];
let tree = build(
&roots,
vec![
leaf(1, "/p/team/a/node_modules", 300),
leaf(2, "/p/team/b/node_modules", 200),
leaf(3, "/p/team/b/dist", 150),
leaf(4, "/p/solo/target", 500),
],
Vec::new(),
);
assert_eq!(tree.key, "/p");
assert_eq!(tree.kind, Kind::Group);
assert_eq!(tree.bytes, 1150);
assert_eq!(tree.items, 4);
assert_eq!(names(&tree), ["team", "solo/target"]);
let team = &tree.children[0];
assert_eq!(team.bytes, 650);
assert_eq!(team.kind, Kind::Folder);
assert_eq!(team.key, "/p/team");
assert!(team.opens());
assert_eq!(names(team), ["b", "a/node_modules"]);
let b = &team.children[0];
assert_eq!(b.bytes, 350);
assert_eq!(names(b), ["node_modules", "dist"]);
let solo = &tree.children[1];
assert_eq!(solo.key, "/p/solo/target");
assert_eq!(solo.item().map(|item| item.id), Some(4));
assert!(!solo.opens());
}
#[test]
fn single_child_chains_collapse_into_one_tile() {
let roots = [PathBuf::from("/p")];
let tree = build(
&roots,
vec![
leaf(1, "/p/deep/er/still/x/node_modules", 10),
leaf(2, "/p/deep/er/still/y/node_modules", 10),
leaf(3, "/p/other/node_modules", 30),
],
Vec::new(),
);
assert_eq!(names(&tree), ["other/node_modules", "deep/er/still"]);
let chain = &tree.children[1];
assert_eq!(
chain.key, "/p/deep/er/still",
"the key is the deepest folder"
);
assert_eq!(chain.path.as_deref(), Some(Path::new("/p/deep/er/still")));
assert_eq!(names(chain), ["x/node_modules", "y/node_modules"]);
}
#[test]
fn tiny_items_are_grouped_per_level() {
let roots = [PathBuf::from("/p")];
let mut leaves = vec![leaf(1, "/p/big/node_modules", 100_000)];
for index in 0..5 {
leaves.push(leaf(
10 + index,
&format!("/p/tiny{index}/node_modules"),
10,
));
}
let tree = build(&roots, leaves, Vec::new());
assert_eq!(tree.children.len(), 2);
let rest = &tree.children[1];
assert_eq!(rest.kind, Kind::Rest(5));
assert_eq!(rest.name, "5 smaller");
assert_eq!(rest.bytes, 50);
assert_eq!(rest.items, 5);
assert!(!rest.opens());
assert_eq!(tree.leaves().len(), 6, "grouped items still count");
let many: Vec<Leaf> = (0..200)
.map(|index| leaf(index, &format!("/p/p{index:03}/node_modules"), 1_000))
.collect();
let tree = build(&roots, many, Vec::new());
assert_eq!(tree.children.len(), MAX_TILES);
assert_eq!(
tree.children[MAX_TILES - 1].kind,
Kind::Rest(200 - (MAX_TILES - 1))
);
let one_small = vec![
leaf(1, "/p/a/node_modules", 100_000),
leaf(2, "/p/b/node_modules", 1),
];
let tree = build(&roots, one_small, Vec::new());
assert_eq!(
names(&tree),
["a/node_modules", "b/node_modules"],
"one is not a group"
);
}
#[test]
fn roots_and_caches_are_groups_of_their_own() {
let roots = [PathBuf::from("/p"), PathBuf::from("/q")];
let tree = build(
&roots,
vec![
leaf(1, "/p/a/node_modules", 40),
leaf(2, "/p/b/node_modules", 40),
leaf(3, "/q/c/node_modules", 10),
leaf(4, "/elsewhere/z/dist", 5),
],
vec![
cache(5, "/home/u/.cache/pip", 70),
cache(6, "/home/u/.npm", 30),
],
);
assert_eq!(tree.key, "");
assert_eq!(tree.name, "Everything");
assert_eq!(tree.bytes, 195);
assert_eq!(
names(&tree),
["Global caches", "/p", "/q", "Outside the scanned folders"]
);
let caches = &tree.children[0];
assert_eq!(caches.key, CACHES);
assert_eq!(names(caches), [".cache/pip", ".npm"]);
assert_eq!(caches.children[0].item().unwrap().source, Source::Cache);
let trail = tree.trail(&["/p".to_owned(), "missing".to_owned()]);
assert_eq!(trail.len(), 2, "the walk stops at a key that is gone");
assert_eq!(trail[1].name, "/p");
}
#[test]
fn an_item_holding_another_item_wins() {
let roots = [PathBuf::from("/p")];
let tree = build(
&roots,
vec![
leaf(1, "/p/a/node_modules", 50),
leaf(2, "/p/a/node_modules/x/node_modules", 20),
leaf(3, "/p/b/dist", 10),
],
Vec::new(),
);
assert_eq!(tree.bytes, 60, "nothing is counted twice");
assert_eq!(tree.leaves().len(), 2);
}
#[test]
fn the_same_report_builds_the_same_tree() {
let roots = [PathBuf::from("/p")];
let leaves = vec![
leaf(1, "/p/a/node_modules", 10),
leaf(2, "/p/b/node_modules", 10),
leaf(3, "/p/c/node_modules", 10),
];
let mut reversed = leaves.clone();
reversed.reverse();
let first = build(&roots, leaves, Vec::new());
assert_eq!(first, build(&roots, reversed, Vec::new()));
assert_eq!(
names(&first),
["a/node_modules", "b/node_modules", "c/node_modules"]
);
assert!(build(&roots, Vec::new(), Vec::new()).children.is_empty());
}
}