async-inspect 0.2.0

X-ray vision for async Rust - inspect and debug async state machines
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
//! Reporting and output formatting
//!
//! This module provides utilities for displaying inspection results.

use crate::inspector::{Inspector, InspectorStats};
use crate::task::{TaskInfo, TaskState};
use crate::timeline::Event;
use std::fmt::Write as FmtWrite;

pub mod html;

/// Reporter for inspection results
pub struct Reporter {
    inspector: Inspector,
}

impl Reporter {
    /// Create a new reporter
    #[must_use]
    pub fn new(inspector: Inspector) -> Self {
        Self { inspector }
    }

    /// Create a reporter using the global inspector
    #[must_use]
    pub fn global() -> Self {
        Self::new(Inspector::global().clone())
    }

    /// Print a summary of all tasks
    pub fn print_summary(&self) {
        let stats = self.inspector.stats();
        let tasks = self.inspector.get_all_tasks();

        println!("┌─────────────────────────────────────────────────────────────┐");
        println!("│ async-inspect - Task Summary                                │");
        println!("├─────────────────────────────────────────────────────────────┤");
        println!("│                                                             │");

        self.print_stats(&stats);

        println!("│                                                             │");
        println!("├─────────────────────────────────────────────────────────────┤");
        println!("│ Tasks                                                       │");
        println!("├─────────────────────────────────────────────────────────────┤");

        if tasks.is_empty() {
            println!("│ No tasks tracked                                            │");
        } else {
            for task in &tasks {
                self.print_task_line(task);
            }
        }

        println!("└─────────────────────────────────────────────────────────────┘");
    }

    /// Print statistics
    fn print_stats(&self, stats: &InspectorStats) {
        println!(
            "│ Total Tasks:     {:>3}",
            stats.total_tasks
        );
        println!(
            "│ Active:          {:>3} (Running: {}, Blocked: {})           │",
            stats.running_tasks + stats.blocked_tasks,
            stats.running_tasks,
            stats.blocked_tasks
        );
        println!(
            "│ Completed:       {:>3}",
            stats.completed_tasks
        );
        println!(
            "│ Failed:          {:>3}",
            stats.failed_tasks
        );
        println!(
            "│ Total Events:    {:>3}",
            stats.total_events
        );
        println!(
            "│ Duration:        {:.2}s                                   │",
            stats.timeline_duration.as_secs_f64()
        );
    }

    /// Print a single task line
    fn print_task_line(&self, task: &TaskInfo) {
        let state_icon = match task.state {
            TaskState::Pending => "[PEND]",
            TaskState::Running => "[RUN] ",
            TaskState::Blocked { .. } => "[WAIT]",
            TaskState::Completed => "[OK]  ",
            TaskState::Failed => "[FAIL]",
        };

        let status = format!("{} {} {}", task.id, state_icon, task.name);
        println!("│ {status:<59} │");

        // Show additional info for blocked tasks
        if let TaskState::Blocked { ref await_point } = task.state {
            let detail = format!(
                "    └─> Waiting: {} ({:.2}s)",
                await_point,
                task.time_since_update().as_secs_f64()
            );
            println!("│ {detail:<59} │");
        }
    }

    /// Print detailed information about a specific task
    pub fn print_task_details(&self, task_id: crate::task::TaskId) {
        let Some(task) = self.inspector.get_task(task_id) else {
            println!("Task {task_id} not found");
            return;
        };

        println!("┌─────────────────────────────────────────────────────────────┐");
        println!(
            "│ Task Details: {}",
            task.id
        );
        println!("├─────────────────────────────────────────────────────────────┤");
        println!("│                                                             │");
        println!("│ Name:            {:<44}│", task.name);
        println!("│ State:           {:<44}│", task.state.to_string());
        println!(
            "│ Age:             {:.2}s{:<38}│",
            task.age().as_secs_f64(),
            ""
        );
        println!("│ Poll Count:      {:<44}│", task.poll_count);
        println!(
            "│ Total Runtime:   {:.2}s{:<38}│",
            task.total_run_time.as_secs_f64(),
            ""
        );

        if let Some(parent) = task.parent {
            println!("│ Parent:          {:<44}│", parent.to_string());
        }

        if let Some(location) = &task.location {
            println!("│ Location:        {location:<44}│");
        }

        println!("│                                                             │");
        println!("├─────────────────────────────────────────────────────────────┤");
        println!("│ Events                                                      │");
        println!("├─────────────────────────────────────────────────────────────┤");

        let events = self.inspector.get_task_events(task_id);
        if events.is_empty() {
            println!("│ No events recorded                                          │");
        } else {
            for event in events.iter().take(20) {
                let event_str = format!("{}", event.kind);
                println!("│ {event_str:<59} │");
            }

            if events.len() > 20 {
                println!(
                    "│ ... and {} more events                                    │",
                    events.len() - 20
                );
            }
        }

        println!("└─────────────────────────────────────────────────────────────┘");
    }

