Skip to content

Commit 58c160e

Browse files
authored
Replace config.yml with settings UI (#58)
1 parent 195142c commit 58c160e

20 files changed

+1166
-339
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ _ide_helper.php
1616
.env.backup
1717
.phpstorm.meta.php
1818
.phpunit.result.cache
19-
config.yml
2019
auth.json
2120
docker-compose.yml
2221
Homestead.json

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/public/storage
55
/storage/*.key
66
/vendor
7-
config.yml
7+
88
.env
99
.env.backup
1010
.phpunit.result.cache

.phpstorm.meta.php

Lines changed: 66 additions & 0 deletions
Large diffs are not rendered by default.

app/Console/Commands/TestInfluxDB.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace App\Console\Commands;
44

55
use App\Models\Result;
6+
use App\Settings\InfluxDbSettings;
67
use Illuminate\Console\Command;
7-
use Illuminate\Support\Facades\File;
8-
use Symfony\Component\Yaml\Yaml;
98

109
class TestInfluxDB extends Command
1110
{
@@ -28,19 +27,15 @@ class TestInfluxDB extends Command
2827
*
2928
* @return int
3029
*/
31-
public function handle()
30+
public function handle(InfluxDbSettings $settings)
3231
{
33-
if (File::exists(base_path().'/config.yml')) {
34-
$config = Yaml::parseFile(
35-
base_path().'/config.yml'
36-
);
37-
}
38-
39-
if (File::exists('/app/config.yml')) {
40-
$config = Yaml::parseFile('/app/config.yml');
41-
}
42-
43-
$influxdb = $config['influxdb'];
32+
$influxdb = [
33+
'enabled' => $settings->v2_enabled,
34+
'url' => optional($settings)->v2_url,
35+
'org' => optional($settings)->v2_org,
36+
'bucket' => optional($settings)->v2_bucket,
37+
'token' => optional($settings)->v2_token,
38+
];
4439

4540
if ($influxdb['enabled'] == true) {
4641
$result = Result::factory()->create();

app/Filament/Pages/Dashboard.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Filament\Widgets\StatsOverview;
66
use App\Jobs\ExecSpeedtest;
7+
use App\Settings\GeneralSettings;
78
use Filament\Notifications\Notification;
89
use Filament\Pages\Actions\Action;
910
use Filament\Pages\Dashboard as BasePage;
@@ -28,9 +29,15 @@ public function getHeaderWidgets(): array
2829
];
2930
}
3031

31-
public function queueSpeedtest()
32+
public function queueSpeedtest(GeneralSettings $settings)
3233
{
33-
ExecSpeedtest::dispatch();
34+
$speedtest = [
35+
'enabled' => ! blank($settings->speedtest_schedule),
36+
'schedule' => optional($settings)->speedtest_schedule,
37+
'ookla_server_id' => optional($settings)->speedtest_server,
38+
];
39+
40+
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: false);
3441

3542
Notification::make()
3643
->title('Speedtest added to the queue')
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace App\Filament\Pages\Settings;
4+
5+
use App\Settings\GeneralSettings;
6+
use Filament\Forms\Components\Card;
7+
use Filament\Forms\Components\Grid;
8+
use Filament\Forms\Components\Section;
9+
use Filament\Forms\Components\Select;
10+
use Filament\Forms\Components\TextInput;
11+
use Filament\Forms\Components\Toggle;
12+
use Filament\Pages\SettingsPage;
13+
use Squire\Models\Timezone;
14+
15+
class General extends SettingsPage
16+
{
17+
protected static ?string $navigationIcon = 'heroicon-o-cog';
18+
19+
protected static ?string $navigationGroup = 'Settings';
20+
21+
protected static ?int $navigationSort = 1;
22+
23+
protected static string $settings = GeneralSettings::class;
24+
25+
protected function getFormSchema(): array
26+
{
27+
return [
28+
Grid::make([
29+
'default' => 1,
30+
'md' => 3,
31+
])
32+
->schema([
33+
Grid::make([
34+
'default' => 1,
35+
])
36+
->schema([
37+
Section::make('Site Settings')
38+
->collapsible()
39+
->schema([
40+
TextInput::make('site_name')
41+
->maxLength(50)
42+
->required()
43+
->columnSpan(['md' => 2]),
44+
Select::make('timezone')
45+
->options(Timezone::all()->pluck('code', 'code'))
46+
->searchable()
47+
->required()
48+
->columnSpan(1),
49+
])
50+
->columns([
51+
'default' => 1,
52+
'md' => 2,
53+
]),
54+
55+
Section::make('Speedtest Settings')
56+
->collapsible()
57+
->schema([
58+
TextInput::make('speedtest_schedule')
59+
->helperText('Leave empty to disable the schedule. You can also use the cron expression generator [HERE](https://crontab.cronhub.io/) to help you make schedules.')
60+
->nullable()
61+
->columnSpan(1),
62+
TextInput::make('speedtest_server')
63+
->helperText('Leave empty to let the system pick the best server.')
64+
->nullable()
65+
->columnSpan(1),
66+
])
67+
->columns([
68+
'default' => 1,
69+
'md' => 2,
70+
]),
71+
])
72+
->columnSpan([
73+
'md' => 2,
74+
]),
75+
76+
Card::make()
77+
->schema([
78+
Toggle::make('auth_enabled')
79+
->label('Authentication enabled')
80+
->helperText("NOTE: Authentication is currently required. It's on the roadmap to be able to disabled it though.")
81+
->disabled(),
82+
])
83+
->columnSpan([
84+
'md' => 1,
85+
]),
86+
]),
87+
];
88+
}
89+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace App\Filament\Pages\Settings;
4+
5+
use App\Settings\InfluxDbSettings;
6+
use Filament\Forms\Components\Card;
7+
use Filament\Forms\Components\Grid;
8+
use Filament\Forms\Components\Section;
9+
use Filament\Forms\Components\TextInput;
10+
use Filament\Forms\Components\Toggle;
11+
use Filament\Pages\SettingsPage;
12+
13+
class InfluxDb extends SettingsPage
14+
{
15+
protected static ?string $navigationIcon = 'heroicon-o-database';
16+
17+
protected static ?string $navigationLabel = 'InfluxDB';
18+
19+
protected static ?string $navigationGroup = 'Settings';
20+
21+
protected static ?int $navigationSort = 2;
22+
23+
protected static string $settings = InfluxDbSettings::class;
24+
25+
protected function getFormSchema(): array
26+
{
27+
return [
28+
Grid::make([
29+
'default' => 1,
30+
'md' => 3,
31+
])
32+
->schema([
33+
Grid::make([
34+
'default' => 1,
35+
])
36+
->schema([
37+
Section::make('InfluxDB v2 Settings')
38+
->collapsible()
39+
->schema([
40+
TextInput::make('v2_url')
41+
->label('URL')
42+
->placeholder('http://your-influxdb-instance')
43+
->maxLength(255)
44+
->columnSpan(['md' => 2]),
45+
TextInput::make('v2_org')
46+
->label('Org')
47+
->maxLength(255)
48+
->columnSpan(1),
49+
TextInput::make('v2_bucket')
50+
->placeholder('speedtest-tracker')
51+
->label('Bucket')
52+
->maxLength(255)
53+
->columnSpan(1),
54+
TextInput::make('v2_token')
55+
->label('Token')
56+
->maxLength(255)
57+
->password()
58+
->disableAutocomplete()
59+
->columnSpan(['md' => 2]),
60+
])
61+
->columns([
62+
'default' => 1,
63+
'md' => 2,
64+
]),
65+
])
66+
->columnSpan([
67+
'md' => 2,
68+
]),
69+
70+
Card::make()
71+
->schema([
72+
Toggle::make('v2_enabled')
73+
->label('v2 enabled')
74+
->helperText('NOTE: At this time only InfluxDB v2 is supported'),
75+
])
76+
->columnSpan([
77+
'md' => 1,
78+
]),
79+
]),
80+
];
81+
}
82+
}

app/Jobs/SearchForSpeedtests.php

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,37 @@
22

33
namespace App\Jobs;
44

5+
use App\Settings\GeneralSettings;
56
use Cron\CronExpression;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Foundation\Bus\Dispatchable;
910
use Illuminate\Queue\InteractsWithQueue;
1011
use Illuminate\Queue\SerializesModels;
11-
use Illuminate\Support\Facades\File;
12-
use Symfony\Component\Yaml\Yaml;
1312

1413
class SearchForSpeedtests implements ShouldQueue
1514
{
1615
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1716

18-
/**
19-
* Create a new job instance.
20-
*
21-
* @return void
22-
*/
23-
public function __construct()
24-
{
25-
//
26-
}
27-
2817
/**
2918
* Execute the job.
3019
*
3120
* @return void
3221
*/
33-
public function handle()
22+
public function handle(GeneralSettings $settings)
3423
{
35-
if (File::exists(base_path().'/config.yml')) {
36-
$config = Yaml::parseFile(
37-
base_path().'/config.yml'
38-
);
39-
}
40-
41-
if (File::exists('/app/config.yml')) {
42-
$config = Yaml::parseFile('/app/config.yml');
43-
}
44-
45-
$speedtest = $config['speedtest'];
46-
47-
$cron = new CronExpression($speedtest['schedule']);
48-
49-
if ($cron->isDue() && $speedtest['enabled']) {
50-
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: true);
24+
$speedtest = [
25+
'enabled' => ! blank($settings->speedtest_schedule),
26+
'schedule' => optional($settings)->speedtest_schedule,
27+
'ookla_server_id' => optional($settings)->speedtest_server,
28+
];
29+
30+
if ($speedtest['enabled']) {
31+
$cron = new CronExpression($speedtest['schedule']);
32+
33+
if ($cron->isDue()) {
34+
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: true);
35+
}
5136
}
5237
}
5338
}

app/Observers/ResultObserver.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace App\Observers;
44

55
use App\Models\Result;
6-
use Illuminate\Support\Facades\File;
6+
use App\Settings\InfluxDbSettings;
77
use Illuminate\Support\Facades\Log;
88
use InfluxDB2\Client;
9-
use Symfony\Component\Yaml\Yaml;
109

1110
class ResultObserver
1211
{
@@ -17,6 +16,13 @@ class ResultObserver
1716
*/
1817
public $afterCommit = true;
1918

19+
public $settings;
20+
21+
public function __construct(InfluxDbSettings $settings)
22+
{
23+
$this->settings = $settings;
24+
}
25+
2026
/**
2127
* Handle the Result "created" event.
2228
*
@@ -25,17 +31,13 @@ class ResultObserver
2531
*/
2632
public function created(Result $result)
2733
{
28-
if (File::exists(base_path().'/config.yml')) {
29-
$config = Yaml::parseFile(
30-
base_path().'/config.yml'
31-
);
32-
}
33-
34-
if (File::exists('/app/config.yml')) {
35-
$config = Yaml::parseFile('/app/config.yml');
36-
}
37-
38-
$influxdb = $config['influxdb'];
34+
$influxdb = [
35+
'enabled' => $this->settings->v2_enabled,
36+
'url' => optional($this->settings)->v2_url,
37+
'org' => optional($this->settings)->v2_org,
38+
'bucket' => optional($this->settings)->v2_bucket,
39+
'token' => optional($this->settings)->v2_token,
40+
];
3941

4042
if ($influxdb['enabled'] == true) {
4143
$client = new Client([

app/Providers/FilamentServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function boot()
2727
{
2828
Filament::serving(function () {
2929
Filament::registerNavigationGroups([
30+
'Settings',
3031
'Links',
3132
]);
3233

0 commit comments

Comments
 (0)