mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
migrating tests (Metrics, NotificationChannels, PHP, Profile and Projects)
This commit is contained in:
@ -3,15 +3,11 @@
|
||||
namespace App\Actions\Monitoring;
|
||||
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateMetricSettings
|
||||
{
|
||||
public function update(Server $server, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$service = $server->monitoring();
|
||||
|
||||
$data = $service->handler()->data();
|
||||
@ -20,13 +16,14 @@ public function update(Server $server, array $input): void
|
||||
$service->save();
|
||||
}
|
||||
|
||||
private function validate(array $input): void
|
||||
public static function rules(): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
return [
|
||||
'data_retention' => [
|
||||
'required',
|
||||
Rule::in(config('core.metrics_data_retention')),
|
||||
'numeric',
|
||||
'min:1',
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\NotificationChannel;
|
||||
use App\Models\User;
|
||||
use Exception;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@ -11,6 +12,7 @@ class AddChannel
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function add(User $user, array $input): void
|
||||
{
|
||||
@ -23,18 +25,24 @@ public function add(User $user, array $input): void
|
||||
$channel->data = $channel->provider()->createData($input);
|
||||
$channel->save();
|
||||
|
||||
if (! $channel->provider()->connect()) {
|
||||
$channel->delete();
|
||||
try {
|
||||
if (! $channel->provider()->connect()) {
|
||||
$channel->delete();
|
||||
|
||||
if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => __('Could not connect! Make sure you configured `.env` file correctly.'),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => __('Could not connect! Make sure you configured `.env` file correctly.'),
|
||||
'provider' => __('Could not connect'),
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$channel->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'provider' => __('Could not connect'),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$channel->connected = true;
|
||||
|
@ -14,31 +14,36 @@ class MetricPolicy
|
||||
public function viewAny(User $user, Server $server): bool
|
||||
{
|
||||
return ($user->isAdmin() || $server->project->users->contains($user)) &&
|
||||
$server->service('monitoring');
|
||||
$server->service('monitoring') &&
|
||||
$server->isReady();
|
||||
}
|
||||
|
||||
public function view(User $user, Metric $metric): bool
|
||||
{
|
||||
|
||||
return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
|
||||
$metric->server->service('monitoring');
|
||||
$metric->server->service('monitoring') &&
|
||||
$metric->server->isReady();
|
||||
}
|
||||
|
||||
public function create(User $user, Server $server): bool
|
||||
{
|
||||
return ($user->isAdmin() || $server->project->users->contains($user)) &&
|
||||
$server->service('monitoring');
|
||||
$server->service('monitoring') &&
|
||||
$server->isReady();
|
||||
}
|
||||
|
||||
public function update(User $user, Metric $metric): bool
|
||||
{
|
||||
return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
|
||||
$metric->server->service('monitoring');
|
||||
$metric->server->service('monitoring') &&
|
||||
$metric->server->isReady();
|
||||
}
|
||||
|
||||
public function delete(User $user, Metric $metric): bool
|
||||
{
|
||||
return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
|
||||
$metric->server->service('monitoring');
|
||||
$metric->server->service('monitoring') &&
|
||||
$metric->server->isReady();
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,13 @@
|
||||
|
||||
namespace App\Web\Pages\Servers\Metrics;
|
||||
|
||||
use App\Actions\Monitoring\UpdateMetricSettings;
|
||||
use App\Models\Metric;
|
||||
use App\Web\Pages\Servers\Page;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
@ -26,6 +31,37 @@ public function getWidgets(): array
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
return [
|
||||
Action::make('data-retention')
|
||||
->button()
|
||||
->color('gray')
|
||||
->icon('heroicon-o-trash')
|
||||
->label('Data Retention')
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->form([
|
||||
Select::make('data_retention')
|
||||
->options([
|
||||
7 => '7 days',
|
||||
14 => '14 days',
|
||||
30 => '30 days',
|
||||
60 => '60 days',
|
||||
90 => '90 days',
|
||||
180 => '180 days',
|
||||
365 => '365 days',
|
||||
])
|
||||
->rules(UpdateMetricSettings::rules()['data_retention'])
|
||||
->label('Data Retention')
|
||||
->default($this->server->monitoring()?->type_data['data_retention'] ?? 30),
|
||||
])
|
||||
->modalSubmitActionLabel('Save')
|
||||
->action(function (array $data) {
|
||||
app(UpdateMetricSettings::class)->update($this->server, $data);
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Data retention updated')
|
||||
->send();
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,9 @@
|
||||
use App\Web\Pages\Servers\Page;
|
||||
use App\Web\Pages\Servers\PHP\Widgets\PHPList;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\ActionGroup;
|
||||
use Filament\Support\Enums\IconPosition;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
@ -30,31 +31,35 @@ public function getWidgets(): array
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
$phps = [];
|
||||
foreach (config('core.php_versions') as $version) {
|
||||
if (! $this->server->service('php', $version) && $version !== 'none') {
|
||||
$phps[] = Action::make($version)
|
||||
->label($version)
|
||||
->requiresConfirmation()
|
||||
->modalHeading('Install PHP '.$version)
|
||||
->modalSubmitActionLabel('Install')
|
||||
->action(function () use ($version) {
|
||||
app(InstallNewPHP::class)->install($this->server, ['version' => $version]);
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
});
|
||||
}
|
||||
}
|
||||
$installedPHPs = $this->server->installedPHPVersions();
|
||||
|
||||
return [
|
||||
ActionGroup::make($phps)
|
||||
Action::make('install')
|
||||
->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server]))
|
||||
->label('Install PHP')
|
||||
->icon('heroicon-o-chevron-up-down')
|
||||
->iconPosition(IconPosition::After)
|
||||
->dropdownPlacement('bottom-end')
|
||||
->color('primary')
|
||||
->button(),
|
||||
->icon('heroicon-o-archive-box-arrow-down')
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->form([
|
||||
Select::make('version')
|
||||
->options(
|
||||
collect(config('core.php_versions'))
|
||||
->filter(fn ($version) => ! in_array($version, $installedPHPs))
|
||||
->mapWithKeys(fn ($version) => [$version => $version])
|
||||
->toArray()
|
||||
)
|
||||
->rules(InstallNewPHP::rules($this->server)['version']),
|
||||
])
|
||||
->modalSubmitActionLabel('Install')
|
||||
->action(function (array $data) {
|
||||
app(InstallNewPHP::class)->install($this->server, $data);
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Installing PHP...')
|
||||
->send();
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -43,9 +43,13 @@ protected function getHeaderActions(): array
|
||||
->authorize('create', NotificationChannel::class)
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->action(function (array $data) {
|
||||
Actions\Create::action($data);
|
||||
try {
|
||||
Actions\Create::action($data);
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
$this->dispatch('$refresh');
|
||||
} catch (\Exception) {
|
||||
$this->halt();
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ public function getTable(): Table
|
||||
->modalHeading('Delete Notification Channel')
|
||||
->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record))
|
||||
->using(function (array $data, NotificationChannel $record) {
|
||||
//
|
||||
$record->delete();
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
use App\Actions\Projects\CreateProject;
|
||||
use App\Models\Project;
|
||||
use App\Web\Components\Page;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
@ -42,13 +42,11 @@ public function getWidgets(): array
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
Action::make('create')
|
||||
->label('Create Project')
|
||||
->icon('heroicon-o-plus')
|
||||
->authorize('create', Project::class)
|
||||
->using(function (array $data) {
|
||||
return app(CreateProject::class)->create(auth()->user(), $data);
|
||||
})
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->form(function (Form $form) {
|
||||
return $form->schema([
|
||||
TextInput::make('name')
|
||||
@ -56,8 +54,11 @@ protected function getHeaderActions(): array
|
||||
->rules(CreateProject::rules()['name']),
|
||||
])->columns(1);
|
||||
})
|
||||
->createAnother(false)
|
||||
->modalWidth(MaxWidth::Large),
|
||||
->action(function (array $data) {
|
||||
app(CreateProject::class)->create(auth()->user(), $data);
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList;
|
||||
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
|
||||
use Exception;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
|
||||
@ -54,17 +54,24 @@ public function getTitle(): string|Htmlable
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make()
|
||||
Action::make('delete')
|
||||
->record($this->project)
|
||||
->color('danger')
|
||||
->label('Delete Project')
|
||||
->icon('heroicon-o-trash')
|
||||
->modalHeading('Delete Project')
|
||||
->modalDescription('Are you sure you want to delete this project? This action will delete all associated data and cannot be undone.')
|
||||
->using(function (Project $record) {
|
||||
->requiresConfirmation()
|
||||
->action(function (Project $record) {
|
||||
try {
|
||||
app(DeleteProject::class)->delete(auth()->user(), $record);
|
||||
|
||||
$this->redirectRoute(Index::getUrl());
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Project deleted successfully.')
|
||||
->send();
|
||||
|
||||
$this->redirect(Index::getUrl());
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
|
@ -12,6 +12,8 @@
|
||||
|
||||
class ProjectsList extends Widget
|
||||
{
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return Project::query();
|
||||
|
Reference in New Issue
Block a user