Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
37f16c6f
Commit
37f16c6f
authored
Jun 29, 2023
by
李科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 预警分类树形结构
parent
195c3777
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
152 additions
and
1 deletion
+152
-1
src/bean/vo/response/alert_class.go
src/bean/vo/response/alert_class.go
+8
-1
src/bean/vo/response/alert_class_test.go
src/bean/vo/response/alert_class_test.go
+74
-0
src/controller/alert_class.go
src/controller/alert_class.go
+15
-0
src/router/metricconfigrouter.go
src/router/metricconfigrouter.go
+1
-0
src/service/alert_class.go
src/service/alert_class.go
+54
-0
No files found.
src/bean/vo/response/alert_class.go
View file @
37f16c6f
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"`
}
src/bean/vo/response/alert_class_test.go
0 → 100644
View file @
37f16c6f
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
))
}
src/controller/alert_class.go
View file @
37f16c6f
...
@@ -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
{
...
...
src/router/metricconfigrouter.go
View file @
37f16c6f
...
@@ -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
)
}
}
}
}
src/service/alert_class.go
View file @
37f16c6f
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment