mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-31 04:36:51 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			87 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Filament\Resources\NodeResource\Widgets;
 | |
| 
 | |
| use App\Models\Node;
 | |
| use Carbon\Carbon;
 | |
| use Filament\Support\RawJs;
 | |
| use Filament\Widgets\ChartWidget;
 | |
| use Illuminate\Database\Eloquent\Model;
 | |
| use Illuminate\Support\Number;
 | |
| 
 | |
| class NodeMemoryChart extends ChartWidget
 | |
| {
 | |
|     protected static ?string $pollingInterval = '5s';
 | |
|     protected static ?string $maxHeight = '300px';
 | |
| 
 | |
|     public ?Model $record = null;
 | |
| 
 | |
|     protected function getData(): array
 | |
|     {
 | |
|         /** @var Node $node */
 | |
|         $node = $this->record;
 | |
| 
 | |
|         $memUsed = collect(cache()->get("nodes.$node->id.memory_used"))->slice(-10)
 | |
|             ->map(fn ($value, $key) => [
 | |
|                 'memory' => Number::format(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language),
 | |
|                 'timestamp' => Carbon::createFromTimestamp($key, (auth()->user()->timezone ?? 'UTC'))->format('H:i:s'),
 | |
|             ])
 | |
|             ->all();
 | |
| 
 | |
|         return [
 | |
|             'datasets' => [
 | |
|                 [
 | |
|                     'data' => array_column($memUsed, 'memory'),
 | |
|                     'backgroundColor' => [
 | |
|                         'rgba(96, 165, 250, 0.3)',
 | |
|                     ],
 | |
|                     'tension' => '0.3',
 | |
|                     'fill' => true,
 | |
|                 ],
 | |
|             ],
 | |
|             'labels' => array_column($memUsed, 'timestamp'),
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     protected function getType(): string
 | |
|     {
 | |
|         return 'line';
 | |
|     }
 | |
| 
 | |
|     protected function getOptions(): RawJs
 | |
|     {
 | |
|         return RawJs::make(<<<'JS'
 | |
|         {
 | |
|             scales: {
 | |
|                 y: {
 | |
|                     min: 0,
 | |
|                 },
 | |
|             },
 | |
|             plugins: {
 | |
|                 legend: {
 | |
|                     display: false,
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|     JS);
 | |
|     }
 | |
| 
 | |
|     public function getHeading(): string
 | |
|     {
 | |
|         /** @var Node $node */
 | |
|         $node = $this->record;
 | |
|         $latestMemoryUsed = collect(cache()->get("nodes.$node->id.memory_used"))->last();
 | |
|         $totalMemory = collect(cache()->get("nodes.$node->id.memory_total"))->last();
 | |
| 
 | |
|         $used = config('panel.use_binary_prefix')
 | |
|             ? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
 | |
|             : Number::format($latestMemoryUsed / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
 | |
| 
 | |
|         $total = config('panel.use_binary_prefix')
 | |
|             ? Number::format($totalMemory / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
 | |
|             : Number::format($totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
 | |
| 
 | |
|         return 'Memory - ' . $used . ' Of ' . $total;
 | |
|     }
 | |
| }
 | 
