cargo-hammerwork 1.1.0

A comprehensive cargo subcommand for managing Hammerwork job queues with advanced tooling and monitoring capabilities
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
use anyhow::Result;
use clap::Subcommand;
use sqlx::Row;
use std::time::Duration;
use tokio::time::interval;

use crate::config::Config;
use crate::utils::database::DatabasePool;

#[derive(Subcommand)]
pub enum MonitorCommand {
    #[command(about = "Real-time monitoring dashboard")]
    Dashboard {
        #[arg(short = 'u', long, help = "Database connection URL")]
        database_url: Option<String>,
        #[arg(
            short = 'i',
            long,
            default_value = "5",
            help = "Refresh interval in seconds"
        )]
        refresh: u64,
        #[arg(short = 'n', long, help = "Specific queue to monitor")]
        queue: Option<String>,
    },
    #[command(about = "Check system health")]
    Health {
        #[arg(short = 'u', long, help = "Database connection URL")]
        database_url: Option<String>,
        #[arg(long, help = "Output format (table, json)")]
        format: Option<String>,
    },
    #[command(about = "Show performance metrics")]
    Metrics {
        #[arg(short = 'u', long, help = "Database connection URL")]
        database_url: Option<String>,
        #[arg(short = 't', long, help = "Time period (1h, 24h, 7d)")]
        period: Option<String>,
        #[arg(short = 'n', long, help = "Specific queue to analyze")]
        queue: Option<String>,
    },
    #[command(about = "Tail job logs (placeholder)")]
    Logs {
        #[arg(short = 'u', long, help = "Database connection URL")]
        database_url: Option<String>,
        #[arg(short = 'l', long, help = "Number of recent log entries")]
        lines: Option<u32>,
        #[arg(short = 'n', long, help = "Filter by queue")]
        queue: Option<String>,
        #[arg(long, help = "Follow logs in real-time")]
        follow: bool,
    },
}

impl MonitorCommand {
    pub async fn execute(&self, config: &Config) -> Result<()> {
        let db_url = self.get_database_url(config)?;
        let pool = DatabasePool::connect(&db_url, config.get_connection_pool_size()).await?;

        match self {
            MonitorCommand::Dashboard { refresh, queue, .. } => {
                run_dashboard(pool, *refresh, queue.clone()).await?;
            }
            MonitorCommand::Health { format, .. } => {
                check_health(pool, format.clone()).await?;
            }
            MonitorCommand::Metrics { period, queue, .. } => {
                show_metrics(pool, period.clone(), queue.clone()).await?;
            }
            MonitorCommand::Logs {
                lines,
                queue,
                follow,
                ..
            } => {
                show_logs(pool, *lines, queue.clone(), *follow).await?;
            }
        }
        Ok(())
    }

    fn get_database_url(&self, config: &Config) -> Result<String> {
        let url_option = match self {
            MonitorCommand::Dashboard { database_url, .. } => database_url,
            MonitorCommand::Health { database_url, .. } => database_url,
            MonitorCommand::Metrics { database_url, .. } => database_url,
            MonitorCommand::Logs { database_url, .. } => database_url,
        };

        url_option
            .as_ref()
            .map(|s| s.as_str())
            .or(config.get_database_url())
            .map(|s| s.to_string())
            .ok_or_else(|| anyhow::anyhow!("Database URL is required"))
    }
}

