|
10 | 10 | use App\Telegram\TelegramNotification; |
11 | 11 | use Filament\Notifications\Notification; |
12 | 12 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 13 | +use Illuminate\Support\Facades\Http; |
13 | 14 | use Illuminate\Support\Facades\Log; |
14 | 15 | use Illuminate\Support\Facades\Mail; |
15 | 16 |
|
@@ -60,6 +61,11 @@ public function handle(ResultCreated $event): void |
60 | 61 | if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) { |
61 | 62 | $this->telegramChannel($event); |
62 | 63 | } |
| 64 | + |
| 65 | + // Webhook notification channel |
| 66 | + if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) { |
| 67 | + $this->webhookChannel($event); |
| 68 | + } |
63 | 69 | } |
64 | 70 |
|
65 | 71 | /** |
@@ -211,4 +217,59 @@ protected function telegramChannel(ResultCreated $event): void |
211 | 217 | } |
212 | 218 | } |
213 | 219 | } |
| 220 | + |
| 221 | + /** |
| 222 | + * Handle webhook notifications. |
| 223 | + */ |
| 224 | + protected function webhookChannel(ResultCreated $event): void |
| 225 | + { |
| 226 | + $failedThresholds = []; |
| 227 | + |
| 228 | + if (! count($this->notificationSettings->webhook_urls) > 0) { |
| 229 | + Log::info('Skipping sending webhook notification, no urls.'); |
| 230 | + } |
| 231 | + |
| 232 | + // Download threshold |
| 233 | + if ($this->thresholdSettings->absolute_download > 0) { |
| 234 | + if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { |
| 235 | + array_push($failedThresholds, [ |
| 236 | + 'name' => 'Download', |
| 237 | + 'threshold' => $this->thresholdSettings->absolute_download, |
| 238 | + 'value' => toBits(convertSize($event->result->download), 2), |
| 239 | + ]); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + // Upload threshold |
| 244 | + if ($this->thresholdSettings->absolute_upload > 0) { |
| 245 | + if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { |
| 246 | + array_push($failedThresholds, [ |
| 247 | + 'name' => 'Upload', |
| 248 | + 'threshold' => $this->thresholdSettings->absolute_upload, |
| 249 | + 'value' => toBits(convertSize($event->result->upload), 2), |
| 250 | + ]); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // Ping threshold |
| 255 | + if ($this->thresholdSettings->absolute_ping > 0) { |
| 256 | + if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { |
| 257 | + array_push($failedThresholds, [ |
| 258 | + 'name' => 'Ping', |
| 259 | + 'threshold' => $this->thresholdSettings->absolute_ping, |
| 260 | + 'value' => round($event->result->ping, 2), |
| 261 | + ]); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + if (count($failedThresholds)) { |
| 266 | + foreach ($this->notificationSettings->webhook_urls as $url) { |
| 267 | + Http::post($url['url'], [ |
| 268 | + 'result_id' => $event->result->id, |
| 269 | + 'site_name' => $this->generalSettings->site_name, |
| 270 | + 'metrics' => $failedThresholds, |
| 271 | + ]); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
214 | 275 | } |
0 commit comments