1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\ServerMonitor\Models\Host; |
7
|
|
|
use Spatie\ServerMonitor\Models\Check; |
8
|
|
|
|
9
|
|
|
class ListChecks extends BaseCommand |
10
|
|
|
{ |
11
|
|
|
protected $signature = 'server-monitor:list-checks |
12
|
|
|
{--host= : Only show checks for certain host} |
13
|
|
|
{--check= : Only show certain check type}'; |
14
|
|
|
|
15
|
|
|
protected $description = 'List all checks for host(s)'; |
16
|
|
|
|
17
|
|
|
public function handle() |
18
|
|
|
{ |
19
|
|
|
if (Host::count() === 0) { |
20
|
|
|
return $this->info('There are no hosts configured'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->unhealthyChecks(); |
24
|
|
|
|
25
|
|
|
$this->healthyChecks(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
protected function unhealthyChecks() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->tableWithTitle( |
31
|
|
|
'Unhealthy checks', |
32
|
|
|
['Host', 'Check', 'Message', 'Last checked', 'Next check'], |
33
|
|
|
$this->getTableRows(Check::unhealthy()->get()) |
|
|
|
|
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
protected function healthyChecks() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this->tableWithTitle( |
40
|
|
|
'Healthy checks', |
41
|
|
|
['Host', 'Check', 'Message', 'Last checked', 'Next check'], |
42
|
|
|
$this->getTableRows(Check::healthy()->get()) |
|
|
|
|
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function tableWithTitle(string $title, array $header, array $rows) |
47
|
|
|
{ |
48
|
|
|
if (count($rows) === 0) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->info($title); |
53
|
|
|
$this->info('================'); |
54
|
|
|
$this->table($header, $rows); |
55
|
|
|
$this->comment(''); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getTableRows(Collection $checks): array |
59
|
|
|
{ |
60
|
|
View Code Duplication |
if ($hostName = $this->option('host')) { |
|
|
|
|
61
|
|
|
$checks = $checks->filter(function (Check $check) use ($hostName) { |
62
|
|
|
return $check->host->name === $hostName; |
|
|
|
|
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if ($checkType = $this->option('check')) { |
|
|
|
|
67
|
|
|
$checks = $checks->filter(function (Check $check) use ($checkType) { |
68
|
|
|
return $check->type === $checkType; |
|
|
|
|
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $checks |
73
|
|
|
->map(function (Check $check) { |
74
|
|
|
return [ |
75
|
|
|
'name' => $check->host->name, |
|
|
|
|
76
|
|
|
'check' => $check->type, |
|
|
|
|
77
|
|
|
'message' => $check->message, |
|
|
|
|
78
|
|
|
'last_checked' => $check->getLatestRunDiffAttribute(), |
79
|
|
|
'next_check' => $check->getNextRunDiffAttribute(), |
80
|
|
|
]; |
81
|
|
|
}) |
82
|
|
|
->toArray(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.