nornir 0.4.6

Companion to cargo: dependency tracking, release gating, deploy, benchmarks, and documentation assembly. Project-agnostic.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Connected **dep graph → call graph** tab.
//!
//! Drill `repo → crate → function calls`. The levels are one nested graph: repos
//! and crates come from `symbol_facts` (and line up with `dep_graph_edges.via`),
//! and the leaf call graph comes from `call_edges` (`caller_path → callee_ident`).
//! Data is read over `Warehouse.Scan` (columns + stringified rows), so this works
//! both **embedded** (local warehouse) and **remote** (against a `nornir-server`,
//! scoped by the `nornir-workspace` header), exactly like the warehouse browser.

use std::collections::HashMap;
use std::path::PathBuf;

use eframe::egui::{self, Color32, FontId, Pos2, Sense, Stroke, Vec2};

use crate::warehouse::iceberg::{IcebergWarehouse, TablePreview};

/// Rows scanned per table. Call graphs are bounded by this; we note truncation.
const SCAN_LIMIT: u32 = 8000;
/// Cap rendered nodes (highest-degree kept) so a huge crate stays legible.
const MAX_NODES: usize = 160;

enum Src {
    Local(PathBuf),
    Remote { endpoint: String, token: String },
}

struct Node {
    id: String,
    label: String,
    pos: Pos2,
    deg: usize,
}

pub struct CallGraphState {
    src: Src,
    /// Selected workspace (the `nornir-workspace` gRPC header); empty for local.
    workspace: String,
    loaded: bool,
    error: Option<String>,
    /// Distinct repos and their crates, from `symbol_facts`.
    repos: Vec<String>,
    crates_by_repo: HashMap<String, Vec<String>>,
    sel_repo: Option<String>,
    sel_crate: Option<String>,
    /// Cached `call_edges` scan (reused across crate switches).
    call_rows: Option<TablePreview>,
    /// Cached `symbol_facts` scan — the details panel looks up a clicked
    /// node's kind/file/line/signature here.
    sym_rows: Option<TablePreview>,
    /// Clicked node (index into `nodes`) — drives the details side panel.
    selected: Option<usize>,
    /// Current laid-out graph.
    nodes: Vec<Node>,
    edges: Vec<(usize, usize)>,
    built_for: Option<(String, String)>,
    truncated: bool,
    pan: Vec2,
    zoom: f32,
}

impl CallGraphState {
    pub fn local(root: PathBuf) -> Self {
        Self::with(Src::Local(root), String::new())
    }
    pub fn remote(endpoint: String, token: String, workspace: String) -> Self {
        Self::with(Src::Remote { endpoint, token }, workspace)
    }
    fn with(src: Src, workspace: String) -> Self {
        Self {
            src,
            workspace,
            loaded: false,
            error: None,
            repos: Vec::new(),
            crates_by_repo: HashMap::new(),
            sel_repo: None,
            sel_crate: None,
            call_rows: None,
            sym_rows: None,
            selected: None,
            nodes: Vec::new(),
            edges: Vec::new(),
            built_for: None,
            truncated: false,
            pan: Vec2::ZERO,
            zoom: 1.0,
        }
    }

    fn scan(&self, table: &str) -> Result<TablePreview, String> {
        match &self.src {
            Src::Local(root) => IcebergWarehouse::open(root)
                .and_then(|wh| wh.scan_preview(table, SCAN_LIMIT as usize))
                .map_err(|e| format!("{e:#}")),
            Src::Remote { endpoint, token } => {
                super::remote::scan_table(endpoint, token, table, SCAN_LIMIT, &self.workspace)
                    .map_err(|e| format!("{e:#}"))
            }
        }
    }

    /// Re-scope to a different workspace (the picker switched): drop all cached
    /// scans/graph so the next draw rebuilds from the new workspace's warehouse.
    pub(crate) fn set_workspace(&mut self, workspace: String) {
        self.workspace = workspace;
        self.loaded = false;
        self.error = None;
        self.repos.clear();
        self.crates_by_repo.clear();
        self.sel_repo = None;
        self.sel_crate = None;
        self.call_rows = None;
        self.sym_rows = None;
        self.selected = None;
        self.nodes.clear();
        self.edges.clear();
        self.built_for = None;
    }

