mielin-cli 0.1.0-rc.1

Command-line interface and control plane for MielinOS distributed agent mesh
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//! Monitoring command implementations

use crate::output::OutputFormat;
use crate::progress::with_spinner;
use anyhow::Result;
use clap::{Args, Subcommand};
use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::Duration;

/// Monitoring commands
#[derive(Debug, Args)]
pub struct MonitorCommand {
    #[command(subcommand)]
    command: MonitorSubcommand,
}

#[derive(Debug, Subcommand)]
enum MonitorSubcommand {
    /// Real-time resource monitoring (like top)
    #[command(visible_aliases = &["resources", "htop"])]
    Top {
        /// Refresh interval in seconds
        #[arg(short = 'i', long, default_value = "2")]
        interval: u64,

        /// Number of iterations (0 = infinite)
        #[arg(short = 'n', long, default_value = "0")]
        iterations: u64,

        /// Sort by field (cpu, memory, name)
        #[arg(short = 's', long, default_value = "cpu")]
        sort_by: String,

        /// Filter by agent name pattern
        #[arg(short = 'f', long)]
        filter: Option<String>,
    },

    /// Watch command output (refresh periodically)
    #[command(visible_aliases = &["repeat", "poll"])]
    Watch {
        /// Command to watch (e.g., "node list")
        command: Vec<String>,

        /// Refresh interval in seconds
        #[arg(short = 'i', long, default_value = "2")]
        interval: u64,

        /// Number of iterations (0 = infinite)
        #[arg(short = 'n', long, default_value = "0")]
        iterations: u64,

        /// Highlight differences between iterations
        #[arg(short = 'd', long)]
        differences: bool,
    },

    /// Stream events from the system
    #[command(visible_aliases = &["stream", "log", "tail"])]
    Events {
        /// Filter by event type
        #[arg(short = 't', long)]
        event_type: Option<String>,

        /// Filter by agent ID
        #[arg(short = 'a', long)]
        agent: Option<String>,

        /// Filter by node ID
        #[arg(short = 'n', long)]
        node: Option<String>,

        /// Follow mode (stream continuously)
        #[arg(short = 'f', long)]
        follow: bool,

        /// Show last N events
        #[arg(short = 'l', long, default_value = "50")]
        limit: usize,

        /// Output to file
        #[arg(short = 'o', long)]
        output: Option<PathBuf>,
    },

    /// Show system metrics dashboard
    #[command(visible_aliases = &["dash", "overview"])]
    Dashboard {
        /// Refresh interval in seconds
        #[arg(short = 'i', long, default_value = "5")]
        interval: u64,

        /// Output format
        #[arg(short = 'o', long, value_enum)]
        output: Option<OutputFormat>,
    },
}

impl MonitorCommand {
    pub async fn execute(&self, output_format: OutputFormat) -> Result<()> {
        match &self.command {
            MonitorSubcommand::Top {
                interval,
                iterations,
                sort_by,
                filter,
            } => {
                top_command(
                    *interval,
                    *iterations,
                    sort_by,
                    filter.as_deref(),
                    output_format,
                )
                .await
            }
            MonitorSubcommand::Watch {
                command,
                interval,
                iterations,
                differences,
            } => watch_command(command, *interval, *iterations, *differences).await,
            MonitorSubcommand::Events {
                event_type,
                agent,
                node,
                follow,
                limit,
                output,
            } => {
                events_command(
                    event_type.as_deref(),
                    agent.as_deref(),
                    node.as_deref(),
                    *follow,
                    *limit,
                    output.as_ref(),
                    output_format,
                )
                .await
            }
            MonitorSubcommand::Dashboard { interval, output } => {
                let format = output.unwrap_or(output_format);
                dashboard_command(*interval, format).await
            }
        }
    }
}

