[增添]增加了网络流量监测的过滤选择功能

This commit is contained in:
makotocc0107
2024-08-30 15:14:55 +08:00
committed by Coding
parent 2bfbd6d956
commit 0eebedac4b
2 changed files with 106 additions and 53 deletions

View File

@@ -14,70 +14,98 @@ class NetworkMonitor extends ChartWidget
// 固定显示的数据点数
private const MAX_POINTS = 6;
// 定义下拉菜单选项
protected function getFilters(): array
{
return [
'upload' => '上传流量',
'download' => '下载流量',
];
}
protected function getData(): array
{
$prometheus = new PrometheusService();
// 查询过去 1 分钟内每秒的上传流量
$uploadQuery = 'rate(node_network_transmit_bytes_total[1m])';
$uploadResult = $prometheus->queryRange(
$uploadQuery,
now()->subMinutes(1)->timestamp,
now()->timestamp,
'5s' // 每秒一个数据点
);
// 查询过去 1 分钟内每秒的下载流量
$downloadQuery = 'rate(node_network_receive_bytes_total[1m])';
$downloadResult = $prometheus->queryRange(
$downloadQuery,
now()->subMinutes(1)->timestamp,
now()->timestamp,
'5s' // 每秒一个数据点
);
// 根据用户选择的过滤器决定查询的流量类型
$selectedFilter = $this->filter ?? 'upload';
$labels = [];
$uploadData = [];
$downloadData = [];
if (!empty($uploadResult['data']['result'][0]['values']) && !empty($downloadResult['data']['result'][0]['values'])) {
date_default_timezone_set('Asia/Shanghai');
if ($selectedFilter === 'upload') {
// 查询过去 1 分钟内每秒的上传流量
$uploadQuery = 'rate(node_network_transmit_bytes_total[1m])';
$uploadResult = $prometheus->queryRange(
$uploadQuery,
now()->subMinutes(1)->timestamp,
now()->timestamp,
'5s' // 每秒一个数据点
);
$uploadValues = array_column($uploadResult['data']['result'][0]['values'], 1, 0);
$downloadValues = array_column($downloadResult['data']['result'][0]['values'], 1, 0);
if (!empty($uploadResult['data']['result'][0]['values'])) {
$uploadValues = array_column($uploadResult['data']['result'][0]['values'], 1, 0);
// 获取最新的数据点
$latestTimestamp = max(array_keys($uploadValues));
// 获取最新的数据点
$timestamps = array_keys($uploadValues);
$timestamps = array_slice($timestamps, -self::MAX_POINTS);
// 只保留最新的 MAX_POINTS 个数据点
$timestamps = array_keys($uploadValues);
$timestamps = array_slice($timestamps, -self::MAX_POINTS);
foreach ($timestamps as $timestamp) {
$labels[] = date('H:i:s', $timestamp); // 格式化时间戳为小时:分钟:秒
$uploadData[] = round(floatval($uploadValues[$timestamp]) / (1024 * 1024), 2); // 转换为 MB/s
$downloadData[] = round(floatval($downloadValues[$timestamp]) / (1024 * 1024), 2); // 转换为 MB/s
foreach ($timestamps as $timestamp) {
$labels[] = date('H:i:s', $timestamp); // 格式化时间戳为小时:分钟:秒
$uploadData[] = round(floatval($uploadValues[$timestamp]) / (1024 * 1024), 2); // 转换为 MB/s
}
}
}
if ($selectedFilter === 'download') {
// 查询过去 1 分钟内每秒的下载流量
$downloadQuery = 'rate(node_network_receive_bytes_total[1m])';
$downloadResult = $prometheus->queryRange(
$downloadQuery,
now()->subMinutes(1)->timestamp,
now()->timestamp,
'5s' // 每秒一个数据点
);
if (!empty($downloadResult['data']['result'][0]['values'])) {
$downloadValues = array_column($downloadResult['data']['result'][0]['values'], 1, 0);
// 获取最新的数据点
$timestamps = array_keys($downloadValues);
$timestamps = array_slice($timestamps, -self::MAX_POINTS);
foreach ($timestamps as $timestamp) {
$labels[] = date('H:i:s', $timestamp); // 格式化时间戳为小时:分钟:秒
$downloadData[] = round(floatval($downloadValues[$timestamp]) / (1024 * 1024), 2); // 转换为 MB/s
}
}
}
// 处理显示的数据集
$datasets = [];
if ($selectedFilter === 'upload') {
$datasets[] = [
'label' => '上传流量 (MB/s)',
'data' => $uploadData,
'borderColor' => 'rgba(255, 99, 132, 0.6)',
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'fill' => false,
];
}
if ($selectedFilter === 'download') {
$datasets[] = [
'label' => '下载流量 (MB/s)',
'data' => $downloadData,
'borderColor' => 'rgba(54, 162, 235, 0.6)',
'backgroundColor' => 'rgba(54, 162, 235, 0.2)',
'fill' => false,
];
}
return [
'labels' => $labels,
'datasets' => [
[
'label' => '上传流量 (MB/s)',
'data' => $uploadData,
'borderColor' => 'rgba(255, 99, 132, 0.6)',
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'fill' => false,
],
[
'label' => '下载流量 (MB/s)',
'data' => $downloadData,
'borderColor' => 'rgba(54, 162, 235, 0.6)',
'backgroundColor' => 'rgba(54, 162, 235, 0.2)',
'fill' => false,
],
],
'datasets' => $datasets,
];
}