Add DiskSpaceService and DiskSpaceBar component for monitoring disk usage
Implement DiskSpaceService to provide snapshots of disk space usage, including total, free, and used bytes, as well as human-readable formats. Create DiskSpaceBar component to display disk usage information in the UI, including a progress bar that visually represents used space. Update app layout to include the DiskSpaceBar and add tests to ensure functionality of the disk space service and its integration in the layout.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use App\Services\DiskSpaceService;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class DiskSpaceBar extends Component
|
||||
{
|
||||
/**
|
||||
* @var array{
|
||||
* total_bytes: int,
|
||||
* free_bytes: int,
|
||||
* used_bytes: int,
|
||||
* used_percent: float,
|
||||
* free_percent: float,
|
||||
* total_human: string,
|
||||
* free_human: string,
|
||||
* used_human: string
|
||||
* }|null
|
||||
*/
|
||||
public readonly ?array $disk;
|
||||
|
||||
public function __construct(DiskSpaceService $diskSpace)
|
||||
{
|
||||
$this->disk = $diskSpace->snapshot();
|
||||
}
|
||||
|
||||
public function shouldRender(): bool
|
||||
{
|
||||
return $this->disk !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Progress fill color based on remaining free space.
|
||||
*/
|
||||
public function barColor(): string
|
||||
{
|
||||
$freePercent = $this->disk['free_percent'] ?? 100;
|
||||
|
||||
if ($freePercent <= 5) {
|
||||
return 'bg-red-600';
|
||||
}
|
||||
|
||||
if ($freePercent <= 15) {
|
||||
return 'bg-amber-500';
|
||||
}
|
||||
|
||||
return 'bg-teal-600';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the view / contents that represent the component.
|
||||
*/
|
||||
public function render(): View|Closure|string
|
||||
{
|
||||
return view('components.disk-space-bar');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user