mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 18:04:46 +02:00

* Remove old admin * Remove controller test * Remove unused exceptions * Remove unused files * More small tweaks * Fix doc block * Remove unused service * Restore these * Add back autoDeploy * Revert "Add back autoDeploy" This reverts commit 630c1e08acf8056ce8e612f376fcd00c23d90aea. * Add these back * Add back exception * Remove ApiController again --------- Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com> Co-authored-by: Boy132 <mail@boy132.de> Co-authored-by: notCharles <charles@pelican.dev>
85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers\Api\Application;
|
|
|
|
use App\Models\Node;
|
|
use App\Models\Server;
|
|
use App\Models\Allocation;
|
|
use League\Fractal\Resource\Collection;
|
|
use League\Fractal\Resource\NullResource;
|
|
|
|
class NodeTransformer extends BaseTransformer
|
|
{
|
|
/**
|
|
* List of resources that can be included.
|
|
*/
|
|
protected array $availableIncludes = ['allocations', 'servers'];
|
|
|
|
/**
|
|
* Return the resource name for the JSONAPI output.
|
|
*/
|
|
public function getResourceName(): string
|
|
{
|
|
return Node::RESOURCE_NAME;
|
|
}
|
|
|
|
/**
|
|
* Return a node transformed into a format that can be consumed by the external administrative API.
|
|
*/
|
|
public function transform(Node $node): array
|
|
{
|
|
$response = collect($node->toArray())
|
|
->mapWithKeys(fn ($value, $key) => [snake_case($key) => $value])
|
|
->toArray();
|
|
|
|
$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);
|
|
$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);
|
|
|
|
$resources = $node->servers()->select(['memory', 'disk', 'cpu'])->get();
|
|
|
|
$response['allocated_resources'] = [
|
|
'memory' => $resources->sum('memory'),
|
|
'disk' => $resources->sum('disk'),
|
|
'cpu' => $resources->sum('cpu'),
|
|
];
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Return the nodes associated with this location.
|
|
*/
|
|
public function includeAllocations(Node $node): Collection|NullResource
|
|
{
|
|
if (!$this->authorize(Allocation::RESOURCE_NAME)) {
|
|
return $this->null();
|
|
}
|
|
|
|
$node->loadMissing('allocations');
|
|
|
|
return $this->collection(
|
|
$node->getRelation('allocations'),
|
|
$this->makeTransformer(AllocationTransformer::class),
|
|
'allocation'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Return the nodes associated with this location.
|
|
*/
|
|
public function includeServers(Node $node): Collection|NullResource
|
|
{
|
|
if (!$this->authorize(Server::RESOURCE_NAME)) {
|
|
return $this->null();
|
|
}
|
|
|
|
$node->loadMissing('servers');
|
|
|
|
return $this->collection(
|
|
$node->getRelation('servers'),
|
|
$this->makeTransformer(ServerTransformer::class),
|
|
'server'
|
|
);
|
|
}
|
|
}
|