neuronic 0.1.0

Real-time graphical visualization of Caryatid message bus flow
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
//! Main application window.

use crate::config::NeuronicConfig;
use crate::graph::{HealthConfig, MessageFlowGraph, ModuleNode, TopicEdge};
use crate::subscriber;
use buswatch_types::Snapshot;
use egui::{Pos2, Vec2};
use egui_graphs::Graph;
use petgraph::stable_graph::StableGraph;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::mpsc as std_mpsc;
use std::time::Instant;

use super::animations;
use super::drawing::{self, DrawContext};
use super::export;
use super::input::{self, KeyboardAction};
use super::layout;
use super::panels;
use super::search;
use super::theme::Theme;
use super::types::*;

/// Main application state.
pub struct NeuronicApp {
    // Data and connection
    flow_graph: MessageFlowGraph,
    #[allow(dead_code)]
    egui_graph: Graph<ModuleNode, TopicEdge>,
    snapshot_rx: Option<std_mpsc::Receiver<Snapshot>>,
    _runtime: tokio::runtime::Runtime,
    connected: bool,
    connection_error: Option<String>,

    // Layout and positioning
    node_positions: HashMap<String, Pos2>,
    node_velocities: HashMap<String, Vec2>,
    layout_mode: LayoutMode,
    zoom: f32,
    pan: Vec2,

    // Selection and interaction
    selected_node: Option<String>,
    dragged_node: Option<String>,
    search_query: String,
    search_focused: bool,
    highlighted_node: Option<String>,
    search_matches: Vec<String>,

    // Animation state
    node_activity: HashMap<String, NodeActivity>,
    synapse_particles: HashMap<(String, String, String), Vec<SynapseParticle>>,
    pulse_rings: HashMap<String, Vec<PulseRing>>,
    last_frame: Instant,

    // Filtering and grouping
    topic_filters: Vec<String>,
    new_filter: String,
    node_groups: Vec<NodeGroup>,
    new_group_pattern: String,

    // UI visibility toggles
    show_labels: bool,
    show_legend: bool,
    show_filter_panel: bool,
    show_group_panel: bool,
    show_gradient_edges: bool,
    show_pulse_rings: bool,
    show_minimap: bool,
    #[allow(dead_code)]
    sound_enabled: bool,
    paused: bool,
    theme: Theme,

    // Stats
    update_count: u64,
}

impl NeuronicApp {
    pub fn new(cc: &eframe::CreationContext<'_>, config_path: PathBuf, topic: String) -> Self {
        let neuronic_config = NeuronicConfig::load(&config_path).unwrap_or_else(|e| {
            tracing::warn!("Failed to load config: {}, using defaults", e);
            NeuronicConfig::default()
        });

        // Set up dark theme
        let theme = Theme::Dark;
        theme.apply_to_egui(&cc.egui_ctx);

        let health_config = HealthConfig {
            backlog_warning: neuronic_config.graph.backlog_warning,
            backlog_critical: neuronic_config.graph.backlog_critical,
            pending_warning_us: neuronic_config.graph.pending_warning_ms * 1000,
            pending_critical_us: neuronic_config.graph.pending_critical_ms * 1000,
        };
        let flow_graph = MessageFlowGraph::new_with_config(
            health_config,
            neuronic_config.filter.ignored_topics.clone(),
        );

        let runtime = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
        let (sync_tx, sync_rx) = std_mpsc::channel();

        let config_path_clone = config_path.clone();
        let topic_clone = topic.clone();
        let (connected, connection_error) = {
            let sync_tx = sync_tx.clone();
            match runtime.block_on(async {
                subscriber::create_subscriber(&config_path_clone, &topic_clone).await
            }) {
                Ok((mut async_rx, _handle)) => {
                    runtime.spawn(async move {
                        while let Some(snapshot) = async_rx.recv().await {
                            if sync_tx.send(snapshot).is_err() {
                                break;
                            }
                        }
                    });
                    (true, None)
                }
                Err(e) => {
                    tracing::error!("Failed to connect to RabbitMQ: {}", e);
                    (false, Some(e.to_string()))
                }
            }
        };

        Self {
            flow_graph,
            egui_graph: Graph::new(StableGraph::new()),
            snapshot_rx: Some(sync_rx),
            _runtime: runtime,
            connected,
            connection_error,
            node_positions: HashMap::new(),
            node_velocities: HashMap::new(),
            layout_mode: LayoutMode::ForceDirected,
            zoom: 1.0,
            pan: Vec2::ZERO,
            selected_node: None,
            dragged_node: None,
            search_query: String::new(),
            search_focused: false,
            highlighted_node: None,
            search_matches: Vec::new(),
            node_activity: HashMap::new(),
            synapse_particles: HashMap::new(),
            pulse_rings: HashMap::new(),
            last_frame: Instant::now(),
            topic_filters: neuronic_config.filter.ignored_topics.clone(),
            new_filter: String::new(),
            node_groups: Vec::new(),
            new_group_pattern: String::new(),
            show_labels: true,
            show_legend: true,
            show_filter_panel: false,
            show_group_panel: false,
            show_gradient_edges: false,
            show_pulse_rings: true,
            show_minimap: false,
            sound_enabled: false,
            paused: false,
            theme,
            update_count: 0,
        }
    }

