|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\UptimeMonitor\Exceptions\InvalidConfiguration; |
|
7
|
|
|
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus; |
|
8
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
|
9
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
|
10
|
|
|
|
|
11
|
|
|
class MonitorRepository |
|
12
|
|
|
{ |
|
13
|
|
|
public static function getAllEnabledMonitors(): Collection |
|
14
|
|
|
{ |
|
15
|
|
|
return self::query() |
|
16
|
|
|
->get() |
|
17
|
|
|
->sortByHost(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function getAllForUptimeCheck(): MonitorCollection |
|
21
|
|
|
{ |
|
22
|
|
|
$monitors = self::query() |
|
23
|
|
|
->get() |
|
24
|
|
|
->filter(function (Monitor $monitor) { |
|
25
|
|
|
return $monitor->shouldCheckUptime(); |
|
26
|
|
|
}) |
|
27
|
|
|
->sortByHost(); |
|
28
|
|
|
|
|
29
|
|
|
return new MonitorCollection($monitors); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function getAllForSslCheck(): Collection |
|
33
|
|
|
{ |
|
34
|
|
|
return self::query() |
|
35
|
|
|
->where('check_ssl_certificate', true) |
|
36
|
|
|
->get() |
|
37
|
|
|
->sortByHost(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function healthyMonitors(): Collection |
|
41
|
|
|
{ |
|
42
|
|
|
return self::query() |
|
43
|
|
|
->get() |
|
44
|
|
|
->filter(function (Monitor $monitor) { |
|
45
|
|
|
return $monitor->isHealthy(); |
|
46
|
|
|
}) |
|
47
|
|
|
->sortByHost(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function getAllFailing(): Collection |
|
51
|
|
|
{ |
|
52
|
|
|
return self::query() |
|
53
|
|
|
->where('uptime_status', UptimeStatus::DOWN) |
|
54
|
|
|
->get() |
|
55
|
|
|
->sortByHost(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function getAllWithSslProblems(): Collection |
|
59
|
|
|
{ |
|
60
|
|
|
return self::query() |
|
61
|
|
|
->where('check_ssl_certificate', true) |
|
62
|
|
|
->where('ssl_certificate_status', SslCertificateStatus::INVALID) |
|
63
|
|
|
->get() |
|
64
|
|
|
->sortByHost(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function getAllUnhealthy(): Collection |
|
68
|
|
|
{ |
|
69
|
|
|
return self::query() |
|
70
|
|
|
->get() |
|
71
|
|
|
->reject(function (Monitor $monitor) { |
|
72
|
|
|
return $monitor->isHealthy(); |
|
73
|
|
|
}) |
|
74
|
|
|
->sortByHost(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public static function getAllUnchecked(): Collection |
|
78
|
|
|
{ |
|
79
|
|
|
return self::query() |
|
80
|
|
|
->where('uptime_status', UptimeStatus::NOT_YET_CHECKED) |
|
81
|
|
|
->get() |
|
82
|
|
|
->sortByHost(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected static function query() |
|
86
|
|
|
{ |
|
87
|
|
|
$modelClass = static::determineMonitorModel(); |
|
88
|
|
|
|
|
89
|
|
|
return $modelClass::enabled(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function determineMonitorModel(): string |
|
93
|
|
|
{ |
|
94
|
|
|
$monitorModel = config('laravel-uptime-monitor.monitor_model') ?? Monitor::class; |
|
95
|
|
|
|
|
96
|
|
|
if (! is_a($monitorModel, Monitor::class, true)) { |
|
97
|
|
|
throw InvalidConfiguration::modelIsNotValid($monitorModel); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $monitorModel; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|