2015-05-03 02:17:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-03 20:59:37 +08:00
|
|
|
"time"
|
2015-05-03 02:17:51 +08:00
|
|
|
|
|
|
|
"github.com/divan/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
|
2015-05-03 23:22:26 +08:00
|
|
|
Pars []*termui.Par
|
2015-05-03 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}()
|
|
|
|
|
2015-05-03 23:22:26 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-03 03:29:02 +08:00
|
|
|
var sparklines []termui.Sparkline
|
2015-05-03 02:17:51 +08:00
|
|
|
for _, name := range data.Vars {
|
2015-05-03 03:29:02 +08:00
|
|
|
spl := termui.NewSparkline()
|
|
|
|
spl.Height = 1
|
|
|
|
spl.TitleColor = colorByKind(name.Kind())
|
|
|
|
spl.LineColor = colorByKind(name.Kind())
|
|
|
|
spl.Title = name.Long()
|
|
|
|
sparklines = append(sparklines, spl)
|
2015-05-03 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Sparkline = func() *termui.Sparklines {
|
2015-05-03 03:29:02 +08:00
|
|
|
s := termui.NewSparklines(sparklines...)
|
|
|
|
s.Height = 2*len(sparklines) + 2
|
2015-05-03 02:17:51 +08:00
|
|
|
s.HasBorder = true
|
|
|
|
s.Border.Label = fmt.Sprintf("Monitoring")
|
|
|
|
return s
|
|
|
|
}()
|
|
|
|
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Relayout()
|
2015-05-03 02:17:51 +08:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2015-05-03 03:54:00 +08:00
|
|
|
t.Title.Text = fmt.Sprintf("monitoring %s every %v, press q to quit", service.Name, *interval)
|
2015-05-03 20:59:37 +08:00
|
|
|
t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format(time.Stamp))
|
2015-05-03 02:17:51 +08:00
|
|
|
|
2015-05-03 23:22:26 +08:00
|
|
|
// Pars
|
|
|
|
for i, name := range data.Vars {
|
|
|
|
t.Pars[i].Text = service.Value(name)
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:17:51 +08:00
|
|
|
// Sparklines
|
|
|
|
for i, name := range data.Vars {
|
|
|
|
spl := &t.Sparkline.Lines[i]
|
2015-05-03 03:29:02 +08:00
|
|
|
|
2015-05-03 03:39:39 +08:00
|
|
|
max := formatMax(service.Max(name))
|
|
|
|
spl.Title = fmt.Sprintf("%s: %v%s", name.Long(), service.Value(name), max)
|
2015-05-03 02:17:51 +08:00
|
|
|
spl.TitleColor = colorByKind(name.Kind())
|
|
|
|
spl.LineColor = colorByKind(name.Kind())
|
2015-05-03 03:48:27 +08:00
|
|
|
|
|
|
|
if name.Kind() == KindString {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-03 02:17:51 +08:00
|
|
|
spl.Data = service.Values(name)
|
|
|
|
}
|
|
|
|
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Relayout()
|
|
|
|
|
|
|
|
var widgets []termui.Bufferer
|
|
|
|
widgets = append(widgets, t.Title, t.Status, t.Sparkline)
|
2015-05-03 23:22:26 +08:00
|
|
|
for _, par := range t.Pars {
|
|
|
|
widgets = append(widgets, par)
|
|
|
|
}
|
2015-05-03 23:03:49 +08:00
|
|
|
termui.Render(widgets...)
|
2015-05-03 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down UI module.
|
|
|
|
func (t *TermUISingle) Close() {
|
|
|
|
termui.Close()
|
|
|
|
}
|
2015-05-03 03:39:39 +08:00
|
|
|
|
2015-05-03 23:03:49 +08:00
|
|
|
// 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 += 1
|
|
|
|
}
|
|
|
|
t.Status.Height = firstRowH
|
|
|
|
t.Status.Width = tw / 2
|
|
|
|
t.Status.X = t.Title.X + t.Title.Width
|
|
|
|
h -= firstRowH
|
|
|
|
|
2015-05-03 23:22:26 +08:00
|
|
|
// 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
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Sparkline.Width = tw
|
|
|
|
t.Sparkline.Height = h
|
|
|
|
t.Sparkline.Y = th - h
|
|
|
|
}
|
|
|
|
|
2015-05-03 03:39:39 +08:00
|
|
|
func formatMax(max interface{}) string {
|
|
|
|
var str string
|
|
|
|
if max != nil {
|
|
|
|
str = fmt.Sprintf(" (max: %v)", max)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|