    /// Scan `symbol_facts` (for the repo/crate pickers) + `call_edges` once.
    fn ensure_loaded(&mut self) {
        if self.loaded {
            return;
        }
        self.loaded = true;
        let syms = match self.scan("symbol_facts") {
            Ok(p) => p,
            Err(e) => {
                self.error = Some(format!("scan symbol_facts: {e}"));
                return;
            }
        };
        let (Some(ri), Some(ci)) = (col(&syms, "repo"), col(&syms, "crate_name")) else {
            self.error = Some("symbol_facts missing repo/crate_name columns".into());
            return;
        };
        let mut repos: Vec<String> = Vec::new();
        for row in &syms.rows {
            let (repo, krate) = (cell(row, ri), cell(row, ci));
            if repo.is_empty() {
                continue;
            }
            if !repos.contains(&repo) {
                repos.push(repo.clone());
            }
            let cs = self.crates_by_repo.entry(repo).or_default();
            if !krate.is_empty() && !cs.contains(&krate) {
                cs.push(krate);
            }
        }
        repos.sort();
        for cs in self.crates_by_repo.values_mut() {
            cs.sort();
        }
        self.repos = repos;
        if self.sel_repo.is_none() {
            self.sel_repo = self.repos.first().cloned();
        }
        if let Some(r) = &self.sel_repo {
            self.sel_crate = self.crates_by_repo.get(r).and_then(|c| c.first().cloned());
        }
        self.sym_rows = Some(syms); // kept for the click-to-inspect details panel

        match self.scan("call_edges") {
            Ok(p) => self.call_rows = Some(p),
            Err(e) => self.error = Some(format!("scan call_edges: {e}")),
        }
    }

    /// Build + lay out the call graph for the selected (repo, crate).
    fn build_graph(&mut self) {
        let (Some(repo), Some(krate)) = (self.sel_repo.clone(), self.sel_crate.clone()) else {
            self.nodes.clear();
            self.edges.clear();
            return;
        };
        if self.built_for.as_ref() == Some(&(repo.clone(), krate.clone())) {
            return;
        }
        self.built_for = Some((repo.clone(), krate.clone()));
        self.nodes.clear();
        self.edges.clear();
        self.selected = None;
        self.truncated = false;

        let Some(rows) = &self.call_rows else { return };
        let (Some(ri), Some(ci), Some(fi), Some(ti)) = (
            col(rows, "repo"),
            col(rows, "crate_name"),
            col(rows, "caller_path"),
            col(rows, "callee_ident"),
        ) else {
            self.error = Some("call_edges missing expected columns".into());
            return;
        };

        // Collect raw edges for this crate, counting node degree.
        let mut deg: HashMap<String, usize> = HashMap::new();
        let mut raw: Vec<(String, String)> = Vec::new();
        for row in &rows.rows {
            if cell(row, ri) != repo || cell(row, ci) != krate {
                continue;
            }
            let (from, to) = (short(&cell(row, fi)), short(&cell(row, ti)));
            if from.is_empty() || to.is_empty() {
                continue;
            }
            *deg.entry(from.clone()).or_default() += 1;
            *deg.entry(to.clone()).or_default() += 1;
            raw.push((from, to));
        }

        // Cap to the highest-degree nodes for legibility.
        let mut keep: Vec<String> = deg.keys().cloned().collect();
        keep.sort_by(|a, b| deg[b].cmp(&deg[a]).then(a.cmp(b)));
        if keep.len() > MAX_NODES {
            keep.truncate(MAX_NODES);
            self.truncated = true;
        }
        let mut idx: HashMap<String, usize> = HashMap::new();
        let n = keep.len().max(1) as f32;
        for (i, id) in keep.iter().enumerate() {
            // Seed deterministically on a circle (no RNG → resume-safe).
            let ang = std::f32::consts::TAU * (i as f32) / n;
            idx.insert(id.clone(), self.nodes.len());
            self.nodes.push(Node {
                id: id.clone(),
                label: id.clone(),
                pos: Pos2::new(220.0 * ang.cos(), 220.0 * ang.sin()),
                deg: deg[id],
            });
        }
        for (from, to) in raw {
            if let (Some(&a), Some(&b)) = (idx.get(&from), idx.get(&to)) {
                if a != b {
                    self.edges.push((a, b));
                }
            }
        }
        self.layout();
        self.pan = Vec2::ZERO;
        self.zoom = 1.0;
    }

