Skip to content

Commit

Permalink
Result created event (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Nov 10, 2022
1 parent 2bb13fa commit 67132b3
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 148 deletions.
29 changes: 29 additions & 0 deletions app/Events/ResultCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Events;

use App\Models\Result;
use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ResultCreated
{
use Dispatchable, SerializesModels;

public $result;

public $user;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Result $result)
{
$this->result = $result;

$this->user = User::find(1); // find the admin user, this isn't ideal but oh well
}
}
52 changes: 32 additions & 20 deletions app/Filament/Pages/Settings/InfluxDbPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Pages\Settings;

use App\Settings\InfluxDbSettings;
use Closure;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -39,27 +40,38 @@ protected function getFormSchema(): array
->schema([
Toggle::make('v2_enabled')
->label('Enable')
->reactive()
->columnSpan(2),
TextInput::make('v2_url')
->label('URL')
->placeholder('http://your-influxdb-instance')
->maxLength(255)
->columnSpan(['md' => 2]),
TextInput::make('v2_org')
->label('Org')
->maxLength(255)
->columnSpan(1),
TextInput::make('v2_bucket')
->placeholder('speedtest-tracker')
->label('Bucket')
->maxLength(255)
->columnSpan(1),
TextInput::make('v2_token')
->label('Token')
->maxLength(255)
->password()
->disableAutocomplete()
->columnSpan(['md' => 2]),
Grid::make([
'default' => 1,
])
->hidden(fn (Closure $get) => $get('v2_enabled') !== true)
->schema([
TextInput::make('v2_url')
->label('URL')
->placeholder('http://your-influxdb-instance')
->maxLength(255)
->required(fn (Closure $get) => $get('v2_enabled') == true)
->columnSpan(['md' => 2]),
TextInput::make('v2_org')
->label('Org')
->maxLength(255)
->required(fn (Closure $get) => $get('v2_enabled') == true)
->columnSpan(1),
TextInput::make('v2_bucket')
->placeholder('speedtest-tracker')
->label('Bucket')
->maxLength(255)
->required(fn (Closure $get) => $get('v2_enabled') == true)
->columnSpan(1),
TextInput::make('v2_token')
->label('Token')
->maxLength(255)
->password()
->required(fn (Closure $get) => $get('v2_enabled') == true)
->disableAutocomplete()
->columnSpan(['md' => 2]),
]),
])
->compact()
->columns([
Expand Down
21 changes: 15 additions & 6 deletions app/Filament/Pages/Settings/ThresholdsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,27 @@ protected function getFormSchema(): array
TextInput::make('absolute_download')
->label('Download')
->hint('Mbps')
->helperText('Leave empty to skip this metric.')
->numeric(),
->helperText('Set to zero to disable this metric.')
->default(0)
->minValue(0)
->numeric()
->required(),
TextInput::make('absolute_upload')
->label('Upload')
->hint('Mbps')
->helperText('Leave empty to skip this metric.')
->numeric(),
->helperText('Set to zero to disable this metric.')
->default(0)
->minValue(0)
->numeric()
->required(),
TextInput::make('absolute_ping')
->label('Ping')
->hint('Ms')
->helperText('Leave empty to skip this metric.')
->numeric(),
->helperText('Set to zero to disable this metric.')
->default(0)
->minValue(0)
->numeric()
->required(),
])
->columns([
'default' => 1,
Expand Down
36 changes: 36 additions & 0 deletions app/Listeners/Data/InfluxDb2Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Listeners\Data;

use App\Events\ResultCreated;
use App\Jobs\SendDataToInfluxDbV2;
use App\Settings\InfluxDbSettings;
use Illuminate\Contracts\Queue\ShouldQueue;

class InfluxDb2Listener implements ShouldQueue
{
public $influxDbSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->influxDbSettings = new (InfluxDbSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if ($this->influxDbSettings->v2_enabled) {
SendDataToInfluxDbV2::dispatch($event->result, $this->influxDbSettings);
}
}
}
42 changes: 42 additions & 0 deletions app/Listeners/SpeedtestCompletedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Listeners;

use App\Events\ResultCreated;
use App\Settings\NotificationSettings;
use Filament\Notifications\Notification;

class SpeedtestCompletedListener
{
public $notificationSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->notificationSettings = new (NotificationSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if (! $this->notificationSettings->database_enabled) {
return;
}

if ($this->notificationSettings->database_on_speedtest_run) {
Notification::make()
->title('Speedtest completed')
->success()
->sendToDatabase($event->user);
}
}
}
49 changes: 49 additions & 0 deletions app/Listeners/Threshold/AbsoluteDownloadListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Listeners\Threshold;

use App\Events\ResultCreated;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class AbsoluteDownloadListener implements ShouldQueue
{
public $notificationSettings;

public $thresholdSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->notificationSettings = new (NotificationSettings::class);

$this->thresholdSettings = new (ThresholdSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_download) {
return;
}

if (formatBits(formatBytesToBits($event->result->download), 2, false) < $this->thresholdSettings->absolute_download) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the download threshold of '.$this->thresholdSettings->absolute_download.'Mbps at '.formatBits(formatBytesToBits($event->result->download), 2, false).'Mbps.')
->warning()
->sendToDatabase($event->user);
}
}
}
49 changes: 49 additions & 0 deletions app/Listeners/Threshold/AbsolutePingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Listeners\Threshold;

use App\Events\ResultCreated;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class AbsolutePingListener implements ShouldQueue
{
public $notificationSettings;

public $thresholdSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->notificationSettings = new (NotificationSettings::class);

$this->thresholdSettings = new (ThresholdSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_ping) {
return;
}

if ($event->result->ping > $this->thresholdSettings->absolute_ping) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the ping threshold of '.$this->thresholdSettings->absolute_ping.'ms at '.$event->result->ping.'ms.')
->warning()
->sendToDatabase($event->user);
}
}
}
49 changes: 49 additions & 0 deletions app/Listeners/Threshold/AbsoluteUploadListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Listeners\Threshold;

use App\Events\ResultCreated;
use App\Settings\NotificationSettings;
use App\Settings\ThresholdSettings;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class AbsoluteUploadListener implements ShouldQueue
{
public $notificationSettings;

public $thresholdSettings;

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->notificationSettings = new (NotificationSettings::class);

$this->thresholdSettings = new (ThresholdSettings::class);
}

/**
* Handle the event.
*
* @param \App\Events\ResultCreated $event
* @return void
*/
public function handle(ResultCreated $event)
{
if (! $this->thresholdSettings->absolute_enabled && ! $this->thresholdSettings->absolute_upload) {
return;
}

if (formatBits(formatBytesToBits($event->result->upload), 2, false) < $this->thresholdSettings->absolute_upload) {
Notification::make()
->title('Threshold breached')
->body('Speedtest #'.$event->result->id.' breached the upload threshold of '.$this->thresholdSettings->absolute_upload.'Mbps at '.formatBits(formatBytesToBits($event->result->upload), 2, false).'Mbps.')
->warning()
->sendToDatabase($event->user);
}
}
}
10 changes: 10 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Events\ResultCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -44,6 +45,15 @@ class Result extends Model
'created_at' => 'datetime',
];

/**
* Event mapping for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => ResultCreated::class,
];

/**
* The attributes to be passed to influxdb
*/
Expand Down
Loading

0 comments on commit 67132b3

Please sign in to comment.