[增添]添加了CPU占用率饼图
This commit is contained in:
59
management-panel/app/Filament/Widgets/CPUStatus.php
Normal file
59
management-panel/app/Filament/Widgets/CPUStatus.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Services\PrometheusService;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
class CPUStatus extends ChartWidget
|
||||
{
|
||||
protected static ?string $heading = 'CPU 占用率';
|
||||
|
||||
protected static ?int $sort = 5;
|
||||
|
||||
protected int | string | array $columnSpan = '1';
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$prometheus = new PrometheusService();
|
||||
|
||||
// 查询 CPU 使用率数据,获取最近 60 秒内的平均值
|
||||
$query = '100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[60s])) * 100)';
|
||||
$cpuUsageResult = $prometheus->query($query);
|
||||
|
||||
// 计算使用率和空闲率
|
||||
$cpuUsage = 0;
|
||||
if (isset($cpuUsageResult['data']['result'][0]['value'][1])) {
|
||||
$cpuUsage = floatval($cpuUsageResult['data']['result'][0]['value'][1]);
|
||||
}
|
||||
$cpuIdle = 100 - $cpuUsage;
|
||||
|
||||
$data = [
|
||||
round($cpuUsage, 2),
|
||||
round($cpuIdle, 2),
|
||||
];
|
||||
|
||||
return [
|
||||
'labels' => ['CPU 使用率', 'CPU 空闲率'],
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'CPU 占用情况',
|
||||
'data' => $data,
|
||||
'backgroundColor' => ['#FBD1B7', '#E0F5B9'], // 柔和颜色
|
||||
'borderColor' => ['#FBD1B7', '#E0F5B9'], // 边框颜色
|
||||
'borderWidth' => 1, // 边框宽度
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'pie'; // 设置为饼图
|
||||
}
|
||||
|
||||
protected function getPollingInterval(): ?string
|
||||
{
|
||||
return '1s'; // 每秒更新一次
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class SystemStats extends BaseWidget
|
||||
return $item['value'][1] ?? 0;
|
||||
}, $networkReceiveResult['data']['result'])) / (1024 * 1024), 2);
|
||||
|
||||
$networkDisplay = "上行: {$networkTransmit} MB/s\n下行: {$networkReceive} MB/s";
|
||||
$networkDisplay = "上传: {$networkTransmit} MB/s\n下载: {$networkReceive} MB/s";
|
||||
|
||||
return [
|
||||
BaseWidget\Stat::make('内存占用', $memoryDisplay),
|
||||
|
||||
Reference in New Issue
Block a user