From 56e9550611801ec524ce7ceffdf2dc5409139086 Mon Sep 17 00:00:00 2001 From: sqshq Date: Tue, 19 Feb 2019 23:03:19 -0500 Subject: [PATCH] barchart sample consuming and default options --- config.yml | 16 +++++++--- config/default.go | 21 ++++++++++++ widgets/barchart/barchart.go | 62 +++++++++++++++++++++--------------- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/config.yml b/config.yml index 2fdeb63..e979c0f 100644 --- a/config.yml +++ b/config.yml @@ -47,10 +47,18 @@ runcharts: script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" barcharts: - title: ANCHOR CONTEXT EVENTS - - label: ACTIVE - script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" - - label: INACTIVE - script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" + refresh-rate-ms: 300 + position: + w: 30 + h: 10 + size: + w: 16 + h: 4 + items: + - label: ACTIVE + script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" + - label: INACTIVE + script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" asciiboxes: - title: COUNT position: diff --git a/config/default.go b/config/default.go index b2e04e5..9b76120 100644 --- a/config/default.go +++ b/config/default.go @@ -40,6 +40,18 @@ func (c *Config) setDefaultValues() { c.RunCharts[i] = chart } + for i, chart := range c.BarCharts { + if chart.RefreshRateMs == nil { + r := defaultRefreshRateMs + chart.RefreshRateMs = &r + } + if chart.Scale == nil { + p := defaultScale + chart.Scale = &p + } + c.BarCharts[i] = chart + } + for i, box := range c.AsciiBoxes { if box.RefreshRateMs == nil { r := defaultRefreshRateMs @@ -78,4 +90,13 @@ func (c *Config) setDefaultColors() { } } } + + for _, chart := range c.BarCharts { + for j, item := range chart.Items { + if item.Color == nil { + item.Color = &palette.Colors[j%colorsCount] + chart.Items[j] = item + } + } + } } diff --git a/widgets/barchart/barchart.go b/widgets/barchart/barchart.go index 9136baf..223b8e1 100644 --- a/widgets/barchart/barchart.go +++ b/widgets/barchart/barchart.go @@ -1,10 +1,13 @@ package barchart import ( + "fmt" rw "github.com/mattn/go-runewidth" + "github.com/sqshq/sampler/console" "github.com/sqshq/sampler/data" ui "github.com/sqshq/termui" "image" + "math" "strconv" ) @@ -33,7 +36,7 @@ func NewBarChart(title string, scale int) *BarChart { Block: block, bars: []Bar{}, scale: scale, - maxValue: 0, + maxValue: -math.MaxFloat64, } } @@ -56,6 +59,10 @@ func (b *BarChart) ConsumeSample(sample data.Sample) { } } + if float > b.maxValue { + b.maxValue = float + } + bar := b.bars[index] bar.value = float b.bars[index] = bar @@ -64,46 +71,51 @@ func (b *BarChart) ConsumeSample(sample data.Sample) { func (b *BarChart) Draw(buf *ui.Buffer) { b.Block.Draw(buf) - maxVal := b.maxValue - + barWidth := b.Inner.Dx() / len(b.bars) barXCoordinate := b.Inner.Min.X - for i, data := range b.Data { + labelStyle := ui.NewStyle(console.ColorWhite) + + for _, bar := range b.bars { // draw bar - height := int((data / maxVal) * float64(b.Inner.Dy()-1)) - for x := barXCoordinate; x < ui.MinInt(barXCoordinate+b.BarWidth, b.Inner.Max.X); x++ { + height := int((bar.value / b.maxValue) * float64(b.Inner.Dy()-1)) + for x := barXCoordinate; x < ui.MinInt(barXCoordinate+barWidth, b.Inner.Max.X); x++ { for y := b.Inner.Max.Y - 2; y > (b.Inner.Max.Y-2)-height; y-- { - c := ui.NewCell(barSymbol, ui.NewStyle(ui.SelectColor(b.BarColors, i))) + c := ui.NewCell(barSymbol, ui.NewStyle(bar.color)) buf.SetCell(c, image.Pt(x, y)) } } // draw label - if i < len(b.Labels) { - labelXCoordinate := barXCoordinate + - int((float64(b.BarWidth) / 2)) - - int((float64(rw.StringWidth(b.Labels[i])) / 2)) - buf.SetString( - b.Labels[i], - ui.SelectStyle(b.LabelStyles, i), - image.Pt(labelXCoordinate, b.Inner.Max.Y-1), - ) - } + labelXCoordinate := barXCoordinate + + int(float64(barWidth)/2) - + int(float64(rw.StringWidth(bar.label))/2) + buf.SetString( + bar.label, + labelStyle, + image.Pt(labelXCoordinate, b.Inner.Max.Y-1), + ) // draw value - numberXCoordinate := barXCoordinate + int((float64(b.BarWidth) / 2)) + numberXCoordinate := barXCoordinate + int(float64(barWidth)/2) if numberXCoordinate <= b.Inner.Max.X { buf.SetString( - b.NumFmt(data), - ui.NewStyle( - ui.SelectStyle(b.NumStyles, i+1).Fg, - ui.SelectColor(b.BarColors, i), - ui.SelectStyle(b.NumStyles, i+1).Modifier, - ), + formatValue(bar.value, b.scale), + labelStyle, image.Pt(numberXCoordinate, b.Inner.Max.Y-2), ) } - barXCoordinate += (b.BarWidth + b.BarGap) + barXCoordinate += barWidth + barIndent + } +} + +// TODO extract to utils +func formatValue(value float64, scale int) string { + if math.Abs(value) == math.MaxFloat64 { + return "Inf" + } else { + format := "%." + strconv.Itoa(scale) + "f" + return fmt.Sprintf(format, value) } }