Commit cf574e36 authored by jingbo.wang's avatar jingbo.wang

优化报表sql

parent 648368d2
...@@ -14,19 +14,15 @@ type Node struct { ...@@ -14,19 +14,15 @@ type Node struct {
//cpu //cpu
averageCpu int averageCpu int
maxCpu int maxCpu int
medianCpu int
//mem //mem
averageMem int averageMem int
maxMem int maxMem int
medianMem int
//disk //disk
averageDisk int averageDisk int
maxDisk int maxDisk int
medianDisk int
//thread //thread
averageThread int averageThread int
maxThread int maxThread int
medianThread int
} }
func NewNode(serviceName string, address string, startTime time.Time, endTime time.Time) *Node { func NewNode(serviceName string, address string, startTime time.Time, endTime time.Time) *Node {
...@@ -45,11 +41,11 @@ func NewNode(serviceName string, address string, startTime time.Time, endTime ti ...@@ -45,11 +41,11 @@ func NewNode(serviceName string, address string, startTime time.Time, endTime ti
return rtn return rtn
} }
//依次返回:中位、平均、最大 //依次返回:平均、最大
//如果获取失败返回0,0,0 //如果获取失败返回0,0,0
func (n *Node) queryKey(key string) (int, int, int) { func (n *Node) queryKey(key string) (int, int) {
//注:这里使用了子查询,InfluxDB目前不支持函数参数为表达式,如:sum("key"*10) //注:这里使用了子查询,InfluxDB目前不支持函数参数为表达式,如:sum("key"*10)
sqlFormat := `SELECT median("x"), mean("x"), max("x") ` + sqlFormat := `SELECT mean("x"), max("x") ` +
`FROM (` + `FROM (` +
`SELECT %s AS "x" FROM machine_info ` + `SELECT %s AS "x" FROM machine_info ` +
`WHERE sys_name = '%s' AND host = '%s'AND time >= %d AND time < %d` + `WHERE sys_name = '%s' AND host = '%s'AND time >= %d AND time < %d` +
...@@ -61,49 +57,47 @@ func (n *Node) queryKey(key string) (int, int, int) { ...@@ -61,49 +57,47 @@ func (n *Node) queryKey(key string) (int, int, int) {
glog.Error("node query key:", err) glog.Error("node query key:", err)
} }
if len(resp.Results) != 1 { if len(resp.Results) != 1 {
return 0, 0, 0 return 0, 0
} }
res := resp.Results[0] res := resp.Results[0]
if len(res.Series) != 1 { if len(res.Series) != 1 {
return 0, 0, 0 return 0, 0
} }
row := res.Series[0] row := res.Series[0]
if len(row.Values) != 1 { if len(row.Values) != 1 {
return 0, 0, 0 return 0, 0
} }
value := row.Values[0] value := row.Values[0]
medianIdx, ok1 := getModRowKeyIdx("median", row)
meanIdx, ok2 := getModRowKeyIdx("mean", row) meanIdx, ok2 := getModRowKeyIdx("mean", row)
maxIdx, ok3 := getModRowKeyIdx("max", row) maxIdx, ok3 := getModRowKeyIdx("max", row)
if !ok1 || !ok2 || !ok3 { if !ok2 || !ok3 {
return 0, 0, 0 return 0, 0
} }
median, ok1 := jsonNumberToInt(value[medianIdx])
mean, ok2 := jsonNumberToInt(value[meanIdx]) mean, ok2 := jsonNumberToInt(value[meanIdx])
max, ok3 := jsonNumberToInt(value[maxIdx]) max, ok3 := jsonNumberToInt(value[maxIdx])
if !ok1 || !ok2 || !ok3 { if !ok2 || !ok3 {
return 0, 0, 0 return 0, 0
} }
return median, mean, max return mean, max
} }
func (n *Node) initCpu() { func (n *Node) initCpu() {
n.medianCpu, n.averageCpu, n.maxCpu = n.queryKey(`"processors"`) n.averageCpu, n.maxCpu = n.queryKey(`"processors"`)
} }
func (n *Node) initMem() { func (n *Node) initMem() {
n.medianMem, n.averageMem, n.maxMem = n.queryKey(`(mem_tol-mem_free)/mem_tol*100`) n.averageMem, n.maxMem = n.queryKey(`(mem_tol-mem_free)/mem_tol*100`)
} }
func (n *Node) initDisk() { func (n *Node) initDisk() {
n.medianDisk, n.averageDisk, n.maxDisk = n.queryKey(`(disk_tol-disk_free)/disk_tol*100`) n.averageDisk, n.maxDisk = n.queryKey(`(disk_tol-disk_free)/disk_tol*100`)
} }
func (n *Node) initThread() { func (n *Node) initThread() {
n.medianThread, n.averageThread, n.maxThread = n.queryKey(`"thread_peak"`) n.averageThread, n.maxThread = n.queryKey(`"thread_peak"`)
} }
func (n *Node) GetServiceName() string { func (n *Node) GetServiceName() string {
...@@ -126,10 +120,6 @@ func (n *Node) GetAverageCpu() int { ...@@ -126,10 +120,6 @@ func (n *Node) GetAverageCpu() int {
return n.averageCpu return n.averageCpu
} }
func (n *Node) GetMedianCpu() int {
return n.medianCpu
}
func (n *Node) GetMaxCpu() int { func (n *Node) GetMaxCpu() int {
return n.maxCpu return n.maxCpu
} }
...@@ -138,10 +128,6 @@ func (n *Node) GetAverageMem() int { ...@@ -138,10 +128,6 @@ func (n *Node) GetAverageMem() int {
return n.averageMem return n.averageMem
} }
func (n *Node) GetMedianMem() int {
return n.medianMem
}
func (n *Node) GetMaxMem() int { func (n *Node) GetMaxMem() int {
return n.maxMem return n.maxMem
} }
...@@ -150,10 +136,6 @@ func (n *Node) GetAverageDisk() int { ...@@ -150,10 +136,6 @@ func (n *Node) GetAverageDisk() int {
return n.averageDisk return n.averageDisk
} }
func (n *Node) GetMedianDisk() int {
return n.medianDisk
}
func (n *Node) GetMaxDisk() int { func (n *Node) GetMaxDisk() int {
return n.maxDisk return n.maxDisk
} }
...@@ -162,10 +144,6 @@ func (n *Node) GetAverageThread() int { ...@@ -162,10 +144,6 @@ func (n *Node) GetAverageThread() int {
return n.averageThread return n.averageThread
} }
func (n *Node) GetMedianThread() int {
return n.medianThread
}
func (n *Node) GetMaxThread() int { func (n *Node) GetMaxThread() int {
return n.maxThread return n.maxThread
} }
...@@ -18,8 +18,8 @@ func TestNewNode(t *testing.T) { ...@@ -18,8 +18,8 @@ func TestNewNode(t *testing.T) {
node := NewNode("cuishou", "172.16.6.92", startTime, endTime) node := NewNode("cuishou", "172.16.6.92", startTime, endTime)
t.Log(node.GetServiceName()) t.Log(node.GetServiceName())
t.Log(node.GetAddress()) t.Log(node.GetAddress())
t.Log("cpu", node.GetAverageCpu(), node.GetMedianCpu(), node.GetMaxCpu()) t.Log("cpu", node.GetAverageCpu(), node.GetMaxCpu())
t.Log("mem", node.GetAverageMem(), node.GetMedianMem(), node.GetMaxMem()) t.Log("mem", node.GetAverageMem(), node.GetMaxMem())
t.Log("disk", node.GetAverageDisk(), node.GetMedianDisk(), node.GetMaxDisk()) t.Log("disk", node.GetAverageDisk(), node.GetMaxDisk())
t.Log("thread", node.GetAverageThread(), node.GetMedianThread(), node.GetMaxThread()) t.Log("thread", node.GetAverageThread(), node.GetMaxThread())
} }
...@@ -120,7 +120,7 @@ func (t *Table) AddRecord(values ...interface{}) error { ...@@ -120,7 +120,7 @@ func (t *Table) AddRecord(values ...interface{}) error {
func Run(n int) string { func Run(n int) string {
const ( const (
cpuMin = 5 cpuMin = 1
memMin = 10 memMin = 10
diskMin = 20 diskMin = 20
) )
...@@ -196,7 +196,7 @@ func Run(n int) string { ...@@ -196,7 +196,7 @@ func Run(n int) string {
if s.GetAverageMem() < memMin { if s.GetAverageMem() < memMin {
break break
} }
_ = memTable.AddRecord(s.Name(), s.GetAverageMem(), s.GetCount(), s.GetAverageDuration()) _ = memTable.AddRecord(s.GetAverageMem(), s.Name(), s.GetCount(), s.GetAverageDuration())
} }
rtn.WriteString(memTable.ToString()) rtn.WriteString(memTable.ToString())
rtn.WriteString("\n") rtn.WriteString("\n")
......
...@@ -58,6 +58,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service { ...@@ -58,6 +58,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
if _, ok := IgnorePathMap[strings.ToLower(pathSplit[1])]; ok { if _, ok := IgnorePathMap[strings.ToLower(pathSplit[1])]; ok {
continue continue
} }
glog.Info("init path:", rtn.name, path)
pathObj, ok := NewPath(rtn.name, path, rtn.startTime, rtn.endTime) pathObj, ok := NewPath(rtn.name, path, rtn.startTime, rtn.endTime)
if !ok { if !ok {
continue continue
...@@ -71,6 +72,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service { ...@@ -71,6 +72,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
//初始化node //初始化node
nodeList := rtn.getNodeList() nodeList := rtn.getNodeList()
for _, node := range nodeList { for _, node := range nodeList {
glog.Info("init node:", rtn.name, node)
rtn.nodeMap[node] = NewNode(rtn.name, node, rtn.startTime, rtn.endTime) rtn.nodeMap[node] = NewNode(rtn.name, node, rtn.startTime, rtn.endTime)
} }
...@@ -241,12 +243,6 @@ func (s *Service) GetAverageCpu() int { ...@@ -241,12 +243,6 @@ func (s *Service) GetAverageCpu() int {
}) })
} }
func (s *Service) GetMedianCpu() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianCpu()
})
}
func (s *Service) GetMaxCpu() int { func (s *Service) GetMaxCpu() int {
return s.getNodeMapValue(func(node *Node) int { return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxCpu() return node.GetMaxCpu()
...@@ -259,12 +255,6 @@ func (s *Service) GetAverageMem() int { ...@@ -259,12 +255,6 @@ func (s *Service) GetAverageMem() int {
}) })
} }
func (s *Service) GetMedianMem() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianMem()
})
}
func (s *Service) GetMaxMem() int { func (s *Service) GetMaxMem() int {
return s.getNodeMapValue(func(node *Node) int { return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxMem() return node.GetMaxMem()
...@@ -277,12 +267,6 @@ func (s *Service) GetAverageDisk() int { ...@@ -277,12 +267,6 @@ func (s *Service) GetAverageDisk() int {
}) })
} }
func (s *Service) GetMedianDisk() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianDisk()
})
}
func (s *Service) GetMaxDisk() int { func (s *Service) GetMaxDisk() int {
return s.getNodeMapValue(func(node *Node) int { return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxDisk() return node.GetMaxDisk()
...@@ -295,12 +279,6 @@ func (s *Service) GetAverageThread() int { ...@@ -295,12 +279,6 @@ func (s *Service) GetAverageThread() int {
}) })
} }
func (s *Service) GetMedianThread() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianThread()
})
}
func (s *Service) GetMaxThread() int { func (s *Service) GetMaxThread() int {
return s.getNodeMapValue(func(node *Node) int { return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxThread() return node.GetMaxThread()
......
package report_form package report_form
import ( import (
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"sort" "sort"
"time" "time"
) )
...@@ -21,6 +22,7 @@ func NewServiceMap(startTime time.Time, endTime time.Time) *ServiceMap { ...@@ -21,6 +22,7 @@ func NewServiceMap(startTime time.Time, endTime time.Time) *ServiceMap {
//初始化service //初始化service
serviceList := rtn.getServiceList() serviceList := rtn.getServiceList()
for _, serviceName := range serviceList { for _, serviceName := range serviceList {
glog.Info("init service :", serviceName, " start")
rtn.serviceMap[serviceName] = NewService(serviceName, startTime, endTime) rtn.serviceMap[serviceName] = NewService(serviceName, startTime, endTime)
} }
return rtn return rtn
...@@ -69,6 +71,7 @@ func (sm *ServiceMap) GetAverageCpuServiceList() []*Service { ...@@ -69,6 +71,7 @@ func (sm *ServiceMap) GetAverageCpuServiceList() []*Service {
} }
return false return false
}) })
return rtn return rtn
} }
......
...@@ -24,8 +24,8 @@ func TestNewService(t *testing.T) { ...@@ -24,8 +24,8 @@ func TestNewService(t *testing.T) {
t.Log("average_max_path:", average.GetPath()) t.Log("average_max_path:", average.GetPath())
} }
t.Log("cpu:", s.GetMedianCpu(), s.GetAverageCpu(), s.GetMaxCpu()) t.Log("cpu:", s.GetAverageCpu(), s.GetMaxCpu())
t.Log("mem:", s.GetMedianMem(), s.GetAverageMem(), s.GetMaxMem()) t.Log("mem:", s.GetAverageMem(), s.GetMaxMem())
t.Log("disk:", s.GetMedianDisk(), s.GetAverageDisk(), s.GetMaxDisk()) t.Log("disk:", s.GetAverageDisk(), s.GetMaxDisk())
t.Log("trace", s.GetMedianThread(), s.GetAverageThread(), s.GetMaxThread()) t.Log("trace", s.GetAverageThread(), s.GetMaxThread())
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment