use eframe::egui::{
self, Align, Align2, Color32, FontId, Layout, Pos2, Rect, Rounding, Sense, Stroke, Vec2,
};
use uuid::Uuid;
use super::model::{BenchHistory, Timeline};
pub struct TimeTravelState {
pub idx: usize,
pub playing: bool,
pub speed_secs: f32,
pub last_tick: std::time::Instant,
}
impl Default for TimeTravelState {
fn default() -> Self {
Self {
idx: 0,
playing: false,
speed_secs: 1.5,
last_tick: std::time::Instant::now(),
}
}
}
impl TimeTravelState {
pub fn clamp(&mut self, n: usize) {
if n == 0 {
self.idx = 0;
} else if self.idx >= n {
self.idx = n - 1;
}
}
}
pub fn draw_timetravel(
ui: &mut egui::Ui,
tl: &Timeline,
state: &mut TimeTravelState,
) -> Option<Uuid> {
let n = tl.release_order.len();
if n == 0 {
let max_bench = tl
.bench_history
.values()
.map(|h| h.points.len())
.max()
.unwrap_or(0);
if max_bench == 0 {
ui.label("No releases or bench runs yet.");
return None;
}
if state.idx >= max_bench { state.idx = max_bench - 1; }
if state.playing && state.last_tick.elapsed().as_secs_f32() >= state.speed_secs {
state.idx = (state.idx + 1) % max_bench;
state.last_tick = std::time::Instant::now();
}
if state.playing {
ui.ctx().request_repaint_after(std::time::Duration::from_millis(120));
}
let max_idx = max_bench.saturating_sub(1);
ui.horizontal(|ui| {
if ui.button("⏮").clicked() { state.idx = 0; }
if ui.button("◀").clicked() { state.idx = state.idx.saturating_sub(1); }
if ui.button(if state.playing { "⏸ pause" } else { "▶ play" }).clicked() {
state.playing = !state.playing;
state.last_tick = std::time::Instant::now();
}
if ui.button("▶").clicked() { if state.idx + 1 < max_bench { state.idx += 1; } }
if ui.button("⏭").clicked() { state.idx = max_idx; }
ui.separator();
ui.label(format!("bench {} of {}", state.idx + 1, max_bench));
ui.add(egui::Slider::new(&mut state.idx, 0..=max_idx).text("scrub"));
ui.add(egui::Slider::new(&mut state.speed_secs, 0.25..=5.0)
.text("s/step").logarithmic(true));
});
ui.separator();
ui.colored_label(
Color32::from_rgb(200, 160, 60),
"ℹ no release_lineage in this warehouse — showing bench history only",
);
reel_benches(ui, tl, state.idx);
return None;
}
state.clamp(n);
if state.playing && state.last_tick.elapsed().as_secs_f32() >= state.speed_secs {
state.idx = (state.idx + 1) % n;
state.last_tick = std::time::Instant::now();
}
if state.playing {
ui.ctx().request_repaint_after(std::time::Duration::from_millis(120));
}
let max_idx = n.saturating_sub(1);
ui.horizontal(|ui| {
if ui.button("⏮").on_hover_text("first").clicked() {
state.idx = 0;
}
if ui.button("◀").on_hover_text("previous").clicked() {
state.idx = state.idx.saturating_sub(1);
}
if ui
.button(if state.playing { "⏸ pause" } else { "▶ play" })
.clicked()
{
state.playing = !state.playing;
state.last_tick = std::time::Instant::now();
}
if ui.button("▶").on_hover_text("next").clicked() {
if state.idx + 1 < n {
state.idx += 1;
}
}
if ui.button("⏭").on_hover_text("latest").clicked() {
state.idx = max_idx;
}
ui.separator();
ui.label(format!("release {} of {}", state.idx + 1, n));
ui.separator();
ui.add(egui::Slider::new(&mut state.idx, 0..=max_idx).text("scrub"));
ui.separator();
ui.add(
egui::Slider::new(&mut state.speed_secs, 0.25..=5.0)
.text("s/step")
.logarithmic(true),
);
});
let rid = tl.release_order[state.idx];
ui.separator();
ui.label(
egui::RichText::new(format!("⏱ pinned to release {rid}"))
.monospace()
.color(Color32::from_rgb(180, 180, 220)),
);
ui.add_space(8.0);
let avail = ui.available_size();
let col_w = (avail.x - 24.0) / 3.0;
ui.horizontal_top(|ui| {
ui.allocate_ui_with_layout(
Vec2::new(col_w, avail.y - 40.0),
Layout::top_down(Align::Min),
|ui| reel_versions(ui, tl, rid),
);
ui.separator();
ui.allocate_ui_with_layout(
Vec2::new(col_w, avail.y - 40.0),
Layout::top_down(Align::Min),
|ui| reel_dependencies(ui, tl, rid),
);
ui.separator();
ui.allocate_ui_with_layout(
Vec2::new(col_w, avail.y - 40.0),
Layout::top_down(Align::Min),
|ui| reel_benches(ui, tl, state.idx),
);
});
Some(rid)
}
fn reel_versions(ui: &mut egui::Ui, tl: &Timeline, rid: Uuid) {
ui.heading("📦 Component versions");
ui.separator();
egui::ScrollArea::vertical()
.id_source("reel-versions")
.show(ui, |ui| {
for lane in &tl.lanes {
let Some(node) = lane.nodes.iter().find(|n| n.release_id == rid) else {
ui.colored_label(
Color32::DARK_GRAY,
format!("{}: (not part of this release)", lane.repo),
);
continue;
};
ui.group(|ui| {
ui.horizontal(|ui| {
ui.strong(&lane.repo);
ui.label(
egui::RichText::new(format!(
"@{}",
&node.sha[..node.sha.len().min(10)]
))
.monospace()
.color(Color32::from_rgb(160, 200, 240)),
);
if node.dirty {
ui.colored_label(Color32::YELLOW, "✱ dirty");
}
});
ui.label(format!("branch: {} • {}", node.branch, node.gate_status));
if node.published_versions.is_empty() {
ui.colored_label(Color32::DARK_GRAY, "(no published crates)");
} else {
ui.label("published:");
for (c, v) in &node.published_versions {
ui.monospace(format!(" {c} @ {v}"));
}
}
});
ui.add_space(4.0);
}
});
}
fn reel_dependencies(ui: &mut egui::Ui, tl: &Timeline, rid: Uuid) {
ui.heading("🔗 Dependencies");
ui.separator();
let Some(snap) = tl.snapshot_for(&rid) else {
ui.colored_label(Color32::DARK_GRAY, "(no snapshot for this release)");
return;
};
ui.label(format!("snapshot: {}", snap.snapshot_id));
ui.label(format!("{} cross-repo edge(s)", snap.edges.len()));
ui.separator();
egui::ScrollArea::vertical()
.id_source("reel-deps")
.show(ui, |ui| {
if snap.edges.is_empty() {
ui.colored_label(Color32::DARK_GRAY, "(no edges)");
}
for edge in &snap.edges {
ui.group(|ui| {
ui.horizontal(|ui| {
ui.strong(&edge.from);
ui.label("→");
ui.strong(&edge.to);
});
let names = edge
.via
.iter()
.map(|c| c.as_str())
.collect::<Vec<_>>()
.join(", ");
ui.monospace(format!("via: {names}"));
});
ui.add_space(2.0);
}
});
}
fn reel_benches(ui: &mut egui::Ui, tl: &Timeline, release_idx: usize) {
ui.heading("⚡ Benchmarks");
ui.separator();
egui::ScrollArea::vertical()
.id_source("reel-benches")
.show(ui, |ui| {
for lane in &tl.lanes {
let Some(hist) = tl.bench_history.get(&lane.repo) else {
continue;
};
ui.group(|ui| {
ui.horizontal(|ui| {
ui.strong(&lane.repo);
ui.label(format!("({} runs)", hist.points.len()));
});
draw_sparkline(ui, hist, release_idx);
});
ui.add_space(4.0);
}
});
}
fn draw_sparkline(ui: &mut egui::Ui, hist: &BenchHistory, release_idx: usize) {
let n = hist.points.len();
if n == 0 {
ui.colored_label(Color32::DARK_GRAY, "(no runs)");
return;
}
let (mn, mx) = hist.min_max(None).unwrap_or((0.0, 1.0));
let span = (mx - mn).max(1e-9);
let (rect, _resp) = ui.allocate_exact_size(Vec2::new(ui.available_width(), 70.0), Sense::hover());
let painter = ui.painter_at(rect);
painter.rect_filled(rect, Rounding::same(3.0), Color32::from_rgb(22, 22, 30));
let pad = 6.0;
let inner = rect.shrink(pad);
let xs = if n == 1 {
vec![inner.center().x]
} else {
(0..n)
.map(|i| inner.left() + inner.width() * (i as f32) / ((n - 1) as f32))
.collect()
};
let mut prev: Option<Pos2> = None;
for (i, p) in hist.points.iter().enumerate() {
let t = ((p.primary_metric_value - mn) / span) as f32;
let y = inner.bottom() - t * inner.height();
let pt = Pos2::new(xs[i], y);
if let Some(pp) = prev {
painter.line_segment(
[pp, pt],
Stroke::new(1.6, Color32::from_rgb(140, 200, 240)),
);
}
painter.circle_filled(pt, 2.5, Color32::from_rgb(180, 220, 255));
prev = Some(pt);
}
let cursor_i = if n == 1 || release_idx == 0 {
0
} else {
((release_idx as f32) * ((n - 1) as f32)
/ ((release_idx.max(n - 1)) as f32))
.round() as usize
}
.min(n - 1);
let cx = xs[cursor_i];
painter.line_segment(
[Pos2::new(cx, inner.top()), Pos2::new(cx, inner.bottom())],
Stroke::new(1.5, Color32::from_rgba_unmultiplied(255, 220, 80, 200)),
);
let pt = &hist.points[cursor_i];
painter.text(
Pos2::new(inner.left() + 2.0, inner.top() - 2.0),
Align2::LEFT_BOTTOM,
format!(
"{} = {:.2} ({}/{}/{})",
pt.primary_metric_name,
pt.primary_metric_value,
pt.version,
pt.machine,
pt.timestamp.format("%Y-%m-%d")
),
FontId::proportional(11.0),
Color32::from_rgb(220, 220, 230),
);
let _ = Rect::NOTHING; }