barchart sample consuming and default options
This commit is contained in:
parent
56fbf0cc0d
commit
56e9550611
16
config.yml
16
config.yml
|
@ -47,10 +47,18 @@ runcharts:
|
||||||
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
|
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
|
||||||
barcharts:
|
barcharts:
|
||||||
- title: ANCHOR CONTEXT EVENTS
|
- title: ANCHOR CONTEXT EVENTS
|
||||||
- label: ACTIVE
|
refresh-rate-ms: 300
|
||||||
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()"
|
position:
|
||||||
- label: INACTIVE
|
w: 30
|
||||||
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
|
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:
|
asciiboxes:
|
||||||
- title: COUNT
|
- title: COUNT
|
||||||
position:
|
position:
|
||||||
|
|
|
@ -40,6 +40,18 @@ func (c *Config) setDefaultValues() {
|
||||||
c.RunCharts[i] = chart
|
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 {
|
for i, box := range c.AsciiBoxes {
|
||||||
if box.RefreshRateMs == nil {
|
if box.RefreshRateMs == nil {
|
||||||
r := defaultRefreshRateMs
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package barchart
|
package barchart
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
rw "github.com/mattn/go-runewidth"
|
rw "github.com/mattn/go-runewidth"
|
||||||
|
"github.com/sqshq/sampler/console"
|
||||||
"github.com/sqshq/sampler/data"
|
"github.com/sqshq/sampler/data"
|
||||||
ui "github.com/sqshq/termui"
|
ui "github.com/sqshq/termui"
|
||||||
"image"
|
"image"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,7 +36,7 @@ func NewBarChart(title string, scale int) *BarChart {
|
||||||
Block: block,
|
Block: block,
|
||||||
bars: []Bar{},
|
bars: []Bar{},
|
||||||
scale: scale,
|
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 := b.bars[index]
|
||||||
bar.value = float
|
bar.value = float
|
||||||
b.bars[index] = bar
|
b.bars[index] = bar
|
||||||
|
@ -64,46 +71,51 @@ func (b *BarChart) ConsumeSample(sample data.Sample) {
|
||||||
func (b *BarChart) Draw(buf *ui.Buffer) {
|
func (b *BarChart) Draw(buf *ui.Buffer) {
|
||||||
b.Block.Draw(buf)
|
b.Block.Draw(buf)
|
||||||
|
|
||||||
maxVal := b.maxValue
|
barWidth := b.Inner.Dx() / len(b.bars)
|
||||||
|
|
||||||
barXCoordinate := b.Inner.Min.X
|
barXCoordinate := b.Inner.Min.X
|
||||||
|
|
||||||
for i, data := range b.Data {
|
labelStyle := ui.NewStyle(console.ColorWhite)
|
||||||
|
|
||||||
|
for _, bar := range b.bars {
|
||||||
// draw bar
|
// draw bar
|
||||||
height := int((data / maxVal) * float64(b.Inner.Dy()-1))
|
height := int((bar.value / b.maxValue) * float64(b.Inner.Dy()-1))
|
||||||
for x := barXCoordinate; x < ui.MinInt(barXCoordinate+b.BarWidth, b.Inner.Max.X); x++ {
|
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-- {
|
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))
|
buf.SetCell(c, image.Pt(x, y))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw label
|
// draw label
|
||||||
if i < len(b.Labels) {
|
labelXCoordinate := barXCoordinate +
|
||||||
labelXCoordinate := barXCoordinate +
|
int(float64(barWidth)/2) -
|
||||||
int((float64(b.BarWidth) / 2)) -
|
int(float64(rw.StringWidth(bar.label))/2)
|
||||||
int((float64(rw.StringWidth(b.Labels[i])) / 2))
|
buf.SetString(
|
||||||
buf.SetString(
|
bar.label,
|
||||||
b.Labels[i],
|
labelStyle,
|
||||||
ui.SelectStyle(b.LabelStyles, i),
|
image.Pt(labelXCoordinate, b.Inner.Max.Y-1),
|
||||||
image.Pt(labelXCoordinate, b.Inner.Max.Y-1),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw value
|
// draw value
|
||||||
numberXCoordinate := barXCoordinate + int((float64(b.BarWidth) / 2))
|
numberXCoordinate := barXCoordinate + int(float64(barWidth)/2)
|
||||||
if numberXCoordinate <= b.Inner.Max.X {
|
if numberXCoordinate <= b.Inner.Max.X {
|
||||||
buf.SetString(
|
buf.SetString(
|
||||||
b.NumFmt(data),
|
formatValue(bar.value, b.scale),
|
||||||
ui.NewStyle(
|
labelStyle,
|
||||||
ui.SelectStyle(b.NumStyles, i+1).Fg,
|
|
||||||
ui.SelectColor(b.BarColors, i),
|
|
||||||
ui.SelectStyle(b.NumStyles, i+1).Modifier,
|
|
||||||
),
|
|
||||||
image.Pt(numberXCoordinate, b.Inner.Max.Y-2),
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue