refactor data hist structs, add Val field

This commit is contained in:
Bradley Cicenas 2017-03-01 12:03:19 +11:00
parent 423ad8e753
commit 1c74377cbd
4 changed files with 48 additions and 62 deletions

View File

@ -16,8 +16,8 @@ func NewExpandedCpu() *ExpandedCpu {
cpu.Width = 50
cpu.X = 0
cpu.Y = 4
cpu.Data = cpu.hist.data
cpu.DataLabels = cpu.hist.labels
cpu.Data = cpu.hist.Data
cpu.DataLabels = cpu.hist.Labels
cpu.AxesColor = ui.ColorDefault
cpu.LineColor = ui.ColorGreen
return cpu

View File

@ -1,74 +1,60 @@
package expanded
type IntHist struct {
data []int
labels []string
Val int // most current data point
Data []int // historical data points
Labels []string
}
func NewIntHist(max int) IntHist {
return IntHist{
data: make([]int, max),
labels: make([]string, max),
func NewIntHist(max int) *IntHist {
return &IntHist{
Data: make([]int, max),
Labels: make([]string, max),
}
}
func (h IntHist) Append(val int) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
func (h *IntHist) Append(val int) {
if len(h.Data) == cap(h.Data) {
h.Data = append(h.Data[:0], h.Data[1:]...)
}
h.Val = val
h.Data = append(h.Data, val)
}
h.data = append(h.data, val)
type DiffHist struct {
*IntHist
lastVal int
}
func NewDiffHist(max int) *DiffHist {
return &DiffHist{NewIntHist(max), -1}
}
func (h *DiffHist) Append(val int) {
if h.lastVal >= 0 { // skip append if this is the initial update
diff := val - h.lastVal
h.IntHist.Append(diff)
}
h.lastVal = val
}
type FloatHist struct {
data []float64
labels []string
Val float64 // most current data point
Data []float64 // historical data points
Labels []string
}
func NewFloatHist(max int) FloatHist {
return FloatHist{
data: make([]float64, max),
labels: make([]string, max),
Data: make([]float64, max),
Labels: make([]string, max),
}
}
func (h FloatHist) Append(val float64) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
if len(h.Data) == cap(h.Data) {
h.Data = append(h.Data[:0], h.Data[1:]...)
}
h.data = append(h.data, val)
}
type DiffHist struct {
data []int // data point derivatives
srcData []int // principal input data
labels []string
}
func NewDiffHist(max int) DiffHist {
return DiffHist{
data: make([]int, max),
srcData: make([]int, max),
labels: make([]string, max),
}
}
// return most recent value
func (h DiffHist) Last() int {
return h.data[len(h.data)-1]
}
func (h DiffHist) Append(val int) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
}
if len(h.srcData) == cap(h.srcData) {
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)
h.Val = val
h.Data = append(h.Data, val)
}

View File

@ -7,7 +7,7 @@ import (
type ExpandedMem struct {
*ui.BarChart
hist IntHist
hist *IntHist
}
func NewExpandedMem() *ExpandedMem {
@ -23,9 +23,9 @@ func NewExpandedMem() *ExpandedMem {
mem.X = 0
mem.Y = 14
mem.TextColor = ui.ColorDefault
mem.Data = mem.hist.data
mem.Data = mem.hist.Data
mem.BarColor = ui.ColorGreen
mem.DataLabels = mem.hist.labels
mem.DataLabels = mem.hist.Labels
mem.NumFmt = cwidgets.ByteFormatInt
return mem
}

View File

@ -10,8 +10,8 @@ import (
type ExpandedNet struct {
*ui.Sparklines
rxHist DiffHist
txHist DiffHist
rxHist *DiffHist
txHist *DiffHist
}
func NewExpandedNet() *ExpandedNet {
@ -25,14 +25,14 @@ func NewExpandedNet() *ExpandedNet {
rx := ui.NewSparkline()
rx.Title = "RX"
rx.Height = 1
rx.Data = net.rxHist.data
rx.Data = net.rxHist.Data
rx.TitleColor = ui.ColorDefault
rx.LineColor = ui.ColorGreen
tx := ui.NewSparkline()
tx.Title = "TX"
tx.Height = 1
tx.Data = net.txHist.data
tx.Data = net.txHist.Data
tx.TitleColor = ui.ColorDefault
tx.LineColor = ui.ColorYellow
@ -44,10 +44,10 @@ func (w *ExpandedNet) Update(rx int64, tx int64) {
var rate string
w.rxHist.Append(int(rx))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.rxHist.Last()))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.rxHist.Val))
w.Lines[0].Title = fmt.Sprintf("RX [%s/s]", rate)
w.txHist.Append(int(tx))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.txHist.Last()))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.txHist.Val))
w.Lines[1].Title = fmt.Sprintf("TX [%s/s]", rate)
}