Skip to content

Commit 67132b3

Browse files
authored
Result created event (alexjustesen#138)
1 parent 2bb13fa commit 67132b3

File tree

13 files changed

+331
-148
lines changed

13 files changed

+331
-148
lines changed

app/Events/ResultCreated.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Result;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class ResultCreated
11+
{
12+
use Dispatchable, SerializesModels;
13+
14+
public $result;
15+
16+
public $user;
17+
18+
/**
19+
* Create a new event instance.
20+
*
21+
* @return void
22+
*/
23+
public function __construct(Result $result)
24+
{
25+
$this->result = $result;
26+
27+
$this->user = User::find(1); // find the admin user, this isn't ideal but oh well
28+
}
29+
}

app/Filament/Pages/Settings/InfluxDbPage.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Filament\Pages\Settings;
44

55
use App\Settings\InfluxDbSettings;
6+
use Closure;
67
use Filament\Forms\Components\Grid;
78
use Filament\Forms\Components\Section;
89
use Filament\Forms\Components\TextInput;
@@ -39,27 +40,38 @@ protected function getFormSchema(): array
3940
->schema([
4041
Toggle::make('v2_enabled')
4142
->label('Enable')
43+
->reactive()
4244
->columnSpan(2),
43-
TextInput::make('v2_url')
44-
->label('URL')
45-
->placeholder('http://your-influxdb-instance')
46-
->maxLength(255)
47-
->columnSpan(['md' => 2]),
48-
TextInput::make('v2_org')
49-
->label('Org')
50-
->maxLength(255)
51-
->columnSpan(1),
52-
TextInput::make('v2_bucket')
53-
->placeholder('speedtest-tracker')
54-
->label('Bucket')
55-
->maxLength(255)
56-
->columnSpan(1),
57-
TextInput::make('v2_token')
58-
->label('Token')
59-
->maxLength(255)
60-
->password()
61-
->disableAutocomplete()
62-
->columnSpan(['md' => 2]),
45+
Grid::make([
46+
'default' => 1,
47+
])
48+
->hidden(fn (Closure $get) => $get('v2_enabled') !== true)
49+
->schema([
50+
TextInput::make('v2_url')
51+
->label('URL')
52+
->placeholder('http://your-influxdb-instance')
53+
->maxLength(255)
54+
->required(fn (Closure $get) => $get('v2_enabled') == true)
55+
->columnSpan(['md' => 2]),
56+
TextInput::make('v2_org')
57+
->label('Org')
58+
->maxLength(255)
59+
->required(fn (Closure $get) => $get('v2_enabled') == true)
60+
->columnSpan(1),
61+
TextInput::make('v2_bucket')
62+
->placeholder('speedtest-tracker')
63+
->label('Bucket')
64+
->maxLength(255)
65+
->required(fn (Closure $get) => $get('v2_enabled') == true)
66+
->columnSpan(1),
67+
TextInput::make('v2_token')
68+
->label('Token')
69+
->maxLength(255)
70+
->password()
71+
->required(fn (Closure $get) => $get('v2_enabled') == true)
72+
->disableAutocomplete()
73+
->columnSpan(['md' => 2]),
74+
]),
6375
])
6476
->compact()
6577
->columns([

app/Filament/Pages/Settings/ThresholdsPage.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,27 @@ protected function getFormSchema(): array
5656
TextInput::make('absolute_download')
5757
->label('Download')
5858
->hint('Mbps')
59-
->helperText('Leave empty to skip this metric.')
60-
->numeric(),
59+
->helperText('Set to zero to disable this metric.')
60+
->default(0)
61+
->minValue(0)
62+
->numeric()
63+
->required(),
6164
TextInput::make('absolute_upload')
6265
->label('Upload')
6366
->hint('Mbps')
64-
->helperText('Leave empty to skip this metric.')
65-
->numeric(),
67+
->helperText('Set to zero to disable this metric.')
68+
->default(0)
69+
->minValue(0)
70+
->numeric()
71+
->required(),
6672
TextInput::make('absolute_ping')
6773
->label('Ping')
6874
->hint('Ms')
69-
->helperText('Leave empty to skip this metric.')
70-
->numeric(),
75+
->helperText('Set to zero to disable this metric.')
76+
->default(0)
77+
->minValue(0)
78+
->numeric()
79+
->required(),
7180
])
7281
->columns([
7382
'default' => 1,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Listeners\Data;
4+
5+
use App\Events\ResultCreated;
6+
use App\Jobs\SendDataToInfluxDbV2;
7+
use App\Settings\InfluxDbSettings;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class InfluxDb2Listener implements ShouldQueue
11+
{
12+
public $influxDbSettings;
13+
14+
/**
15+
* Create the event listener.
16+
*
17+
* @return void
18+
*/
19+
public function __construct()
20+
{
21+
$this->influxDbSettings = new (InfluxDbSettings::class);
22+
}
23+
24+
/**
25+
* Handle the event.
26+
*
27+
* @param \App\Events\ResultCreated $event
28+
* @return void
29+
*/
30+
public function handle(ResultCreated $event)
31+
{
32+
if ($this->influxDbSettings->v2_enabled) {
33+
SendDataToInfluxDbV2::dispatch($event->result, $this->influxDbSettings);
34+
}
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ResultCreated;
6+
use App\Settings\NotificationSettings;
7+
use Filament\Notifications\Notification;
8+
9+
class SpeedtestCompletedListener
10+
{
11+
public $notificationSettings;
12+
13+
/**
14+
* Create the event listener.
15+
*
16+
* @return void
17+
*/
18+
public function __construct()
19+
{
20+
$this->notificationSettings = new (NotificationSettings::class);
21+
}
22+
23+
/**
24+
* Handle the event.
25+
*
26+
* @param \App\Events\ResultCreated $event
27+
* @return void
28+
*/
29+
public function handle(ResultCreated $event)
30+
{
31+
if (! $this->notificationSettings->database_enabled) {
32+
return;
33+
}
34+
35+
if ($this->notificationSettings->database_on_speedtest_run) {
36+
Notification::make()
37+
->title('Speedtest completed')
38+
->success()
39+
->sendToDatabase($event->user);
40+
}
41+
}
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Listeners\Threshold;
4+
5+
use App\Events\ResultCreated;
6+
use App\Settings\NotificationSettings;
7+
use App\Settings\ThresholdSettings;
8+
use Filament\Notifications\Notification;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
11+
class AbsoluteDownloadListener implements ShouldQueue
12+
{
13+
public $notificationSettings;
14+
15+
public $thresholdSettings;
16+
17+
/**
18+
* Create the event listener.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
$this->notificationSettings = new (NotificationSettings::class);
25+
26+
$this->thresholdSettings = new (ThresholdSettings::class);
27+
}
28+
29+
/**
30+
* Handle the event.
31+
*
32+
* @param \App\Events\ResultCreated $event
33+
* @return void
34+
*/
35+
public function handle(ResultCreated $event)
36+
{
37+
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_download) {
38+
return;
39+
}
40+
41+
if (formatBits(formatBytesToBits($event->result->download), 2, false) < $this->thresholdSettings->absolute_download) {
42+
Notification::make()
43+
->title('Threshold breached')
44+
->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.formatBits(formatBytesToBits($event->result->download), 2, false).'Mbps.')
45+
->warning()
46+
->sendToDatabase($event->user);
47+
}
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Listeners\Threshold;
4+
5+
use App\Events\ResultCreated;
6+
use App\Settings\NotificationSettings;
7+
use App\Settings\ThresholdSettings;
8+
use Filament\Notifications\Notification;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
11+
class AbsolutePingListener implements ShouldQueue
12+
{
13+
public $notificationSettings;
14+
15+
public $thresholdSettings;
16+
17+
/**
18+
* Create the event listener.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
$this->notificationSettings = new (NotificationSettings::class);
25+
26+
$this->thresholdSettings = new (ThresholdSettings::class);
27+
}
28+
29+
/**
30+
* Handle the event.
31+
*
32+
* @param \App\Events\ResultCreated $event
33+
* @return void
34+
*/
35+
public function handle(ResultCreated $event)
36+
{
37+
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_ping) {
38+
return;
39+
}
40+
41+
if ($event->result->ping > $this->thresholdSettings->absolute_ping) {
42+
Notification::make()
43+
->title('Threshold breached')
44+
->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.')
45+
->warning()
46+
->sendToDatabase($event->user);
47+
}
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Listeners\Threshold;
4+
5+
use App\Events\ResultCreated;
6+
use App\Settings\NotificationSettings;
7+
use App\Settings\ThresholdSettings;
8+
use Filament\Notifications\Notification;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
11+
class AbsoluteUploadListener implements ShouldQueue
12+
{
13+
public $notificationSettings;
14+
15+
public $thresholdSettings;
16+
17+
/**
18+
* Create the event listener.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
$this->notificationSettings = new (NotificationSettings::class);
25+
26+
$this->thresholdSettings = new (ThresholdSettings::class);
27+
}
28+
29+
/**
30+
* Handle the event.
31+
*
32+
* @param \App\Events\ResultCreated $event
33+
* @return void
34+
*/
35+
public function handle(ResultCreated $event)
36+
{
37+
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_upload) {
38+
return;
39+
}
40+
41+
if (formatBits(formatBytesToBits($event->result->upload), 2, false) < $this->thresholdSettings->absolute_upload) {
42+
Notification::make()
43+
->title('Threshold breached')
44+
->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.formatBits(formatBytesToBits($event->result->upload), 2, false).'Mbps.')
45+
->warning()
46+
->sendToDatabase($event->user);
47+
}
48+
}
49+
}

app/Models/Result.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Events\ResultCreated;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78

@@ -44,6 +45,15 @@ class Result extends Model
4445
'created_at' => 'datetime',
4546
];
4647

48+
/**
49+
* Event mapping for the model.
50+
*
51+
* @var array
52+
*/
53+
protected $dispatchesEvents = [
54+
'created' => ResultCreated::class,
55+
];
56+
4757
/**
4858
* The attributes to be passed to influxdb
4959
*/

0 commit comments

Comments
 (0)