From 5bd9c5fefc7131bb2c9ceec18797ae963c8f1945 Mon Sep 17 00:00:00 2001 From: alick-liming Date: Tue, 8 Dec 2020 23:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=85=A8=E5=B1=80=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=9F=E8=AE=A1=201.rdb=E5=88=86=E7=B1=BB=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=95=B0=E9=87=8F=202.=E5=91=8A=E8=AD=A6=E6=95=B0?= =?UTF-8?q?=E9=87=8F=20(#447)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/mon_event.go | 18 ++++++++-- src/models/mon_event_cur.go | 18 ++++++++-- src/modules/monapi/http/router_event.go | 4 +-- src/modules/rdb/http/router.go | 2 ++ src/modules/rdb/http/router_resource.go | 44 +++++++++++++++++++++++-- 5 files changed, 77 insertions(+), 9 deletions(-) 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 != "" {