/// Resource monitoring data
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ResourceSnapshot {
    timestamp: chrono::DateTime<chrono::Utc>,
    agents: Vec<AgentResource>,
    system: SystemResource,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct AgentResource {
    id: String,
    name: String,
    cpu_percent: f64,
    memory_mb: f64,
    network_kbps: f64,
    status: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct SystemResource {
    total_cpu_percent: f64,
    total_memory_mb: f64,
    total_memory_used_mb: f64,
    total_agents: usize,
    active_agents: usize,
}

async fn top_command(
    interval: u64,
    iterations: u64,
    sort_by: &str,
    filter: Option<&str>,
    format: OutputFormat,
) -> Result<()> {
    with_spinner("Initializing resource monitor", async {
        tokio::time::sleep(Duration::from_millis(500)).await;
        Ok::<(), anyhow::Error>(())
    })
    .await?;

    let mut iteration = 0u64;

    loop {
        let mut snapshot = fetch_resource_snapshot().await?;

        // Filter agents if pattern provided
        if let Some(pattern) = filter {
            snapshot
                .agents
                .retain(|a| a.name.contains(pattern) || a.id.contains(pattern));
        }

        // Sort agents
        match sort_by {
            "cpu" => snapshot
                .agents
                .sort_by(|a, b| b.cpu_percent.partial_cmp(&a.cpu_percent).unwrap()),
            "memory" => snapshot
                .agents
                .sort_by(|a, b| b.memory_mb.partial_cmp(&a.memory_mb).unwrap()),
            "name" => snapshot.agents.sort_by(|a, b| a.name.cmp(&b.name)),
            _ => {}
        }

        // Clear screen for top-like display (only in table mode)
        if matches!(format, OutputFormat::Table) {
            print!("\x1B[2J\x1B[1;1H"); // Clear screen and move cursor to top
        }

        // Display snapshot
        display_resource_snapshot(&snapshot, format)?;

        iteration += 1;
        if iterations > 0 && iteration >= iterations {
            break;
        }

        // Wait for next iteration
        tokio::time::sleep(Duration::from_secs(interval)).await;
    }

    Ok(())
}

async fn fetch_resource_snapshot() -> Result<ResourceSnapshot> {
    // Stub implementation - would fetch actual data from daemon
    use rand::Rng;
    let mut rng = rand::rng();

    let agents = vec![
        AgentResource {
            id: "agent-001".to_string(),
            name: "web-server".to_string(),
            cpu_percent: rng.random_range(5.0..95.0),
            memory_mb: rng.random_range(50.0..500.0),
            network_kbps: rng.random_range(10.0..1000.0),
            status: "Running".to_string(),
        },
        AgentResource {
            id: "agent-002".to_string(),
            name: "database".to_string(),
            cpu_percent: rng.random_range(5.0..95.0),
            memory_mb: rng.random_range(100.0..800.0),
            network_kbps: rng.random_range(10.0..1000.0),
            status: "Running".to_string(),
        },
        AgentResource {
            id: "agent-003".to_string(),
            name: "cache".to_string(),
            cpu_percent: rng.random_range(5.0..95.0),
            memory_mb: rng.random_range(20.0..200.0),
            network_kbps: rng.random_range(10.0..1000.0),
            status: "Running".to_string(),
        },
    ];

    let total_cpu = agents.iter().map(|a| a.cpu_percent).sum::<f64>() / agents.len() as f64;
    let total_memory_used = agents.iter().map(|a| a.memory_mb).sum::<f64>();

    Ok(ResourceSnapshot {
        timestamp: chrono::Utc::now(),
        agents,
        system: SystemResource {
            total_cpu_percent: total_cpu,
            total_memory_mb: 16384.0,
            total_memory_used_mb: total_memory_used,
            total_agents: 3,
            active_agents: 3,
        },
    })
}

fn display_resource_snapshot(snapshot: &ResourceSnapshot, format: OutputFormat) -> Result<()> {
    match format {
        OutputFormat::Json => {
            println!("{}", serde_json::to_string_pretty(&snapshot)?);
        }
        OutputFormat::Yaml => {
            println!("{}", serde_yaml::to_string(&snapshot)?);
        }
        OutputFormat::Quiet => {
            for agent in &snapshot.agents {
                println!("{}", agent.id);
            }
        }
        OutputFormat::Table => {
            // System summary
            println!(
                "MielinOS Resource Monitor - {}",
                snapshot.timestamp.format("%H:%M:%S")
            );
            println!(
                "Agents: {} total, {} active | CPU: {:.1}% | Memory: {:.1}/{:.1} MB ({:.1}%)",
                snapshot.system.total_agents,
                snapshot.system.active_agents,
                snapshot.system.total_cpu_percent,
                snapshot.system.total_memory_used_mb,
                snapshot.system.total_memory_mb,
                (snapshot.system.total_memory_used_mb / snapshot.system.total_memory_mb) * 100.0
            );
            println!();

            // Agent table
            let mut table = Table::new();
            table
                .load_preset(UTF8_FULL)
                .set_content_arrangement(ContentArrangement::Dynamic)
                .set_header(vec![
                    "ID",
                    "Name",
                    "Status",
                    "CPU %",
                    "Memory (MB)",
                    "Network (KB/s)",
                ]);

            for agent in &snapshot.agents {
                let cpu_cell = if agent.cpu_percent > 80.0 {
                    Cell::new(format!("{:.1}", agent.cpu_percent)).fg(Color::Red)
                } else if agent.cpu_percent > 50.0 {
                    Cell::new(format!("{:.1}", agent.cpu_percent)).fg(Color::Yellow)
                } else {
                    Cell::new(format!("{:.1}", agent.cpu_percent)).fg(Color::Green)
                };

                let status_cell = if agent.status == "Running" {
                    Cell::new(&agent.status).fg(Color::Green)
                } else {
                    Cell::new(&agent.status).fg(Color::Red)
                };

                table.add_row(vec![
                    Cell::new(&agent.id),
                    Cell::new(&agent.name),
                    status_cell,
                    cpu_cell,
                    Cell::new(format!("{:.1}", agent.memory_mb)),
                    Cell::new(format!("{:.1}", agent.network_kbps)),
                ]);
            }

            println!("{}", table);
        }
    }

    Ok(())
}

async fn watch_command(
    command: &[String],
    interval: u64,
    iterations: u64,
    differences: bool,
) -> Result<()> {
    println!("Watching command: {}", command.join(" "));
    println!("Interval: {}s, Press Ctrl+C to stop", interval);
    println!();

    let mut iteration = 0u64;
    let mut previous_output = String::new();

    loop {
        let timestamp = chrono::Utc::now();

        // Clear screen for watch-like display
        print!("\x1B[2J\x1B[1;1H");

        println!(
            "Every {}s: {}  {}",
            interval,
            command.join(" "),
            timestamp.format("%H:%M:%S")
        );
        println!();

        // Execute command (stub implementation)
        let current_output = format!(
            "Output from command: {}\nIteration: {}\nTimestamp: {}",
            command.join(" "),
            iteration + 1,
            timestamp
        );

        if differences && !previous_output.is_empty() && previous_output != current_output {
            println!("[Changes detected]");
        }

        println!("{}", current_output);
        previous_output = current_output;

        iteration += 1;
        if iterations > 0 && iteration >= iterations {
            break;
        }

        tokio::time::sleep(Duration::from_secs(interval)).await;
    }

    Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Event {
    timestamp: chrono::DateTime<chrono::Utc>,
    event_type: String,
    severity: String,
    source: String,
    message: String,
    metadata: serde_json::Value,
}

async fn events_command(
    event_type_filter: Option<&str>,
    agent_filter: Option<&str>,
    node_filter: Option<&str>,
    follow: bool,
    limit: usize,
    output_file: Option<&PathBuf>,
    format: OutputFormat,
) -> Result<()> {
    let events = fetch_events(event_type_filter, agent_filter, node_filter, limit).await?;

    // Write to file if specified
    if let Some(path) = output_file {
        let content = serde_json::to_string_pretty(&events)?;
        std::fs::write(path, content)?;
        println!("Events written to: {}", path.display());
        return Ok(());
    }

    // Display events
    display_events(&events, format)?;

    // Follow mode - stream events continuously
    if follow {
        println!("\n[Following events... Press Ctrl+C to stop]");
        loop {
            tokio::time::sleep(Duration::from_secs(2)).await;

            // Fetch new events (stub - would stream from daemon)
            let new_events = fetch_events(event_type_filter, agent_filter, node_filter, 5).await?;

            for event in new_events {
                display_event(&event, format)?;
            }
        }
    }

    Ok(())
}

async fn fetch_events(
    event_type_filter: Option<&str>,
    agent_filter: Option<&str>,
    node_filter: Option<&str>,
    limit: usize,
) -> Result<Vec<Event>> {
    // Stub implementation - would fetch actual events from daemon
    use rand::Rng;
    let mut rng = rand::rng();

    let event_types = [
        "agent.started",
        "agent.stopped",
        "migration.started",
        "migration.completed",
        "health.check",
    ];
    let sources = ["node-001", "node-002", "agent-001", "agent-002"];
    let severities = ["info", "warning", "error"];

    let mut events = Vec::new();

    for i in 0..limit {
        let event_type = event_types[rng.random_range(0..event_types.len())].to_string();
        let source = sources[rng.random_range(0..sources.len())].to_string();
        let severity = severities[rng.random_range(0..severities.len())].to_string();

        // Apply filters
        if let Some(et_filter) = event_type_filter {
            if !event_type.contains(et_filter) {
                continue;
            }
        }

        if let Some(agent_f) = agent_filter {
            if !source.contains(agent_f) {
                continue;
            }
        }

        if let Some(node_f) = node_filter {
            if !source.contains(node_f) {
                continue;
            }
        }

        events.push(Event {
            timestamp: chrono::Utc::now() - chrono::Duration::seconds((limit - i) as i64 * 10),
            event_type,
            severity,
            source,
            message: format!("Event #{} - Sample event message", i + 1),
            metadata: serde_json::json!({"index": i, "random": rng.random::<u32>()}),
        });
    }

    Ok(events)
}

fn display_events(events: &[Event], format: OutputFormat) -> Result<()> {
    match format {
        OutputFormat::Json => {
            println!("{}", serde_json::to_string_pretty(&events)?);
        }
        OutputFormat::Yaml => {
            println!("{}", serde_yaml::to_string(&events)?);
        }
        OutputFormat::Quiet => {
            for event in events {
                println!("{}", event.event_type);
            }
        }
        OutputFormat::Table => {
            let mut table = Table::new();
            table
                .load_preset(UTF8_FULL)
                .set_content_arrangement(ContentArrangement::Dynamic)
                .set_header(vec!["Timestamp", "Type", "Severity", "Source", "Message"]);

            for event in events {
                let severity_cell = match event.severity.as_str() {
                    "error" => Cell::new(&event.severity).fg(Color::Red),
                    "warning" => Cell::new(&event.severity).fg(Color::Yellow),
                    _ => Cell::new(&event.severity).fg(Color::Green),
                };

                table.add_row(vec![
                    Cell::new(event.timestamp.format("%Y-%m-%d %H:%M:%S")),
                    Cell::new(&event.event_type),
                    severity_cell,
                    Cell::new(&event.source),
                    Cell::new(&event.message),
                ]);
            }

            println!("{}", table);
        }
    }

    Ok(())
}

fn display_event(event: &Event, format: OutputFormat) -> Result<()> {
    match format {
        OutputFormat::Json => {
            println!("{}", serde_json::to_string(event)?);
        }
        OutputFormat::Yaml => {
            println!("{}", serde_yaml::to_string(event)?);
        }
        OutputFormat::Quiet => {
            println!("{}", event.event_type);
        }
        OutputFormat::Table => {
            println!(
                "{} [{}] {} - {} - {}",
                event.timestamp.format("%H:%M:%S"),
                event.severity.to_uppercase(),
                event.event_type,
                event.source,
                event.message
            );
        }
    }

    Ok(())
}

async fn dashboard_command(interval: u64, format: OutputFormat) -> Result<()> {
    loop {
        let snapshot = fetch_resource_snapshot().await?;
        let events = fetch_events(None, None, None, 10).await?;

        // Clear screen
        if matches!(format, OutputFormat::Table) {
            print!("\x1B[2J\x1B[1;1H");
        }

        println!("╔══════════════════════════════════════════════════════════════════╗");
        println!(
            "║           MielinOS System Dashboard - {}",
            snapshot.timestamp.format("%H:%M:%S")
        );
        println!("╚══════════════════════════════════════════════════════════════════╝");
        println!();

        // System overview
        println!("┌─ System Overview ───────────────────────────────────────────────┐");
        println!(
            "│ Agents:  {} total, {} active",
            snapshot.system.total_agents, snapshot.system.active_agents
        );
        println!("│ CPU:     {:.1}%", snapshot.system.total_cpu_percent);
        println!(
            "│ Memory:  {:.1} / {:.1} MB ({:.1}%)",
            snapshot.system.total_memory_used_mb,
            snapshot.system.total_memory_mb,
            (snapshot.system.total_memory_used_mb / snapshot.system.total_memory_mb) * 100.0
        );
        println!("└─────────────────────────────────────────────────────────────────┘");
        println!();

        // Top agents
        println!("┌─ Top Agents (by CPU) ───────────────────────────────────────────┐");
        for (i, agent) in snapshot.agents.iter().take(5).enumerate() {
            println!(
                "{}. {} - CPU: {:.1}%, Mem: {:.1}MB",
                i + 1,
                agent.name,
                agent.cpu_percent,
                agent.memory_mb
            );
        }
        println!("└─────────────────────────────────────────────────────────────────┘");
        println!();

        // Recent events
        println!("┌─ Recent Events ─────────────────────────────────────────────────┐");
        for event in events.iter().take(5) {
            println!(
                "{} [{}] {}",
                event.timestamp.format("%H:%M:%S"),
                event.severity,
                event.message
            );
        }
        println!("└─────────────────────────────────────────────────────────────────┘");

        println!("\nRefreshing in {}s... (Press Ctrl+C to exit)", interval);

        tokio::time::sleep(Duration::from_secs(interval)).await;
    }
}

/// Handle monitor command
pub async fn handle_monitor_command(command: MonitorCommand, format: OutputFormat) -> Result<()> {
    command.execute(format).await
}