mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add diffhist struct, update expandednet widget
This commit is contained in:
parent
98a8cecfa1
commit
3b0373b34f
@ -8,30 +8,30 @@ import (
|
||||
|
||||
type ExpandedNet struct {
|
||||
*ui.Sparklines
|
||||
rxHist IntHistData
|
||||
txHist IntHistData
|
||||
rxHist DiffHistData
|
||||
txHist DiffHistData
|
||||
}
|
||||
|
||||
func NewExpandedNet() *ExpandedNet {
|
||||
net := &ExpandedNet{ui.NewSparklines(), NewIntHistData(60), NewIntHistData(60)}
|
||||
net := &ExpandedNet{ui.NewSparklines(), NewDiffHistData(30), NewDiffHistData(30)}
|
||||
|
||||
rx := ui.NewSparkline()
|
||||
rx.Title = "RX"
|
||||
rx.Height = 3
|
||||
rx.Height = 2
|
||||
rx.Data = net.rxHist.data
|
||||
rx.TitleColor = ui.ColorDefault
|
||||
rx.LineColor = ui.ColorGreen
|
||||
|
||||
tx := ui.NewSparkline()
|
||||
tx.Title = "TX"
|
||||
tx.Height = 3
|
||||
tx.Height = 2
|
||||
tx.Data = net.txHist.data
|
||||
tx.TitleColor = ui.ColorDefault
|
||||
tx.LineColor = ui.ColorGreen
|
||||
tx.LineColor = ui.ColorYellow
|
||||
|
||||
net.Lines = []ui.Sparkline{rx, tx}
|
||||
net.Height = 12
|
||||
net.Width = 50
|
||||
net.Height = 8
|
||||
net.Width = 35
|
||||
net.X = 0
|
||||
net.Y = 15
|
||||
return net
|
||||
@ -39,7 +39,8 @@ func NewExpandedNet() *ExpandedNet {
|
||||
|
||||
func (w *ExpandedNet) Update(rx int64, tx int64) {
|
||||
w.rxHist.Append(int(rx))
|
||||
w.Lines[0].Title = fmt.Sprintf("RX [%s/s]", byteFormat(int64(w.rxHist.Last())))
|
||||
|
||||
w.txHist.Append(int(tx))
|
||||
w.Lines[0].Title = fmt.Sprintf("RX (%s)", byteFormat(rx))
|
||||
w.Lines[1].Title = fmt.Sprintf("TX (%s)", byteFormat(tx))
|
||||
w.Lines[1].Title = fmt.Sprintf("TX [%s/s]", byteFormat(int64(w.txHist.Last())))
|
||||
}
|
||||
|
@ -44,3 +44,37 @@ func (h FloatHistData) Append(val float64) {
|
||||
}
|
||||
h.data = append(h.data, val)
|
||||
}
|
||||
|
||||
type DiffHistData struct {
|
||||
HistData
|
||||
data []int
|
||||
srcData []int
|
||||
}
|
||||
|
||||
func NewDiffHistData(max int) DiffHistData {
|
||||
return DiffHistData{
|
||||
NewHistData(max),
|
||||
make([]int, max),
|
||||
make([]int, max),
|
||||
}
|
||||
}
|
||||
|
||||
// return most recent value
|
||||
func (h DiffHistData) Last() int {
|
||||
return h.data[len(h.data)-1]
|
||||
}
|
||||
|
||||
func (h DiffHistData) Append(val int) {
|
||||
if len(h.data) >= h.maxLen {
|
||||
h.data = append(h.data[:0], h.data[1:]...)
|
||||
}
|
||||
if len(h.srcData) >= h.maxLen {
|
||||
h.srcData = append(h.srcData[:0], h.srcData[1:]...)
|
||||
}
|
||||
|
||||
diff := val - h.srcData[len(h.srcData)-1]
|
||||
if diff != val { // skip adding to data if this is the initial update
|
||||
h.data = append(h.data, diff)
|
||||
}
|
||||
h.srcData = append(h.srcData, val)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user