Added more json types

This commit is contained in:
Ivan Daniluk 2015-05-02 01:21:52 +03:00
parent f8f3dfff8c
commit 4a6ead5d7e
4 changed files with 61 additions and 15 deletions

View File

@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"sync"
"github.com/antonholmquist/jason"
)
// Service represents constantly updating info about single service.
@ -52,12 +54,29 @@ func (s *Service) Update(wg *sync.WaitGroup) {
// For all vars, fetch desired value from Json and push to it's own stack.
for name, stack := range s.values {
value, err := expvar.GetInt64(name.ToSlice()...)
value, err := expvar.GetValue(name.ToSlice()...)
if err != nil {
continue
}
stack.Push(int(value))
v := guessValue(value)
if v != nil {
stack.Push(v)
}
}
}
func guessValue(value *jason.Value) interface{} {
if v, err := value.Int64(); err == nil {
return v
} else if v, err := value.Float64(); err == nil {
return v
} else if v, err := value.Boolean(); err == nil {
return v
} else if v, err := value.String(); err == nil {
return v
}
return nil
}
// Addr returns fully qualified host:port pair for service.
@ -96,23 +115,23 @@ func (s Service) Value(name VarName) string {
if !ok {
return "N/A"
}
if val.Front() == 0 {
if val.Front() == nil {
return "N/A"
}
return fmt.Sprintf("%d", val.Front())
return fmt.Sprintf("%v", val.Front())
}
// Values returns slice of ints with recent values of the given var,
// to be used with sparkline.
// Values returns slice of ints with recent
// values of the given var, to be used with sparkline.
func (s Service) Values(name VarName) []int {
if s.Err != nil {
return nil
}
val, ok := s.values[name]
stack, ok := s.values[name]
if !ok {
return nil
}
return val.Values
return stack.IntValues()
}

View File

@ -8,7 +8,7 @@ const DefaultSize = 1200
// Stack is a limited FIFO for holding sparkline values.
type Stack struct {
Values []int
Values []interface{}
Len int
}
@ -20,13 +20,13 @@ func NewStack() *Stack {
// NewStackWithSize inits new Stack with size limit.
func NewStackWithSize(size int) *Stack {
return &Stack{
Values: make([]int, size),
Values: make([]interface{}, size),
Len: size,
}
}
// Push inserts data to stack, preserving constant length.
func (s *Stack) Push(val int) {
func (s *Stack) Push(val interface{}) {
s.Values = append(s.Values, val)
if len(s.Values) > s.Len {
s.Values = s.Values[1:]
@ -34,9 +34,33 @@ func (s *Stack) Push(val int) {
}
// Front returns front value.
func (s *Stack) Front() int {
func (s *Stack) Front() interface{} {
if len(s.Values) == 0 {
return 0
return nil
}
return s.Values[len(s.Values)-1]
}
// IntValues returns stack values explicitly casted to int.
//
// Main case is to use with termui.Sparklines.
func (s *Stack) IntValues() []int {
ret := make([]int, s.Len)
for i, v := range s.Values {
n, ok := v.(int64)
if ok {
ret[i] = int(n)
continue
}
b, ok := v.(bool)
if ok {
if b {
ret[i] = 1
} else {
ret[i] = 0
}
}
}
return ret
}

View File

@ -21,7 +21,7 @@ func TestStack(t *testing.T) {
}
}
if s.Front() != 14 {
if s.Front().(int) != 14 {
t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, s.Front())
}
}

View File

@ -104,12 +104,14 @@ func (t *TermUI) Update(data UIData) {
t.Title.Text = fmt.Sprintf("monitoring %d services, press q to quit", len(data.Services))
t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format("15:04:05 02/Jan/06"))
// List with service names
var services []string
for _, service := range data.Services {
services = append(services, service.StatusLine())
}
t.Services.Items = services
// Lists with values
for _, name := range data.Vars {
var lines []string
for _, service := range data.Services {
@ -119,9 +121,10 @@ func (t *TermUI) Update(data UIData) {
}
// Sparklines
topVar := data.Vars[0]
for i, service := range data.Services {
t.MemSparkline.Lines[i].Title = service.Name
t.MemSparkline.Lines[i].Data = service.Values(data.Vars[0])
t.MemSparkline.Lines[i].Data = service.Values(topVar)
}
termui.Body.Width = termui.TermWidth()