    fn process_snapshots(&mut self) {
        if self.paused {
            if let Some(rx) = &self.snapshot_rx {
                while rx.try_recv().is_ok() {}
            }
            return;
        }

        if let Some(rx) = &self.snapshot_rx {
            let mut latest: Option<Snapshot> = None;
            while let Ok(snapshot) = rx.try_recv() {
                latest = Some(snapshot);
            }

            if let Some(snapshot) = latest {
                animations::detect_activity(
                    &snapshot,
                    &mut self.node_activity,
                    &mut self.synapse_particles,
                    &mut self.pulse_rings,
                    self.show_pulse_rings,
                );

                self.flow_graph.update_from_snapshot(&snapshot);
                self.update_count += 1;
            }
        }
    }

    fn draw_controls(&mut self, ui: &mut egui::Ui) {
        ui.horizontal(|ui| {
            // Connection status
            if self.connected {
                ui.label(egui::RichText::new("● Connected").color(self.theme.neuron_active()));
            } else {
                ui.label(egui::RichText::new("● Disconnected").color(self.theme.neuron_critical()));
                if let Some(err) = &self.connection_error {
                    ui.label(
                        egui::RichText::new(err)
                            .color(self.theme.neuron_critical())
                            .small(),
                    );
                }
            }

            ui.separator();

            // Search box
            let old_query = self.search_query.clone();
            if let Some(selected) = search::draw_search_box(
                ui,
                &mut self.search_query,
                &mut self.search_focused,
                &self.search_matches,
            ) {
                self.highlighted_node = Some(selected.clone());
                self.selected_node = Some(selected);
            }

            if self.search_query != old_query {
                self.search_matches =
                    search::find_matching_modules(&self.flow_graph, &self.search_query);
                self.highlighted_node =
                    search::get_best_match(&self.search_matches, &self.search_query);
            }

            ui.separator();

            // Layout toggle
            let layout_label = match self.layout_mode {
                LayoutMode::ForceDirected => "Force",
                LayoutMode::Hierarchical => "Hierarchy",
            };
            if ui.button(layout_label).clicked() {
                self.layout_mode = match self.layout_mode {
                    LayoutMode::ForceDirected => LayoutMode::Hierarchical,
                    LayoutMode::Hierarchical => LayoutMode::ForceDirected,
                };
            }

            ui.separator();

            // Theme toggle
            if ui
                .button(if self.theme == Theme::Dark {
                    "Light"
                } else {
                    "Dark"
                })
                .clicked()
            {
                self.theme = self.theme.toggle();
            }

            // Pause button
            if ui
                .button(if self.paused { "Resume" } else { "Pause" })
                .clicked()
            {
                self.paused = !self.paused;
            }

            ui.separator();

            // Toggle buttons
            ui.checkbox(&mut self.show_labels, "Labels");
            ui.checkbox(&mut self.show_minimap, "Minimap");
            ui.checkbox(&mut self.show_gradient_edges, "Gradient");
            ui.checkbox(&mut self.show_pulse_rings, "Pulses");

            ui.separator();

            // Panel toggles
            if ui.button("Filters").clicked() {
                self.show_filter_panel = !self.show_filter_panel;
            }
            if ui.button("Groups").clicked() {
                self.show_group_panel = !self.show_group_panel;
            }

            ui.separator();

            // Export
            export::show_export_dialog(ui, &self.flow_graph, &self.node_positions, &self.theme);

            ui.separator();

            // Zoom controls
            if ui.button("-").clicked() {
                self.zoom = (self.zoom * 0.8).max(0.1);
            }
            ui.label(format!("{:.0}%", self.zoom * 100.0));
            if ui.button("+").clicked() {
                self.zoom = (self.zoom * 1.25).min(5.0);
            }
            if ui.button("Reset").clicked() {
                self.zoom = 1.0;
                self.pan = Vec2::ZERO;
            }

            ui.separator();

            // Stats
            ui.label(format!(
                "Nodes: {} | Edges: {} | Updates: {}",
                self.flow_graph.module_count(),
                self.flow_graph.edge_count(),
                self.update_count
            ));
        });
    }
}

