|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
|
6
|
|
|
use Spatie\ServerMonitor\Models\Host; |
|
7
|
|
|
|
|
8
|
|
|
class ListHosts extends BaseCommand |
|
9
|
|
|
{ |
|
10
|
|
|
protected $signature = 'monitor:list |
|
11
|
|
|
{--host= : Filter hosts by name} |
|
12
|
|
|
{--check= : Filter checks by type}'; |
|
13
|
|
|
|
|
14
|
|
|
protected $description = 'List all hosts with their checks'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle() |
|
17
|
|
|
{ |
|
18
|
|
|
$hostName = $this->option('host'); |
|
19
|
|
|
$checkType = $this->option('check'); |
|
20
|
|
|
|
|
21
|
|
|
$hostsQuery = Host::query(); |
|
22
|
|
|
|
|
23
|
|
|
if ($hostName) { |
|
24
|
|
|
$hostsQuery->where('name', 'LIKE', "%{$hostName}%"); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if ($checkType) { |
|
28
|
|
|
$hostsQuery |
|
|
|
|
|
|
29
|
|
|
->whereHas('checks', function ($query) use ($checkType) { |
|
30
|
|
|
$query->where('type', 'LIKE', "%{$checkType}%"); |
|
31
|
|
|
}) |
|
32
|
|
|
->with(['checks' => function ($query) use ($checkType) { |
|
33
|
|
|
$query->where('type', 'LIKE', "%{$checkType}%"); |
|
34
|
|
|
}]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$hosts = $hostsQuery->get(); |
|
38
|
|
|
|
|
39
|
|
|
$this->renderTable($hosts); |
|
40
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function renderTable($hosts) |
|
44
|
|
|
{ |
|
45
|
|
|
$rows = $hosts->map(function ($host) { |
|
46
|
|
|
$name = $host->name; |
|
47
|
|
|
|
|
48
|
|
|
$checks = $this->formatCheckStatusCountForHost($host); |
|
49
|
|
|
|
|
50
|
|
|
$messages = $this->formatCheckMessagesForHost($host); |
|
51
|
|
|
|
|
52
|
|
|
return compact('name', 'checks', 'messages'); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
$header = ['Host', 'Checks', 'Message']; |
|
56
|
|
|
|
|
57
|
|
|
$this->table($header, $rows); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function formatCheckStatusCountForHost (Host $host): string |
|
61
|
|
|
{ |
|
62
|
|
|
$statuses = collect([CheckStatus::SUCCESS, CheckStatus::FAILED, CheckStatus::NOT_YET_CHECKED, CheckStatus::WARNING]); |
|
63
|
|
|
|
|
64
|
|
|
$checks = $statuses |
|
65
|
|
|
->map(function ($status) use ($host) { |
|
66
|
|
|
$checksWithStatus = $host->checks->where('status', $status); |
|
|
|
|
|
|
67
|
|
|
$statusCount = $checksWithStatus->count(); |
|
68
|
|
|
$emoji = $checksWithStatus->first()->statusAsEmoji ?? ''; |
|
69
|
|
|
|
|
70
|
|
|
return $statusCount ? "{$emoji} {$statusCount} " : ''; |
|
71
|
|
|
}) |
|
72
|
|
|
->implode(''); |
|
73
|
|
|
return substr($checks, 0, -2); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function formatCheckMessagesForHost(Host $host): string |
|
77
|
|
|
{ |
|
78
|
|
|
return $host->checks |
|
|
|
|
|
|
79
|
|
|
->filter(function ($check) { |
|
80
|
|
|
return !empty($check->message); |
|
81
|
|
|
}) |
|
82
|
|
|
->map(function ($check) { |
|
83
|
|
|
return "<fg=black;bg=cyan>{$check->type}</>: {$check->message}"; |
|
84
|
|
|
}) |
|
85
|
|
|
->implode("\n"); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: