Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Refactor InfluxDB integration + add bulk import results (⚠️ breaking change) #1866

Merged
merged 17 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/Actions/Influxdb/v2/BuildPointData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Actions\Influxdb\v2;

use App\Helpers\Bitrate;
use App\Models\Result;
use Illuminate\Support\Arr;
use InfluxDB2\Point;
use Lorisleiva\Actions\Concerns\AsAction;

class BuildPointData
{
use AsAction;

public function handle(Result $result): Point
{
$point = Point::measurement('speedtest')
->addTag('app_name', config('app.name'))
->time($result->created_at->timestamp ?? time());

// Qualitative tags
$point->addTag('result_id', $result->id)
->addTag('external_ip', Arr::get($result->data, 'interface.externalIp'))
->addTag('id', $result->id)
->addTag('isp', Arr::get($result->data, 'isp'))
->addTag('service', $result->service->value)
->addTag('server_id', Arr::get($result->data, 'server.id'))
->addTag('server_name', Arr::get($result->data, 'server.name'))
->addTag('server_country', Arr::get($result->data, 'server.country'))
->addTag('server_location', Arr::get($result->data, 'server.location'))
->addTag('healthy', $this->evalHealthyTag($result->healthy))
->addTag('status', $result->status->value)
->addTag('scheduled', $result->scheduled ? 'true' : 'false');

// Quantitative fields
$point->addField('download', $result->download)
->addField('upload', $result->upload)
->addField('ping', $result->ping)
->addField('download_bits', ! blank($result->download) ? Bitrate::bytesToBits($result->download) : null)
->addField('upload_bits', ! blank($result->upload) ? Bitrate::bytesToBits($result->upload) : null)
->addField('download_jitter', Arr::get($result->data, 'download.latency.jitter'))
->addField('upload_jitter', Arr::get($result->data, 'upload.latency.jitter'))
->addField('ping_jitter', Arr::get($result->data, 'ping.jitter'))
->addField('download_latency_avg', Arr::get($result->data, 'download.latency.iqm'))
->addField('download_latency_high', Arr::get($result->data, 'download.latency.high'))
->addField('download_latency_low', Arr::get($result->data, 'download.latency.low'))
->addField('upload_latency_avg', Arr::get($result->data, 'upload.latency.iqm'))
->addField('upload_latency_high', Arr::get($result->data, 'upload.latency.high'))
->addField('upload_latency_low', Arr::get($result->data, 'upload.latency.low'))
->addField('packet_loss', Arr::get($result->data, 'packetLoss'));

return $point;
}

private function evalHealthyTag(?bool $value): ?string
{
if (is_null($value)) {
return null;
}

return $value
? 'true'
: 'false';
}
}
27 changes: 27 additions & 0 deletions app/Actions/Influxdb/v2/CreateClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Actions\Influxdb\v2;

use App\Settings\DataIntegrationSettings;
use InfluxDB2\Client;
use InfluxDB2\Model\WritePrecision;
use Lorisleiva\Actions\Concerns\AsAction;

class CreateClient
{
use AsAction;

public function handle(): Client
{
$settings = app(DataIntegrationSettings::class);

return new Client([
'url' => $settings->influxdb_v2_url,
'token' => $settings->influxdb_v2_token,
'bucket' => $settings->influxdb_v2_bucket,
'org' => $settings->influxdb_v2_org,
'verifySSL' => $settings->influxdb_v2_verify_ssl,
'precision' => WritePrecision::S,
]);
}
}
42 changes: 0 additions & 42 deletions app/Console/Commands/TestInfluxDB.php

This file was deleted.

1 change: 1 addition & 0 deletions app/Enums/ResultService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

enum ResultService: string implements HasLabel
{
case Faker = 'faker';
case Ookla = 'ookla';

public function getLabel(): ?string
Expand Down
126 changes: 126 additions & 0 deletions app/Filament/Pages/Settings/DataIntegrationPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Filament\Pages\Settings;

use App\Jobs\Influxdb\v2\BulkWriteResults;
use App\Jobs\Influxdb\v2\TestConnectionJob;
use App\Settings\DataIntegrationSettings;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\SettingsPage;
use Illuminate\Support\Facades\Auth;

class DataIntegrationPage extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-circle-stack';

protected static ?string $navigationGroup = 'Settings';

protected static ?int $navigationSort = 2;

protected static ?string $title = 'Data Integration';

protected static ?string $navigationLabel = 'Data Integration';

protected static string $settings = DataIntegrationSettings::class;

public static function canAccess(): bool
{
return auth()->user()->is_admin;
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Grid::make([
'default' => 1,
'md' => 3,
])
->schema([
Forms\Components\Section::make('InfluxDB v2')
->description('When enabled, all new Speedtest results will also be sent to InfluxDB.')
->schema([
Forms\Components\Toggle::make('influxdb_v2_enabled')
->label('Enable')
->reactive()
->columnSpanFull(),
Forms\Components\Grid::make(['default' => 1, 'md' => 3])
->hidden(fn (Forms\Get $get) => $get('influxdb_v2_enabled') !== true)
->schema([
Forms\Components\TextInput::make('influxdb_v2_url')
->label('URL')
->placeholder('http://your-influxdb-instance')
->maxLength(255)
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
->columnSpan(['md' => 1]),
Forms\Components\TextInput::make('influxdb_v2_org')
->label('Org')
->maxLength(255)
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
->columnSpan(['md' => 1]),
Forms\Components\TextInput::make('influxdb_v2_bucket')
->placeholder('speedtest-tracker')
->label('Bucket')
->maxLength(255)
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
->columnSpan(['md' => 2]),
Forms\Components\TextInput::make('influxdb_v2_token')
->label('Token')
->maxLength(255)
->password()
->required(fn (Forms\Get $get) => $get('influxdb_v2_enabled') === true)
->disableAutocomplete()
->columnSpan(['md' => 2]),
Forms\Components\Checkbox::make('influxdb_v2_verify_ssl')
->label('Verify SSL')
->columnSpanFull(),
// Button to send old data to InfluxDB
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('Export current results')
->label('Export current results')
->action(function () {
Notification::make()
->title('Starting bulk data write to Influxdb')
->info()
->send();

BulkWriteResults::dispatch(Auth::user());
})
->color('primary')
->icon('heroicon-o-cloud-arrow-up')
->visible(fn (): bool => app(DataIntegrationSettings::class)->influxdb_v2_enabled),
]),
// Button to test InfluxDB connection
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('Test connection')
->label('Test connection')
->action(function () {
Notification::make()
->title('Sending test data to Influxdb')
->info()
->send();

TestConnectionJob::dispatch(Auth::user());
})
->color('primary')
->icon('heroicon-o-check-circle')
->visible(fn (): bool => app(DataIntegrationSettings::class)->influxdb_v2_enabled),
]),
]),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
]),
]);
}
}
92 changes: 0 additions & 92 deletions app/Filament/Pages/Settings/InfluxDbPage.php

This file was deleted.

Loading