支持全局数据统计 1.rdb分类资源数量 2.告警数量 (#447)

This commit is contained in:
alick-liming 2020-12-08 23:47:09 +08:00 committed by GitHub
parent 68bccb4d3f
commit 5bd9c5fefc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 9 deletions

View File

@ -102,7 +102,14 @@ func (e *Event) GetEventDetail() ([]EventDetail, error) {
}
func EventTotal(stime, etime int64, nodePath, query, eventType string, priorities, sendTypes []string) (int64, error) {
session := DB["mon"].Where("etime > ? and etime < ? and (node_path = ? or node_path like ?)", stime, etime, nodePath, nodePath+".%")
sql := "etime > ? and etime < ?"
sqlParamValue := []interface{}{stime, etime}
if nodePath != "" {
sql += " and (node_path = ? or node_path like ?) "
sqlParamValue = []interface{}{stime, etime, nodePath, nodePath + ".%"}
}
session := DB["mon"].Where(sql, sqlParamValue...)
if len(priorities) > 0 && priorities[0] != "" {
session = session.In("priority", priorities)
}
@ -134,7 +141,14 @@ func EventTotal(stime, etime int64, nodePath, query, eventType string, prioritie
func EventGets(stime, etime int64, nodePath, query, eventType string, priorities, sendTypes []string, limit, offset int) ([]Event, error) {
var objs []Event
session := DB["mon"].Where("etime > ? and etime < ? and (node_path = ? or node_path like ?)", stime, etime, nodePath, nodePath+".%")
sql := "etime > ? and etime < ?"
sqlParamValue := []interface{}{stime, etime}
if nodePath != "" {
sql += " and (node_path = ? or node_path like ?) "
sqlParamValue = []interface{}{stime, etime, nodePath, nodePath + ".%"}
}
session := DB["mon"].Where(sql, sqlParamValue...)
if len(priorities) > 0 && priorities[0] != "" {
session = session.In("priority", priorities)
}

View File

@ -163,7 +163,14 @@ func SaveEventCurStatus(hashid uint64, status string) error {
}
func EventCurTotal(stime, etime int64, nodePath, query string, priorities, sendTypes []string) (int64, error) {
session := DB["mon"].Where("etime > ? and etime < ? and (node_path = ? or node_path like ?) and ignore_alert=0", stime, etime, nodePath, nodePath+".%")
sql := "etime > ? and etime < ? and ignore_alert=0"
sqlParamValue := []interface{}{stime, etime}
if nodePath != "" {
sql += " and (node_path = ? or node_path like ?) "
sqlParamValue = []interface{}{stime, etime, nodePath, nodePath + ".%"}
}
session := DB["mon"].Where(sql, sqlParamValue...)
if len(priorities) > 0 && priorities[0] != "" {
session = session.In("priority", priorities)
}
@ -191,7 +198,14 @@ func EventCurTotal(stime, etime int64, nodePath, query string, priorities, sendT
func EventCurGets(stime, etime int64, nodePath, query string, priorities, sendTypes []string, limit, offset int) ([]EventCur, error) {
var obj []EventCur
session := DB["mon"].Where("etime > ? and etime < ? and (node_path = ? or node_path like ?) and ignore_alert=0", stime, etime, nodePath, nodePath+".%")
sql := "etime > ? and etime < ? and ignore_alert=0"
sqlParamValue := []interface{}{stime, etime}
if nodePath != "" {
sql += " and (node_path = ? or node_path like ?) "
sqlParamValue = []interface{}{stime, etime, nodePath, nodePath + ".%"}
}
session := DB["mon"].Where(sql, sqlParamValue...)
if len(priorities) > 0 && priorities[0] != "" {
session = session.In("priority", priorities)
}

View File

@ -52,7 +52,7 @@ func eventCurGets(c *gin.Context) {
stime := mustQueryInt64(c, "stime")
etime := mustQueryInt64(c, "etime")
nodePath := mustQueryStr(c, "nodepath")
nodePath := queryStr(c, "nodepath", "")
limit := queryInt(c, "limit", 20)
@ -146,7 +146,7 @@ func eventHisGets(c *gin.Context) {
stime := mustQueryInt64(c, "stime")
etime := mustQueryInt64(c, "etime")
nodePath := mustQueryStr(c, "nodepath")
nodePath := queryStr(c, "nodepath", "")
limit := queryInt(c, "limit", 20)

View File

@ -129,6 +129,8 @@ func Config(r *gin.Engine) {
userLogin.PUT("/resources/note", resourceNotePut)
userLogin.GET("/resources/bindings", resourceBindingsGet)
userLogin.GET("/resources/orphan", resourceOrphanGet)
userLogin.GET("/resources/cate-count", renderAllResourcesCountByCate)
}
v1 := r.Group("/v1/rdb").Use(shouldBeService())

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/slice"
"github.com/toolkits/pkg/str"
@ -487,9 +488,9 @@ type nodeResourcesCountResp struct {
Count int `json:"count"`
}
func renderNodeResourcesCountByCate(c *gin.Context) {
needSourceList := []string{"physical", "virtual", "redis", "mongo", "mysql", "container", "sw", "volume"}
var needSourceList = []string{"physical", "virtual", "redis", "mongo", "mysql", "container", "sw", "volume"}
func renderNodeResourcesCountByCate(c *gin.Context) {
nodeId := urlParamInt64(c, "id")
node := Node(nodeId)
leadIds, err := node.LeafIds()
@ -503,7 +504,44 @@ func renderNodeResourcesCountByCate(c *gin.Context) {
ress, err := models.ResourceUnderNodeGets(leadIds, query, batch, field, limit, 0)
dangerous(err)
aggDat := make(map[string]int, len(ress))
aggDat := make(map[string]int, len(needSourceList))
for _, res := range ress {
cate := res.Cate
if cate != "" {
if _, ok := aggDat[cate]; !ok {
aggDat[cate] = 0
}
aggDat[cate]++
}
}
for _, need := range needSourceList {
if _, ok := aggDat[need]; !ok {
aggDat[need] = 0
}
}
var list []*nodeResourcesCountResp
for n, c := range aggDat {
ns := new(nodeResourcesCountResp)
ns.Name = n
ns.Count = c
list = append(list, ns)
}
renderData(c, list, nil)
}
func renderAllResourcesCountByCate(c *gin.Context) {
aggDat := make(map[string]int, len(needSourceList))
ress, err := models.ResourceGets("", nil)
if err != nil {
logger.Error(err)
dangerous(err)
}
for _, res := range ress {
cate := res.Cate
if cate != "" {