Skip to content

Commit dc89746

Browse files
authored
[Feature] Sent actual data in webhook test (alexjustesen#2202)
1 parent 3013e2c commit dc89746

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

app/Actions/Notifications/SendWebhookTestNotification.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Actions\Notifications;
44

5+
use App\Models\Result;
6+
use App\Services\SpeedtestFakeResultGenerator;
57
use Filament\Notifications\Notification;
68
use Lorisleiva\Actions\Concerns\AsAction;
79
use Spatie\WebhookServer\WebhookCall;
@@ -14,17 +16,30 @@ public function handle(array $webhooks)
1416
{
1517
if (! count($webhooks)) {
1618
Notification::make()
17-
->title('You need to add webhook urls!')
19+
->title('You need to add webhook URLs!')
1820
->warning()
1921
->send();
2022

2123
return;
2224
}
2325

26+
// Generate a fake Result (NOT saved to database)
27+
$fakeResult = SpeedtestFakeResultGenerator::completed();
28+
2429
foreach ($webhooks as $webhook) {
2530
WebhookCall::create()
2631
->url($webhook['url'])
27-
->payload(['message' => '👋 Testing the Webhook notification channel.'])
32+
->payload([
33+
'result_id' => fake()->uuid(),
34+
'site_name' => 'Webhook Notification Testing',
35+
'isp' => $fakeResult->data['isp'],
36+
'ping' => $fakeResult->ping,
37+
'download' => $fakeResult->download,
38+
'upload' => $fakeResult->upload,
39+
'packetLoss' => $fakeResult->data['packetLoss'],
40+
'speedtest_url' => $fakeResult->data['result']['url'],
41+
'url' => url('/admin/results'),
42+
])
2843
->doNotSign()
2944
->dispatch();
3045
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Models\Result;
6+
use Illuminate\Support\Str;
7+
8+
class SpeedtestFakeResultGenerator
9+
{
10+
public static function completed(): Result
11+
{
12+
$data = [
13+
'isp' => 'Speedtest Communications',
14+
'ping' => [
15+
'low' => 17.841,
16+
'high' => 24.077,
17+
'jitter' => 1.878,
18+
'latency' => 19.133,
19+
],
20+
'type' => 'result',
21+
'result' => [
22+
'id' => (string) Str::uuid(),
23+
'url' => 'https://docs.speedtest-tracker.dev',
24+
'persisted' => true,
25+
],
26+
'server' => [
27+
'id' => 1234,
28+
'ip' => '127.0.0.1',
29+
'host' => 'docs.speedtest-tracker.dev',
30+
'name' => 'Speedtest',
31+
'port' => 8080,
32+
'country' => 'United States',
33+
'location' => 'New York City, NY',
34+
],
35+
'upload' => [
36+
'bytes' => 124297377,
37+
'elapsed' => 9628,
38+
'latency' => [
39+
'iqm' => 341.111,
40+
'low' => 16.663,
41+
'high' => 529.86,
42+
'jitter' => 37.587,
43+
],
44+
'bandwidth' => 113750000,
45+
],
46+
'download' => [
47+
'bytes' => 230789788,
48+
'elapsed' => 14301,
49+
'latency' => [
50+
'iqm' => 104.125,
51+
'low' => 23.72,
52+
'high' => 269.563,
53+
'jitter' => 13.447,
54+
],
55+
'bandwidth' => 115625000,
56+
],
57+
'interface' => [
58+
'name' => 'eth0',
59+
'isVpn' => false,
60+
'macAddr' => '00:00:00:00:00:00',
61+
'externalIp' => '127.0.0.1',
62+
'internalIp' => '127.0.0.1',
63+
],
64+
'timestamp' => now()->toIso8601String(),
65+
'packetLoss' => 11,
66+
];
67+
68+
return new Result([
69+
'ping' => $data['ping']['latency'],
70+
'ping_low' => $data['ping']['low'],
71+
'ping_high' => $data['ping']['high'],
72+
'ping_jitter' => $data['ping']['jitter'],
73+
'packet_loss' => $data['packetLoss'],
74+
'download' => $data['download']['bandwidth'],
75+
'download_bits' => $data['download']['bandwidth'],
76+
'download_bytes' => $data['download']['bytes'],
77+
'download_elapsed' => $data['download']['elapsed'],
78+
'download_latency_iqm' => $data['download']['latency']['iqm'],
79+
'download_latency_low' => $data['download']['latency']['low'],
80+
'download_latency_high' => $data['download']['latency']['high'],
81+
'download_latency_jitter' => $data['download']['latency']['jitter'],
82+
'upload' => $data['upload']['bandwidth'],
83+
'upload_bits' => $data['upload']['bandwidth'],
84+
'upload_bytes' => $data['upload']['bytes'],
85+
'upload_elapsed' => $data['upload']['elapsed'],
86+
'upload_latency_iqm' => $data['upload']['latency']['iqm'],
87+
'upload_latency_low' => $data['upload']['latency']['low'],
88+
'upload_latency_high' => $data['upload']['latency']['high'],
89+
'upload_latency_jitter' => $data['upload']['latency']['jitter'],
90+
'server_id' => $data['server']['id'],
91+
'server_ip' => $data['server']['ip'],
92+
'server_host' => $data['server']['host'],
93+
'server_name' => $data['server']['name'],
94+
'server_port' => $data['server']['port'],
95+
'server_country' => $data['server']['country'],
96+
'server_location' => $data['server']['location'],
97+
'interface_name' => $data['interface']['name'],
98+
'interface_is_vpn' => $data['interface']['isVpn'],
99+
'interface_mac_addr' => $data['interface']['macAddr'],
100+
'interface_internal_ip' => $data['interface']['internalIp'],
101+
'ip_address' => $data['interface']['externalIp'],
102+
'uuid' => $data['result']['id'],
103+
'result_url' => $data['result']['url'],
104+
'data' => $data,
105+
'status' => 'completed',
106+
'service' => 'faker',
107+
'scheduled' => false,
108+
]);
109+
}
110+
111+
public static function failed(): Result
112+
{
113+
$data = [
114+
'type' => 'log',
115+
'level' => 'error',
116+
'message' => 'A faked error message.',
117+
'timestamp' => now()->toIso8601String(),
118+
];
119+
120+
return new Result([
121+
'data' => $data,
122+
'status' => 'failed',
123+
'service' => 'faker',
124+
'scheduled' => false,
125+
]);
126+
}
127+
}

0 commit comments

Comments
 (0)