mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-11-04 12:56:53 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Http\Controllers\Api\Client\Servers;
 | 
						|
 | 
						|
use App\Http\Controllers\Api\Client\ClientApiController;
 | 
						|
use App\Http\Requests\Api\Client\Servers\GetServerRequest;
 | 
						|
use App\Models\Server;
 | 
						|
use App\Repositories\Daemon\DaemonServerRepository;
 | 
						|
use App\Transformers\Api\Client\StatsTransformer;
 | 
						|
use Carbon\Carbon;
 | 
						|
use Dedoc\Scramble\Attributes\Group;
 | 
						|
use Illuminate\Cache\Repository;
 | 
						|
use Illuminate\Http\Client\ConnectionException;
 | 
						|
 | 
						|
#[Group('Server', weight: 3)]
 | 
						|
class ResourceUtilizationController extends ClientApiController
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * ResourceUtilizationController constructor.
 | 
						|
     */
 | 
						|
    public function __construct(private Repository $cache, private DaemonServerRepository $repository)
 | 
						|
    {
 | 
						|
        parent::__construct();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * View resources
 | 
						|
     *
 | 
						|
     * Return the current resource utilization for a server. This value is cached for up to
 | 
						|
     * 20 seconds at a time to ensure that repeated requests to this endpoint do not cause
 | 
						|
     * a flood of unnecessary API calls.
 | 
						|
     *
 | 
						|
     * @return array<array-key, mixed>
 | 
						|
     *
 | 
						|
     * @throws ConnectionException
 | 
						|
     */
 | 
						|
    public function __invoke(GetServerRequest $request, Server $server): array
 | 
						|
    {
 | 
						|
        $key = "resources:$server->uuid";
 | 
						|
        $stats = $this->cache->remember($key, Carbon::now()->addSeconds(20), function () use ($server) {
 | 
						|
            return $this->repository->setServer($server)->getDetails();
 | 
						|
        });
 | 
						|
 | 
						|
        return $this->fractal->item($stats)
 | 
						|
            ->transformWith($this->getTransformer(StatsTransformer::class))
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
}
 |