Like the graph visualization, needs some work to make it more readable, and need to do some testing to validate the numbers are correct, but good foundation for now - especially considering how easy it was to add!
YA5ITLOV2UWAQZWFJND2WM45DLWG7PTECNJQOLPZAHH2GETPI3HQC
use crate::timings;
use guppy::graph::{DependencyDirection, PackageGraph};
pub fn nodes(graph: &PackageGraph) -> Vec<&str> {
graph.packages().map(|pkg| pkg.name()).collect()
}
pub fn links<'graph>(
graph: &'graph PackageGraph,
timings: &timings::Output,
) -> Vec<(&'graph str, &'graph str, f64)> {
let package_set = graph.resolve_all();
let mut links = Vec::with_capacity(package_set.len());
for link in package_set.links(DependencyDirection::Forward) {
let (from, to) = link.endpoints();
let time_taken = timings.pkg_time(to.id()).unwrap_or(0_f64);
// Can't just set the edge weight to duration - if the package has N dependents
// it would appear to take N times longer, just need to divide duration by
// direct dependents to fix
let direct_dependents = to
.direct_links_directed(DependencyDirection::Reverse)
.count();
// Make sure to not divide by 0
let edge_weight = if direct_dependents == 0 {
time_taken
} else {
time_taken / (direct_dependents as f64)
};
links.push((from.name(), to.name(), edge_weight));
}
links
}