未验证 提交 5bd9c5fe 编写于 作者: qd_lm's avatar qd_lm 提交者: GitHub

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

上级 68bccb4d
......@@ -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)
}
......
......@@ -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)
}
......
......@@ -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)
......
......@@ -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())
......
......@@ -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 != "" {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册