    /// Print timeline of all events
    pub fn print_timeline(&self) {
        let events = self.inspector.get_events();

        println!("┌─────────────────────────────────────────────────────────────┐");
        println!("│ async-inspect - Timeline                                    │");
        println!("├─────────────────────────────────────────────────────────────┤");

        if events.is_empty() {
            println!("│ No events recorded                                          │");
        } else {
            for event in events.iter().take(50) {
                self.print_event_line(event);
            }

            if events.len() > 50 {
                println!("│                                                             │");
                println!(
                    "│ ... and {} more events                                    │",
                    events.len() - 50
                );
            }
        }

        println!("└─────────────────────────────────────────────────────────────┘");
    }

    /// Print a single event line
    fn print_event_line(&self, event: &Event) {
        let time_str = format!("[{:.3}s]", event.age().as_secs_f64());
        let event_str = format!("{} {}: {}", time_str, event.task_id, event.kind);

        // Truncate if too long
        let truncated = if event_str.len() > 59 {
            format!("{}...", &event_str[..56])
        } else {
            event_str
        };

        println!("│ {truncated:<59} │");
    }

    /// Generate a text report
    #[must_use]
    pub fn generate_report(&self) -> String {
        let mut report = String::new();
        let stats = self.inspector.stats();
        let tasks = self.inspector.get_all_tasks();

        writeln!(report, "[async-inspect] Report").unwrap();
        writeln!(report, "=======================").unwrap();
        writeln!(report).unwrap();
        writeln!(report, "Statistics:").unwrap();
        writeln!(report, "  Total Tasks:     {}", stats.total_tasks).unwrap();
        writeln!(report, "  Pending:         {}", stats.pending_tasks).unwrap();
        writeln!(report, "  Running:         {}", stats.running_tasks).unwrap();
        writeln!(report, "  Blocked:         {}", stats.blocked_tasks).unwrap();
        writeln!(report, "  Completed:       {}", stats.completed_tasks).unwrap();
        writeln!(report, "  Failed:          {}", stats.failed_tasks).unwrap();
        writeln!(report, "  Total Events:    {}", stats.total_events).unwrap();
        writeln!(
            report,
            "  Duration:        {:.2}s",
            stats.timeline_duration.as_secs_f64()
        )
        .unwrap();
        writeln!(report).unwrap();

        writeln!(report, "Tasks:").unwrap();
        for task in &tasks {
            writeln!(report, "  {task}").unwrap();
        }

        report
    }

    /// Print a compact one-line summary
    pub fn print_compact_summary(&self) {
        let stats = self.inspector.stats();
        println!(
            "async-inspect: {} tasks ({} active, {} completed, {} failed) | {} events | {:.2}s",
            stats.total_tasks,
            stats.running_tasks + stats.blocked_tasks,
            stats.completed_tasks,
            stats.failed_tasks,
            stats.total_events,
            stats.timeline_duration.as_secs_f64()
        );
    }

