@@ -24,19 +24,19 @@ func NewPrometheusDataFormatter(timezone string) *PrometheusDataFormatter {
2424
2525// FormattedTimeData 格式化后的时间数据结构
2626type FormattedTimeData struct {
27- Timestamp string `json:"timestamp"` // 格式化的时间戳
28- TimestampUnix float64 `json:"timestamp_unix"` // 原始Unix时间戳
29- Value interface {} `json:"value"` // 数据值
30- Metric map [string ]string `json:"metric"` // 指标标签
31- Metadata map [string ]interface {} `json:"metadata"` // 元数据信息
27+ Timestamp string `json:"timestamp"` // 格式化的时间戳
28+ TimestampUnix float64 `json:"timestamp_unix"` // 原始Unix时间戳
29+ Value any `json:"value"` // 数据值
30+ Metric map [string ]string `json:"metric"` // 指标标签
31+ Metadata map [string ]any `json:"metadata"` // 元数据信息
3232}
3333
3434// PrometheusResponse Prometheus查询响应结构
3535type PrometheusResponse struct {
3636 Status string `json:"status"`
3737 Data struct {
38- ResultType string `json:"resultType"`
39- Result []interface {} `json:"result"`
38+ ResultType string `json:"resultType"`
39+ Result []any `json:"result"`
4040 } `json:"data"`
4141}
4242
@@ -82,7 +82,7 @@ func (f *PrometheusDataFormatter) FormatPrometheusData(rawData string, includeMe
8282 var response PrometheusResponse
8383 if err := json .Unmarshal ([]byte (rawData ), & response ); err != nil {
8484 // 如果标准格式解析失败,尝试解析为数组格式(兼容旧版本)
85- var arrayResult []interface {}
85+ var arrayResult []any
8686 if arrayErr := json .Unmarshal ([]byte (rawData ), & arrayResult ); arrayErr != nil {
8787 return "" , fmt .Errorf ("解析Prometheus响应失败: %w" , err )
8888 }
@@ -118,7 +118,7 @@ func (f *PrometheusDataFormatter) FormatPrometheusData(rawData string, includeMe
118118 }
119119
120120 // 创建最终的响应结构
121- finalResponse := map [string ]interface {} {
121+ finalResponse := map [string ]any {
122122 "status" : "success" ,
123123 "result_type" : response .Data .ResultType ,
124124 "result_count" : len (formattedResults ),
@@ -137,17 +137,17 @@ func (f *PrometheusDataFormatter) FormatPrometheusData(rawData string, includeMe
137137}
138138
139139// formatVectorResult 格式化瞬时向量查询结果
140- func (f * PrometheusDataFormatter ) formatVectorResult (results []interface {} , includeMetadata bool ) []FormattedTimeData {
140+ func (f * PrometheusDataFormatter ) formatVectorResult (results []any , includeMetadata bool ) []FormattedTimeData {
141141 var formattedResults []FormattedTimeData
142142
143143 for _ , result := range results {
144- if resultMap , ok := result .(map [string ]interface {} ); ok {
144+ if resultMap , ok := result .(map [string ]any ); ok {
145145 formattedData := FormattedTimeData {
146146 Metric : make (map [string ]string ),
147147 }
148148
149149 // 处理指标标签
150- if metric , ok := resultMap ["metric" ].(map [string ]interface {} ); ok {
150+ if metric , ok := resultMap ["metric" ].(map [string ]any ); ok {
151151 for key , value := range metric {
152152 if strValue , ok := value .(string ); ok {
153153 formattedData .Metric [key ] = strValue
@@ -156,7 +156,7 @@ func (f *PrometheusDataFormatter) formatVectorResult(results []interface{}, incl
156156 }
157157
158158 // 处理值
159- if value , ok := resultMap ["value" ].([]interface {} ); ok && len (value ) >= 2 {
159+ if value , ok := resultMap ["value" ].([]any ); ok && len (value ) >= 2 {
160160 if timestamp , ok := value [0 ].(float64 ); ok {
161161 // 格式化时间戳
162162 formattedTime , err := f .FormatTimestampWithTimezone (timestamp , "" )
@@ -172,7 +172,7 @@ func (f *PrometheusDataFormatter) formatVectorResult(results []interface{}, incl
172172
173173 // 添加元数据(可选)
174174 if includeMetadata {
175- formattedData .Metadata = map [string ]interface {} {
175+ formattedData .Metadata = map [string ]any {
176176 "result_type" : "vector" ,
177177 "processed_at" : time .Now ().Format ("2006-01-02T15:04:05-07:00" ),
178178 }
@@ -186,14 +186,14 @@ func (f *PrometheusDataFormatter) formatVectorResult(results []interface{}, incl
186186}
187187
188188// formatMatrixResult 格式化范围向量查询结果
189- func (f * PrometheusDataFormatter ) formatMatrixResult (results []interface {} , includeMetadata bool ) []FormattedTimeData {
189+ func (f * PrometheusDataFormatter ) formatMatrixResult (results []any , includeMetadata bool ) []FormattedTimeData {
190190 var formattedResults []FormattedTimeData
191191
192192 for _ , result := range results {
193- if resultMap , ok := result .(map [string ]interface {} ); ok {
193+ if resultMap , ok := result .(map [string ]any ); ok {
194194 // 处理指标标签
195195 metric := make (map [string ]string )
196- if metricData , ok := resultMap ["metric" ].(map [string ]interface {} ); ok {
196+ if metricData , ok := resultMap ["metric" ].(map [string ]any ); ok {
197197 for key , value := range metricData {
198198 if strValue , ok := value .(string ); ok {
199199 metric [key ] = strValue
@@ -202,9 +202,9 @@ func (f *PrometheusDataFormatter) formatMatrixResult(results []interface{}, incl
202202 }
203203
204204 // 处理时间序列数据
205- if values , ok := resultMap ["values" ].([]interface {} ); ok {
205+ if values , ok := resultMap ["values" ].([]any ); ok {
206206 for _ , valuePoint := range values {
207- if point , ok := valuePoint .([]interface {} ); ok && len (point ) >= 2 {
207+ if point , ok := valuePoint .([]any ); ok && len (point ) >= 2 {
208208 formattedData := FormattedTimeData {
209209 Metric : metric ,
210210 }
@@ -223,7 +223,7 @@ func (f *PrometheusDataFormatter) formatMatrixResult(results []interface{}, incl
223223
224224 // 添加元数据(可选)
225225 if includeMetadata {
226- formattedData .Metadata = map [string ]interface {} {
226+ formattedData .Metadata = map [string ]any {
227227 "result_type" : "matrix" ,
228228 "processed_at" : time .Now ().Format ("2006-01-02T15:04:05-07:00" ),
229229 }
@@ -240,11 +240,11 @@ func (f *PrometheusDataFormatter) formatMatrixResult(results []interface{}, incl
240240}
241241
242242// formatScalarResult 格式化标量查询结果
243- func (f * PrometheusDataFormatter ) formatScalarResult (results []interface {} , includeMetadata bool ) []FormattedTimeData {
243+ func (f * PrometheusDataFormatter ) formatScalarResult (results []any , includeMetadata bool ) []FormattedTimeData {
244244 var formattedResults []FormattedTimeData
245245
246246 if len (results ) > 0 {
247- if scalar , ok := results [0 ].([]interface {} ); ok && len (scalar ) >= 2 {
247+ if scalar , ok := results [0 ].([]any ); ok && len (scalar ) >= 2 {
248248 formattedData := FormattedTimeData {
249249 Metric : make (map [string ]string ),
250250 }
@@ -263,7 +263,7 @@ func (f *PrometheusDataFormatter) formatScalarResult(results []interface{}, incl
263263
264264 // 添加元数据(可选)
265265 if includeMetadata {
266- formattedData .Metadata = map [string ]interface {} {
266+ formattedData .Metadata = map [string ]any {
267267 "result_type" : "scalar" ,
268268 "processed_at" : time .Now ().Format ("2006-01-02T15:04:05-07:00" ),
269269 }
0 commit comments