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

临时

parent 5362a8b4
package service_check
import (
"fmt"
"encoding/json"
"git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"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);",
"count": "select count(traceId) from trace_info where time > now() - 25m group by sys_name,path,time(5m) fill(1);",
"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);",
"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);",
"mem": "select last(mem_free)/last(mem_tol) from machine_info where time > now() - 5m group by sys_name,host,time(1m) fill(0);",
"disk": "select last(disk_free)/last(disk_tol) from machine_info where time > now() - 3m group by sys_name,host,time(1m) fill(0);",
//项
type Item struct {
Sql string
ResultMap map[string]float64
}
func NewItem(sql string) *Item {
return &Item{
Sql: sql,
ResultMap: make(map[string]float64),
}
}
func query(sql string) (*client.Response, error) {
......@@ -36,63 +39,90 @@ func query(sql string) (*client.Response, error) {
return rtn, nil
}
type item struct {
sql string
resultMap map[string]float64 //sql的执行结果,按照sys_name分类
}
func (i *Item) Run() {
//清理
i.Clean()
func newItem(sql string) *item {
return &item{
sql: sql,
resultMap: make(map[string]float64),
}
}
func (i *item) Run() {
resp, err := query(i.sql)
//重新拉数据
resp, err := query(i.Sql)
if err != nil {
glog.Error("查询失败:", i.Sql)
return
}
for _, r := range resp.Results {
for _, row := range r.Series {
for _, v := range row.Values {
fmt.Println(row.Tags)
fmt.Println(v)
id := i.tagsToId(row.Tags)
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() {
i.resultMap = make(map[string]float64)
//目前支持三种tag:sys_name path host
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 {
itemMap map[string]*item
ItemMap map[string]*Item
}
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);"),
}}
}
//刷新item中的数据
func (i *Interpreter) FlashData() {
//加载数据
for _, v := range i.ItemMap {
v.Run()
}
}
//(((x1>=10)&&(x2<=20))||x3=0)
//解析一条表达式
// (((x1>=10)&&(x2<=20))||(x3=0))
// () //调整优先级
// > < = >= <= && || //运算符
// abc //变量,对应itemMap中sql的执行结果
// 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) {
i.itemMap[name] = newItem(sql)
//
//true
func (i *Interpreter) Explain(e string) {
//i.ItemMap["123"]
}
......@@ -4,8 +4,6 @@ import "testing"
func TestInterpreter_RunItem(t *testing.T) {
i := NewInterpreter()
for k, v := range sqlMap {
i.AddItem(k, v)
}
i.RunItem()
//i.FlashData()
i.Explain(" ( ) ")
}
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