Skip to content

Commit f443e09

Browse files
committed
Started work on thresholds
1 parent 50f8c6f commit f443e09

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Filament\Notifications\Notification;
1111
use Illuminate\Support\Facades\Mail;
1212
use Spatie\WebhookServer\WebhookCall;
13+
use Illuminate\Support\Facades\Http;
1314

1415
class SpeedtestCompletedListener
1516
{
@@ -75,6 +76,26 @@ public function handle(ResultCreated $event): void
7576
}
7677
}
7778

79+
if ($this->notificationSettings->discord_enabled) {
80+
if ($this->notificationSettings->discord_on_speedtest_run && count($this->notificationSettings->discord_webhooks)) {
81+
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
82+
// Construct the payload
83+
$payload = [
84+
'content' => 'There are new speedtest results for your network.' .
85+
"\nResult ID: " . $event->result->id .
86+
"\nSite Name: " . $this->generalSettings->site_name .
87+
"\nPing: " . $event->result->ping . " ms" .
88+
"\nDownload: " . ($event->result->downloadBits / 1000000) . " Mbits" .
89+
"\nUpload: " . ($event->result->uploadBits / 1000000) . " Mbits",
90+
];
91+
92+
// Send the request using Laravel's HTTP client
93+
$response = Http::post($webhook['discord_webhook_url'], $payload);
94+
95+
}
96+
}
97+
}
98+
7899
if ($this->notificationSettings->webhook_enabled) {
79100
if ($this->notificationSettings->webhook_on_speedtest_run && count($this->notificationSettings->webhook_urls)) {
80101
foreach ($this->notificationSettings->webhook_urls as $url) {

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,55 @@ protected function telegramChannel(ResultCreated $event): void
219219
}
220220
}
221221

222+
/**
223+
* Handle Discord notifications.
224+
*/
225+
226+
protected function sendDiscordNotification(ResultCreated $event): void
227+
{
228+
if ($this->notificationSettings->discord_enabled) {
229+
$failedThresholds = []; // Initialize an array to keep track of failed thresholds
230+
231+
// Check Download threshold
232+
if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->downloadBits)) {
233+
$failedThresholds[] = 'Download';
234+
}
235+
236+
// Check Upload threshold
237+
if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->uploadBits)) {
238+
$failedThresholds[] = 'Upload';
239+
}
240+
241+
// Check Ping threshold
242+
if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
243+
$failedThresholds[] = 'Ping';
244+
}
245+
246+
// Proceed with sending notifications only if there are any failed thresholds
247+
if (count($failedThresholds) > 0) {
248+
if ($this->notificationSettings->discord_on_threshold_failure && count($this->notificationSettings->discord_webhooks)) {
249+
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
250+
// Construct the payload with the failed thresholds information
251+
$contentLines = [
252+
'There are new speedtest results for your network.',
253+
"Result ID: " . $event->result->id,
254+
"Site Name: " . $this->generalSettings->site_name
255+
];
256+
foreach ($failedThresholds as $metric) {
257+
$contentLines[] = "{$metric} threshold failed.";
258+
}
259+
$payload = [
260+
'content' => 'Testing Threshold Run',
261+
];
262+
263+
// Send the request using Laravel's HTTP client
264+
$response = Http::post($webhook['discord_webhook_url'], $payload);
265+
}
266+
}
267+
}
268+
}
269+
}
270+
222271
/**
223272
* Handle webhook notifications.
224273
*

0 commit comments

Comments
 (0)