Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into boy132/oauth-settings
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/Providers/AppServiceProvider.php
  • Loading branch information
Boy132 committed Jan 15, 2025
2 parents 1c2ee31 + 885e03e commit 2b431da
Show file tree
Hide file tree
Showing 26 changed files with 246 additions and 146 deletions.
51 changes: 48 additions & 3 deletions app/Filament/Admin/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,38 @@ private function mailSettings(): array
->icon('tabler-send')
->hidden(fn (Get $get) => $get('MAIL_MAILER') === 'log')
->authorize(fn () => auth()->user()->can('update settings'))
->action(function () {
->action(function (Get $get) {
// Store original mail configuration
$originalConfig = [
'mail.default' => config('mail.default'),
'mail.mailers.smtp.host' => config('mail.mailers.smtp.host'),
'mail.mailers.smtp.port' => config('mail.mailers.smtp.port'),
'mail.mailers.smtp.username' => config('mail.mailers.smtp.username'),
'mail.mailers.smtp.password' => config('mail.mailers.smtp.password'),
'mail.mailers.smtp.encryption' => config('mail.mailers.smtp.encryption'),
'mail.from.address' => config('mail.from.address'),
'mail.from.name' => config('mail.from.name'),
'services.mailgun.domain' => config('services.mailgun.domain'),
'services.mailgun.secret' => config('services.mailgun.secret'),
'services.mailgun.endpoint' => config('services.mailgun.endpoint'),
];

try {
// Update mail configuration dynamically
config([
'mail.default' => $get('MAIL_MAILER'),
'mail.mailers.smtp.host' => $get('MAIL_HOST'),
'mail.mailers.smtp.port' => $get('MAIL_PORT'),
'mail.mailers.smtp.username' => $get('MAIL_USERNAME'),
'mail.mailers.smtp.password' => $get('MAIL_PASSWORD'),
'mail.mailers.smtp.encryption' => $get('MAIL_ENCRYPTION'),
'mail.from.address' => $get('MAIL_FROM_ADDRESS'),
'mail.from.name' => $get('MAIL_FROM_NAME'),
'services.mailgun.domain' => $get('MAILGUN_DOMAIN'),
'services.mailgun.secret' => $get('MAILGUN_SECRET'),
'services.mailgun.endpoint' => $get('MAILGUN_ENDPOINT'),
]);

MailNotification::route('mail', auth()->user()->email)
->notify(new MailTested(auth()->user()));

Expand All @@ -282,6 +312,8 @@ private function mailSettings(): array
->body($exception->getMessage())
->danger()
->send();
} finally {
config($originalConfig);
}
})
),
Expand Down Expand Up @@ -325,8 +357,21 @@ private function mailSettings(): array
ToggleButtons::make('MAIL_ENCRYPTION')
->label('Encryption')
->inline()
->options(['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None'])
->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls'))),
->options([
'tls' => 'TLS',
'ssl' => 'SSL',
'' => 'None',
])
->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls')))
->live()
->afterStateUpdated(function ($state, Set $set) {
$port = match ($state) {
'tls' => 587,
'ssl' => 465,
default => 25,
};
$set('MAIL_PORT', $port);
}),
]),
Section::make('Mailgun Configuration')
->columns()
Expand Down
13 changes: 11 additions & 2 deletions app/Filament/Admin/Resources/EggResource/Pages/CreateEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Unique;