    /// Print a Gantt-style concurrency timeline
    pub fn print_gantt_timeline(&self) {
        let tasks = self.inspector.get_all_tasks();

        if tasks.is_empty() {
            println!("No tasks to display");
            return;
        }

        // Calculate time bounds
        let start_time = tasks
            .iter()
            .map(|t| t.created_at)
            .min()
            .expect("At least one task");

        let end_time = tasks
            .iter()
            .map(|t| t.created_at + t.age())
            .max()
            .expect("At least one task");

        let total_duration = end_time.duration_since(start_time);

        // Timeline configuration
        const TIMELINE_WIDTH: usize = 50;

        println!("┌────────────────────────────────────────────────────────────────────────────┐");
        println!("│ Concurrency Timeline (Gantt View)                                         │");
        println!("├────────────────────────────────────────────────────────────────────────────┤");
        println!("│                                                                            │");

        // Print time scale
        let time_markers = self.generate_time_markers(total_duration, TIMELINE_WIDTH);
        println!("│ Time:  {time_markers}");
        println!("{}", self.generate_timeline_ruler(TIMELINE_WIDTH));
        println!("│                                                                            │");

        // Print each task as a timeline bar
        for task in &tasks {
            let task_line =
                self.generate_task_timeline(task, start_time, total_duration, TIMELINE_WIDTH);
            println!("{task_line}");
        }

        println!("│                                                                            │");
        println!("│ Legend: █ Running  ░ Blocked  ─ Waiting  ✓ Completed  ✗ Failed           │");
        println!("└────────────────────────────────────────────────────────────────────────────┘");
    }

    /// Generate time markers for the timeline
    fn generate_time_markers(&self, total_duration: std::time::Duration, width: usize) -> String {
        let mut markers = String::new();
        let millis = total_duration.as_millis();

        // Show markers at 0%, 25%, 50%, 75%, 100%
        let positions = [0, width / 4, width / 2, 3 * width / 4, width];
        let mut last_end = 0;

        for &pos in &positions {
            let time_ms = (millis as f64 * pos as f64 / width as f64) as u128;
            let marker = format!("{time_ms}ms");

            // Add spacing
            if pos > last_end {
                let spaces = pos.saturating_sub(last_end);
                markers.push_str(&" ".repeat(spaces));
            }

            markers.push_str(&marker);
            last_end = pos + marker.len();
        }

        // Pad to width
        if markers.len() < width {
            markers.push_str(&" ".repeat(width - markers.len()));
        }

        markers
    }

    /// Generate timeline ruler
    fn generate_timeline_ruler(&self, width: usize) -> String {
        let mut ruler = String::new();
        for i in 0..width {
            if i % 10 == 0 {
                ruler.push('|');
            } else if i % 5 == 0 {
                ruler.push('·');
            } else {
                ruler.push('');
            }
        }
        ruler
    }

    /// Generate a timeline bar for a single task
    fn generate_task_timeline(
        &self,
        task: &TaskInfo,
        start_time: std::time::Instant,
        total_duration: std::time::Duration,
        width: usize,
    ) -> String {
        let mut line = String::new();

        // Task name (first 12 chars)
        let name = if task.name.len() > 12 {
            format!("{:.9}...", task.name)
        } else {
            format!("{:<12}", task.name)
        };
        line.push_str(&name);
        line.push_str(": ");

        // Calculate task position and length
        let task_start = task.created_at.duration_since(start_time);
        let task_duration = task.age();

        let start_pos = ((task_start.as_millis() as f64 / total_duration.as_millis() as f64)
            * width as f64) as usize;
        let task_len = ((task_duration.as_millis() as f64 / total_duration.as_millis() as f64)
            * width as f64)
            .max(1.0) as usize;

        // Build the timeline bar
        for i in 0..width {
            if i < start_pos {
                line.push(' ');
            } else if i < start_pos + task_len {
                // Determine character based on task state
                let ch = match task.state {
                    TaskState::Running => '',
                    TaskState::Blocked { .. } => '',
                    TaskState::Completed => '',
                    TaskState::Failed => '',
                    TaskState::Pending => '',
                };
                line.push(ch);
            } else {
                line.push(' ');
            }
        }

        // Add state indicator
        let indicator = match task.state {
            TaskState::Completed => "",
            TaskState::Failed => "",
            TaskState::Running => "",
            TaskState::Blocked { .. } => "",
            TaskState::Pending => "",
        };
        line.push_str(indicator);

        // Pad to consistent width
        while line.len() < 74 {
            line.push(' ');
        }

        line
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_reporter_creation() {
        let inspector = Inspector::new();
        let reporter = Reporter::new(inspector);
        // Just verify it doesn't panic
        reporter.print_compact_summary();
    }

    #[test]
    fn test_generate_report() {
        let inspector = Inspector::new();
        inspector.register_task("test".to_string());

        let reporter = Reporter::new(inspector);
        let report = reporter.generate_report();

        assert!(report.contains("[async-inspect] Report"));
        assert!(report.contains("Total Tasks:     1"));
    }
}