impl eframe::App for NeuronicApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        let now = Instant::now();
        let dt = now.duration_since(self.last_frame).as_secs_f32();
        self.last_frame = now;

        self.theme.apply_to_egui(ctx);
        self.process_snapshots();

        animations::update_animations(
            dt,
            &mut self.node_activity,
            &mut self.synapse_particles,
            &mut self.pulse_rings,
        );

        ctx.request_repaint();

        // Handle keyboard shortcuts
        egui::CentralPanel::default().show(ctx, |ui| {
            match input::handle_keyboard(ui, &mut self.search_focused) {
                KeyboardAction::FocusSearch => {}
                KeyboardAction::ClearSearch => {
                    self.search_query.clear();
                    self.highlighted_node = None;
                    self.search_matches.clear();
                }
                KeyboardAction::None => {}
            }
        });

        // Top panel with controls
        egui::TopBottomPanel::top("controls").show(ctx, |ui| {
            self.draw_controls(ui);
        });

        // Right panel with details
        egui::SidePanel::right("details")
            .min_width(200.0)
            .show(ctx, |ui| {
                panels::draw_details_panel(
                    ui,
                    &self.flow_graph,
                    &self.selected_node,
                    &self.node_activity,
                    &self.theme,
                );

                if self.show_legend {
                    ui.add_space(16.0);
                    panels::draw_legend(ui, &self.theme);
                }
            });

        // Left panel for filters/groups if open
        if self.show_filter_panel || self.show_group_panel {
            egui::SidePanel::left("left_panel")
                .min_width(180.0)
                .show(ctx, |ui| {
                    if self.show_filter_panel {
                        if panels::draw_filter_panel(
                            ui,
                            &mut self.topic_filters,
                            &mut self.new_filter,
                        ) {
                            // Filters changed - update graph's ignored topics
                            self.flow_graph.ignored_topic_prefixes = self.topic_filters.clone();
                        }
                        ui.add_space(16.0);
                    }

                    if self.show_group_panel {
                        panels::draw_group_panel(
                            ui,
                            &mut self.node_groups,
                            &mut self.new_group_pattern,
                            &self.flow_graph,
                        );
                    }
                });
        }

        // Central panel with graph
        egui::CentralPanel::default().show(ctx, |ui| {
            let rect = ui.available_rect_before_wrap();

            // Handle input
            let input_result = input::handle_input(
                ui,
                rect,
                &self.flow_graph,
                &mut self.node_positions,
                &mut self.node_velocities,
                &mut self.zoom,
                &mut self.pan,
                &mut self.dragged_node,
            );

            if let Some(clicked) = input_result.clicked_node {
                self.selected_node = Some(clicked);
            }

            // Apply layout
            layout::apply_layout(
                self.layout_mode,
                &self.flow_graph,
                &mut self.node_positions,
                &mut self.node_velocities,
                rect,
            );

            // Focus on highlighted node
            if let Some(ref node_name) = self.highlighted_node {
                if self.search_focused {
                    search::focus_on_node(
                        node_name,
                        &self.node_positions,
                        rect,
                        &mut self.pan,
                        &mut self.zoom,
                    );
                }
            }

            // Draw the graph
            let draw_ctx = DrawContext {
                graph: &self.flow_graph,
                positions: &self.node_positions,
                activity: &self.node_activity,
                particles: &self.synapse_particles,
                pulse_rings: &self.pulse_rings,
                selected_node: self.selected_node.as_ref(),
                highlighted_node: self.highlighted_node.as_ref(),
                theme: &self.theme,
                zoom: self.zoom,
                pan: self.pan,
                show_labels: self.show_labels,
                show_gradient_edges: self.show_gradient_edges,
                show_pulse_rings: self.show_pulse_rings,
            };

            draw_ctx.draw_graph(ui, rect);

            // Draw minimap
            if self.show_minimap {
                drawing::draw_minimap(
                    ui,
                    &self.flow_graph,
                    &self.node_positions,
                    rect,
                    self.zoom,
                    self.pan,
                    &self.theme,
                );
            }
        });
    }
}