Use Dokploy projects in dashboard widget
This commit is contained in:
@@ -14,11 +14,63 @@ class DokployPlatformHealth extends Widget
|
||||
|
||||
protected function getViewData(): array
|
||||
{
|
||||
$projects = $this->loadProjects();
|
||||
|
||||
return [
|
||||
'composes' => $this->loadComposes(),
|
||||
'projects' => $projects,
|
||||
'composes' => empty($projects) ? $this->loadComposes() : [],
|
||||
];
|
||||
}
|
||||
|
||||
protected function loadProjects(): array
|
||||
{
|
||||
$client = app(DokployClient::class);
|
||||
$projectMap = config('dokploy.projects', []);
|
||||
$results = [];
|
||||
|
||||
if (empty($projectMap)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($projectMap as $label => $projectId) {
|
||||
$project = $client->findProject((string) $projectId);
|
||||
|
||||
if (! $project) {
|
||||
$results[] = [
|
||||
'label' => ucfirst((string) $label),
|
||||
'project_id' => (string) $projectId,
|
||||
'name' => (string) $projectId,
|
||||
'status' => 'unreachable',
|
||||
'error' => "Project {$projectId} not found.",
|
||||
'applications' => [],
|
||||
'services' => [],
|
||||
'updated_at' => null,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$applicationsPayload = Arr::get($project, 'applications', []);
|
||||
$applications = $this->formatApplications(is_array($applicationsPayload) ? $applicationsPayload : [], $client);
|
||||
$services = $this->formatProjectServices($project);
|
||||
|
||||
$results[] = [
|
||||
'label' => ucfirst((string) $label),
|
||||
'project_id' => Arr::get($project, 'projectId', $projectId),
|
||||
'name' => Arr::get($project, 'name') ?? Arr::get($project, 'projectName') ?? (string) $projectId,
|
||||
'description' => Arr::get($project, 'description'),
|
||||
'status' => $this->deriveProjectStatus($applications, $services),
|
||||
'applications' => $applications,
|
||||
'services' => $services,
|
||||
'updated_at' => Arr::get($project, 'updatedAt') ?? Arr::get($project, 'createdAt'),
|
||||
'applications_count' => count($applications),
|
||||
'services_count' => count($services),
|
||||
];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected function loadComposes(): array
|
||||
{
|
||||
$client = app(DokployClient::class);
|
||||
@@ -62,7 +114,7 @@ class DokployPlatformHealth extends Widget
|
||||
'label' => 'Dokploy',
|
||||
'compose_id' => '-',
|
||||
'status' => 'unconfigured',
|
||||
'error' => 'Set DOKPLOY_COMPOSE_IDS in .env to enable monitoring.',
|
||||
'error' => 'Set DOKPLOY_PROJECT_IDS or DOKPLOY_COMPOSE_IDS in .env to enable monitoring.',
|
||||
],
|
||||
];
|
||||
}
|
||||
@@ -70,6 +122,158 @@ class DokployPlatformHealth extends Widget
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected function formatApplications(array $applications, DokployClient $client): array
|
||||
{
|
||||
return collect($applications)
|
||||
->map(function (array $application) use ($client) {
|
||||
$applicationId = $this->extractApplicationId($application);
|
||||
$statusPayload = [];
|
||||
|
||||
if ($applicationId) {
|
||||
try {
|
||||
$statusPayload = $client->applicationStatus($applicationId);
|
||||
} catch (\Throwable $exception) {
|
||||
$statusPayload = [];
|
||||
}
|
||||
}
|
||||
|
||||
$applicationDetails = Arr::get($statusPayload, 'application', []);
|
||||
$monitoring = Arr::get($statusPayload, 'monitoring', []);
|
||||
|
||||
$status = Arr::get($application, 'applicationStatus')
|
||||
?? Arr::get($application, 'status')
|
||||
?? Arr::get($applicationDetails, 'applicationStatus')
|
||||
?? Arr::get($applicationDetails, 'status')
|
||||
?? 'unknown';
|
||||
|
||||
return [
|
||||
'id' => $applicationId ?? Arr::get($application, 'id'),
|
||||
'name' => Arr::get($application, 'name')
|
||||
?? Arr::get($application, 'appName')
|
||||
?? Arr::get($applicationDetails, 'name')
|
||||
?? Arr::get($applicationDetails, 'appName')
|
||||
?? $applicationId,
|
||||
'status' => $status,
|
||||
'repository' => Arr::get($application, 'repository')
|
||||
?? Arr::get($applicationDetails, 'repository')
|
||||
?? Arr::get($application, 'repo')
|
||||
?? Arr::get($applicationDetails, 'repo'),
|
||||
'branch' => Arr::get($application, 'branch')
|
||||
?? Arr::get($applicationDetails, 'branch')
|
||||
?? Arr::get($application, 'gitBranch')
|
||||
?? Arr::get($applicationDetails, 'gitBranch'),
|
||||
'url' => Arr::get($application, 'url')
|
||||
?? Arr::get($applicationDetails, 'url')
|
||||
?? Arr::get($application, 'domain')
|
||||
?? Arr::get($applicationDetails, 'domain'),
|
||||
'server' => Arr::get($application, 'serverName')
|
||||
?? Arr::get($applicationDetails, 'serverName')
|
||||
?? Arr::get($application, 'server'),
|
||||
'last_deploy' => Arr::get($application, 'lastDeploymentAt')
|
||||
?? Arr::get($applicationDetails, 'lastDeploymentAt')
|
||||
?? Arr::get($application, 'updatedAt')
|
||||
?? Arr::get($applicationDetails, 'updatedAt')
|
||||
?? Arr::get($application, 'createdAt'),
|
||||
'monitoring' => $this->formatMonitoring($monitoring),
|
||||
];
|
||||
})
|
||||
->filter(fn (array $application) => filled($application['name']))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function extractApplicationId(array $application): ?string
|
||||
{
|
||||
return Arr::get($application, 'applicationId')
|
||||
?? Arr::get($application, 'appId')
|
||||
?? Arr::get($application, 'id');
|
||||
}
|
||||
|
||||
protected function formatProjectServices(array $project): array
|
||||
{
|
||||
return collect([
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'compose', []), 'compose', 'composeId', 'composeStatus'),
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'mysql', []), 'mysql', 'mysqlId', 'applicationStatus'),
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'postgres', []), 'postgres', 'postgresId', 'applicationStatus'),
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'mariadb', []), 'mariadb', 'mariadbId', 'applicationStatus'),
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'mongo', []), 'mongo', 'mongoId', 'applicationStatus'),
|
||||
...$this->normalizeServiceList((array) Arr::get($project, 'redis', []), 'redis', 'redisId', 'applicationStatus'),
|
||||
])
|
||||
->filter(fn (array $service) => filled($service['name']))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function normalizeServiceList(array $services, string $type, string $idKey, string $statusKey): array
|
||||
{
|
||||
return collect($services)
|
||||
->map(function (array $service) use ($type, $idKey, $statusKey) {
|
||||
return [
|
||||
'type' => $type,
|
||||
'id' => Arr::get($service, $idKey) ?? Arr::get($service, 'id'),
|
||||
'name' => Arr::get($service, 'name') ?? Arr::get($service, 'appName') ?? Arr::get($service, 'serviceName'),
|
||||
'status' => Arr::get($service, $statusKey) ?? Arr::get($service, 'status') ?? Arr::get($service, 'composeStatus', 'unknown'),
|
||||
'version' => Arr::get($service, 'dockerImage') ?? Arr::get($service, 'image'),
|
||||
'external_port' => Arr::get($service, 'externalPort'),
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function formatMonitoring(array $monitoring): array
|
||||
{
|
||||
$metrics = [];
|
||||
$allowed = [
|
||||
'cpuPercent' => 'CPU',
|
||||
'cpu' => 'CPU',
|
||||
'memoryPercent' => 'Memory',
|
||||
'memory' => 'Memory',
|
||||
'uptime' => 'Uptime',
|
||||
];
|
||||
|
||||
foreach ($allowed as $key => $label) {
|
||||
$value = Arr::get($monitoring, $key);
|
||||
|
||||
if (filled($value) && ! is_array($value)) {
|
||||
$metrics[] = [
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $metrics;
|
||||
}
|
||||
|
||||
protected function deriveProjectStatus(array $applications, array $services): string
|
||||
{
|
||||
$statuses = collect($applications)
|
||||
->pluck('status')
|
||||
->merge(collect($services)->pluck('status'))
|
||||
->filter()
|
||||
->map(fn ($status) => strtolower((string) $status))
|
||||
->values();
|
||||
|
||||
if ($statuses->contains(fn ($status) => in_array($status, ['error', 'failed', 'unreachable', 'unhealthy'], true))) {
|
||||
return 'error';
|
||||
}
|
||||
|
||||
if ($statuses->contains(fn ($status) => in_array($status, ['deploying', 'pending', 'starting'], true))) {
|
||||
return 'deploying';
|
||||
}
|
||||
|
||||
if ($statuses->contains(fn ($status) => in_array($status, ['stopped', 'inactive', 'paused'], true))) {
|
||||
return 'warning';
|
||||
}
|
||||
|
||||
if ($statuses->contains(fn ($status) => in_array($status, ['done', 'running', 'healthy'], true))) {
|
||||
return 'done';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
protected function formatServices(array $services): array
|
||||
{
|
||||
return collect($services)
|
||||
|
||||
Reference in New Issue
Block a user