-
Notifications
You must be signed in to change notification settings - Fork 3
/
widgets.go
149 lines (122 loc) · 2.4 KB
/
widgets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"reflect"
"strings"
)
const (
GaugeType = "Gauge"
LineChartType = "LineChart"
TextType = "Text"
)
type Widget interface {
ID() string
SetID(string)
Title() string
HasLegend() bool
Series() []string
}
type LineChart struct {
cid string `json:"-"`
Metric *Metric `json:"-"`
MetricName string `json:"metric"`
ShowLegend *bool `json:"show_legend"`
Services []string `json:"services"`
}
func (c *LineChart) ID() string {
return c.cid
}
func (c *LineChart) SetID(id string) {
c.cid = id
}
func (c *LineChart) Title() string {
return c.Metric.String()
}
func (c *LineChart) HasLegend() bool {
if c.ShowLegend == nil {
return true
}
return *c.ShowLegend
}
func (c *LineChart) Series() []string {
return c.Services
}
type Gauge struct {
cid string `json:"-"`
Metric *Metric `json:"-"`
MetricName string `json:"metric"`
Service string `json:"service"`
MaxValue int64 `json:"max"`
}
func (g *Gauge) ID() string {
return g.cid
}
func (g *Gauge) SetID(id string) {
g.cid = id
}
func (g *Gauge) Title() string {
return g.Metric.String()
}
func (g *Gauge) HasLegend() bool {
return false
}
func (g *Gauge) Series() []string {
return []string{}
}
type Text struct {
cid string `json:"-"`
Metric *Metric `json:"-"`
MetricName string `json:"metric"`
Service string `json:"service"`
}
func (t *Text) ID() string {
return t.cid
}
func (t *Text) SetID(id string) {
t.cid = id
}
func (t *Text) Title() string {
return t.Metric.String()
}
func (t *Text) HasLegend() bool {
return false
}
func (t *Text) Series() []string {
return []string{}
}
type Widgets struct {
nextID int
Gauges []*Gauge
LineCharts []*LineChart
Texts []*Text
}
func (ww *Widgets) NextID() string {
ww.nextID++
return fmt.Sprintf("c%d", ww.nextID)
}
func (ww *Widgets) Append(widget Widget) error {
switch c := widget.(type) {
case *Gauge:
ww.Gauges = append(ww.Gauges, c)
return nil
case *LineChart:
ww.LineCharts = append(ww.LineCharts, c)
return nil
case *Text:
ww.Texts = append(ww.Texts, c)
return nil
default:
return fmt.Errorf("Unknown widget type: %s", reflect.TypeOf(widget))
}
}
type Metric struct {
Path []string
}
func NewMetric(name string) (*Metric, error) {
return &Metric{
Path: strings.Split(name, "."),
}, nil
}
func (m *Metric) String() string {
return strings.Join(m.Path, ".")
}