record->name . '{data="real"}'; $start = now()->subMinutes(2)->timestamp; $end = now()->timestamp; $step = 5; // 查询 Prometheus 数据 $realdata = $prometheus->queryRange($query1, $start, $end, $step); // 检查查询结果是否有数据 if (!empty($realdata['data']['result']) && !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); // 保留两位小数 } } } catch (\Exception $e) { error_log('Failed to retrieve data from Prometheus: ' . $e->getMessage()); // 处理错误,返回默认的空数据集或提示信息 return [ 'labels' => [], 'datasets' => [], ]; } // 返回格式化后的数据 return [ 'labels' => $labels, 'datasets' => [ [ 'label' => $this->record->name, 'data' => $data, 'borderColor' => '#4CAF50', 'backgroundColor' => 'rgba(76, 175, 80, 0.2)', 'fill' => true, 'tension' => 0.2, ], ], ]; } }