async fn run_dashboard(pool: DatabasePool, refresh_secs: u64, queue: Option<String>) -> Result<()> {
    println!("📊 Hammerwork Dashboard");
    println!("Press Ctrl+C to exit\n");

    let mut interval = interval(Duration::from_secs(refresh_secs));

    loop {
        tokio::select! {
            _ = interval.tick() => {
                // Clear screen
                print!("\x1B[2J\x1B[1;1H");

                println!("📊 Hammerwork Dashboard - {}", chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC"));
                if let Some(q) = &queue {
                    println!("đŸŽ¯ Queue: {}", q);
                }
                println!("🔄 Refresh: {}s\n", refresh_secs);

                if let Err(e) = display_dashboard_data(&pool, &queue).await {
                    println!("❌ Error updating dashboard: {}", e);
                }

                println!("\nPress Ctrl+C to exit");
            }
            _ = tokio::signal::ctrl_c() => {
                println!("\n👋 Dashboard stopped");
                break;
            }
        }
    }

    Ok(())
}

async fn display_dashboard_data(pool: &DatabasePool, queue: &Option<String>) -> Result<()> {
    // Quick stats
    let mut stats_table = comfy_table::Table::new();
    stats_table.set_header(vec!["Status", "Count"]);

    let queue_filter = if let Some(q) = queue {
        format!(" WHERE queue_name = '{}'", q)
    } else {
        String::new()
    };

    match pool {
        DatabasePool::Postgres(pg_pool) => {
            let query = format!(
                "SELECT status, COUNT(*) as count FROM hammerwork_jobs{} GROUP BY status ORDER BY status",
                queue_filter
            );
            let rows = sqlx::query(&query).fetch_all(pg_pool).await?;

            for row in rows {
                let status: String = row.try_get("status")?;
                let count: i64 = row.try_get("count")?;

                let status_with_icon = match status.as_str() {
                    "pending" => format!("🟡 {}", status),
                    "running" => format!("đŸ”ĩ {}", status),
                    "completed" => format!("đŸŸĸ {}", status),
                    "failed" => format!("🔴 {}", status),
                    "dead" => format!("💀 {}", status),
                    _ => status,
                };

                stats_table.add_row(vec![status_with_icon, count.to_string()]);
            }
        }
        DatabasePool::MySQL(mysql_pool) => {
            let query = format!(
                "SELECT status, COUNT(*) as count FROM hammerwork_jobs{} GROUP BY status ORDER BY status",
                queue_filter
            );
            let rows = sqlx::query(&query).fetch_all(mysql_pool).await?;

            for row in rows {
                let status: String = row.try_get("status")?;
                let count: i64 = row.try_get("count")?;

                let status_with_icon = match status.as_str() {
                    "pending" => format!("🟡 {}", status),
                    "running" => format!("đŸ”ĩ {}", status),
                    "completed" => format!("đŸŸĸ {}", status),
                    "failed" => format!("🔴 {}", status),
                    "dead" => format!("💀 {}", status),
                    _ => status,
                };

                stats_table.add_row(vec![status_with_icon, count.to_string()]);
            }
        }
    }

    println!("📈 Job Status Overview");
    println!("{}", stats_table);

    // Recent activity (last 10 jobs)
    let recent_query = format!(
        "SELECT id, queue_name, status, priority, created_at FROM hammerwork_jobs{} ORDER BY created_at DESC LIMIT 10",
        queue_filter
    );

    let mut recent_table = comfy_table::Table::new();
    recent_table.set_header(vec!["ID", "Queue", "Status", "Priority", "Created"]);

    match pool {
        DatabasePool::Postgres(pg_pool) => {
            let rows = sqlx::query(&recent_query).fetch_all(pg_pool).await?;
            for row in rows {
                let id: uuid::Uuid = row.try_get("id")?;
                let queue_name: String = row.try_get("queue_name")?;
                let status: String = row.try_get("status")?;
                let priority: String = row.try_get("priority")?;
                let created_at: chrono::DateTime<chrono::Utc> = row.try_get("created_at")?;

                recent_table.add_row(vec![
                    &id.to_string()[..8],
                    &queue_name,
                    &status,
                    &priority,
                    &created_at.format("%H:%M:%S").to_string(),
                ]);
            }
        }
        DatabasePool::MySQL(mysql_pool) => {
            let rows = sqlx::query(&recent_query).fetch_all(mysql_pool).await?;
            for row in rows {
                let id: String = row.try_get("id")?;
                let queue_name: String = row.try_get("queue_name")?;
                let status: String = row.try_get("status")?;
                let priority: String = row.try_get("priority")?;
                let created_at: chrono::DateTime<chrono::Utc> = row.try_get("created_at")?;

                recent_table.add_row(vec![
                    &id[..8],
                    &queue_name,
                    &status,
                    &priority,
                    &created_at.format("%H:%M:%S").to_string(),
                ]);
            }
        }
    }

    println!("\n🕐 Recent Activity");
    println!("{}", recent_table);

    Ok(())
}

async fn check_health(pool: DatabasePool, format: Option<String>) -> Result<()> {
    let mut health_data: Vec<(&str, &str, String)> = Vec::new();

    // Check database connectivity
    let db_status: (&str, &str, String) = match pool {
        DatabasePool::Postgres(ref pg_pool) => {
            match sqlx::query("SELECT 1").fetch_one(pg_pool).await {
                Ok(_) => ("đŸŸĸ", "Database", "Connected".to_string()),
                Err(_) => ("🔴", "Database", "Connection Failed".to_string()),
            }
        }
        DatabasePool::MySQL(ref mysql_pool) => {
            match sqlx::query("SELECT 1").fetch_one(mysql_pool).await {
                Ok(_) => ("đŸŸĸ", "Database", "Connected".to_string()),
                Err(_) => ("🔴", "Database", "Connection Failed".to_string()),
            }
        }
    };
    health_data.push(db_status);

    // Check for stuck jobs (running > 1 hour)
    let stuck_count = match pool {
        DatabasePool::Postgres(ref pg_pool) => {
            let result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE status = 'running' AND started_at < NOW() - INTERVAL '1 hour'",
            )
            .fetch_one(pg_pool)
            .await?;
            result.try_get::<i64, _>("count").unwrap_or(0)
        }
        DatabasePool::MySQL(ref mysql_pool) => {
            let result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE status = 'running' AND started_at < DATE_SUB(NOW(), INTERVAL 1 HOUR)",
            )
            .fetch_one(mysql_pool)
            .await?;
            result.try_get::<i64, _>("count").unwrap_or(0)
        }
    };

    let stuck_status = if stuck_count == 0 {
        ("đŸŸĸ", "Stuck Jobs", "None".to_string())
    } else if stuck_count < 5 {
        ("🟡", "Stuck Jobs", format!("{} jobs", stuck_count))
    } else {
        ("🔴", "Stuck Jobs", format!("{} jobs", stuck_count))
    };
    health_data.push(stuck_status);

    // Check failure rate in last hour
    let (total_recent, failed_recent) = match pool {
        DatabasePool::Postgres(ref pg_pool) => {
            let total_result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE created_at > NOW() - INTERVAL '1 hour'",
            )
            .fetch_one(pg_pool)
            .await?;

            let failed_result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE status = 'failed' AND failed_at > NOW() - INTERVAL '1 hour'",
            )
            .fetch_one(pg_pool)
            .await?;

            (
                total_result.try_get::<i64, _>("count").unwrap_or(0),
                failed_result.try_get::<i64, _>("count").unwrap_or(0),
            )
        }
        DatabasePool::MySQL(ref mysql_pool) => {
            let total_result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
            )
            .fetch_one(mysql_pool)
            .await?;

            let failed_result = sqlx::query(
                "SELECT COUNT(*) as count FROM hammerwork_jobs 
                 WHERE status = 'failed' AND failed_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
            )
            .fetch_one(mysql_pool)
            .await?;

            (
                total_result.try_get::<i64, _>("count").unwrap_or(0),
                failed_result.try_get::<i64, _>("count").unwrap_or(0),
            )
        }
    };

    let failure_rate = if total_recent > 0 {
        (failed_recent as f64 / total_recent as f64) * 100.0
    } else {
        0.0
    };

    let failure_status = if failure_rate < 5.0 {
        ("đŸŸĸ", "Failure Rate", format!("{:.1}%", failure_rate))
    } else if failure_rate < 15.0 {
        ("🟡", "Failure Rate", format!("{:.1}%", failure_rate))
    } else {
        ("🔴", "Failure Rate", format!("{:.1}%", failure_rate))
    };
    health_data.push(failure_status);

    // Output results
    match format.as_deref() {
        Some("json") => {
            let json_health = serde_json::json!({
                "timestamp": chrono::Utc::now().to_rfc3339(),
                "checks": health_data.iter().map(|(status, check, value)| {
                    serde_json::json!({
                        "check": check,
                        "status": if status.contains("đŸŸĸ") { "healthy" } else if status.contains("🟡") { "warning" } else { "critical" },
                        "value": value
                    })
                }).collect::<Vec<_>>()
            });
            println!("{}", serde_json::to_string_pretty(&json_health)?);
        }
        _ => {
            let mut table = comfy_table::Table::new();
            table.set_header(vec!["Status", "Check", "Value"]);

            for (status, check, value) in health_data {
                table.add_row(vec![status, check, &value]);
            }

            println!("đŸĨ System Health Check");
            println!("{}", table);
        }
    }

    Ok(())
}

