|
| 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 | +} |
0 commit comments