[增添]增添了错误捕捉,避免页面报错显示
This commit is contained in:
@@ -19,44 +19,44 @@ class MetricWidgetChart extends InlineChartWidget
|
||||
protected function getData(): array
|
||||
{
|
||||
$prometheus = new PrometheusService();
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
$query1 = $this->record->name . '{data="real"}';
|
||||
try {
|
||||
$query1 = $this->record->name . '{data="real"}';
|
||||
|
||||
$start = now()->subMinutes(2)->timestamp;
|
||||
$end = now()->timestamp;
|
||||
$step = 5;
|
||||
|
||||
$start = now()->now()->subMinutes(2)->timestamp;
|
||||
$end = now()->timestamp;
|
||||
$step = 5;
|
||||
$realdata = $prometheus->queryRange($query1, $start, $end, $step);
|
||||
// 查询 Prometheus 数据
|
||||
$realdata = $prometheus->queryRange($query1, $start, $end, $step);
|
||||
|
||||
// 检查查询结果是否有数据
|
||||
if (!empty($realdata['data']['result']) && !empty($realdata['data']['result'][0]['values'][1])) {
|
||||
|
||||
// 检查查询结果是否有数据
|
||||
if (empty($realdata['data']['result'])) {
|
||||
// 如果没有数据,返回一个空数据集或提示信息
|
||||
// 获取数据
|
||||
$realdataValue = array_column($realdata['data']['result'][0]['values'], 1, 0);
|
||||
|
||||
// 只保留最新的 MAX_POINTS 个数据点
|
||||
$timestamps = array_keys($realdataValue);
|
||||
$timestamps = array_slice($timestamps, -self::MAX_POINTS);
|
||||
|
||||
foreach ($timestamps as $timestamp) {
|
||||
$labels[] = date('H:i:s', $timestamp); // 格式化时间戳
|
||||
$data[] = round(floatval($realdataValue[$timestamp]), 2); // 保留两位小数
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
error_log('Failed to retrieve data from Prometheus: ' . $e->getMessage());
|
||||
// 处理错误,返回默认的空数据集或提示信息
|
||||
return [
|
||||
'labels' => [],
|
||||
'datasets' => [],
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($realdata['data']['result'][0]['values'][1])) {
|
||||
|
||||
// 获取内存使用率数据
|
||||
$realdataValue = array_column($realdata['data']['result'][0]['values'], 1, 0);
|
||||
|
||||
// 只保留最新的 MAX_POINTS 个数据点
|
||||
$timestamps = array_keys($realdataValue);
|
||||
$timestamps = array_slice($timestamps, -self::MAX_POINTS);
|
||||
|
||||
foreach ($timestamps as $timestamp) {
|
||||
$labels[] = date('H:i:s', $timestamp); // 格式化时间戳为小时:分钟:秒
|
||||
$data[] = round(floatval($realdataValue[$timestamp]), 2); // 取出每个时间点的内存使用率,并保留两位小数
|
||||
}
|
||||
}
|
||||
|
||||
// 将数据格式化为 InlineChartWidget 所需的格式
|
||||
// $formattedRealData = $this->formatData($realdata, 'Real Data');
|
||||
|
||||
|
||||
// 返回格式化后的数据
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'datasets' => [
|
||||
|
||||
Reference in New Issue
Block a user