async fn show_metrics(
    pool: DatabasePool,
    period: Option<String>,
    queue: Option<String>,
) -> Result<()> {
    let period_str = period.as_deref().unwrap_or("24h");
    let (pg_interval, mysql_interval) = match period_str {
        "1h" => ("1 hour", "1 HOUR"),
        "24h" => ("24 hours", "24 HOUR"),
        "7d" => ("7 days", "7 DAY"),
        _ => ("24 hours", "24 HOUR"),
    };

    println!("📊 Performance Metrics ({})", period_str);
    if let Some(q) = &queue {
        println!("đŸŽ¯ Queue: {}", q);
    }
    println!("═══════════════════════════════");

    let queue_filter = if let Some(q) = queue {
        format!(" AND queue_name = '{}'", q)
    } else {
        String::new()
    };

    match pool {
        DatabasePool::Postgres(pg_pool) => {
            // Throughput metrics
            let throughput_result = sqlx::query(&format!(
                "SELECT 
                    COUNT(*) as total_jobs,
                    COUNT(CASE WHEN status = 'completed' THEN 1 END) as completed_jobs,
                    COUNT(CASE WHEN status = 'failed' THEN 1 END) as failed_jobs
                 FROM hammerwork_jobs 
                 WHERE created_at > NOW() - INTERVAL '{}'{}",
                pg_interval, queue_filter
            ))
            .fetch_one(&pg_pool)
            .await?;

            let total: i64 = throughput_result.try_get("total_jobs")?;
            let completed: i64 = throughput_result.try_get("completed_jobs")?;
            let failed: i64 = throughput_result.try_get("failed_jobs")?;

            println!("📈 Throughput:");
            println!("   Total Jobs: {}", total);
            println!(
                "   Completed: {} ({:.1}%)",
                completed,
                if total > 0 {
                    (completed as f64 / total as f64) * 100.0
                } else {
                    0.0
                }
            );
            println!(
                "   Failed: {} ({:.1}%)",
                failed,
                if total > 0 {
                    (failed as f64 / total as f64) * 100.0
                } else {
                    0.0
                }
            );

            // Average processing time for completed jobs
            let avg_time_result = sqlx::query(&format!(
                "SELECT AVG(EXTRACT(EPOCH FROM (completed_at - started_at))) as avg_duration
                 FROM hammerwork_jobs 
                 WHERE status = 'completed' AND completed_at > NOW() - INTERVAL '{}'{}",
                pg_interval, queue_filter
            ))
            .fetch_one(&pg_pool)
            .await?;

            if let Ok(Some(avg_duration)) =
                avg_time_result.try_get::<Option<f64>, _>("avg_duration")
            {
                println!("   Avg Processing Time: {:.1}s", avg_duration);
            }
        }
        DatabasePool::MySQL(mysql_pool) => {
            let throughput_result = sqlx::query(&format!(
                "SELECT 
                    COUNT(*) as total_jobs,
                    COUNT(CASE WHEN status = 'completed' THEN 1 END) as completed_jobs,
                    COUNT(CASE WHEN status = 'failed' THEN 1 END) as failed_jobs
                 FROM hammerwork_jobs 
                 WHERE created_at > DATE_SUB(NOW(), INTERVAL {}){}",
                mysql_interval, queue_filter
            ))
            .fetch_one(&mysql_pool)
            .await?;

            let total: i64 = throughput_result.try_get("total_jobs")?;
            let completed: i64 = throughput_result.try_get("completed_jobs")?;
            let failed: i64 = throughput_result.try_get("failed_jobs")?;

            println!("📈 Throughput:");
            println!("   Total Jobs: {}", total);
            println!(
                "   Completed: {} ({:.1}%)",
                completed,
                if total > 0 {
                    (completed as f64 / total as f64) * 100.0
                } else {
                    0.0
                }
            );
            println!(
                "   Failed: {} ({:.1}%)",
                failed,
                if total > 0 {
                    (failed as f64 / total as f64) * 100.0
                } else {
                    0.0
                }
            );

            // Average processing time for completed jobs
            let avg_time_result = sqlx::query(&format!(
                "SELECT AVG(TIMESTAMPDIFF(SECOND, started_at, completed_at)) as avg_duration
                 FROM hammerwork_jobs 
                 WHERE status = 'completed' AND completed_at > DATE_SUB(NOW(), INTERVAL {}){}",
                mysql_interval, queue_filter
            ))
            .fetch_one(&mysql_pool)
            .await?;

            if let Ok(Some(avg_duration)) =
                avg_time_result.try_get::<Option<f64>, _>("avg_duration")
            {
                println!("   Avg Processing Time: {:.1}s", avg_duration);
            }
        }
    }

    Ok(())
}

