Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Filament/Pages/Settings/NotificationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Forms\Components\TestDatabaseNotification;
use App\Forms\Components\TestMailNotification;
use App\Forms\Components\TestTelegramNotification;
use App\Forms\Components\TestWebhookNotification;
use App\Mail\Test;
use App\Notifications\Telegram\TestNotification as TelegramTestNotification;
use App\Settings\NotificationSettings;
Expand Down Expand Up @@ -162,6 +163,45 @@ public function form(Form $form): Form
'default' => 1,
'md' => 2,
]),

Forms\Components\Section::make('Webhook')
->schema([
Forms\Components\Toggle::make('webhook_enabled')
->label('Enable webhook notifications')
->reactive()
->columnSpan(2),
Forms\Components\Grid::make([
'default' => 1, ])
->hidden(fn (Forms\Get $get) => $get('webhook_enabled') !== true)
->schema([
Forms\Components\Fieldset::make('Triggers')
->schema([
Forms\Components\Toggle::make('webhook_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpan(2),
Forms\Components\Toggle::make('webhook_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpan(2),
]),
]),
Forms\Components\Repeater::make('webhook_urls')
->label('Recipients')
->schema([
Forms\Components\TextInput::make('url')
->maxLength(100)
->required()
->columnSpan(['md' => 2]),
])
->hidden(fn (Forms\Get $get) => $get('webhook_enabled') !== true)
->columnSpan(['md' => 2]),
TestWebhookNotification::make('test channel')
->hidden(fn (Forms\Get $get) => $get('webhook_enabled') !== true),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
])
->columnSpan([
'md' => 2,
Expand Down
10 changes: 10 additions & 0 deletions app/Forms/Components/TestWebhookNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Field;

class TestWebhookNotification extends Field
{
protected string $view = 'forms.components.test-webhook-notification';
}
15 changes: 15 additions & 0 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Settings\NotificationSettings;
use App\Telegram\TelegramNotification;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;

class SpeedtestCompletedListener
Expand Down Expand Up @@ -73,5 +74,19 @@ public function handle(ResultCreated $event): void
}
}
}

if ($this->notificationSettings->webhook_enabled) {
if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) {
foreach ($this->notificationSettings->webhook_urls as $url) {
Http::post($url['url'], [
'result_id' => $event->result->id,
'site_name' => $this->generalSettings->site_name,
'ping' => $event->result->ping,
'download' => toBits(convertSize($event->result->download)),
'upload' => toBits(convertSize($event->result->upload)),
]);
}
}
}
}
}
61 changes: 61 additions & 0 deletions app/Listeners/Threshold/AbsoluteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Telegram\TelegramNotification;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

Expand Down Expand Up @@ -60,6 +61,11 @@ public function handle(ResultCreated $event): void
if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) {
$this->telegramChannel($event);
}

// Webhook notification channel
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
$this->webhookChannel($event);
}
}

/**
Expand Down Expand Up @@ -211,4 +217,59 @@ protected function telegramChannel(ResultCreated $event): void
}
}
}

/**
* Handle webhook notifications.
*/
protected function webhookChannel(ResultCreated $event): void
{
$failedThresholds = [];

if (! count($this->notificationSettings->webhook_urls) > 0) {
Log::info('Skipping sending webhook notification, no urls.');
}

// Download threshold
if ($this->thresholdSettings->absolute_download > 0) {
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
array_push($failedThresholds, [
'name' => 'Download',
'threshold' => $this->thresholdSettings->absolute_download,
'value' => toBits(convertSize($event->result->download), 2),
]);
}
}

// Upload threshold
if ($this->thresholdSettings->absolute_upload > 0) {
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
array_push($failedThresholds, [
'name' => 'Upload',
'threshold' => $this->thresholdSettings->absolute_upload,
'value' => toBits(convertSize($event->result->upload), 2),
]);
}
}

// Ping threshold
if ($this->thresholdSettings->absolute_ping > 0) {
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
array_push($failedThresholds, [
'name' => 'Ping',
'threshold' => $this->thresholdSettings->absolute_ping,
'value' => round($event->result->ping, 2),
]);
}
}

if (count($failedThresholds)) {
foreach ($this->notificationSettings->webhook_urls as $url) {
Http::post($url['url'], [
'result_id' => $event->result->id,
'site_name' => $this->generalSettings->site_name,
'metrics' => $failedThresholds,
]);
}
}
}
}
8 changes: 8 additions & 0 deletions app/Settings/NotificationSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class NotificationSettings extends Settings

public ?array $telegram_recipients;

public bool $webhook_enabled;

public bool $webhook_on_speedtest_run;

public bool $webhook_on_threshold_failure;

public ?array $webhook_urls;

public static function group(): string
{
return 'notification';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
/**
* Run the migrations.
*/
public function up(): void
{
$this->migrator->add('notification.webhook_enabled', false);
$this->migrator->add('notification.webhook_on_speedtest_run', false);
$this->migrator->add('notification.webhook_on_threshold_failure', false);
$this->migrator->add('notification.webhook_urls', null);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<x-filament::button wire:click="sendTestWebhookNotification()">
{{ __('Test webhook channel') }}
</x-filament::button>
</div>