Skip to content

Commit

Permalink
logdashboard v2的schema发生了变化:displayName会出现在basicOptions字段中,外侧display的…
Browse files Browse the repository at this point in the history
…displayName会变成空字符串;添加同时基于x轴和y轴的charts排序
  • Loading branch information
shinny-taojiachun committed Nov 30, 2023
1 parent 0a0b3b5 commit 0730f41
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions alicloud/resource_alicloud_log_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,45 @@ func resourceAlicloudLogDashboardRead(d *schema.ResourceData, meta interface{})
return WrapError(err)
}
}

// 先取出charts
charts := dashboard["charts"].([]interface{})
// 字段清理
for k, v := range charts {
// 将charts中的action字段删除
if action, actionOK := v.(map[string]interface{})["action"]; actionOK {
if action == nil {
delete(charts[k].(map[string]interface{}), "action")
}
}
// 如果有basicOptions字段请basicOptions中的displayName字段存在,则将display字段中的displayName字段删除
var display, basicOptions interface{}
var ok bool
if display, ok = v.(map[string]interface{})["display"]; !ok {
continue
} else if basicOptions, ok = display.(map[string]interface{})["basicOptions"]; !ok {
continue
} else if _, ok = basicOptions.(map[string]interface{})["displayName"]; !ok {
continue
} else if _, ok = display.(map[string]interface{})["displayName"]; ok {
delete(display.(map[string]interface{}), "displayName")
}
}
sort.Slice(charts, func(i, j int) bool {
return charts[i].(map[string]interface{})["display"].(map[string]interface{})["yPos"].(float64) < charts[j].(map[string]interface{})["display"].(map[string]interface{})["yPos"].(float64)

// 再将charts按照yPos排序
sort.SliceStable(charts, func(i, j int) bool {
yi := charts[i].(map[string]interface{})["display"].(map[string]interface{})["yPos"].(float64)
yj := charts[j].(map[string]interface{})["display"].(map[string]interface{})["yPos"].(float64)
xi := charts[i].(map[string]interface{})["display"].(map[string]interface{})["xPos"].(float64)
xj := charts[j].(map[string]interface{})["display"].(map[string]interface{})["xPos"].(float64)
if yj != yi {
return yi < yj
}
return xi < xj
})
charlist, err := json.Marshal(dashboard["charts"])

// 最后将charts转为json字符串
charlist, err := json.Marshal(charts)
if err != nil {
return WrapError(err)
}
Expand Down

0 comments on commit 0730f41

Please sign in to comment.