diff --git a/src/models/mon_event.go b/src/models/mon_event.go index 7416a1cb..d43ac5c3 100644 --- a/src/models/mon_event.go +++ b/src/models/mon_event.go @@ -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) } diff --git a/src/models/mon_event_cur.go b/src/models/mon_event_cur.go index 0f08bf1d..28e57956 100644 --- a/src/models/mon_event_cur.go +++ b/src/models/mon_event_cur.go @@ -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) } diff --git a/src/modules/monapi/http/router_event.go b/src/modules/monapi/http/router_event.go index 2712568c..2dabfb75 100644 --- a/src/modules/monapi/http/router_event.go +++ b/src/modules/monapi/http/router_event.go @@ -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) diff --git a/src/modules/rdb/http/router.go b/src/modules/rdb/http/router.go index 871a6021..e560eb46 100644 --- a/src/modules/rdb/http/router.go +++ b/src/modules/rdb/http/router.go @@ -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()) diff --git a/src/modules/rdb/http/router_resource.go b/src/modules/rdb/http/router_resource.go index 5b72abba..4e6204a7 100644 --- a/src/modules/rdb/http/router_resource.go +++ b/src/modules/rdb/http/router_resource.go @@ -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 != "" {