swirl/model/metric.go

99 lines
2.5 KiB
Go
Raw Normal View History

2018-03-22 08:13:54 +00:00
package model
import (
2018-04-02 08:19:12 +00:00
"fmt"
2018-03-22 08:13:54 +00:00
"github.com/cuigh/auxo/data"
2018-04-02 08:19:12 +00:00
"github.com/cuigh/auxo/util/cast"
2018-03-22 08:13:54 +00:00
)
// Chart represents a dashboard chart.
type Chart struct {
2018-07-02 09:33:14 +00:00
Name string `json:"name" bson:"_id" valid:"required"` // unique, the name of build-in charts has '$' prefix.
Title string `json:"title" valid:"required"`
Description string `json:"desc"`
Legend string `json:"-"`
Query string `json:"-"`
Metrics []ChartMetric `json:"metrics" valid:"required"`
Kind string `json:"kind"` // builtin/custom
Dashboard string `json:"dashboard"` // home/service/task...
Type string `json:"type"` // pie/line...
Unit string `json:"unit"` // bytes/milliseconds/percent:100...
Width int32 `json:"width"` // 1-12(12 columns total)
Height int32 `json:"height"` // default 50
Options data.Map `json:"options"`
2018-04-02 08:19:12 +00:00
//Colors []string `json:"colors"`
2018-03-22 08:13:54 +00:00
}
2018-04-02 08:19:12 +00:00
func NewChart(dashboard, name, title, legend, query, unit string) *Chart {
2018-03-22 08:13:54 +00:00
return &Chart{
Name: name,
Title: title,
Description: title,
2018-07-02 09:33:14 +00:00
Metrics: []ChartMetric{
{Legend: legend, Query: query},
},
Dashboard: dashboard,
Type: "line",
Unit: unit,
Width: 12,
Height: 200,
2018-03-22 08:13:54 +00:00
}
}
2018-07-02 09:33:14 +00:00
type ChartMetric struct {
Legend string `json:"legend"`
Query string `json:"query"`
}
2018-04-02 08:19:12 +00:00
type ChartOption struct {
Name string `json:"name"`
Width int32 `json:"width"`
Height int32 `json:"height"`
//Colors []string `json:"colors"`
2018-03-22 08:13:54 +00:00
}
2018-03-27 08:32:30 +00:00
type ChartDashboard struct {
2018-04-02 08:19:12 +00:00
Name string `json:"name"`
Key string `json:"key"`
Period int32 `json:"period"` // minutes
RefreshInterval int32 `json:"refresh_interval"` // seconds, 0 means disabled.
Charts []ChartOption `json:"charts"`
2018-03-22 08:13:54 +00:00
}
2018-03-27 08:32:30 +00:00
func (cd *ChartDashboard) ID() string {
if cd.Key == "" {
return cd.Name
}
return cd.Name + ":" + cd.Key
}
2018-03-22 08:13:54 +00:00
type ChartPoint struct {
X int64 `json:"x"`
Y float64 `json:"y"`
}
2018-04-02 08:19:12 +00:00
func (p *ChartPoint) MarshalJSON() ([]byte, error) {
return cast.StringToBytes(fmt.Sprintf("[%v,%v]", p.X, p.Y)), nil
2018-03-22 08:13:54 +00:00
}
2018-04-02 08:19:12 +00:00
type ChartLine struct {
Name string `json:"name"`
Data []ChartPoint `json:"data"`
2018-03-22 08:13:54 +00:00
}
2018-04-02 08:19:12 +00:00
type ChartMatrixData struct {
Legend []string `json:"legend"`
Series []ChartLine `json:"series"`
2018-03-22 08:13:54 +00:00
}
2018-04-02 08:19:12 +00:00
type ChartValue struct {
Name string `json:"name"`
Value float64 `json:"value"`
2018-03-22 08:13:54 +00:00
}
2018-04-02 08:19:12 +00:00
type ChartVectorData struct {
Legend []string `json:"legend"`
Data []ChartValue `json:"data"`
2018-03-22 08:13:54 +00:00
}