class CreateEgg extends CreateRecord
{
Expand Down Expand Up @@ -172,8 +174,11 @@ public function form(Form $form): Form
->debounce(750)
->maxLength(255)
->columnSpanFull()
->afterStateUpdated(fn (Set $set, $state) => $set('env_variable', str($state)->trim()->snake()->upper()->toString())
)
->afterStateUpdated(fn (Set $set, $state) => $set('env_variable', str($state)->trim()->snake()->upper()->toString()))
->unique(modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('egg_id', $get('../../id')), ignoreRecord: true)
->validationMessages([
'unique' => 'A variable with this name already exists.',
])
->required(),
Textarea::make('description')->columnSpanFull(),
TextInput::make('env_variable')
Expand All @@ -183,6 +188,10 @@ public function form(Form $form): Form
->suffix('}}')
->hintIcon('tabler-code')
->hintIconTooltip(fn ($state) => "{{{$state}}}")
->unique(modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('egg_id', $get('../../id')), ignoreRecord: true)
->validationMessages([
'unique' => 'A variable with this name already exists.',
])
->required(),
TextInput::make('default_value')->maxLength(255),
Fieldset::make('User Permissions')
Expand Down
15 changes: 12 additions & 3 deletions app/Filament/Admin/Resources/EggResource/Pages/EditEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Validation\Rules\Unique;

class EditEgg extends EditRecord
{
Expand Down Expand Up @@ -163,8 +165,11 @@ public function form(Form $form): Form
->debounce(750)
->maxLength(255)
->columnSpanFull()
->afterStateUpdated(fn (Set $set, $state) => $set('env_variable', str($state)->trim()->snake()->upper()->toString())
)
->afterStateUpdated(fn (Set $set, $state) => $set('env_variable', str($state)->trim()->snake()->upper()->toString()))
->unique(modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('egg_id', $get('../../id')), ignoreRecord: true)
->validationMessages([
'unique' => 'A variable with this name already exists.',
])
->required(),
Textarea::make('description')->columnSpanFull(),
TextInput::make('env_variable')
Expand All @@ -174,6 +179,10 @@ public function form(Form $form): Form
->suffix('}}')
->hintIcon('tabler-code')
->hintIconTooltip(fn ($state) => "{{{$state}}}")
->unique(modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('egg_id', $get('../../id')), ignoreRecord: true)
->validationMessages([
'unique' => 'A variable with this name already exists.',
])
->required(),
TextInput::make('default_value')->maxLength(255),
Fieldset::make('User Permissions')
Expand Down Expand Up @@ -239,7 +248,7 @@ protected function getHeaderActions(): array
return [
DeleteAction::make()
->disabled(fn (Egg $egg): bool => $egg->servers()->count() > 0)
->label(fn (Egg $egg): string => $egg->servers()->count() <= 0 ? 'Delete' : 'In Use'),
->label(fn (Egg $egg): string => $egg->servers()->count() <= 0 ? trans('filament-actions::delete.single.label') : 'In Use'),
ExportEggAction::make(),
ImportEggAction::make(),
$this->getSaveFormAction()->formId('form'),
Expand Down
5 changes: 3 additions & 2 deletions app/Filament/Admin/Resources/NodeResource/Pages/EditNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function form(Forms\Form $form): Forms\Form
'md' => 2,
'lg' => 2,
]),
// TODO: Make purdy View::make('filament.components.node-storage-chart')->columnSpan(3),
View::make('filament.components.node-storage-chart')
->columnSpanFull(),
]),
Tab::make('Basic Settings')
->icon('tabler-server')
Expand Down Expand Up @@ -592,7 +593,7 @@ protected function getHeaderActions(): array
return [
Actions\DeleteAction::make()
->disabled(fn (Node $node) => $node->servers()->count() > 0)
->label(fn (Node $node) => $node->servers()->count() > 0 ? 'Node Has Servers' : 'Delete'),
->label(fn (Node $node) => $node->servers()->count() > 0 ? 'Node Has Servers' : trans('filament-actions::delete.single.label')),
$this->getSaveFormAction()->formId('form'),
];
}
Expand Down
21 changes: 0 additions & 21 deletions app/Filament/Admin/Resources/NodeResource/Pages/ListNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Number;

