expvarmon/ui_multi.go

234 lines
5.5 KiB
Go
Raw Normal View History

2015-05-03 21:52:50 +08:00
package main
import (
"fmt"
"time"
2015-05-04 00:46:36 +08:00
"github.com/gizak/termui"
2015-05-03 21:52:50 +08:00
)
// TermUI is a termUI implementation of UI interface.
type TermUI struct {
Title *termui.Par
Status *termui.Par
Services *termui.List
2015-05-03 22:45:42 +08:00
Lists []*termui.List
2015-05-03 21:52:50 +08:00
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
}
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.Services = func() *termui.List {
list := termui.NewList()
list.ItemFgColor = termui.ColorGreen
2015-05-03 22:45:42 +08:00
list.Border.LabelFgColor = termui.ColorGreen | termui.AttrBold
2015-05-03 21:52:50 +08:00
list.Border.Label = "Services"
list.Height = len(data.Services) + 2
return list
}()
2015-05-03 22:45:42 +08:00
t.Lists = make([]*termui.List, len(data.Vars))
for i, name := range data.Vars {
list := termui.NewList()
list.ItemFgColor = colorByKind(name.Kind())
list.Border.Label = name.Short()
list.Border.LabelFgColor = termui.ColorGreen
if i < 2 {
list.Border.LabelFgColor = termui.ColorGreen | termui.AttrBold
2015-05-03 21:52:50 +08:00
}
2015-05-03 22:45:42 +08:00
list.Height = len(data.Services) + 2
t.Lists[i] = list
2015-05-03 21:52:50 +08:00
}
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.HasBorder = true
s.Border.Label = 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])
}
2015-05-03 22:45:42 +08:00
t.Relayout()
2015-05-03 21:52:50 +08:00
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
2015-05-03 22:45:42 +08:00
for i, name := range data.Vars {
2015-05-03 21:52:50 +08:00
var lines []string
for _, service := range data.Services {
lines = append(lines, service.Value(name))
}
2015-05-03 22:45:42 +08:00
t.Lists[i].Items = lines
2015-05-03 21:52:50 +08:00
}
// 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])
}
}
2015-05-03 22:45:42 +08:00
t.Relayout()
2015-05-03 23:03:49 +08:00
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...)
2015-05-03 22:45:42 +08:00
}
2015-05-03 23:03:49 +08:00
// Relayout recalculates widgets sizes and coords.
2015-05-03 22:45:42 +08:00
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
2015-05-03 23:03:49 +08:00
if tw%2 == 1 {
2015-05-04 02:59:45 +08:00
t.Title.Width++
2015-05-03 23:03:49 +08:00
}
2015-05-03 22:45:42 +08:00
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
2015-05-03 22:45:42 +08:00
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
2015-05-03 23:03:49 +08:00
if tw%2 == 1 {
2015-05-04 02:59:45 +08:00
t.Sparkline2.Width++
2015-05-03 23:03:49 +08:00
}
2015-05-03 22:45:42 +08:00
t.Sparkline2.X = t.Sparkline1.X + t.Sparkline1.Width
t.Sparkline2.Height = h
t.Sparkline2.Y = th - h
}
2015-05-03 21:52:50 +08:00
}
// 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 {
2015-05-04 00:02:19 +08:00
return fmt.Sprintf("[E] ⛔ %s failed", s.Name)
2015-05-03 21:52:50 +08:00
}
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
}
}