made chart labels/colors order reproducable, based on config

This commit is contained in:
sqshq 2019-02-16 11:59:35 -05:00
parent 83ab5d6e64
commit 94c7d5011e
6 changed files with 31 additions and 40 deletions

View File

@ -1,14 +1,11 @@
package data package data
import . "github.com/sqshq/termui"
type Consumer interface { type Consumer interface {
ConsumeSample(sample Sample) ConsumeSample(sample Sample)
} }
type Sample struct { type Sample struct {
Label string Label string
Color Color
Value string Value string
Error error Error error
} }

View File

@ -1,15 +1,15 @@
package data package data
import ( import (
. "github.com/sqshq/termui" ui "github.com/sqshq/termui"
"os/exec" "os/exec"
"strings" "strings"
) )
type Item struct { type Item struct {
Script string `yaml:"script"` Script string `yaml:"script"`
Label string `yaml:"label"` Label string `yaml:"label"`
Color Color `yaml:"color"` Color ui.Color `yaml:"color"`
} }
func (self *Item) nextValue() (value string, err error) { func (self *Item) nextValue() (value string, err error) {

View File

@ -29,7 +29,6 @@ func (self *Sampler) sample() {
sample := Sample{ sample := Sample{
Value: value, Value: value,
Error: err, Error: err,
Color: self.item.Color,
Label: self.item.Label, Label: self.item.Label,
} }

View File

@ -28,6 +28,7 @@ func main() {
layout.AddComponent(chart, c.Title, c.Position, c.Size, widgets.TypeRunChart) layout.AddComponent(chart, c.Title, c.Position, c.Size, widgets.TypeRunChart)
for _, item := range c.Items { for _, item := range c.Items {
chart.AddLine(item.Label, item.Color)
data.NewSampler(chart, item, c.RefreshRateMs) data.NewSampler(chart, item, c.RefreshRateMs)
} }
} }

View File

@ -36,15 +36,13 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) {
// draw origin cell // draw origin cell
buffer.SetCell( buffer.SetCell(
ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(ui.ColorWhite)), ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(ui.ColorWhite)),
image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, c.Inner.Max.Y-xAxisLabelsHeight-1), image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, c.Inner.Max.Y-xAxisLabelsHeight-1))
)
// draw x axis line // draw x axis line
for i := c.grid.minTimeWidth + 1; i < c.Inner.Dx(); i++ { for i := c.grid.minTimeWidth + 1; i < c.Inner.Dx(); i++ {
buffer.SetCell( buffer.SetCell(
ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(ui.ColorWhite)), ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(ui.ColorWhite)),
image.Pt(i+c.Inner.Min.X, c.Inner.Max.Y-xAxisLabelsHeight-1), image.Pt(i+c.Inner.Min.X, c.Inner.Max.Y-xAxisLabelsHeight-1))
)
} }
// draw grid lines // draw grid lines
@ -52,8 +50,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) {
for x := 1; x <= c.grid.linesCount; x++ { for x := 1; x <= c.grid.linesCount; x++ {
buffer.SetCell( buffer.SetCell(
ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorDarkGrey)), ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorDarkGrey)),
image.Pt(c.grid.maxTimeWidth-x*xAxisGridWidth, y+c.Inner.Min.Y+1), image.Pt(c.grid.maxTimeWidth-x*xAxisGridWidth, y+c.Inner.Min.Y+1))
)
} }
} }
@ -61,8 +58,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) {
for i := 0; i < c.Inner.Dy()-xAxisLabelsHeight-1; i++ { for i := 0; i < c.Inner.Dy()-xAxisLabelsHeight-1; i++ {
buffer.SetCell( buffer.SetCell(
ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(ui.ColorWhite)), ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(ui.ColorWhite)),
image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, i+c.Inner.Min.Y), image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, i+c.Inner.Min.Y))
)
} }
// draw x axis time labels // draw x axis time labels
@ -71,8 +67,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) {
buffer.SetString( buffer.SetString(
labelTime.Format("15:04:05"), labelTime.Format("15:04:05"),
ui.NewStyle(ui.ColorWhite), ui.NewStyle(ui.ColorWhite),
image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1), image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1))
)
} }
// draw y axis labels // draw y axis labels
@ -84,8 +79,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) {
buffer.SetString( buffer.SetString(
formatValue(value, c.precision), formatValue(value, c.precision),
ui.NewStyle(ui.ColorWhite), ui.NewStyle(ui.ColorWhite),
image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight)), image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight)))
)
} }
} else { } else {
buffer.SetString( buffer.SetString(

View File

@ -111,6 +111,16 @@ func (c *RunChart) Draw(buffer *ui.Buffer) {
c.mutex.Unlock() c.mutex.Unlock()
} }
func (c *RunChart) AddLine(Label string, color ui.Color) {
line := TimeLine{
points: []TimePoint{},
color: color,
label: Label,
extrema: ValueExtrema{max: -math.MaxFloat64, min: math.MaxFloat64},
}
c.lines = append(c.lines, line)
}
func (c *RunChart) ConsumeSample(sample data.Sample) { func (c *RunChart) ConsumeSample(sample data.Sample) {
float, err := strconv.ParseFloat(sample.Value, 64) float, err := strconv.ParseFloat(sample.Value, 64)
@ -121,40 +131,30 @@ func (c *RunChart) ConsumeSample(sample data.Sample) {
c.mutex.Lock() c.mutex.Lock()
lineIndex := -1 index := -1
for i, line := range c.lines { for i, line := range c.lines {
if line.label == sample.Label { if line.label == sample.Label {
lineIndex = i index = i
} }
} }
if lineIndex == -1 { line := c.lines[index]
line := &TimeLine{
points: []TimePoint{},
color: sample.Color,
label: sample.Label,
extrema: ValueExtrema{max: float, min: float},
}
c.lines = append(c.lines, *line)
lineIndex = len(c.lines) - 1
}
line := c.lines[lineIndex]
if float < line.extrema.min { if float < line.extrema.min {
line.extrema.min = float line.extrema.min = float
} }
if float > line.extrema.max { if float > line.extrema.max {
line.extrema.max = float line.extrema.max = float
} }
timePoint := c.newTimePoint(float) line.points = append(line.points, c.newTimePoint(float))
line.points = append(line.points, timePoint) c.lines[index] = line
c.lines[lineIndex] = line
// perform cleanup once in a while
if len(line.points)%100 == 0 {
c.trimOutOfRangeValues()
}
c.trimOutOfRangeValues()
c.mutex.Unlock() c.mutex.Unlock()
} }