-
Notifications
You must be signed in to change notification settings - Fork 101
/
ui_multi.go
235 lines (202 loc) · 5.55 KB
/
ui_multi.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"fmt"
"time"
"github.com/gizak/termui"
)
// TermUI is a termUI implementation of UI interface.
type TermUI struct {
Title *termui.Paragraph
Status *termui.Paragraph
Services *termui.List
Lists []*termui.List
Sparkline1 *termui.Sparklines
Sparkline2 *termui.Sparklines
}
// Init creates widgets, sets sizes and labels.
func (t *TermUI) Init(data UIData) error {
err := termui.Init()
if err != nil {
return err
}
t.Title = func() *termui.Paragraph {
p := termui.NewParagraph("")
p.Height = 3
p.TextFgColor = termui.ColorWhite
p.Border = true
p.BorderLabel = "Services Monitor"
p.BorderFg = termui.ColorCyan
return p
}()
t.Status = func() *termui.Paragraph {
p := termui.NewParagraph("")
p.Height = 3
p.TextFgColor = termui.ColorWhite
p.Border = true
p.BorderLabel = "Status"
p.BorderFg = termui.ColorCyan
return p
}()
t.Services = func() *termui.List {
list := termui.NewList()
list.ItemFgColor = termui.ColorGreen
list.Border = true
list.BorderLabelFg = termui.ColorGreen | termui.AttrBold
list.BorderLabel = "Services"
list.Height = len(data.Services) + 2
return list
}()
t.Lists = make([]*termui.List, len(data.Vars))
for i, name := range data.Vars {
list := termui.NewList()
list.ItemFgColor = colorByKind(name.Kind())
list.Border = true
list.BorderLabel = name.Short()
list.BorderLabelFg = termui.ColorGreen
if i < 2 {
list.BorderLabelFg = termui.ColorGreen | termui.AttrBold
}
list.Height = len(data.Services) + 2
t.Lists[i] = list
}
makeSparkline := func(name VarName) *termui.Sparklines {
var sparklines []termui.Sparkline
for _, service := range data.Services {
spl := termui.NewSparkline()
spl.Height = 1
spl.LineColor = termui.ColorGreen
spl.Title = service.Name
sparklines = append(sparklines, spl)
}
s := termui.NewSparklines(sparklines...)
s.Height = 2*len(data.Services) + 2
s.Border = true
s.BorderLabel = fmt.Sprintf("Monitoring %s", name.Long())
return s
}
t.Sparkline1 = makeSparkline(data.Vars[0])
if len(data.Vars) > 1 {
t.Sparkline2 = makeSparkline(data.Vars[1])
}
t.Relayout()
return nil
}
// Update updates UI widgets from UIData.
func (t *TermUI) Update(data UIData) {
t.Title.Text = fmt.Sprintf("monitoring %d services every %v, press q to quit", len(data.Services), *interval)
t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format(time.Stamp))
// List with service names
var services []string
for _, service := range data.Services {
services = append(services, StatusLine(service))
}
t.Services.Items = services
// Lists with values
for i, name := range data.Vars {
var lines []string
for _, service := range data.Services {
lines = append(lines, service.Value(name))
}
t.Lists[i].Items = lines
}
// Sparklines
for i, service := range data.Services {
max := formatMax(service.Max(data.Vars[0]))
t.Sparkline1.Lines[i].Title = fmt.Sprintf("%s%s", service.Name, max)
t.Sparkline1.Lines[i].Data = service.Values(data.Vars[0])
if len(data.Vars) > 1 {
max = formatMax(service.Max(data.Vars[1]))
t.Sparkline2.Lines[i].Title = fmt.Sprintf("%s%s", service.Name, max)
t.Sparkline2.Lines[i].Data = service.Values(data.Vars[1])
}
}
t.Relayout()
var widgets []termui.Bufferer
widgets = append(widgets, t.Title, t.Status, t.Services, t.Sparkline1)
for _, list := range t.Lists {
widgets = append(widgets, list)
}
if t.Sparkline2 != nil {
widgets = append(widgets, t.Sparkline2)
}
termui.Render(widgets...)
}
// Relayout recalculates widgets sizes and coords.
func (t *TermUI) 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
num := len(t.Lists) + 1
listW := tw / num
// Services list must have visible names
minNameWidth := 20
t.Services.Width = minNameWidth
if listW > minNameWidth {
t.Services.Width = listW
}
// Recalculate widths for each list
listW = (tw - t.Services.Width) / (num - 1)
// Finally, enlarge services list, if there is a space left
if listW*(num-1)+t.Services.Width < tw {
t.Services.Width = tw - (listW * (num - 1))
}
t.Services.Y = th - h
for i, list := range t.Lists {
list.Y = th - h
list.Width = listW
list.Height = len(t.Lists[0].Items) + 2
list.X = t.Services.X + t.Services.Width + i*listW
}
h -= t.Lists[0].Height
// Third row: sparklines for two vars
t.Sparkline1.Width = tw
t.Sparkline1.Height = h
t.Sparkline1.Y = th - h
if t.Sparkline2 != nil {
t.Sparkline1.Width = tw / 2
t.Sparkline2.Width = tw / 2
if tw%2 == 1 {
t.Sparkline2.Width++
}
t.Sparkline2.X = t.Sparkline1.X + t.Sparkline1.Width
t.Sparkline2.Height = h
t.Sparkline2.Y = th - h
}
}
// Close shuts down UI module.
func (t *TermUI) Close() {
termui.Close()
}
// StatusLine returns status line for service with it's name and status.
func StatusLine(s *Service) string {
if s.Err != nil {
return fmt.Sprintf("[E] ⛔ %s failed", s.Name)
}
if s.Restarted {
return fmt.Sprintf("[R] 🔥 %s", s.Name)
}
return fmt.Sprintf("[R] %s", s.Name)
}
func colorByKind(kind VarKind) termui.Attribute {
switch kind {
case KindMemory:
return termui.ColorRed | termui.AttrBold
case KindDuration:
return termui.ColorYellow | termui.AttrBold
case KindString:
return termui.ColorGreen | termui.AttrBold
default:
return termui.ColorBlue | termui.AttrBold
}
}