Commit 37f16c6f authored by 李科's avatar 李科

feat: 预警分类树形结构

parent 195c3777
package response package response
import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
)
type AlertClassItem struct { type AlertClassItem struct {
entity.AlertClass `xorm:"extends"` entity.AlertClass `xorm:"extends"`
...@@ -10,3 +12,8 @@ type AlertClassList struct { ...@@ -10,3 +12,8 @@ type AlertClassList struct {
TotalCount int64 `json:"total_count"` TotalCount int64 `json:"total_count"`
List []AlertClassItem `json:"list"` List []AlertClassItem `json:"list"`
} }
type AlertClassNode struct {
entity.AlertClass
Children []*AlertClassNode `json:"children"`
}
package response
import (
"fmt"
json "github.com/json-iterator/go"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"sort"
"testing"
)
func BuildTree(nodes []entity.AlertClass) ([]*AlertClassNode, error) {
nodeMap := make(map[int]*AlertClassNode)
// 创建所有节点并存储到映射表中
for _, node := range nodes {
tree := &AlertClassNode{
AlertClass: node,
Children: []*AlertClassNode{},
}
nodeMap[node.ClassId] = tree
}
var rootNodes []*AlertClassNode
for _, node := range nodes {
if node.ParentId == 0 {
rootNodes = append(rootNodes, nodeMap[node.ClassId])
} else {
parent := nodeMap[node.ParentId]
if parent == nil {
return nil, fmt.Errorf("parent node not found for ClassId: %d", node.ClassId)
}
parent.Children = append(parent.Children, nodeMap[node.ClassId])
}
}
sortTree(rootNodes)
return rootNodes, nil
}
func sortTree(nodes []*AlertClassNode) {
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].SortOrder < nodes[j].SortOrder
})
for _, node := range nodes {
sortTree(node.Children)
}
}
func TestTree(t *testing.T) {
// 示例数据
data := []entity.AlertClass{
{ClassId: 1, ClassName: "Root", ParentId: 0, SortOrder: 0},
{ClassId: 2, ClassName: "Child 1", ParentId: 1, SortOrder: 0},
{ClassId: 3, ClassName: "Child 2", ParentId: 1, SortOrder: 0},
{ClassId: 4, ClassName: "Grandchild 3", ParentId: 2, SortOrder: 3},
{ClassId: 5, ClassName: "Grandchild 1", ParentId: 2, SortOrder: 1},
{ClassId: 6, ClassName: "Grandchild 2", ParentId: 2, SortOrder: 2},
}
rootNodes, err := BuildTree(data)
if err != nil {
fmt.Println("Failed to build tree:", err)
return
}
// 将树形结构转换为 JSON 字符串
jsonData, err := json.Marshal(rootNodes)
if err != nil {
fmt.Println("Failed to marshal tree:", err)
return
}
fmt.Println(string(jsonData))
}
...@@ -90,6 +90,21 @@ func ListAlertClass(c *gin.Context) { ...@@ -90,6 +90,21 @@ func ListAlertClass(c *gin.Context) {
SendJsonResponse(c, resp.OK, data) SendJsonResponse(c, resp.OK, data)
} }
func TreeAlertClass(c *gin.Context) {
var req request.ListAlertClass
if err := c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil)
return
}
svc := service.AlertClassSvc{User: header.GetUser(c)}
data, err := svc.Tree(req)
if err != nil {
SendJsonResponse(c, resp.FAIL.WithError(err), nil)
return
}
SendJsonResponse(c, resp.OK, data)
}
func DeleteAlertClass(c *gin.Context) { func DeleteAlertClass(c *gin.Context) {
var req request.DeleteAlertClass var req request.DeleteAlertClass
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
......
...@@ -26,5 +26,6 @@ func InitMetricConfigRouter(e *gin.Engine) { ...@@ -26,5 +26,6 @@ func InitMetricConfigRouter(e *gin.Engine) {
acGroup.DELETE("", controller.DeleteAlertClass) acGroup.DELETE("", controller.DeleteAlertClass)
acGroup.GET("", controller.DetailAlertClass) acGroup.GET("", controller.DetailAlertClass)
acGroup.GET("list", controller.ListAlertClass) acGroup.GET("list", controller.ListAlertClass)
acGroup.GET("tree", controller.TreeAlertClass)
} }
} }
...@@ -2,6 +2,7 @@ package service ...@@ -2,6 +2,7 @@ package service
import ( import (
"errors" "errors"
"fmt"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
...@@ -9,6 +10,7 @@ import ( ...@@ -9,6 +10,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"sort"
) )
type AlertClassSvc struct { type AlertClassSvc struct {
...@@ -156,6 +158,58 @@ func (m *AlertClassSvc) List(req request.ListAlertClass) (resp response.AlertCla ...@@ -156,6 +158,58 @@ func (m *AlertClassSvc) List(req request.ListAlertClass) (resp response.AlertCla
return return
} }
func (m *AlertClassSvc) Tree(req request.ListAlertClass) (resp []*response.AlertClassNode, err error) {
db, err := client.GetDbClient()
if err != nil {
return
}
session := db.NewSession()
defer session.Close()
var list []entity.AlertClass
_, err = session.OrderBy("sort_order").FindAndCount(&list)
// TODO 对req进行过滤
resp, err = AlertClassTree(list)
return
}
func AlertClassTree(nodes []entity.AlertClass) ([]*response.AlertClassNode, error) {
nodeMap := make(map[int]*response.AlertClassNode)
// 创建所有节点并存储到映射表中
for _, node := range nodes {
tree := &response.AlertClassNode{
AlertClass: node,
Children: []*response.AlertClassNode{},
}
nodeMap[node.ClassId] = tree
}
var rootNodes []*response.AlertClassNode
for _, node := range nodes {
if node.ParentId == 0 {
rootNodes = append(rootNodes, nodeMap[node.ClassId])
} else {
parent := nodeMap[node.ParentId]
if parent == nil {
return nil, fmt.Errorf("parent node not found for ClassId: %d", node.ClassId)
}
parent.Children = append(parent.Children, nodeMap[node.ClassId])
}
}
sortTree(rootNodes)
return rootNodes, nil
}
func sortTree(nodes []*response.AlertClassNode) {
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].SortOrder < nodes[j].SortOrder
})
for _, node := range nodes {
sortTree(node.Children)
}
}
func (m *AlertClassSvc) SortOrderMax(parentId int) (max int, err error) { func (m *AlertClassSvc) SortOrderMax(parentId int) (max int, err error) {
db, err := client.GetDbClient() db, err := client.GetDbClient()
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment