Move `HistoryLimit` to data structure consumer
This commit is contained in:
parent
b10243a3c4
commit
1ff7bd5500
|
@ -6,13 +6,13 @@ package main
|
|||
// are retained.
|
||||
type CircularBuffer struct {
|
||||
data []float64
|
||||
size int64
|
||||
writeCursor int64
|
||||
size int
|
||||
writeCursor int
|
||||
writeOpCount int64
|
||||
}
|
||||
|
||||
// NewCircularBuffer creates a new buffer of a given size
|
||||
func NewCircularBuffer(size int64) *CircularBuffer {
|
||||
func NewCircularBuffer(size int) *CircularBuffer {
|
||||
if size <= 0 {
|
||||
panic("Size must be positive")
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func (b *CircularBuffer) Add(value float64) error {
|
|||
}
|
||||
|
||||
// Size returns the size of the buffer
|
||||
func (b *CircularBuffer) Size() int64 {
|
||||
func (b *CircularBuffer) Size() int {
|
||||
return b.size
|
||||
}
|
||||
|
||||
|
@ -45,11 +45,11 @@ func (b *CircularBuffer) TotalWriteOpCount() int64 {
|
|||
// Data returns ordered data from buffer, from old to new.
|
||||
func (b *CircularBuffer) Data() []float64 {
|
||||
switch {
|
||||
case b.writeOpCount >= b.size && b.writeCursor == 0:
|
||||
case b.writeOpCount >= int64(b.size) && b.writeCursor == 0:
|
||||
out := make([]float64, b.size)
|
||||
copy(out, b.data)
|
||||
return out
|
||||
case b.writeOpCount > b.size:
|
||||
case b.writeOpCount > int64(b.size):
|
||||
out := make([]float64, b.size)
|
||||
copy(out, b.data[b.writeCursor:])
|
||||
copy(out[b.size-b.writeCursor:], b.data[:b.writeCursor])
|
||||
|
|
13
data.go
13
data.go
|
@ -1,8 +1,5 @@
|
|||
package main
|
||||
|
||||
// Should be enough for wide-screen terminals
|
||||
const HistoryLimit = 1024
|
||||
|
||||
// Data holds actual data values, being a buffer for
|
||||
// UI widges and responsible for flushing/updating/deleting
|
||||
// old values.
|
||||
|
@ -14,12 +11,12 @@ type Data struct {
|
|||
}
|
||||
|
||||
// NewData inits new data object.
|
||||
func NewData() *Data {
|
||||
func NewData(historyLimit int) *Data {
|
||||
return &Data{
|
||||
cpu: NewCircularBuffer(HistoryLimit),
|
||||
mem: NewCircularBuffer(HistoryLimit),
|
||||
txBytes: NewCircularBuffer(HistoryLimit),
|
||||
rxBytes: NewCircularBuffer(HistoryLimit),
|
||||
cpu: NewCircularBuffer(historyLimit),
|
||||
mem: NewCircularBuffer(historyLimit),
|
||||
txBytes: NewCircularBuffer(historyLimit),
|
||||
rxBytes: NewCircularBuffer(historyLimit),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
5
main.go
5
main.go
|
@ -10,6 +10,9 @@ import (
|
|||
"github.com/gizak/termui"
|
||||
)
|
||||
|
||||
// Should be enough for wide-screen terminals
|
||||
const HistoryLimit = 1024
|
||||
|
||||
var (
|
||||
debug = flag.Bool("debug", false, "Disable UI and see raw data (debug mode)")
|
||||
csvdump = flag.Bool("csv", false, "Write every point into CSV file [i.e. 20160201_150405.csv]")
|
||||
|
@ -67,7 +70,7 @@ func main() {
|
|||
}
|
||||
|
||||
// init stuff
|
||||
data := NewData()
|
||||
data := NewData(HistoryLimit)
|
||||
var csv *CSVDump
|
||||
if *csvdump {
|
||||
csv, err = NewCSVDump()
|
||||
|
|
Loading…
Reference in New Issue