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
37 changes: 37 additions & 0 deletions app/Actions/Notifications/SendHealthCheckTestNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions\Notifications;

use Filament\Notifications\Notification;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\WebhookServer\WebhookCall;

class SendHealthCheckTestNotification
{
use AsAction;

public function handle(array $webhooks)
{
if (! count($webhooks)) {
Notification::make()
->title('You need to add HealthCheck.io urls!')
->warning()
->send();

return;
}

foreach ($webhooks as $webhook) {
WebhookCall::create()
->url($webhook['url'])
->payload(['message' => 'πŸ‘‹ Testing the HealthCheck.io notification channel.'])
->doNotSign()
->dispatch();
}

Notification::make()
->title('Test HealthCheck.io notification sent.')
->success()
->send();
}
}
46 changes: 46 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\Actions\Notifications\SendDatabaseTestNotification;
use App\Actions\Notifications\SendDiscordTestNotification;
use App\Actions\Notifications\SendGotifyTestNotification;
use App\Actions\Notifications\SendHealthCheckTestNotification;
use App\Actions\Notifications\SendMailTestNotification;
use App\Actions\Notifications\SendSlackTestNotification;
use App\Actions\Notifications\SendTelegramTestNotification;
Expand Down Expand Up @@ -261,6 +262,51 @@ public function form(Form $form): Form
'md' => 2,
]),

Forms\Components\Section::make('Healthcheck.io')
->schema([
Forms\Components\Toggle::make('healthcheck_enabled')
->label('Enable healthcheck.io webhook notifications')
->reactive()
->columnSpanFull(),
Forms\Components\Grid::make([
'default' => 1,
])
->hidden(fn (Forms\Get $get) => $get('healthcheck_enabled') !== true)
->schema([
Forms\Components\Fieldset::make('Triggers')
->schema([
Forms\Components\Toggle::make('healthcheck_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpanFull(),
Forms\Components\Toggle::make('healthcheck_on_threshold_failure')
->label('Notify on threshold failures')
->helperText('Threshold notifications will be sent to the /fail path of the URL.')
->columnSpanFull(),
]),
Forms\Components\Repeater::make('healthcheck_webhooks')
->label('webhooks')
->schema([
Forms\Components\TextInput::make('url')
->placeholder('https://hc-ping.com/your-uuid-here')
->maxLength(2000)
->required()
->url(),
])
->columnSpanFull(),
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('test healthcheck')
->label('Test healthcheck.io webhook')
->action(fn (Forms\Get $get) => SendHealthCheckTestNotification::run(webhooks: $get('healthcheck_webhooks')))
->hidden(fn (Forms\Get $get) => ! count($get('healthcheck_webhooks'))),
]),
]),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),

Forms\Components\Section::make('Telegram')
->schema([
Forms\Components\Toggle::make('telegram_enabled')
Expand Down
50 changes: 50 additions & 0 deletions app/Listeners/HealthCheck/SendSpeedtestCompletedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Listeners\HealthCheck;

use App\Events\SpeedtestCompleted;
use App\Settings\NotificationSettings;
use Illuminate\Support\Facades\Log;
use Spatie\WebhookServer\WebhookCall;

class SendSpeedtestCompletedNotification
{
/**
* Handle the event.
*/
public function handle(SpeedtestCompleted $event): void
{
$notificationSettings = new NotificationSettings();

if (! $notificationSettings->healthcheck_enabled) {
return;
}

if (! $notificationSettings->healthcheck_on_speedtest_run) {
return;
}

if (! count($notificationSettings->healthcheck_webhooks)) {
Log::warning('healthcheck urls not found, check healthcheck notification channel settings.');

return;
}

foreach ($notificationSettings->healthcheck_webhooks as $url) {
WebhookCall::create()
->url($url['url'])
->payload([
'result_id' => $event->result->id,
'site_name' => config('app.name'),
'isp' => $event->result->isp,
'ping' => $event->result->ping,
'download' => $event->result->downloadBits,
'upload' => $event->result->uploadBits,
'packetLoss' => $event->result->packet_loss,
'url' => url('/admin/results'),
])
->doNotSign()
->dispatch();
}
}
}
125 changes: 125 additions & 0 deletions app/Listeners/HealthCheck/SendSpeedtestThresholdNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Listeners\HealthCheck;

use App\Events\SpeedtestCompleted;
use App\Helpers\Number;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Illuminate\Support\Facades\Log;
use Spatie\WebhookServer\WebhookCall;

class SendSpeedtestThresholdNotification
{
/**
* Handle the event.
*/
public function handle(SpeedtestCompleted $event): void
{
$notificationSettings = new NotificationSettings();

if (! $notificationSettings->healthcheck_enabled) {
return;
}

if (! $notificationSettings->healthcheck_on_threshold_failure) {
return;
}

if (! count($notificationSettings->healthcheck_webhooks)) {
Log::warning('HealthCheck urls not found, check healthcheck notification channel settings.');

return;
}

$thresholdSettings = new ThresholdSettings();

if (! $thresholdSettings->absolute_enabled) {
return;
}

$failed = [];

if ($thresholdSettings->absolute_download > 0) {
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings));
}

if ($thresholdSettings->absolute_upload > 0) {
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings));
}

if ($thresholdSettings->absolute_ping > 0) {
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings));
}

$failed = array_filter($failed);

if (! count($failed)) {
Log::warning('Failed healthcheck thresholds not found, won\'t send notification.');

return;
}

foreach ($notificationSettings->healthcheck_webhooks as $url) {
WebhookCall::create()
->url($url['url'].'/fail')
->payload([
'result_id' => $event->result->id,
'site_name' => config('app.name'),
'isp' => $event->result->isp,
'metrics' => $failed,
'url' => url('/admin/results'),
])
->doNotSign()
->dispatch();
}
}

/**
* Build HealthCheck notification if absolute download threshold is breached.
*/
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array
{
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
return false;
}

return [
'name' => 'Download',
'threshold' => $thresholdSettings->absolute_download.' Mbps',
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
];
}

/**
* Build Healthcheck notification if absolute upload threshold is breached.
*/
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array
{
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
return false;
}

return [
'name' => 'Upload',
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
];
}

/**
* Build Healthcheck notification if absolute ping threshold is breached.
*/
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array
{
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
return false;
}

return [
'name' => 'Ping',
'threshold' => $thresholdSettings->absolute_ping.' ms',
'value' => round($event->result->ping, 2).' ms',
];
}
}
8 changes: 8 additions & 0 deletions app/Settings/NotificationSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class NotificationSettings extends Settings

public ?array $discord_webhooks;

public bool $healthcheck_enabled;

public bool $healthcheck_on_speedtest_run;

public bool $healthcheck_on_threshold_failure;

public ?array $healthcheck_webhooks;

public bool $slack_enabled;

public bool $slack_on_speedtest_run;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('notification.healthcheck_enabled', false);
$this->migrator->add('notification.healthcheck_on_speedtest_run', false);
$this->migrator->add('notification.healthcheck_on_threshold_failure', false);
$this->migrator->add('notification.healthcheck_webhooks', null);
}
};