Skip to content

Commit 02d435c

Browse files
authored
Merge pull request #30 from JaD1ng/mock
doc: 重写readme文档并添加API文档
2 parents 411a26c + 517610e commit 02d435c

File tree

12 files changed

+2209
-483
lines changed

12 files changed

+2209
-483
lines changed

mcp-server/pkg/formatter/formatter.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ func NewPrometheusDataFormatter(timezone string) *PrometheusDataFormatter {
2424

2525
// FormattedTimeData 格式化后的时间数据结构
2626
type 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查询响应结构
3535
type 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
}

mcp-server/pkg/models/models.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66

77
// StepResult 定义通用步骤结果结构
88
type StepResult struct {
9-
StepName string `json:"step_name"`
10-
Status string `json:"status"` // success / failed / warning
11-
Summary string `json:"summary"`
12-
Details map[string]interface{} `json:"details"`
13-
Timestamp string `json:"timestamp"`
9+
StepName string `json:"step_name"`
10+
Status string `json:"status"` // success / failed / warning
11+
Summary string `json:"summary"`
12+
Details map[string]any `json:"details"`
13+
Timestamp string `json:"timestamp"`
1414
}
1515

1616
// NewStepResult 工具函数:创建成功结果
17-
func NewStepResult(step string, summary string, details map[string]interface{}) StepResult {
17+
func NewStepResult(step string, summary string, details map[string]any) StepResult {
1818
return StepResult{
1919
StepName: step,
2020
Status: "success",

0 commit comments

Comments
 (0)