class ListNodes extends ListRecords
{
Expand All @@ -39,26 +38,6 @@ public function table(Table $table): Table
->icon('tabler-network')
->sortable()
->searchable(),
TextColumn::make('memory')
->visibleFrom('sm')
->icon('tabler-device-desktop-analytics')
->numeric()
->suffix(config('panel.use_binary_prefix') ? ' GiB' : ' GB')
->formatStateUsing(fn ($state) => Number::format($state / (config('panel.use_binary_prefix') ? 1024 : 1000), maxPrecision: 2, locale: auth()->user()->language))
->sortable(),
TextColumn::make('disk')
->visibleFrom('sm')
->icon('tabler-file')
->numeric()
->suffix(config('panel.use_binary_prefix') ? ' GiB' : ' GB')
->formatStateUsing(fn ($state) => Number::format($state / (config('panel.use_binary_prefix') ? 1024 : 1000), maxPrecision: 2, locale: auth()->user()->language))
->sortable(),
TextColumn::make('cpu')
->visibleFrom('sm')
->icon('tabler-cpu')
->numeric()
->suffix(' %')
->sortable(),
IconColumn::make('scheme')
->visibleFrom('xl')
->label('SSL')
Expand Down
15 changes: 5 additions & 10 deletions app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Carbon\Carbon;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Number;

class NodeCpuChart extends ChartWidget
Expand All @@ -15,15 +14,13 @@ class NodeCpuChart extends ChartWidget

protected static ?string $maxHeight = '300px';

public ?Model $record = null;
public Node $node;

protected function getData(): array
{
/** @var Node $node */
$node = $this->record;
$threads = $node->systemInformation()['cpu_count'] ?? 0;
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;

$cpu = collect(cache()->get("nodes.$node->id.cpu_percent"))
$cpu = collect(cache()->get("nodes.{$this->node->id}.cpu_percent"))
->slice(-10)
->map(fn ($value, $key) => [
'cpu' => Number::format($value * $threads, maxPrecision: 2),
Expand Down Expand Up @@ -72,11 +69,9 @@ protected function getOptions(): RawJs

public function getHeading(): string
{
/** @var Node $node */
$node = $this->record;
$threads = $node->systemInformation()['cpu_count'] ?? 0;
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;

$cpu = Number::format(collect(cache()->get("nodes.$node->id.cpu_percent"))->last() * $threads, maxPrecision: 2, locale: auth()->user()->language);
$cpu = Number::format(collect(cache()->get("nodes.{$this->node->id}.cpu_percent"))->last() * $threads, maxPrecision: 2, locale: auth()->user()->language);
$max = Number::format($threads * 100, locale: auth()->user()->language) . '%';

return 'CPU - ' . $cpu . '% Of ' . $max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Carbon\Carbon;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Number;

class NodeMemoryChart extends ChartWidget
Expand All @@ -15,14 +14,11 @@ class NodeMemoryChart extends ChartWidget

protected static ?string $maxHeight = '300px';

public ?Model $record = null;
public Node $node;

protected function getData(): array
{
/** @var Node $node */
$node = $this->record;

$memUsed = collect(cache()->get("nodes.$node->id.memory_used"))->slice(-10)
$memUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->slice(-10)
->map(fn ($value, $key) => [
'memory' => Number::format(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, maxPrecision: 2),
'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
Expand Down Expand Up @@ -70,10 +66,8 @@ protected function getOptions(): RawJs

public function getHeading(): string
{
/** @var Node $node */
$node = $this->record;
$latestMemoryUsed = collect(cache()->get("nodes.$node->id.memory_used"))->last();
$totalMemory = collect(cache()->get("nodes.$node->id.memory_total"))->last();
$latestMemoryUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->last();
$totalMemory = collect(cache()->get("nodes.{$this->node->id}.memory_total"))->last();

$used = config('panel.use_binary_prefix')
? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use App\Models\Node;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Number;

class NodeStorageChart extends ChartWidget
{
protected static ?string $heading = 'Storage';

protected static ?string $pollingInterval = '60s';
protected static ?string $pollingInterval = '360s';

protected static ?string $maxHeight = '300px';
protected static ?string $maxHeight = '200px';

public ?Model $record = null;
public Node $node;

protected static ?array $options = [
'scales' => [
Expand All @@ -39,30 +39,38 @@ class NodeStorageChart extends ChartWidget

protected function getData(): array
{
/** @var Node $node */
$node = $this->record;
$total = Number::format(config('panel.use_binary_prefix')
? ($this->node->statistics()['disk_total'] ?? 0) / 1024 / 1024 / 1024
: ($this->node->statistics()['disk_total'] ?? 0) / 1000 / 1000 / 1000, maxPrecision: 2);
$used = Number::format(config('panel.use_binary_prefix')
? ($this->node->statistics()['disk_used'] ?? 0) / 1024 / 1024 / 1024
: ($this->node->statistics()['disk_used'] ?? 0) / 1000 / 1000 / 1000, maxPrecision: 2);

$total = ($node->statistics()['disk_total'] ?? 0) / 1024 / 1024 / 1024;
$used = ($node->statistics()['disk_used'] ?? 0) / 1024 / 1024 / 1024;
$unused = $total - $used;

return [
'datasets' => [
[
'data' => [$used, $unused],
'backgroundColor' => [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(59, 130, 246)',
'rgb(74, 222, 128)',
'rgb(255, 205, 86)',
],
],
],
'labels' => ['Used', 'Unused'],
'locale' => auth()->user()->language ?? 'en',
];
}

protected function getType(): string
{
return 'pie';
}

public function getHeading(): string
{
return 'Storage - ' . convert_bytes_to_readable($this->node->statistics()['disk_used'] ?? 0) . ' Of ' . convert_bytes_to_readable($this->node->statistics()['disk_total'] ?? 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ public function table(Table $table): Table
->label('Suspend All Servers')
->color('warning')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->whereNot('status', ServerState::Suspended)->get() as $server) {
$suspensionService->handle($server, SuspendAction::Suspend);
}
collect($user->servers()->get())->filter(fn ($server) => !$server->isSuspended())
->each(fn ($server) => $suspensionService->handle($server, SuspendAction::Suspend));
}),
Actions\Action::make('toggleUnsuspend')
->hidden(fn () => $user->servers()->where('status', ServerState::Suspended)->count() === 0)
->label('Unsuspend All Servers')
->color('primary')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->where('status', ServerState::Suspended)->get() as $server) {
$suspensionService->handle($server, SuspendAction::Unsuspend);
}
collect($user->servers()->get())->filter(fn ($server) => $server->isSuspended())
->each(fn ($server) => $suspensionService->handle($server, SuspendAction::Unsuspend));
}),
])
->columns([
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Components/Actions/ExportEggAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp(): void
{
parent::setUp();

$this->label('Export');
$this->label(trans('filament-actions::export.modal.actions.export.label'));

$this->authorize(fn () => auth()->user()->can('export egg'));

Expand Down
Loading

0 comments on commit 2b431da

Please sign in to comment.