Skip to content

Commit

Permalink
feat: BMW: ES链路结果表详情路由补充DataLabel信息 --story=121706267 (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
EASYGOING45 authored Feb 12, 2025
1 parent c262f06 commit 23a5a14
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/bk-monitor-worker/internal/metadata/service/spaceredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ func (s *SpacePusher) composeEsTableIdDetail(tableId string, options map[string]
return "", "", err
}

var rt resulttable.ResultTable
if err := resulttable.NewResultTableQuerySet(db).Select(resulttable.ResultTableDBSchema.DataLabel).TableIdEq(tableId).One(&rt); err != nil {
return tableId, "", err
}
// 组装数据
detailStr, err := jsonx.MarshalString(map[string]any{
"storage_type": models.StorageTypeES,
Expand All @@ -697,8 +701,13 @@ func (s *SpacePusher) composeEsTableIdDetail(tableId string, options map[string]
"source_type": sourceType,
"options": options,
"storage_cluster_records": clusterRecords,
"data_label": rt.DataLabel,
})
logger.Infof("compose es table id detail success, table_id [%s], detail [%s]", tableId, detailStr)
if err != nil {
return tableId, "", err
}

logger.Infof("composeEsTableIdDetail:compose success, table_id [%s], detail [%s]", tableId, detailStr)
return tableId, detailStr, err
}

Expand Down
112 changes: 112 additions & 0 deletions pkg/bk-monitor-worker/internal/metadata/service/spaceredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package service
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"testing"
Expand Down Expand Up @@ -1318,3 +1319,114 @@ func TestSpacePusher_ComposeData(t *testing.T) {
}
assert.Equal(t, expectedForOthers, valuesForOthers, "Unexpected result for space 1003")
}

func TestSpacePusher_composeEsTableIdDetail(t *testing.T) {
// 初始化数据库
mocker.InitTestDBConfig("../../../bmw_test.yaml")
db := mysql.GetDBSession().DB
db.AutoMigrate(&storage.ESStorage{}, &resulttable.ResultTable{}, &storage.ClusterRecord{})

// 准备测试数据
tableID1 := "1001_bkmonitor_time_series_50010.__default__"
tableID2 := "1001_bkmonitor_time_series_50011.__default__"
dataLabel1 := "a" // 初始化为字符串

// 插入 ResultTable 数据
resultTables := []resulttable.ResultTable{
{
TableId: tableID1,
BkBizId: 1001,
BkBizIdAlias: "appid",
DataLabel: &dataLabel1, // 使用字符串指针
},
{
TableId: tableID2,
BkBizId: 1001,
BkBizIdAlias: "",
DataLabel: nil,
},
}
for _, rt := range resultTables {
db.Delete(&resulttable.ResultTable{}, "table_id = ?", rt.TableId)
assert.NoError(t, db.Create(&rt).Error, "Failed to insert ResultTable")
}

//// 插入 ResultTable 数据
//resultTable := resulttable.ResultTable{
// TableId: tableID1,
// BkBizId: 1001,
// BkBizIdAlias: "appid",
// DataLabel: &dataLabel1, // 使用字符串指针
//}
//
//// 确保数据不存在后重新插入
//db.Delete(&resulttable.ResultTable{}, "table_id = ?", resultTable.TableId)
//assert.NoError(t, db.Create(&resultTable).Error, "Failed to insert ResultTable")

// 准备 SpacePusher 实例
spacePusher := SpacePusher{}

// 调用测试方法
tableID, detailStr, err := spacePusher.composeEsTableIdDetail(
tableID1,
map[string]interface{}{"option1": "value1"},
1,
"sourceType1",
"indexSet1",
)

// 断言返回结果无错误
assert.NoError(t, err, "composeEsTableIdDetail should not return an error")
assert.Equal(t, tableID1, tableID, "TableID should match")

// 期望的 JSON 数据(单个对象)
expectedDetail := map[string]interface{}{
"measurement": "__default__",
"source_type": "sourceType1",
"options": map[string]interface{}{"option1": "value1"},
"storage_cluster_records": []interface{}{},
"data_label": "a",
"storage_type": "elasticsearch",
"storage_id": float64(1), // 修改为 float64
"db": "indexSet1",
}

// 将 detailStr 转换为 map 以便比较
var actualDetail map[string]interface{}
err = json.Unmarshal([]byte(detailStr), &actualDetail)
assert.NoError(t, err, "detailStr should be valid JSON")

// 比较预期值和实际值
assert.Equal(t, expectedDetail, actualDetail, "detailStr should match expected JSON")

// 调用测试方法
resTid, detailStr2, err := spacePusher.composeEsTableIdDetail(
tableID2,
map[string]interface{}{"option1": "value1"},
1,
"sourceType1",
"indexSet1",
)

expectedDetail2 := map[string]interface{}{
"measurement": "__default__",
"source_type": "sourceType1",
"options": map[string]interface{}{"option1": "value1"},
"storage_cluster_records": []interface{}{},
"data_label": nil,
"storage_type": "elasticsearch",
"storage_id": float64(1), // 修改为 float64
"db": "indexSet1",
}

// 将 detailStr 转换为 map 以便比较
var actualDetail2 map[string]interface{}
err = json.Unmarshal([]byte(detailStr2), &actualDetail2)
assert.NoError(t, err, "detailStr should be valid JSON")

assert.NoError(t, err, "composeEsTableIdDetail should not return an error")
assert.Equal(t, resTid, tableID2, "TableID should match")

assert.Equal(t, expectedDetail2, actualDetail2, "detailStr should match expected JSON")

}

0 comments on commit 23a5a14

Please sign in to comment.