2018-03-22 08:13:54 +00:00
|
|
|
package biz
|
|
|
|
|
|
|
|
import (
|
2018-03-26 12:44:42 +00:00
|
|
|
"os"
|
2018-03-22 08:13:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cuigh/auxo/data"
|
2018-03-26 12:44:42 +00:00
|
|
|
"github.com/cuigh/auxo/errors"
|
2018-03-22 08:13:54 +00:00
|
|
|
"github.com/cuigh/auxo/log"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/dao"
|
|
|
|
"github.com/cuigh/swirl/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Chart return a chart biz instance.
|
2018-03-26 12:44:42 +00:00
|
|
|
var Chart = newChartBiz()
|
2018-03-22 08:13:54 +00:00
|
|
|
|
|
|
|
type chartBiz struct {
|
2018-03-26 12:44:42 +00:00
|
|
|
builtin []*model.Chart
|
|
|
|
}
|
|
|
|
|
|
|
|
func newChartBiz() *chartBiz {
|
|
|
|
b := &chartBiz{}
|
|
|
|
b.builtin = append(b.builtin, model.NewChart("service", "$cpu", "CPU", "${name}", `rate(container_cpu_user_seconds_total{container_label_com_docker_swarm_service_name="${service}"}[5m]) * 100`, "percent:100"))
|
|
|
|
b.builtin = append(b.builtin, model.NewChart("service", "$memory", "Memory", "${name}", `container_memory_usage_bytes{container_label_com_docker_swarm_service_name="${service}"}`, "size:bytes"))
|
|
|
|
b.builtin = append(b.builtin, model.NewChart("service", "$network_in", "Network Receive", "${name}", `sum(irate(container_network_receive_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes"))
|
|
|
|
b.builtin = append(b.builtin, model.NewChart("service", "$network_out", "Network Send", "${name}", `sum(irate(container_network_transmit_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes"))
|
|
|
|
// for _, c := range categories {
|
|
|
|
// if c == "java" {
|
|
|
|
// charts = append(charts, model.NewChart("threads", "Threads", "${instance}", `jvm_threads_current{service="%s"}`, ""))
|
|
|
|
// charts = append(charts, model.NewChart("gc_duration", "GC Duration", "${instance}", `rate(jvm_gc_collection_seconds_sum{service="%s"}[1m])`, "time:s"))
|
|
|
|
// } else if c == "go" {
|
|
|
|
// charts = append(charts, model.NewChart("threads", "Threads", "${instance}", `go_threads{service="%s"}`, ""))
|
|
|
|
// charts = append(charts, model.NewChart("goroutines", "Goroutines", "${instance}", `go_goroutines{service="%s"}`, ""))
|
|
|
|
// charts = append(charts, model.NewChart("gc_duration", "GC Duration", "${instance}", `sum(go_gc_duration_seconds{service="%s"}) by (instance)`, "time:s"))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// for i, c := range charts {
|
|
|
|
// charts[i].Query = fmt.Sprintf(c.Query, name)
|
|
|
|
// }
|
|
|
|
return b
|
2018-03-22 08:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) List() (charts []*model.Chart, err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
charts, err = d.ChartList()
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) Create(chart *model.Chart, user web.User) (err error) {
|
|
|
|
do(func(d dao.Interface) {
|
2018-03-26 12:44:42 +00:00
|
|
|
// chart.CreatedAt = time.Now()
|
|
|
|
// chart.UpdatedAt = chart.CreatedAt
|
2018-03-22 08:13:54 +00:00
|
|
|
err = d.ChartCreate(chart)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) Delete(id string, user web.User) (err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
err = d.ChartDelete(id)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) Get(name string) (chart *model.Chart, err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
chart, err = d.ChartGet(name)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:44:42 +00:00
|
|
|
func (b *chartBiz) Batch(names ...string) (charts []*model.Chart, err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
charts, err = d.ChartBatch(names...)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-22 08:13:54 +00:00
|
|
|
func (b *chartBiz) Update(chart *model.Chart, user web.User) (err error) {
|
|
|
|
do(func(d dao.Interface) {
|
2018-03-26 12:44:42 +00:00
|
|
|
// chart.UpdatedAt = time.Now()
|
2018-03-22 08:13:54 +00:00
|
|
|
err = d.ChartUpdate(chart)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) GetServiceCharts(name string) (charts []*model.Chart, err error) {
|
2018-03-26 12:44:42 +00:00
|
|
|
// service, _, err := docker.ServiceInspect(name)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if label := service.Spec.Labels["swirl.metrics"]; label != "" {
|
|
|
|
// names := strings.Split(label, ",")
|
|
|
|
// }
|
|
|
|
charts = b.builtin
|
2018-03-22 08:13:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 08:32:30 +00:00
|
|
|
func (b *chartBiz) GetDashboard(name, key string) (dashboard *model.ChartDashboard, err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
dashboard, err = d.DashboardGet(name, key)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) UpdateDashboard(dashboard *model.ChartDashboard, user web.User) (err error) {
|
|
|
|
do(func(d dao.Interface) {
|
|
|
|
err = d.DashboardUpdate(dashboard)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-22 08:13:54 +00:00
|
|
|
// nolint: gocyclo
|
2018-03-27 08:32:30 +00:00
|
|
|
func (b *chartBiz) GetDashboardCharts(dashboard *model.ChartDashboard) (charts []*model.Chart, err error) {
|
2018-03-22 08:13:54 +00:00
|
|
|
do(func(d dao.Interface) {
|
2018-04-02 10:17:43 +00:00
|
|
|
if dashboard == nil || len(dashboard.Charts) == 0 {
|
2018-03-22 08:13:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 08:32:30 +00:00
|
|
|
names := make([]string, len(dashboard.Charts))
|
|
|
|
for i, c := range dashboard.Charts {
|
2018-03-22 08:13:54 +00:00
|
|
|
names[i] = c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
var cs []*model.Chart
|
2018-03-27 08:32:30 +00:00
|
|
|
cs, err = b.getCharts(names)
|
2018-03-22 08:13:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cs) > 0 {
|
|
|
|
m := make(map[string]*model.Chart)
|
|
|
|
for _, c := range cs {
|
|
|
|
m[c.Name] = c
|
|
|
|
}
|
2018-03-27 08:32:30 +00:00
|
|
|
for _, c := range dashboard.Charts {
|
2018-03-22 08:13:54 +00:00
|
|
|
if chart := m[c.Name]; chart != nil {
|
|
|
|
if c.Width > 0 {
|
|
|
|
chart.Width = c.Width
|
|
|
|
}
|
|
|
|
if c.Height > 0 {
|
|
|
|
chart.Height = c.Height
|
|
|
|
}
|
2018-04-02 08:19:12 +00:00
|
|
|
//if len(c.Colors) > 0 {
|
|
|
|
// chart.Colors = c.Colors
|
|
|
|
//}
|
2018-03-22 08:13:54 +00:00
|
|
|
charts = append(charts, chart)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:44:42 +00:00
|
|
|
func (b *chartBiz) FetchDatas(key string, names []string, period time.Duration) (data.Map, error) {
|
|
|
|
charts, err := b.getCharts(names)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-22 08:13:54 +00:00
|
|
|
datas := data.Map{}
|
|
|
|
end := time.Now()
|
|
|
|
start := end.Add(-period)
|
|
|
|
for _, chart := range charts {
|
2018-03-26 12:44:42 +00:00
|
|
|
query, err := b.formatQuery(chart, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-22 08:13:54 +00:00
|
|
|
switch chart.Type {
|
|
|
|
case "line", "bar":
|
2018-04-02 08:19:12 +00:00
|
|
|
d, err := Metric.GetMatrix(query, chart.Legend, start, end)
|
|
|
|
if err != nil {
|
|
|
|
log.Get("metric").Error(err, query)
|
|
|
|
} else {
|
|
|
|
datas[chart.Name] = d
|
|
|
|
}
|
|
|
|
case "pie":
|
|
|
|
d, err := Metric.GetVector(query, chart.Legend, end)
|
2018-03-22 08:13:54 +00:00
|
|
|
if err != nil {
|
2018-03-26 12:44:42 +00:00
|
|
|
log.Get("metric").Error(err, query)
|
2018-03-22 08:13:54 +00:00
|
|
|
} else {
|
2018-04-02 08:19:12 +00:00
|
|
|
datas[chart.Name] = d
|
2018-03-22 08:13:54 +00:00
|
|
|
}
|
2018-04-02 08:19:12 +00:00
|
|
|
case "gauge":
|
|
|
|
d, err := Metric.GetScalar(query, end)
|
2018-03-22 08:13:54 +00:00
|
|
|
if err != nil {
|
2018-03-26 12:44:42 +00:00
|
|
|
log.Get("metric").Error(err, query)
|
2018-03-22 08:13:54 +00:00
|
|
|
} else {
|
2018-04-02 08:19:12 +00:00
|
|
|
datas[chart.Name] = &model.ChartValue{
|
|
|
|
//Name: "",
|
|
|
|
Value: d,
|
|
|
|
}
|
2018-03-22 08:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
2018-03-26 12:44:42 +00:00
|
|
|
|
|
|
|
func (b *chartBiz) formatQuery(chart *model.Chart, key string) (string, error) {
|
|
|
|
if chart.Dashboard == "home" {
|
|
|
|
return chart.Query, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
m := map[string]string{chart.Dashboard: key}
|
|
|
|
query := os.Expand(chart.Query, func(k string) string {
|
|
|
|
if v, ok := m[k]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
errs = append(errs, errors.New("invalid argument in query: "+chart.Query))
|
|
|
|
return ""
|
|
|
|
})
|
|
|
|
if len(errs) == 0 {
|
|
|
|
return query, nil
|
|
|
|
}
|
|
|
|
return "", errs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *chartBiz) getCharts(names []string) (charts []*model.Chart, err error) {
|
|
|
|
var (
|
|
|
|
customNames []string
|
|
|
|
customCharts []*model.Chart
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, n := range names {
|
|
|
|
if n[0] == '$' {
|
|
|
|
for _, c := range b.builtin {
|
|
|
|
if c.Name == n {
|
|
|
|
charts = append(charts, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
customNames = append(customNames, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(customNames) > 0 {
|
|
|
|
if customCharts, err = b.Batch(customNames...); err == nil {
|
|
|
|
charts = append(charts, customCharts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|