2017-02-26 22:04:24 +00:00
|
|
|
package compact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
"github.com/bcicen/ctop/cwidgets"
|
2017-03-03 07:57:26 +00:00
|
|
|
"github.com/bcicen/ctop/logging"
|
|
|
|
"github.com/bcicen/ctop/metrics"
|
2017-02-26 22:04:24 +00:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
var log = logging.Init()
|
|
|
|
|
2017-02-26 22:04:24 +00:00
|
|
|
const (
|
2017-03-03 07:57:26 +00:00
|
|
|
colSpacing = 1
|
2017-02-26 22:04:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Compact struct {
|
2017-03-03 07:57:26 +00:00
|
|
|
Status *Status
|
|
|
|
Name *TextCol
|
|
|
|
Cid *TextCol
|
|
|
|
Cpu *GaugeCol
|
|
|
|
Memory *GaugeCol
|
|
|
|
Net *TextCol
|
2017-02-26 22:55:45 +00:00
|
|
|
X, Y int
|
|
|
|
Width int
|
|
|
|
Height int
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func NewCompact(id, name string) *Compact {
|
2017-02-26 22:55:45 +00:00
|
|
|
row := &Compact{
|
2017-03-03 07:57:26 +00:00
|
|
|
Status: NewStatus(),
|
|
|
|
Name: NewTextCol(name),
|
|
|
|
Cid: NewTextCol(id),
|
|
|
|
Cpu: NewGaugeCol(),
|
|
|
|
Memory: NewGaugeCol(),
|
|
|
|
Net: NewTextCol("-"),
|
2017-02-26 22:55:45 +00:00
|
|
|
Height: 1,
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
2017-02-26 22:55:45 +00:00
|
|
|
return row
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func (row *Compact) SetMetrics(m metrics.Metrics) {
|
|
|
|
row.SetCPU(m.CPUUtil)
|
|
|
|
row.SetNet(m.NetRx, m.NetTx)
|
|
|
|
row.SetMem(m.MemUsage, m.MemLimit, m.MemPercent)
|
|
|
|
}
|
|
|
|
|
2017-02-26 22:04:24 +00:00
|
|
|
// Set gauges, counters to default unread values
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) Reset() {
|
2017-03-03 07:57:26 +00:00
|
|
|
row.Cpu.Reset()
|
|
|
|
row.Memory.Reset()
|
|
|
|
row.Net.Reset()
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) all() []ui.GridBufferer {
|
2017-02-26 22:04:24 +00:00
|
|
|
return []ui.GridBufferer{
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Status,
|
|
|
|
row.Name,
|
|
|
|
row.Cid,
|
|
|
|
row.Cpu,
|
|
|
|
row.Memory,
|
|
|
|
row.Net,
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) SetY(y int) {
|
|
|
|
if y == row.Y {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, col := range row.all() {
|
2017-02-26 22:04:24 +00:00
|
|
|
col.SetY(y)
|
|
|
|
}
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Y = y
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) SetWidth(width int) {
|
|
|
|
if row.Width == width {
|
|
|
|
return
|
|
|
|
}
|
2017-02-26 22:04:24 +00:00
|
|
|
x := 1
|
|
|
|
autoWidth := calcWidth(width, 5)
|
2017-02-26 22:55:45 +00:00
|
|
|
for n, col := range row.all() {
|
2017-02-26 22:04:24 +00:00
|
|
|
if n == 0 {
|
|
|
|
col.SetX(x)
|
|
|
|
col.SetWidth(statusWidth)
|
|
|
|
x += statusWidth
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
col.SetX(x)
|
|
|
|
col.SetWidth(autoWidth)
|
|
|
|
x += autoWidth + colSpacing
|
|
|
|
}
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Width = width
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) Buffer() ui.Buffer {
|
2017-02-26 22:04:24 +00:00
|
|
|
buf := ui.NewBuffer()
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
buf.Merge(row.Status.Buffer())
|
|
|
|
buf.Merge(row.Name.Buffer())
|
|
|
|
buf.Merge(row.Cid.Buffer())
|
|
|
|
buf.Merge(row.Cpu.Buffer())
|
|
|
|
buf.Merge(row.Memory.Buffer())
|
|
|
|
buf.Merge(row.Net.Buffer())
|
2017-02-26 22:04:24 +00:00
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func (row *Compact) SetNet(rx int64, tx int64) {
|
|
|
|
label := fmt.Sprintf("%s / %s", cwidgets.ByteFormat(rx), cwidgets.ByteFormat(tx))
|
|
|
|
row.Net.Set(label)
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) SetCPU(val int) {
|
|
|
|
row.Cpu.BarColor = cwidgets.ColorScale(val)
|
|
|
|
row.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
2017-02-26 22:04:24 +00:00
|
|
|
if val < 5 {
|
|
|
|
val = 5
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Cpu.BarColor = ui.ColorBlack
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Cpu.Percent = val
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:55:45 +00:00
|
|
|
func (row *Compact) SetMem(val int64, limit int64, percent int) {
|
|
|
|
row.Memory.Label = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(val), cwidgets.ByteFormat(limit))
|
2017-02-26 22:04:24 +00:00
|
|
|
if percent < 5 {
|
|
|
|
percent = 5
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Memory.BarColor = ui.ColorBlack
|
2017-02-26 22:04:24 +00:00
|
|
|
} else {
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Memory.BarColor = ui.ColorGreen
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
2017-02-26 22:55:45 +00:00
|
|
|
row.Memory.Percent = percent
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|