Skip to content

Commit 7348f06

Browse files
[Feature] Refactor InfluxDB integration + add bulk import results (⚠️ breaking change) (alexjustesen#1866)
Co-authored-by: Sven van Ginkel <[email protected]>
1 parent fb27c17 commit 7348f06

File tree

18 files changed

+714
-506
lines changed

18 files changed

+714
-506
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Actions\Influxdb\v2;
4+
5+
use App\Helpers\Bitrate;
6+
use App\Models\Result;
7+
use Illuminate\Support\Arr;
8+
use InfluxDB2\Point;
9+
use Lorisleiva\Actions\Concerns\AsAction;
10+
11+
class BuildPointData
12+
{
13+
use AsAction;
14+
15+
public function handle(Result $result): Point
16+
{
17+
$point = Point::measurement('speedtest')
18+
->addTag('app_name', config('app.name'))
19+
->time($result->created_at->timestamp ?? time());
20+
21+
// Qualitative tags
22+
$point->addTag('result_id', $result->id)
23+
->addTag('external_ip', Arr::get($result->data, 'interface.externalIp'))
24+
->addTag('id', $result->id)
25+
->addTag('isp', Arr::get($result->data, 'isp'))
26+
->addTag('service', $result->service->value)
27+
->addTag('server_id', Arr::get($result->data, 'server.id'))
28+
->addTag('server_name', Arr::get($result->data, 'server.name'))
29+
->addTag('server_country', Arr::get($result->data, 'server.country'))
30+
->addTag('server_location', Arr::get($result->data, 'server.location'))
31+
->addTag('healthy', $this->evalHealthyTag($result->healthy))
32+
->addTag('status', $result->status->value)
33+
->addTag('scheduled', $result->scheduled ? 'true' : 'false');
34+
35+
// Quantitative fields
36+
$point->addField('download', $result->download)
37+
->addField('upload', $result->upload)
38+
->addField('ping', $result->ping)
39+
->addField('download_bits', ! blank($result->download) ? Bitrate::bytesToBits($result->download) : null)
40+
->addField('upload_bits', ! blank($result->upload) ? Bitrate::bytesToBits($result->upload) : null)
41+
->addField('download_jitter', Arr::get($result->data, 'download.latency.jitter'))
42+
->addField('upload_jitter', Arr::get($result->data, 'upload.latency.jitter'))
43+
->addField('ping_jitter', Arr::get($result->data, 'ping.jitter'))
44+
->addField('download_latency_avg', Arr::get($result->data, 'download.latency.iqm'))
45+
->addField('download_latency_high', Arr::get($result->data, 'download.latency.high'))
46+
->addField('download_latency_low', Arr::get($result->data, 'download.latency.low'))
47+
->addField('upload_latency_avg', Arr::get($result->data, 'upload.latency.iqm'))
48+
->addField('upload_latency_high', Arr::get($result->data, 'upload.latency.high'))
49+
->addField('upload_latency_low', Arr::get($result->data, 'upload.latency.low'))
50+
->addField('packet_loss', Arr::get($result->data, 'packetLoss'));
51+
52+
return $point;
53+
}
54+
55+
private function evalHealthyTag(?bool $value): ?string
56+
{
57+
if (is_null($value)) {
58+
return null;
59+
}
60+
61+
return $value
62+
? 'true'
63+
: 'false';
64+
}
65+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Actions\Influxdb\v2;
4+
5+
use App\Settings\DataIntegrationSettings;
6+
use InfluxDB2\Client;
7+
use InfluxDB2\Model\WritePrecision;
8+
use Lorisleiva\Actions\Concerns\AsAction;
9+
10+
class CreateClient
11+
{
12+
use AsAction;
13+
14+
public function handle(): Client
15+
{
16+
$settings = app(DataIntegrationSettings::class);
17+
18+
return new Client([
19+
'url' => $settings->influxdb_v2_url,
20+
'token' => $settings->influxdb_v2_token,
21+
'bucket' => $settings->influxdb_v2_bucket,
22+
'org' => $settings->influxdb_v2_org,
23+
'verifySSL' => $settings->influxdb_v2_verify_ssl,
24+
'precision' => WritePrecision::S,
25+
]);
26+
}
27+
}

app/Console/Commands/TestInfluxDB.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

app/Enums/ResultService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
enum ResultService: string implements HasLabel
99
{
10+
case Faker = 'faker';
1011
case Ookla = 'ookla';
1112

1213
public function getLabel(): ?string
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace App\Filament\Pages\Settings;
4+
5+
use App\Jobs\Influxdb\v2\BulkWriteResults;
6+
use App\Jobs\Influxdb\v2\TestConnectionJob;
7+
use App\Settings\DataIntegrationSettings;
8+
use Filament\Forms;
9+
use Filament\Forms\Form;
10+
use Filament\Notifications\Notification;
11+
use Filament\Pages\SettingsPage;
12+
use Illuminate\Support\Facades\Auth;
13+
14+
class DataIntegrationPage extends SettingsPage
15+
{
16+
protected static ?string $navigationIcon = 'heroicon-o-circle-stack';
17+
18+
protected static ?string $navigationGroup = 'Settings';
19+
20+
protected static ?int $navigationSort = 2;
21+
22+
protected static ?string $title = 'Data Integration';
23+
24+
protected static ?string $navigationLabel = 'Data Integration';
25+
26+
protected static string $settings = DataIntegrationSettings::class;
27+
28+
public static function canAccess(): bool
29+
{
30+
return auth()->user()->is_admin;
31+
}
32+
33+
public static function shouldRegisterNavigation(): bool
34+
{
35+
return auth()->user()->is_admin;
36+
}
37+
38+
public function form(Form $form): Form
39+
{
40+
return $form
41+
->schema([
42+
Forms\Components\Grid::make([
43+
'default' => 1,
44+
'md' => 3,
45+
])
46+
->schema([
47+
Forms\Components\Section::make('InfluxDB v2')
48+
->description('When enabled, all new Speedtest results will also be sent to InfluxDB.')
49+
->schema([
50+
Forms\Components\Toggle::make('influxdb_v2_enabled')
51+
->label('Enable')
52+
->reactive()
53+
->columnSpanFull(),
54+
Forms\Components\Grid::make(['default' => 1, 'md' => 3])
55+
->hidden(fn (Forms\Get $get) => $get('influxdb_v2_enabled') !== true)
56+
->schema([
57+
Forms\Components\TextInput::make('influxdb_v2_url')
58+
->label('URL')
59+
->placeholder('http://your-influxdb-instance')
60+
->maxLength(255)
61+
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
62+
->columnSpan(['md' => 1]),
63+
Forms\Components\TextInput::make('influxdb_v2_org')
64+
->label('Org')
65+
->maxLength(255)
66+
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
67+
->columnSpan(['md' => 1]),
68+
Forms\Components\TextInput::make('influxdb_v2_bucket')
69+
->placeholder('speedtest-tracker')
70+
->label('Bucket')
71+
->maxLength(255)
72+
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
73+
->columnSpan(['md' => 2]),
74+
Forms\Components\TextInput::make('influxdb_v2_token')
75+
->label('Token')
76+
->maxLength(255)
77+
->password()
78+
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
79+
->disableAutocomplete()
80+
->columnSpan(['md' => 2]),
81+
Forms\Components\Checkbox::make('influxdb_v2_verify_ssl')
82+
->label('Verify SSL')
83+
->columnSpanFull(),
84+
// Button to send old data to InfluxDB
85+
Forms\Components\Actions::make([
86+
Forms\Components\Actions\Action::make('Export current results')
87+
->label('Export current results')
88+
->action(function () {
89+
Notification::make()
90+
->title('Starting bulk data write to Influxdb')
91+
->info()
92+
->send();
93+
94+
BulkWriteResults::dispatch(Auth::user());
95+
})
96+
->color('primary')
97+
->icon('heroicon-o-cloud-arrow-up')
98+
->visible(fn (): bool => app(DataIntegrationSettings::class)->influxdb_v2_enabled),
99+
]),
100+
// Button to test InfluxDB connection
101+
Forms\Components\Actions::make([
102+
Forms\Components\Actions\Action::make('Test connection')
103+
->label('Test connection')
104+
->action(function () {
105+
Notification::make()
106+
->title('Sending test data to Influxdb')
107+
->info()
108+
->send();
109+
110+
TestConnectionJob::dispatch(Auth::user());
111+
})
112+
->color('primary')
113+
->icon('heroicon-o-check-circle')
114+
->visible(fn (): bool => app(DataIntegrationSettings::class)->influxdb_v2_enabled),
115+
]),
116+
]),
117+
])
118+
->compact()
119+
->columns([
120+
'default' => 1,
121+
'md' => 2,
122+
]),
123+
]),
124+
]);
125+
}
126+
}

app/Filament/Pages/Settings/InfluxDbPage.php

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)