    /// Tiny Fruchterman–Reingold force layout (deterministic).
    fn layout(&mut self) {
        let n = self.nodes.len();
        if n < 2 {
            return;
        }
        let area = 640.0 * 480.0;
        let k = (area / n as f32).sqrt();
        let mut disp = vec![Vec2::ZERO; n];
        for _ in 0..120 {
            for d in disp.iter_mut() {
                *d = Vec2::ZERO;
            }
            // Repulsion (all pairs).
            for i in 0..n {
                for j in (i + 1)..n {
                    let mut delta = self.nodes[i].pos - self.nodes[j].pos;
                    let mut dist = delta.length();
                    if dist < 0.01 {
                        delta = Vec2::new(0.1 * (i as f32 + 1.0), 0.1);
                        dist = delta.length();
                    }
                    let force = (k * k) / dist;
                    let push = delta / dist * force;
                    disp[i] += push;
                    disp[j] -= push;
                }
            }
            // Attraction (edges).
            for &(a, b) in &self.edges {
                let delta = self.nodes[a].pos - self.nodes[b].pos;
                let dist = delta.length().max(0.01);
                let force = (dist * dist) / k;
                let pull = delta / dist * force;
                disp[a] -= pull;
                disp[b] += pull;
            }
            for i in 0..n {
                let d = disp[i];
                let len = d.length().max(0.01);
                self.nodes[i].pos += d / len * len.min(16.0);
            }
        }
    }

