Move `HistoryLimit` to data structure consumer

This commit is contained in:
Pedro Pombeiro 2018-01-29 17:41:54 +01:00
parent b10243a3c4
commit 1ff7bd5500
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
3 changed files with 15 additions and 15 deletions

View File

@ -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
View File

@ -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),
}
}

View File

@ -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()