async fn show_logs(
    _pool: DatabasePool,
    lines: Option<u32>,
    queue: Option<String>,
    follow: bool,
) -> Result<()> {
    let lines_count = lines.unwrap_or(50);

    println!("📜 Job Logs (Placeholder)");
    if let Some(q) = &queue {
        println!("đŸŽ¯ Queue: {}", q);
    }
    println!("📄 Lines: {}", lines_count);
    if follow {
        println!("đŸ‘ī¸  Following logs...");
    }
    println!("═══════════════════════════");

    // In a real implementation, this would:
    // 1. Query job events/logs from a logs table
    // 2. Show recent job state changes
    // 3. Display error messages and stack traces
    // 4. Support real-time tailing with --follow

    // Mock log entries
    let mock_logs = vec![
        (
            "2024-06-28 10:30:15",
            "INFO",
            "emails",
            "Job 12345678 started processing",
        ),
        (
            "2024-06-28 10:30:14",
            "INFO",
            "emails",
            "Job 87654321 completed successfully",
        ),
        (
            "2024-06-28 10:30:12",
            "ERROR",
            "reports",
            "Job 11111111 failed: Connection timeout",
        ),
        (
            "2024-06-28 10:30:10",
            "INFO",
            "notifications",
            "Job 22222222 enqueued",
        ),
        (
            "2024-06-28 10:30:08",
            "WARN",
            "emails",
            "Job 33333333 retry attempt 2/3",
        ),
    ];

    for (timestamp, level, log_queue, message) in mock_logs.iter().take(lines_count as usize) {
        if let Some(filter_queue) = &queue {
            if log_queue != filter_queue {
                continue;
            }
        }

        let level_icon = match *level {
            "INFO" => "â„šī¸",
            "WARN" => "âš ī¸",
            "ERROR" => "❌",
            _ => "📝",
        };

        println!(
            "{} {} [{}] {}: {}",
            timestamp, level_icon, level, log_queue, message
        );
    }

    if follow {
        println!("\nđŸ‘ī¸  Following logs (Ctrl+C to stop)...");
        println!("💡 This is a placeholder. Real implementation would stream live logs.");

        // Simulate following logs
        tokio::select! {
            _ = tokio::time::sleep(tokio::time::Duration::from_secs(5)) => {
                println!("⏰ Demo timeout - stopping log follow");
            }
            _ = tokio::signal::ctrl_c() => {
                println!("\n👋 Log following stopped");
            }
        }
    }

    Ok(())
}