    pub fn draw(&mut self, ui: &mut egui::Ui) {
        self.ensure_loaded();

        egui::TopBottomPanel::top("cg_controls").show_inside(ui, |ui| {
            ui.horizontal_wrapped(|ui| {
                ui.label("repo:");
                let cur_repo = self.sel_repo.clone().unwrap_or_default();
                egui::ComboBox::from_id_source("cg_repo")
                    .selected_text(if cur_repo.is_empty() { "".into() } else { cur_repo.clone() })
                    .show_ui(ui, |ui| {
                        for r in self.repos.clone() {
                            if ui.selectable_label(self.sel_repo.as_deref() == Some(&r), &r).clicked() {
                                self.sel_repo = Some(r.clone());
                                self.sel_crate =
                                    self.crates_by_repo.get(&r).and_then(|c| c.first().cloned());
                                self.built_for = None;
                            }
                        }
                    });
                ui.separator();
                ui.label("crate:");
                let crates = self
                    .sel_repo
                    .as_ref()
                    .and_then(|r| self.crates_by_repo.get(r))
                    .cloned()
                    .unwrap_or_default();
                let cur_crate = self.sel_crate.clone().unwrap_or_default();
                egui::ComboBox::from_id_source("cg_crate")
                    .selected_text(if cur_crate.is_empty() { "".into() } else { cur_crate.clone() })
                    .show_ui(ui, |ui| {
                        for c in crates {
                            if ui.selectable_label(self.sel_crate.as_deref() == Some(&c), &c).clicked() {
                                self.sel_crate = Some(c);
                                self.built_for = None;
                            }
                        }
                    });
                ui.separator();
                if ui.button("↻ reload").clicked() {
                    self.loaded = false;
                    self.call_rows = None;
                    self.crates_by_repo.clear();
                    self.repos.clear();
                    self.built_for = None;
                    self.error = None;
                }
                if ui.button("⊙ fit").clicked() {
                    self.pan = Vec2::ZERO;
                    self.zoom = 1.0;
                }
            });
        });

        self.build_graph();

        // Details panel for the clicked node: symbol facts + clickable
        // caller/callee navigation (click a name to jump the selection).
        if let Some(sel) = self.selected {
            if sel >= self.nodes.len() {
                self.selected = None;
            } else {
                let mut jump: Option<usize> = None;
                let mut close = false;
                egui::SidePanel::right("cg_details").default_width(300.0).show_inside(ui, |ui| {
                    let node = &self.nodes[sel];
                    ui.horizontal(|ui| {
                        ui.heading(&node.label);
                        if ui.button("").on_hover_text("close").clicked() {
                            close = true;
                        }
                    });
                    ui.label(format!("call degree: {}", node.deg));
                    ui.separator();

                    // symbol_facts lookup by item_name within the selected repo/crate.
                    if let (Some(syms), Some((repo, krate))) = (&self.sym_rows, &self.built_for) {
                        if let (Some(ri), Some(ci), Some(ni)) =
                            (col(syms, "repo"), col(syms, "crate_name"), col(syms, "item_name"))
                        {
                            let (fi, li, ki, si, vi) = (
                                col(syms, "file"),
                                col(syms, "line"),
                                col(syms, "item_kind"),
                                col(syms, "signature"),
                                col(syms, "visibility"),
                            );
                            for row in syms
                                .rows
                                .iter()
                                .filter(|r| {
                                    cell(r, ri) == *repo
                                        && cell(r, ci) == *krate
                                        && cell(r, ni) == node.label
                                })
                                .take(3)
                            {
                                if let (Some(k), Some(v)) = (ki, vi) {
                                    ui.label(format!("{} {}", cell(row, v), cell(row, k)));
                                }
                                if let (Some(f), Some(l)) = (fi, li) {
                                    ui.monospace(format!("{}:{}", cell(row, f), cell(row, l)));
                                }
                                if let Some(s) = si {
                                    let sig = cell(row, s);
                                    if !sig.is_empty() {
                                        ui.add(egui::Label::new(
                                            egui::RichText::new(sig).monospace().size(11.0),
                                        ).wrap());
                                    }
                                }
                                ui.add_space(4.0);
                            }
                        }
                    }

                    // Callers / callees from the laid-out edges — clickable.
                    let callers: Vec<usize> =
                        self.edges.iter().filter(|(_, b)| *b == sel).map(|(a, _)| *a).collect();
                    let callees: Vec<usize> =
                        self.edges.iter().filter(|(a, _)| *a == sel).map(|(_, b)| *b).collect();
                    egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
                        ui.separator();
                        ui.strong(format!("called by ({})", callers.len()));
                        for a in callers {
                            if ui.link(&self.nodes[a].label).clicked() {
                                jump = Some(a);
                            }
                        }
                        ui.separator();
                        ui.strong(format!("calls ({})", callees.len()));
                        for b in callees {
                            if ui.link(&self.nodes[b].label).clicked() {
                                jump = Some(b);
                            }
                        }
                    });
                });
                if close {
                    self.selected = None;
                } else if let Some(j) = jump {
                    self.selected = Some(j);
                    // Center the jumped-to node so navigation reads as movement.
                    self.pan = -(self.nodes[j].pos.to_vec2()) * self.zoom;
                }
            }
        }

        egui::CentralPanel::default().show_inside(ui, |ui| {
            if let Some(err) = &self.error {
                ui.colored_label(Color32::RED, err);
                return;
            }
            if self.nodes.is_empty() {
                ui.label(
                    "no call edges for this crate (pick another repo/crate; \
                     monitoring fills call_edges as it republishes)",
                );
                return;
            }
            let (resp, painter) =
                ui.allocate_painter(ui.available_size(), Sense::click_and_drag());
            if resp.dragged() {
                self.pan += resp.drag_delta();
            }
            if resp.hovered() {
                let scroll = ui.input(|i| i.raw_scroll_delta.y);
                if scroll != 0.0 {
                    self.zoom = (self.zoom * (1.0 + scroll * 0.001)).clamp(0.2, 4.0);
                }
            }
            let origin = resp.rect.center() + self.pan;
            let tf = |p: Pos2| origin + p.to_vec2() * self.zoom;
            let radius = |deg: usize| (3.0 + (deg as f32).sqrt() * 1.6) * self.zoom.clamp(0.6, 2.0);

            // Click-to-inspect: nearest node within its radius (+slop) selects it;
            // clicking empty canvas clears the selection.
            if resp.clicked() {
                if let Some(click) = resp.interact_pointer_pos() {
                    self.selected = self
                        .nodes
                        .iter()
                        .enumerate()
                        .map(|(i, n)| (i, (tf(n.pos) - click).length(), radius(n.deg)))
                        .filter(|(_, d, r)| *d <= r.max(6.0) + 4.0)
                        .min_by(|a, b| a.1.total_cmp(&b.1))
                        .map(|(i, _, _)| i);
                }
            }

            // Edges with a small arrowhead toward the callee; edges touching the
            // selected node are highlighted (orange = outgoing, cyan = incoming).
            for &(a, b) in &self.edges {
                let (pa, pb) = (tf(self.nodes[a].pos), tf(self.nodes[b].pos));
                let stroke = match self.selected {
                    Some(s) if a == s => Stroke::new(1.6, Color32::from_rgb(240, 160, 60)),
                    Some(s) if b == s => Stroke::new(1.6, Color32::from_rgb(80, 190, 220)),
                    _ => Stroke::new(1.0, Color32::from_gray(90)),
                };
                painter.line_segment([pa, pb], stroke);
                let dir = (pb - pa).normalized();
                let head = pb - dir * (6.0 + 4.0 * self.zoom);
                let perp = Vec2::new(-dir.y, dir.x) * 3.0;
                painter.line_segment([pb, head + perp], Stroke::new(1.0, Color32::from_gray(120)));
                painter.line_segment([pb, head - perp], Stroke::new(1.0, Color32::from_gray(120)));
            }
            // Nodes: radius + color scale with call degree; selection ring on top.
            for (i, node) in self.nodes.iter().enumerate() {
                let p = tf(node.pos);
                let r = radius(node.deg);
                painter.circle_filled(p, r, heat(node.deg));
                if self.selected == Some(i) {
                    painter.circle_stroke(p, r + 3.0, Stroke::new(2.0, Color32::WHITE));
                }
                if self.zoom > 0.85 || node.deg >= 4 || self.selected == Some(i) {
                    painter.text(
                        p + Vec2::new(r + 2.0, -r),
                        egui::Align2::LEFT_BOTTOM,
                        &node.label,
                        FontId::proportional(11.0),
                        Color32::from_gray(210),
                    );
                }
            }
            let (repo, krate) = self.built_for.clone().unwrap_or_default();
            let note = format!(
                "{repo} · {krate}{} fns, {} calls{}  ·  drag to pan, scroll to zoom",
                self.nodes.len(),
                self.edges.len(),
                if self.truncated { format!(" (top {MAX_NODES})") } else { String::new() }
            );
            let note = format!("{note} · click a node to inspect");
            painter.text(
                resp.rect.left_top() + Vec2::new(8.0, 8.0),
                egui::Align2::LEFT_TOP,
                note,
                FontId::proportional(12.0),
                Color32::from_gray(160),
            );
        });
    }
}

/// Column index by name in a [`TablePreview`].
fn col(p: &TablePreview, name: &str) -> Option<usize> {
    p.columns.iter().position(|c| c == name)
}

fn cell(row: &[String], i: usize) -> String {
    row.get(i).cloned().unwrap_or_default()
}

/// Last `::`-segment of a path, for compact node labels.
fn short(path: &str) -> String {
    path.rsplit("::").next().unwrap_or(path).to_string()
}

/// Degree → warm color (cool=few calls, hot=hub).
fn heat(deg: usize) -> Color32 {
    let t = (deg as f32 / 12.0).clamp(0.0, 1.0);
    let r = (90.0 + 150.0 * t) as u8;
    let g = (150.0 - 60.0 * t) as u8;
    let b = (200.0 - 150.0 * t) as u8;
    Color32::from_rgb(r, g, b)
}