refactor function

This commit is contained in:
Charles 2025-11-05 06:55:38 -05:00
parent bda39382b4
commit 1271532df8
7 changed files with 31 additions and 57 deletions

View File

@ -4,8 +4,7 @@ namespace App\Filament\Admin\Resources\Nodes\Pages;
use App\Filament\Admin\Resources\Nodes\NodeResource; use App\Filament\Admin\Resources\Nodes\NodeResource;
use App\Models\Node; use App\Models\Node;
use App\Repositories\Daemon\DaemonConfigurationRepository; use App\Repositories\Daemon\DaemonSystemRepository;
use App\Repositories\Daemon\DaemonRepository;
use App\Services\Helpers\SoftwareVersionService; use App\Services\Helpers\SoftwareVersionService;
use App\Services\Nodes\NodeAutoDeployService; use App\Services\Nodes\NodeAutoDeployService;
use App\Services\Nodes\NodeUpdateService; use App\Services\Nodes\NodeUpdateService;
@ -51,17 +50,14 @@ class EditNode extends EditRecord
protected static string $resource = NodeResource::class; protected static string $resource = NodeResource::class;
private DaemonConfigurationRepository $daemonConfigurationRepository; private DaemonSystemRepository $daemonSystemRepository;
private DaemonRepository $daemonRepository;
private NodeUpdateService $nodeUpdateService; private NodeUpdateService $nodeUpdateService;
public function boot(DaemonConfigurationRepository $daemonConfigurationRepository, NodeUpdateService $nodeUpdateService, DaemonRepository $daemonRepository): void public function boot(DaemonSystemRepository $daemonSystemRepository, NodeUpdateService $nodeUpdateService): void
{ {
$this->daemonConfigurationRepository = $daemonConfigurationRepository; $this->daemonSystemRepository = $daemonSystemRepository;
$this->nodeUpdateService = $nodeUpdateService; $this->nodeUpdateService = $nodeUpdateService;
$this->daemonRepository = $daemonRepository;
} }
/** /**
@ -653,7 +649,7 @@ class EditNode extends EditRecord
$logLines = $get('log_lines') ?? 200; $logLines = $get('log_lines') ?? 200;
try { try {
$response = $this->daemonRepository->setNode($node)->getDiagnostics($logLines, $includeEndpoints, $includeLogs); $response = $this->daemonSystemRepository->setNode($node)->getDiagnostics($logLines, $includeEndpoints, $includeLogs);
$set('pulled', true); $set('pulled', true);
$set('log', $response->body()); $set('log', $response->body());
@ -691,7 +687,7 @@ class EditNode extends EditRecord
if ($response->failed()) { if ($response->failed()) {
Notification::make() Notification::make()
->title(trans('admin/node.diagnostics.upload_failed')) ->title(trans('admin/node.diagnostics.upload_failed'))
->body($response->status()) ->body(fn () => $response->status() . ' - ' . $response->body())
->danger() ->danger()
->send(); ->send();
@ -752,7 +748,8 @@ class EditNode extends EditRecord
->hiddenLabel() ->hiddenLabel()
->columnSpanFull() ->columnSpanFull()
->rows(35) ->rows(35)
->visible(fn (Get $get) => ($get('pulled') ?? false) || ($get('uploaded') ?? false)), ]), ->visible(fn (Get $get) => ($get('pulled') ?? false) || ($get('uploaded') ?? false)),
]),
]), ]),
]); ]);
} }
@ -810,7 +807,7 @@ class EditNode extends EditRecord
try { try {
if ($changed) { if ($changed) {
$this->daemonConfigurationRepository->setNode($node)->update($node); $this->daemonSystemRepository->setNode($node)->update($node);
} }
parent::getSavedNotification()?->send(); parent::getSavedNotification()?->send();
} catch (ConnectionException) { } catch (ConnectionException) {

View File

@ -4,7 +4,7 @@ namespace App\Models;
use App\Contracts\Validatable; use App\Contracts\Validatable;
use App\Exceptions\Service\HasActiveServersException; use App\Exceptions\Service\HasActiveServersException;
use App\Repositories\Daemon\DaemonConfigurationRepository; use App\Repositories\Daemon\DaemonSystemRepository;
use App\Traits\HasValidation; use App\Traits\HasValidation;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
@ -316,7 +316,7 @@ class Node extends Model implements Validatable
{ {
return once(function () { return once(function () {
try { try {
return (new DaemonConfigurationRepository()) return (new DaemonSystemRepository())
->setNode($this) ->setNode($this)
->getSystemInformation(); ->getSystemInformation();
} catch (Exception $exception) { } catch (Exception $exception) {

View File

@ -22,8 +22,6 @@ use App\Models\Server;
use App\Models\Task; use App\Models\Task;
use App\Models\User; use App\Models\User;
use App\Models\UserSSHKey; use App\Models\UserSSHKey;
use App\Repositories\Daemon\DaemonRepository;
use App\Repositories\Daemon\HttpDaemonRepository;
use App\Services\Helpers\SoftwareVersionService; use App\Services\Helpers\SoftwareVersionService;
use Dedoc\Scramble\Scramble; use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi; use Dedoc\Scramble\Support\Generator\OpenApi;
@ -122,15 +120,4 @@ class AppServiceProvider extends ServiceProvider
AboutCommand::add('Environment', 'Installation Directory', base_path()); AboutCommand::add('Environment', 'Installation Directory', base_path());
} }
/**
* Register application service providers.
*/
public function register(): void
{
Scramble::ignoreDefaultRoutes();
// Bind the daemon repository abstract to a concrete implementation so it is instantiable.
$this->app->bind(DaemonRepository::class, HttpDaemonRepository::class);
}
} }

