58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Services\PrometheusService;
|
|
use Filament\Widgets\ChartWidget;
|
|
|
|
class PrometheusMemory extends ChartWidget
|
|
{
|
|
protected static ?int $sort = 3;
|
|
|
|
protected static ?string $heading = '内存占用率';
|
|
|
|
protected function getData(): array
|
|
{
|
|
$prometheus = new PrometheusService();
|
|
|
|
|
|
// 查询过去 1 分钟内的每 6 秒一个数据点
|
|
$memoryUsageRange = $prometheus->queryRange(
|
|
'(1 - (avg_over_time(node_memory_MemAvailable_bytes[1m]) / avg_over_time(node_memory_MemTotal_bytes[1m]))) * 100',
|
|
now()->subMinutes(1)->timestamp,
|
|
now()->timestamp,
|
|
6 // 每6秒一个数据点
|
|
);
|
|
|
|
$labels = [];
|
|
$data = [];
|
|
|
|
if (!empty($memoryUsageRange['data']['result'][0]['values'])) {
|
|
date_default_timezone_set('Asia/Shanghai');
|
|
foreach ($memoryUsageRange['data']['result'][0]['values'] as $index => $value) {
|
|
$labels[] = date('H:i:s', $value[0]); // 格式化时间戳为小时:分钟:秒
|
|
$data[] = round($value[1], 2); // 取出每个时间点的内存使用率,并保留两位小数
|
|
}
|
|
}
|
|
|
|
return [
|
|
'labels' => $labels,
|
|
'datasets' => [
|
|
[
|
|
'label' => '内存占用率 (%)',
|
|
'data' => $data,
|
|
'borderColor' => '#4CAF50',
|
|
'backgroundColor' => 'rgba(76, 175, 80, 0.2)',
|
|
'fill' => true,
|
|
'tension' => 0.4,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'line';
|
|
}
|
|
}
|