ctop/widgets/hist.go

81 lines
1.4 KiB
Go
Raw Normal View History

2017-01-06 19:46:30 +00:00
package widgets
2017-01-12 15:06:35 +00:00
type Hist struct {
2017-01-07 20:37:11 +00:00
maxLen int
labels []string
2017-01-06 19:46:30 +00:00
}
2017-01-12 15:06:35 +00:00
func NewHist(max int) Hist {
return Hist{
2017-01-07 20:37:11 +00:00
maxLen: max,
labels: make([]string, max),
2017-01-06 19:46:30 +00:00
}
}
2017-01-12 15:06:35 +00:00
type IntHist struct {
Hist
2017-01-07 20:37:11 +00:00
data []int
}
2017-01-12 15:06:35 +00:00
func NewIntHist(max int) IntHist {
return IntHist{NewHist(max), make([]int, max)}
2017-01-07 20:37:11 +00:00
}
2017-01-12 15:06:35 +00:00
func (h IntHist) Append(val int) {
2017-01-07 20:37:11 +00:00
if len(h.data) >= h.maxLen {
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)
}
2017-01-12 15:06:35 +00:00
type FloatHist struct {
Hist
2017-01-07 20:37:11 +00:00
data []float64
}
2017-01-12 15:06:35 +00:00
func NewFloatHist(max int) FloatHist {
return FloatHist{NewHist(max), make([]float64, max)}
2017-01-07 20:37:11 +00:00
}
2017-01-12 15:06:35 +00:00
func (h FloatHist) Append(val float64) {
2017-01-07 20:37:11 +00:00
if len(h.data) >= h.maxLen {
2017-01-06 19:46:30 +00:00
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)
}
2017-01-12 15:06:35 +00:00
type DiffHist struct {
Hist
data []int
srcData []int
}
2017-01-12 15:06:35 +00:00
func NewDiffHist(max int) DiffHist {
return DiffHist{
NewHist(max),
make([]int, max),
make([]int, max),
}
}
// return most recent value
2017-01-12 15:06:35 +00:00
func (h DiffHist) Last() int {
return h.data[len(h.data)-1]
}
2017-01-12 15:06:35 +00:00
func (h DiffHist) 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)
}