View File

@ -72,23 +72,4 @@ abstract class DaemonRepository
return true; return true;
} }
/**
* Retrieve diagnostics from the daemon for the current node.
*
*
* @throws ConnectionException
*/
public function getDiagnostics(int $lines, bool $includeEndpoints, bool $includeLogs): Response
{
Assert::isInstanceOf($this->node, Node::class);
return $this->getHttpClient()
->timeout(5)
->get('/api/diagnostics', [
'log_lines' => $lines,
'include_endpoints' => $includeEndpoints ? 'true' : 'false',
'include_logs' => $includeLogs ? 'true' : 'false',
]);
}
} }

View File

@ -6,7 +6,7 @@ use App\Models\Node;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
class DaemonConfigurationRepository extends DaemonRepository class DaemonSystemRepository extends DaemonRepository
{ {
/** /**
* Returns system information from the daemon instance. * Returns system information from the daemon instance.
@ -30,6 +30,23 @@ class DaemonConfigurationRepository extends DaemonRepository
})->json(); })->json();
} }
/**
* Retrieve diagnostics from the daemon for the current node.
*
*
* @throws ConnectionException
*/
public function getDiagnostics(int $lines, bool $includeEndpoints, bool $includeLogs): Response
{
return $this->getHttpClient()
->timeout(5)
->get('/api/diagnostics', [
'log_lines' => $lines,
'include_endpoints' => $includeEndpoints ? 'true' : 'false',
'include_logs' => $includeLogs ? 'true' : 'false',
]);
}
/** /**
* Updates the configuration information for a daemon. Updates the information for * Updates the configuration information for a daemon. Updates the information for
* this instance using a passed-in model. This allows us to change plenty of information * this instance using a passed-in model. This allows us to change plenty of information

View File

@ -1,8 +0,0 @@
<?php
namespace App\Repositories\Daemon;
class HttpDaemonRepository extends DaemonRepository
{
// Concrete instantiable implementation. Uses DaemonRepository's HTTP helper methods.
}

View File

@ -4,7 +4,7 @@ namespace App\Services\Nodes;
use App\Exceptions\Service\Node\ConfigurationNotPersistedException; use App\Exceptions\Service\Node\ConfigurationNotPersistedException;
use App\Models\Node; use App\Models\Node;
use App\Repositories\Daemon\DaemonConfigurationRepository; use App\Repositories\Daemon\DaemonSystemRepository;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -17,7 +17,7 @@ class NodeUpdateService
*/ */
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private DaemonConfigurationRepository $configurationRepository, private DaemonSystemRepository $configurationRepository,
) {} ) {}
/** /**