ctop/widgets/hist.go

75 lines
1.4 KiB
Go
Raw Normal View History

2017-01-06 19:46:30 +00:00
package widgets
2017-01-12 15:40:24 +00:00
type IntHist struct {
data []int
2017-01-07 20:37:11 +00:00
labels []string
2017-01-06 19:46:30 +00:00
}
2017-01-12 15:40:24 +00:00
func NewIntHist(max int) IntHist {
return IntHist{
data: make([]int, max),
2017-01-07 20:37:11 +00:00
labels: make([]string, max),
2017-01-06 19:46:30 +00:00
}
}
2017-01-12 15:06:35 +00:00
func (h IntHist) Append(val int) {
2017-01-12 15:40:24 +00:00
if len(h.data) == cap(h.data) {
2017-01-07 20:37:11 +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 FloatHist struct {
2017-01-12 15:40:24 +00:00
data []float64
labels []string
2017-01-07 20:37:11 +00:00
}
2017-01-12 15:06:35 +00:00
func NewFloatHist(max int) FloatHist {
2017-01-12 15:40:24 +00:00
return FloatHist{
data: make([]float64, max),
labels: make([]string, 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-12 15:40:24 +00:00
if len(h.data) == cap(h.data) {
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 {
2017-01-12 18:39:02 +00:00
data []int // data point derivatives
srcData []int // principal input data
labels []string
}
2017-01-12 15:06:35 +00:00
func NewDiffHist(max int) DiffHist {
return DiffHist{
2017-01-12 18:39:02 +00:00
data: make([]int, max),
srcData: make([]int, max),
labels: make([]string, 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) {
2017-01-12 15:40:24 +00:00
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
}
2017-01-12 15:40:24 +00:00
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)
}