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\CertificateStatus; |
8
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
9
|
|
|
use Spatie\UptimeMonitor\Models\Monitor; |
10
|
|
|
|
11
|
|
|
class MonitorRepository |
12
|
|
|
{ |
13
|
|
|
public static function getEnabled(): Collection |
14
|
|
|
{ |
15
|
|
|
return self::query() |
16
|
|
|
->get() |
17
|
|
|
->sortByHost(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function getDisabled(): Collection |
21
|
|
|
{ |
22
|
|
|
$modelClass = static::determineMonitorModel(); |
23
|
|
|
|
24
|
|
|
return $modelClass::where('uptime_check_enabled', false) |
25
|
|
|
->where('certificate_check_enabled', false) |
26
|
|
|
->get(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function getForUptimeCheck(): MonitorCollection |
30
|
|
|
{ |
31
|
|
|
$monitors = self::query() |
32
|
|
|
->get() |
33
|
|
|
->filter(function (Monitor $monitor) { |
34
|
|
|
return $monitor->shouldCheckUptime(); |
35
|
|
|
}) |
36
|
|
|
->sortByHost(); |
37
|
|
|
|
38
|
|
|
return new MonitorCollection($monitors); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function getForCertificateCheck(): Collection |
42
|
|
|
{ |
43
|
|
|
return self::query() |
44
|
|
|
->where('certificate_check_enabled', true) |
45
|
|
|
->get() |
46
|
|
|
->sortByHost(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function getHealthy(): Collection |
50
|
|
|
{ |
51
|
|
|
return self::query() |
52
|
|
|
->get() |
53
|
|
|
->filter(function (Monitor $monitor) { |
54
|
|
|
return $monitor->isHealthy(); |
55
|
|
|
}) |
56
|
|
|
->sortByHost(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function getWithFailingUptimeCheck(): Collection |
60
|
|
|
{ |
61
|
|
|
return self::query() |
62
|
|
|
->where('uptime_check_enabled', true) |
63
|
|
|
->where('uptime_status', UptimeStatus::DOWN) |
64
|
|
|
->get() |
65
|
|
|
->sortByHost(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function getWithFailingCertificateCheck(): Collection |
69
|
|
|
{ |
70
|
|
|
return self::query() |
71
|
|
|
->where('certificate_check_enabled', true) |
72
|
|
|
->where('certificate_status', CertificateStatus::INVALID) |
73
|
|
|
->get() |
74
|
|
|
->sortByHost(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function getUnhealthy(): Collection |
78
|
|
|
{ |
79
|
|
|
return self::query() |
80
|
|
|
->get() |
81
|
|
|
->reject(function (Monitor $monitor) { |
82
|
|
|
return $monitor->isHealthy(); |
83
|
|
|
}) |
84
|
|
|
->sortByHost(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function getUnchecked(): Collection |
88
|
|
|
{ |
89
|
|
|
return self::query() |
90
|
|
|
->whereColumn([ |
91
|
|
|
['uptime_check_enabled', '=', true], |
92
|
|
|
['uptime_status', '=', UptimeStatus::NOT_YET_CHECKED] |
93
|
|
|
]) |
94
|
|
|
->orWhereColumn([ |
95
|
|
|
['certificate_check_enabled', '=', true], |
96
|
|
|
['certificate_status', '=', CertificateStatus::NOT_YET_CHECKED] |
97
|
|
|
]) |
98
|
|
|
->get() |
99
|
|
|
->sortByHost(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|\Spatie\Url\Url $url |
104
|
|
|
* |
105
|
|
|
* @return \Spatie\UptimeMonitor\Models\Monitor |
106
|
|
|
*/ |
107
|
|
|
public static function findByUrl($url) |
108
|
|
|
{ |
109
|
|
|
$model = static::determineMonitorModel(); |
110
|
|
|
|
111
|
|
|
return $model::where('url', (string) $url)->first(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected static function query() |
115
|
|
|
{ |
116
|
|
|
$modelClass = static::determineMonitorModel(); |
117
|
|
|
|
118
|
|
|
return $modelClass::enabled(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected static function determineMonitorModel(): string |
122
|
|
|
{ |
123
|
|
|
$monitorModel = config('laravel-uptime-monitor.monitor_model') ?? Monitor::class; |
124
|
|
|
|
125
|
|
|
if (! is_a($monitorModel, Monitor::class, true)) { |
126
|
|
|
throw InvalidConfiguration::modelIsNotValid($monitorModel); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $monitorModel; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|