add mute color placeholder for low-percent gauages

This commit is contained in:
Bradley Cicenas 2016-12-27 15:32:45 +00:00
parent 407e8fca0e
commit b7823bda5c
4 changed files with 37 additions and 19 deletions

View File

@ -2,28 +2,24 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"github.com/fsouza/go-dockerclient" "github.com/fsouza/go-dockerclient"
) )
type Container struct { type Container struct {
id string id string
name string
done chan bool done chan bool
stats chan *docker.Stats stats chan *docker.Stats
widgets *Widgets widgets *Widgets
reader *StatReader reader *StatReader
} }
func NewContainer(cid string, names []string) *Container { func NewContainer(cid, names string) *Container {
name := strings.Join(names, ",")
return &Container{ return &Container{
id: cid, id: cid,
name: name,
done: make(chan bool), done: make(chan bool),
stats: make(chan *docker.Stats), stats: make(chan *docker.Stats),
widgets: NewWidgets(cid, name), widgets: NewWidgets(cid, names),
reader: &StatReader{}, reader: &StatReader{},
} }
} }

View File

@ -12,7 +12,7 @@ type Grid struct {
containers map[string]*Container containers map[string]*Container
} }
func (g *Grid) AddContainer(id string, names []string) { func (g *Grid) AddContainer(id, names string) {
g.containers[id] = NewContainer(id, names) g.containers[id] = NewContainer(id, names)
} }
@ -60,7 +60,7 @@ func header() *ui.Row {
ui.NewCol(2, 0, headerPar("CPU")), ui.NewCol(2, 0, headerPar("CPU")),
ui.NewCol(2, 0, headerPar("MEM")), ui.NewCol(2, 0, headerPar("MEM")),
ui.NewCol(2, 0, headerPar("NET RX/TX")), ui.NewCol(2, 0, headerPar("NET RX/TX")),
ui.NewCol(2, 0, headerPar("NAMES")), ui.NewCol(2, 0, headerPar("NAME")),
) )
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"os" "os"
"strings"
"github.com/fsouza/go-dockerclient" "github.com/fsouza/go-dockerclient"
) )
@ -19,6 +20,11 @@ func getContainers(client *docker.Client) []docker.APIContainers {
return containers return containers
} }
// Return primary container name
func parseName(names []string) string {
return strings.Replace(names[0], "/", "", -1)
}
func main() { func main() {
dockerhost := os.Getenv("DOCKER_HOST") dockerhost := os.Getenv("DOCKER_HOST")
if dockerhost == "" { if dockerhost == "" {
@ -32,7 +38,7 @@ func main() {
g := &Grid{0, make(map[string]*Container)} g := &Grid{0, make(map[string]*Container)}
for _, c := range getContainers(client) { for _, c := range getContainers(client) {
g.AddContainer(c.ID[:12], c.Names) g.AddContainer(c.ID[:12], parseName(c.Names))
} }
for _, c := range g.containers { for _, c := range g.containers {

View File

@ -9,9 +9,9 @@ import (
type Widgets struct { type Widgets struct {
cid *ui.Par cid *ui.Par
names *ui.Par net *ui.Par
name *ui.Par
cpu *ui.Gauge cpu *ui.Gauge
net *ui.Gauge
memory *ui.Gauge memory *ui.Gauge
} }
@ -21,43 +21,59 @@ func (w *Widgets) MakeRow() *ui.Row {
ui.NewCol(2, 0, w.cpu), ui.NewCol(2, 0, w.cpu),
ui.NewCol(2, 0, w.memory), ui.NewCol(2, 0, w.memory),
ui.NewCol(2, 0, w.net), ui.NewCol(2, 0, w.net),
ui.NewCol(2, 0, w.names), ui.NewCol(2, 0, w.name),
) )
} }
func (w *Widgets) SetCPU(val int) { func (w *Widgets) SetCPU(val int) {
w.cpu.BarColor = colorScale(val) w.cpu.BarColor = colorScale(val)
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
if val < 5 && val > 0 { if val < 5 {
val = 5 val = 5
w.cpu.BarColor = ui.ColorBlack
} }
w.cpu.Percent = val w.cpu.Percent = val
} }
func (w *Widgets) SetNet(rx int64, tx int64) { func (w *Widgets) SetNet(rx int64, tx int64) {
w.net.Label = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx)) //w.net.Label = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
w.net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
//w.net2.Lines[0].Data = []int{0, 2, 5, 10, 20, 20, 2, 2, 0, 0}
} }
func (w *Widgets) SetMem(val int64, limit int64) { func (w *Widgets) SetMem(val int64, limit int64) {
if val < 5 { percent := round((float64(val) / float64(limit)) * 100)
val = 5
}
w.memory.Percent = round((float64(val) / float64(limit)) * 100)
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit)) w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
if percent < 5 {
percent = 5
w.memory.BarColor = ui.ColorBlack
} else {
w.memory.BarColor = ui.ColorGreen
}
w.memory.Percent = percent
} }
func NewWidgets(id string, names string) *Widgets { func NewWidgets(id string, names string) *Widgets {
cid := ui.NewPar(id) cid := ui.NewPar(id)
cid.Border = false cid.Border = false
cid.Height = 1 cid.Height = 1
cid.Width = 20 cid.Width = 20
cid.TextFgColor = ui.ColorWhite cid.TextFgColor = ui.ColorWhite
name := ui.NewPar(names) name := ui.NewPar(names)
name.Border = false name.Border = false
name.Height = 1 name.Height = 1
name.Width = 20 name.Width = 20
name.TextFgColor = ui.ColorWhite name.TextFgColor = ui.ColorWhite
return &Widgets{cid, name, mkGauge(), mkGauge(), mkGauge()}
net := ui.NewPar("-")
net.Border = false
net.Height = 1
net.Width = 20
net.TextFgColor = ui.ColorWhite
return &Widgets{cid, net, name, mkGauge(), mkGauge()}
} }
func mkGauge() *ui.Gauge { func mkGauge() *ui.Gauge {