2019-03-24 22:46:39 +00:00
|
|
|
package sparkline
|
|
|
|
|
|
|
|
import (
|
|
|
|
ui "github.com/gizak/termui/v3"
|
|
|
|
"github.com/sqshq/sampler/component"
|
2019-03-26 03:29:23 +00:00
|
|
|
"github.com/sqshq/sampler/component/util"
|
2019-03-24 22:46:39 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"github.com/sqshq/sampler/data"
|
2019-03-26 03:29:23 +00:00
|
|
|
"image"
|
2019-03-24 22:46:39 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SparkLine struct {
|
|
|
|
*ui.Block
|
|
|
|
*data.Consumer
|
2019-03-26 03:29:23 +00:00
|
|
|
alert *data.Alert
|
|
|
|
values []float64
|
|
|
|
maxValue float64
|
|
|
|
minValue float64
|
|
|
|
scale int
|
|
|
|
color ui.Color
|
|
|
|
palette console.Palette
|
2019-03-24 22:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSparkLine(c config.SparkLineConfig, palette console.Palette) *SparkLine {
|
|
|
|
|
|
|
|
line := &SparkLine{
|
|
|
|
Block: component.NewBlock(c.Title, true, palette),
|
|
|
|
Consumer: data.NewConsumer(),
|
|
|
|
values: []float64{},
|
2019-03-26 03:29:23 +00:00
|
|
|
scale: *c.Scale,
|
|
|
|
color: *c.Item.Color,
|
2019-03-24 22:46:39 +00:00
|
|
|
palette: palette,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case sample := <-line.SampleChannel:
|
|
|
|
line.consumeSample(sample)
|
|
|
|
case alert := <-line.AlertChannel:
|
|
|
|
line.alert = alert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SparkLine) consumeSample(sample *data.Sample) {
|
|
|
|
float, err := strconv.ParseFloat(sample.Value, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.AlertChannel <- &data.Alert{
|
|
|
|
Title: "FAILED TO PARSE NUMBER",
|
|
|
|
Text: err.Error(),
|
|
|
|
Color: sample.Color,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.values = append(s.values, float)
|
2019-03-27 03:52:41 +00:00
|
|
|
max, min := s.values[0], s.values[0]
|
2019-03-26 03:29:23 +00:00
|
|
|
|
|
|
|
for i := len(s.values) - 1; i >= 0; i-- {
|
|
|
|
if len(s.values)-i > s.Dx() {
|
|
|
|
break
|
|
|
|
}
|
2019-03-27 03:52:41 +00:00
|
|
|
if s.values[i] > max {
|
|
|
|
max = s.values[i]
|
2019-03-26 03:29:23 +00:00
|
|
|
}
|
2019-03-27 03:52:41 +00:00
|
|
|
if s.values[i] < min {
|
|
|
|
min = s.values[i]
|
2019-03-26 03:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 03:52:41 +00:00
|
|
|
|
|
|
|
s.maxValue = max
|
|
|
|
s.minValue = min
|
|
|
|
|
|
|
|
// perform cleanup once in a while
|
|
|
|
if len(s.values)%100 == 0 {
|
|
|
|
s.values = append(s.values[:0], s.values[len(s.values)-s.Dx()+1:]...)
|
|
|
|
}
|
2019-03-24 22:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SparkLine) Draw(buffer *ui.Buffer) {
|
2019-03-26 03:29:23 +00:00
|
|
|
|
|
|
|
textStyle := ui.NewStyle(s.palette.BaseColor)
|
|
|
|
lineStyle := ui.NewStyle(s.color)
|
|
|
|
|
|
|
|
minValue := util.FormatValue(s.minValue, s.scale)
|
|
|
|
maxValue := util.FormatValue(s.maxValue, s.scale)
|
|
|
|
curValue := util.FormatValue(s.values[len(s.values)-1], s.scale)
|
|
|
|
|
|
|
|
indent := 2 + util.Max([]int{
|
|
|
|
len(minValue), len(maxValue), len(curValue),
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := len(s.values) - 1; i >= 0; i-- {
|
|
|
|
|
|
|
|
n := len(s.values) - i
|
|
|
|
|
2019-03-27 03:52:41 +00:00
|
|
|
if n > s.Dx()-indent-3 {
|
2019-03-26 03:29:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-03-27 03:52:41 +00:00
|
|
|
top := int((s.values[i] / s.maxValue) * float64(s.Dy()-2))
|
|
|
|
|
|
|
|
if top == 0 {
|
|
|
|
top = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 1; j <= top; j++ {
|
|
|
|
buffer.SetCell(ui.NewCell(console.SymbolSquare, lineStyle), image.Pt(s.Inner.Max.X-n-indent, s.Inner.Max.Y-j))
|
|
|
|
if i == len(s.values)-1 && j == top {
|
|
|
|
buffer.SetString(curValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Inner.Max.Y-j))
|
|
|
|
buffer.SetString(minValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Max.Y-2))
|
|
|
|
buffer.SetString(maxValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Min.Y+1))
|
|
|
|
}
|
2019-03-26 03:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 22:46:39 +00:00
|
|
|
s.Block.Draw(buffer)
|
|
|
|
}
|