Files
fotospiel-app/app/Filament/Widgets/DokployPlatformHealth.php
2025-11-18 16:45:56 +01:00

92 lines
3.1 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Services\Dokploy\DokployClient;
use Filament\Widgets\Widget;
use Illuminate\Support\Arr;
class DokployPlatformHealth extends Widget
{
protected string $view = 'filament.widgets.dokploy-platform-health';
protected ?string $pollingInterval = '60s';
protected function getViewData(): array
{
return [
'applications' => $this->loadApplications(),
];
}
protected function loadApplications(): array
{
$client = app(DokployClient::class);
$applicationMap = config('dokploy.applications', []);
$results = [];
foreach ($applicationMap as $label => $applicationId) {
try {
$status = $client->applicationStatus($applicationId);
$deployments = $client->recentDeployments($applicationId, 1);
$application = Arr::get($status, 'application', []);
$monitoring = Arr::get($status, 'monitoring', []);
$results[] = [
'label' => ucfirst($label),
'application_id' => $applicationId,
'app_name' => Arr::get($application, 'appName') ?? Arr::get($application, 'name'),
'status' => Arr::get($application, 'applicationStatus', 'unknown'),
'cpu' => $this->extractMetric($monitoring, [
'metrics.cpuPercent',
'metrics.cpu_percent',
'cpuPercent',
'cpu_percent',
]),
'memory' => $this->extractMetric($monitoring, [
'metrics.memoryPercent',
'metrics.memory_percent',
'memoryPercent',
'memory_percent',
]),
'last_deploy' => Arr::get($deployments, '0.createdAt')
?? Arr::get($deployments, '0.created_at')
?? Arr::get($application, 'updatedAt')
?? Arr::get($application, 'lastDeploymentAt'),
];
} catch (\Throwable $exception) {
$results[] = [
'label' => ucfirst($label),
'application_id' => $applicationId,
'status' => 'unreachable',
'error' => $exception->getMessage(),
];
}
}
if (empty($results)) {
return [
[
'label' => 'Dokploy',
'application_id' => '-',
'status' => 'unconfigured',
'error' => 'Set DOKPLOY_APPLICATION_IDS in .env to enable monitoring.',
],
];
}
return $results;
}
protected function extractMetric(array $source, array $candidates): mixed
{
foreach ($candidates as $key) {
if (Arr::has($source, $key)) {
return Arr::get($source, $key);
}
}
return null;
}
}