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
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>async-inspect Dashboard</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
    <style>
        .status-dot {
            animation: pulse 2s ease-in-out infinite;
        }
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }
        .task-row:hover {
            background-color: #f3f4f6;
        }
        .event-log {
            font-family: 'Courier New', monospace;
            font-size: 12px;
        }
    </style>
</head>
<body class="bg-gray-100">
    <!-- Header -->
    <div class="bg-white shadow">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center py-4">
                <h1 class="text-2xl font-bold text-gray-900">
                    ⚡ async-inspect Dashboard
                </h1>
                <div class="flex items-center space-x-4">
                    <div id="connection-status" class="flex items-center">
                        <span class="status-dot h-3 w-3 rounded-full bg-green-500 mr-2"></span>
                        <span class="text-sm text-gray-600">Live</span>
                    </div>
                    <button id="pause-btn" class="px-3 py-1 text-sm bg-blue-500 text-white rounded hover:bg-blue-600">
                        Pause
                    </button>
                    <button id="clear-btn" class="px-3 py-1 text-sm bg-red-500 text-white rounded hover:bg-red-600">
                        Clear
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
        <!-- Metrics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-5 gap-4 mb-6">
            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <span class="text-3xl">📊</span>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Total Tasks</dt>
                                <dd class="text-2xl font-semibold text-gray-900" id="metric-total">0</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <span class="text-3xl">▶️</span>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Running</dt>
                                <dd class="text-2xl font-semibold text-blue-600" id="metric-running">0</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <span class="text-3xl"></span>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Completed</dt>
                                <dd class="text-2xl font-semibold text-green-600" id="metric-completed">0</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <span class="text-3xl"></span>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Failed</dt>
                                <dd class="text-2xl font-semibold text-red-600" id="metric-failed">0</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <span class="text-3xl">⏸️</span>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Blocked</dt>
                                <dd class="text-2xl font-semibold text-yellow-600" id="metric-blocked">0</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Timeline Chart -->
        <div class="bg-white shadow rounded-lg p-6 mb-6">
            <h2 class="text-lg font-semibold text-gray-900 mb-4">📈 Timeline (Last 60s)</h2>
            <canvas id="timeline-chart" height="80"></canvas>
        </div>

        <!-- Task List -->
        <div class="bg-white shadow rounded-lg p-6 mb-6">
            <div class="flex justify-between items-center mb-4">
                <h2 class="text-lg font-semibold text-gray-900">📋 Active Tasks</h2>
                <input
                    type="text"
                    id="task-search"
                    placeholder="Search tasks..."
                    class="px-3 py-1 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
                />
            </div>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">State</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Duration</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Polls</th>
                        </tr>
                    </thead>
                    <tbody id="task-table-body" class="bg-white divide-y divide-gray-200">
                        <!-- Tasks will be inserted here -->
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Event Log -->
        <div class="bg-white shadow rounded-lg p-6">
            <div class="flex justify-between items-center mb-4">
                <h2 class="text-lg font-semibold text-gray-900">📜 Event Log</h2>
                <div class="flex space-x-2">
                    <label class="flex items-center text-sm text-gray-600">
                        <input type="checkbox" id="auto-scroll" checked class="mr-2">
                        Auto-scroll
                    </label>
                </div>
            </div>
            <div id="event-log" class="event-log bg-gray-900 text-green-400 p-4 rounded h-64 overflow-y-auto">
                <div class="text-gray-500">Waiting for events...</div>
            </div>
        </div>
    </div>

    <script>
        // WebSocket connection
        let ws;
        let isPaused = false;
        let tasks = new Map();
        let taskStartTimes = new Map();
        let eventLog = [];

        // Chart setup
        const ctx = document.getElementById('timeline-chart').getContext('2d');
        const timelineChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: [],
                datasets: [
                    {
                        label: 'Running',
                        data: [],
                        borderColor: 'rgb(59, 130, 246)',
                        backgroundColor: 'rgba(59, 130, 246, 0.1)',
                        tension: 0.4
                    },
                    {
                        label: 'Blocked',
                        data: [],
                        borderColor: 'rgb(234, 179, 8)',
                        backgroundColor: 'rgba(234, 179, 8, 0.1)',
                        tension: 0.4
                    }
                ]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    x: {
                        display: true,
                        title: {
                            display: true,
                            text: 'Time'
                        }
                    },
                    y: {
                        display: true,
                        title: {
                            display: true,
                            text: 'Count'
                        },
                        beginAtZero: true
                    }
                },
                plugins: {
                    legend: {
                        display: true,
                        position: 'top'
                    }
                }
            }
        });

        // Connect to WebSocket
        function connect() {
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsUrl = `${protocol}//${window.location.host}/ws`;

            ws = new WebSocket(wsUrl);

            ws.onopen = () => {
                console.log('Connected to dashboard');
                updateConnectionStatus(true);
            };

            ws.onclose = () => {
                console.log('Disconnected from dashboard');
                updateConnectionStatus(false);
                setTimeout(connect, 3000); // Reconnect after 3 seconds
            };

            ws.onerror = (error) => {
                console.error('WebSocket error:', error);
            };

            ws.onmessage = (event) => {
                if (isPaused) return;

                try {
                    const data = JSON.parse(event.data);
                    handleEvent(data);
                } catch (e) {
                    console.error('Failed to parse event:', e);
                }
            };
        }

        // Handle dashboard events
        function handleEvent(event) {
            addToEventLog(event);

            switch (event.type) {
                case 'task_spawned':
                    tasks.set(event.task_id, {
                        id: event.task_id,
                        name: event.name,
                        state: 'Running',
                        parent: event.parent,
                        start_time: event.timestamp,
                        polls: 0
                    });
                    taskStartTimes.set(event.task_id, Date.now());
                    updateTaskTable();
                    break;

                case 'task_completed':
                    if (tasks.has(event.task_id)) {
                        tasks.get(event.task_id).state = 'Completed';
                        tasks.get(event.task_id).duration_ms = event.duration_ms;
                        updateTaskTable();
                    }
                    break;

                case 'task_failed':
                    if (tasks.has(event.task_id)) {
                        tasks.get(event.task_id).state = 'Failed';
                        updateTaskTable();
                    }
                    break;

                case 'state_changed':
                    if (tasks.has(event.task_id)) {
                        tasks.get(event.task_id).state = event.new_state;
                        updateTaskTable();
                    }
                    break;

                case 'metrics_snapshot':
                    updateMetrics(event);
                    updateTimeline(event);
                    break;

                case 'await_started':
                    if (tasks.has(event.task_id)) {
                        tasks.get(event.task_id).state = 'Blocked';
                        updateTaskTable();
                    }
                    break;

                case 'await_ended':
                    if (tasks.has(event.task_id)) {
                        tasks.get(event.task_id).state = 'Running';
                        updateTaskTable();
                    }
                    break;
            }
        }

        // Update metrics cards
        function updateMetrics(snapshot) {
            document.getElementById('metric-total').textContent = snapshot.total_tasks;
            document.getElementById('metric-running').textContent = snapshot.running_tasks;
            document.getElementById('metric-completed').textContent = snapshot.completed_tasks;
            document.getElementById('metric-failed').textContent = snapshot.failed_tasks;
            document.getElementById('metric-blocked').textContent = snapshot.blocked_tasks;
        }

        // Update timeline chart
        function updateTimeline(snapshot) {
            const now = new Date();
            const timeLabel = now.toLocaleTimeString();

            timelineChart.data.labels.push(timeLabel);
            timelineChart.data.datasets[0].data.push(snapshot.running_tasks);
            timelineChart.data.datasets[1].data.push(snapshot.blocked_tasks);

            // Keep only last 60 data points
            if (timelineChart.data.labels.length > 60) {
                timelineChart.data.labels.shift();
                timelineChart.data.datasets[0].data.shift();
                timelineChart.data.datasets[1].data.shift();
            }

            timelineChart.update('none'); // Update without animation
        }

        // Update task table
        function updateTaskTable() {
            const tbody = document.getElementById('task-table-body');
            const searchTerm = document.getElementById('task-search').value.toLowerCase();

            const filteredTasks = Array.from(tasks.values())
                .filter(task => task.name.toLowerCase().includes(searchTerm))
                .sort((a, b) => b.start_time - a.start_time)
                .slice(0, 100); // Show max 100 tasks

            tbody.innerHTML = filteredTasks.map(task => {
                const duration = task.duration_ms !== undefined
                    ? `${task.duration_ms.toFixed(2)}ms`
                    : `${((Date.now() - taskStartTimes.get(task.id)) / 1000).toFixed(1)}s`;

                const stateColor = {
                    'Running': 'text-blue-600',
                    'Blocked': 'text-yellow-600',
                    'Completed': 'text-green-600',
                    'Failed': 'text-red-600',
                    'Pending': 'text-gray-600'
                }[task.state] || 'text-gray-600';

                return `
                    <tr class="task-row">
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${task.id}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${task.name}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm ${stateColor} font-medium">${task.state}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${duration}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${task.polls}</td>
                    </tr>
                `;
            }).join('');
        }

        // Add event to log
        function addToEventLog(event) {
            const log = document.getElementById('event-log');
            const timestamp = new Date().toLocaleTimeString() + '.' + String(Date.now() % 1000).padStart(3, '0');

            let message = `[${timestamp}] ${event.type}: `;

            switch (event.type) {
                case 'task_spawned':
                    message += `${event.name} (id=${event.task_id})`;
                    break;
                case 'task_completed':
                    message += `id=${event.task_id}, duration=${event.duration_ms.toFixed(2)}ms`;
                    break;
                case 'task_failed':
                    message += `id=${event.task_id}${event.error ? ', error=' + event.error : ''}`;
                    break;
                case 'state_changed':
                    message += `id=${event.task_id}, ${event.old_state}  ${event.new_state}`;
                    break;
                case 'metrics_snapshot':
                    message += `tasks=${event.total_tasks}, running=${event.running_tasks}`;
                    break;
                default:
                    message += JSON.stringify(event);
            }

            const entry = document.createElement('div');
            entry.textContent = message;
            entry.className = 'text-xs mb-1';

            log.appendChild(entry);

            // Auto-scroll
            if (document.getElementById('auto-scroll').checked) {
                log.scrollTop = log.scrollHeight;
            }

            // Keep only last 1000 entries
            while (log.children.length > 1000) {
                log.removeChild(log.firstChild);
            }
        }

        // Update connection status indicator
        function updateConnectionStatus(connected) {
            const status = document.getElementById('connection-status');
            const dot = status.querySelector('.status-dot');
            const text = status.querySelector('span:last-child');

            if (connected) {
                dot.classList.remove('bg-red-500');
                dot.classList.add('bg-green-500');
                text.textContent = 'Live';
            } else {
                dot.classList.remove('bg-green-500');
                dot.classList.add('bg-red-500');
                text.textContent = 'Disconnected';
            }
        }

        // Event listeners
        document.getElementById('pause-btn').addEventListener('click', () => {
            isPaused = !isPaused;
            document.getElementById('pause-btn').textContent = isPaused ? 'Resume' : 'Pause';
            document.getElementById('pause-btn').classList.toggle('bg-blue-500');
            document.getElementById('pause-btn').classList.toggle('bg-green-500');
        });

        document.getElementById('clear-btn').addEventListener('click', () => {
            tasks.clear();
            taskStartTimes.clear();
            document.getElementById('event-log').innerHTML = '<div class="text-gray-500">Event log cleared</div>';
            updateTaskTable();
        });

        document.getElementById('task-search').addEventListener('input', updateTaskTable);

        // Connect on load
        connect();
    </script>
</body>
</html>