forked from divan/expvarmon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui_single.go
167 lines (141 loc) · 3.64 KB
/
ui_single.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"time"
"github.com/gizak/termui"
)
// TermUISingle is a termUI implementation of UI interface.
type TermUISingle struct {
Title *termui.Par
Status *termui.Par
Sparklines map[VarName]*termui.Sparkline
Sparkline *termui.Sparklines
Pars []*termui.Par
}
// Init creates widgets, sets sizes and labels.
func (t *TermUISingle) Init(data UIData) error {
err := termui.Init()
if err != nil {
return err
}
t.Sparklines = make(map[VarName]*termui.Sparkline)
termui.UseTheme("helloworld")
t.Title = func() *termui.Par {
p := termui.NewPar("")
p.Height = 3
p.TextFgColor = termui.ColorWhite
p.Border.Label = "Services Monitor"
p.Border.FgColor = termui.ColorCyan
return p
}()
t.Status = func() *termui.Par {
p := termui.NewPar("")
p.Height = 3
p.TextFgColor = termui.ColorWhite
p.Border.Label = "Status"
p.Border.FgColor = termui.ColorCyan
return p
}()
t.Pars = make([]*termui.Par, len(data.Vars))
for i, name := range data.Vars {
par := termui.NewPar("")
par.TextFgColor = colorByKind(name.Kind())
par.Border.Label = name.Short()
par.Border.LabelFgColor = termui.ColorGreen
par.Height = 3
t.Pars[i] = par
}
var sparklines []termui.Sparkline
for _, name := range data.Vars {
spl := termui.NewSparkline()
spl.Height = 1
spl.TitleColor = colorByKind(name.Kind())
spl.LineColor = colorByKind(name.Kind())
spl.Title = name.Long()
sparklines = append(sparklines, spl)
}
t.Sparkline = func() *termui.Sparklines {
s := termui.NewSparklines(sparklines...)
s.Height = 2*len(sparklines) + 2
s.HasBorder = true
s.Border.Label = fmt.Sprintf("Monitoring")
return s
}()
t.Relayout()
return nil
}
// Update updates UI widgets from UIData.
func (t *TermUISingle) Update(data UIData) {
// single mode assumes we have one service only to monitor
service := data.Services[0]
t.Title.Text = fmt.Sprintf("monitoring %s every %v, press q to quit", service.Name, *interval)
t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format(time.Stamp))
// Pars
for i, name := range data.Vars {
t.Pars[i].Text = service.Value(name)
}
// Sparklines
for i, name := range data.Vars {
spl := &t.Sparkline.Lines[i]
max := formatMax(service.Max(name))
spl.Title = fmt.Sprintf("%s: %v%s", name.Long(), service.Value(name), max)
spl.TitleColor = colorByKind(name.Kind())
spl.LineColor = colorByKind(name.Kind())
if name.Kind() == KindString {
continue
}
spl.Data = service.Values(name)
}
t.Relayout()
var widgets []termui.Bufferer
widgets = append(widgets, t.Title, t.Status, t.Sparkline)
for _, par := range t.Pars {
widgets = append(widgets, par)
}
termui.Render(widgets...)
}
// Close shuts down UI module.
func (t *TermUISingle) Close() {
termui.Close()
}
// Relayout recalculates widgets sizes and coords.
func (t *TermUISingle) Relayout() {
tw, th := termui.TermWidth(), termui.TermHeight()
h := th
// First row: Title and Status pars
firstRowH := 3
t.Title.Height = firstRowH
t.Title.Width = tw / 2
if tw%2 == 1 {
t.Title.Width++
}
t.Status.Height = firstRowH
t.Status.Width = tw / 2
t.Status.X = t.Title.X + t.Title.Width
h -= firstRowH
// Second row: lists
secondRowH := 3
num := len(t.Pars)
parW := tw / num
for i, par := range t.Pars {
par.Y = th - h
par.Width = parW
par.Height = secondRowH
par.X = i * parW
}
if num*parW < tw {
t.Pars[num-1].Width = tw - ((num - 1) * parW)
}
h -= secondRowH
// Third row: Sparklines
t.Sparkline.Width = tw
t.Sparkline.Height = h
t.Sparkline.Y = th - h
}
func formatMax(max interface{}) string {
var str string
if max != nil {
str = fmt.Sprintf(" (max: %v)", max)
}
return str
}