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

临时

parent 5362a8b4
package service_check package service_check
import ( import (
"fmt" "encoding/json"
"git.quantgroup.cn/DevOps/enoch/pkg/global" "git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/influxdata/influxdb/client/v2" "github.com/influxdata/influxdb/client/v2"
) )
var sqlMap = map[string]string{ //项
"yesterday_count": "select count(traceId) from trace_info where time > now() - 1d25m and time < now() - 1d group by sys_name,path,time(5m) fill(0);", type Item struct {
"count": "select count(traceId) from trace_info where time > now() - 25m group by sys_name,path,time(5m) fill(1);", Sql string
"yesterday_path_count": "select count(traceId) from trace_info where time > now() - 1d25m and time < now() - 1d group by sys_name,path,time(5m) fill(0);", ResultMap map[string]float64
"path_count": "select count(traceId) from trace_info where time > now() - 25m group by sys_name,path,time(5m) fill(1)", }
"apdex": "select (sum(sat)+sum(tol))/sum(ct_all) as cu from apdex where time > now() - 7m30s and time < now() - 1m59s group by sys_name,time(1m) fill(0);",
"cpu": "select last(system_load_average)/last(processors) from machine_info where time > now() - 5m group by sys_name, host,time(1m) fill(1);", func NewItem(sql string) *Item {
"mem": "select last(mem_free)/last(mem_tol) from machine_info where time > now() - 5m group by sys_name,host,time(1m) fill(0);", return &Item{
"disk": "select last(disk_free)/last(disk_tol) from machine_info where time > now() - 3m group by sys_name,host,time(1m) fill(0);", Sql: sql,
ResultMap: make(map[string]float64),
}
} }
func query(sql string) (*client.Response, error) { func query(sql string) (*client.Response, error) {
...@@ -36,63 +39,90 @@ func query(sql string) (*client.Response, error) { ...@@ -36,63 +39,90 @@ func query(sql string) (*client.Response, error) {
return rtn, nil return rtn, nil
} }
type item struct { func (i *Item) Run() {
sql string //清理
resultMap map[string]float64 //sql的执行结果,按照sys_name分类 i.Clean()
}
func newItem(sql string) *item { //重新拉数据
return &item{ resp, err := query(i.Sql)
sql: sql,
resultMap: make(map[string]float64),
}
}
func (i *item) Run() {
resp, err := query(i.sql)
if err != nil { if err != nil {
glog.Error("查询失败:", i.Sql)
return return
} }
for _, r := range resp.Results { for _, r := range resp.Results {
for _, row := range r.Series { for _, row := range r.Series {
for _, v := range row.Values { for _, v := range row.Values {
fmt.Println(row.Tags) id := i.tagsToId(row.Tags)
fmt.Println(v) if len(v) != 2 {
i.ResultMap[id] = 0
continue
}
j, ok := v[1].(json.Number)
if !ok {
continue
}
vFloat64, err := j.Float64()
if err != nil {
continue
}
i.ResultMap[id] = vFloat64
} }
} }
} }
} }
func (i *item) Clean() { //目前支持三种tag:sys_name path host
i.resultMap = make(map[string]float64) func (i *Item) tagsToId(tags map[string]string) string {
tagList := []string{"sys_name", "path", "host"}
rtn := ""
for _, tag := range tagList {
if v, ok := tags[tag]; ok {
rtn += "-" + v
}
}
rtn += "-"
return rtn
}
func (i *Item) Clean() {
i.ResultMap = make(map[string]float64)
} }
//解释器
type Interpreter struct { type Interpreter struct {
itemMap map[string]*item ItemMap map[string]*Item
} }
func NewInterpreter() *Interpreter { func NewInterpreter() *Interpreter {
return &Interpreter{itemMap: make(map[string]*item)} return &Interpreter{ItemMap: map[string]*Item{
"count": NewItem("select count(traceId) as count from trace_info where time>now()-7m and time<now()-2m group by sys_name fill(0);"),
"yesterday_count": NewItem("select count(traceId) as yesterday_count from trace_info where time>now()-1d7m and time<now()-1d2m group by sys_name fill(0);"),
"path_count": NewItem("select count(traceId) as path_count from trace_info where time>now()-7m and time<now()-2m group by sys_name, path fill(0);"),
"yesterday_path_count": NewItem("select count(traceId) as yesterday_path_count from trace_info where time>now()-1d7m and time<now()-1d2m group by sys_name, path fill(0);"),
"apdex": NewItem("select (sum(sat)+sum(tol))/sum(ct_all) as apdex from apdex where time > now()-7m and time < now()-2m group by sys_name fill(0);"),
"cpu": NewItem("select last(system_load_average)/last(processors) from machine_info where time > now() - 5m group by sys_name, host fill(0);"),
"mem": NewItem("select last(mem_free)/last(mem_tol) from machine_info where time > now() - 5m group by sys_name,host fill(0);"),
"disk": NewItem("select last(disk_free)/last(disk_tol) from machine_info where time > now() - 5m group by sys_name,host fill(0);"),
}}
} }
//(((x1>=10)&&(x2<=20))||x3=0) //刷新item中的数据
func (i *Interpreter) FlashData() {
//加载数据
for _, v := range i.ItemMap {
v.Run()
}
}
//解析一条表达式
// (((x1>=10)&&(x2<=20))||(x3=0))
// () //调整优先级 // () //调整优先级
// > < = >= <= && || //运算符 // > < = >= <= && || //运算符
// abc //变量,对应itemMap中sql的执行结果 // abc //变量,对应itemMap中sql的执行结果
// 1.11 //数值,统一为float64类型 // 1.11 //数值,统一为float64类型
func (i *Interpreter) Explain(service string, pattern string) bool {
return false
}
//运行sql
func (i *Interpreter) RunItem() {
for _, im := range i.itemMap {
im.Clean()
im.Run()
}
}
//添加项 //
func (i *Interpreter) AddItem(name string, sql string) { //true
i.itemMap[name] = newItem(sql) func (i *Interpreter) Explain(e string) {
//i.ItemMap["123"]
} }
...@@ -4,8 +4,6 @@ import "testing" ...@@ -4,8 +4,6 @@ import "testing"
func TestInterpreter_RunItem(t *testing.T) { func TestInterpreter_RunItem(t *testing.T) {
i := NewInterpreter() i := NewInterpreter()
for k, v := range sqlMap { //i.FlashData()
i.AddItem(k, v) i.Explain(" ( ) ")
}
i.RunItem()
} }
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