barchart adjustments

This commit is contained in:
sqshq 2019-02-20 23:53:59 -05:00
parent 56e9550611
commit 71dab1e249
3 changed files with 145 additions and 94 deletions

View File

@ -4,7 +4,7 @@ runcharts:
w: 0 w: 0
h: 0 h: 0
size: size:
w: 50 w: 37
h: 14 h: 14
scale: 3 scale: 3
items: items:
@ -17,11 +17,11 @@ runcharts:
- title: SEARCH ENGINE RESPONSE TIME 2 (sec) - title: SEARCH ENGINE RESPONSE TIME 2 (sec)
refresh-rate-ms: 5000 refresh-rate-ms: 5000
position: position:
w: 0 w: 37
h: 14 h: 0
size: size:
w: 17 w: 13
h: 10 h: 14
legend: legend:
enabled: true enabled: true
details: false details: false
@ -46,19 +46,26 @@ runcharts:
- label: INACTIVE - label: INACTIVE
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: DB EVENTS
refresh-rate-ms: 300 refresh-rate-ms: 1000
position: position:
w: 30 w: 0
h: 10 h: 14
size: size:
w: 16 w: 17
h: 4 h: 10
scale: 0
items: items:
- label: ACTIVE - label: ACTIVE
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()"
- label: INACTIVE - label: INACTIVE
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()"
- label: HUACTIVE
script: echo 0
- label: ACTIVE2
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()"
- label: INACTIVE2
script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
asciiboxes: asciiboxes:
- title: COUNT - title: COUNT
position: position:

View File

@ -108,6 +108,12 @@ func (c *Config) findComponent(componentType ComponentType, componentTitle strin
return &c.RunCharts[i].ComponentConfig return &c.RunCharts[i].ComponentConfig
} }
} }
case TypeBarChart:
for i, component := range c.BarCharts {
if component.Title == componentTitle {
return &c.BarCharts[i].ComponentConfig
}
}
case TypeAsciiBox: case TypeAsciiBox:
for i, component := range c.AsciiBoxes { for i, component := range c.AsciiBoxes {
if component.Title == componentTitle { if component.Title == componentTitle {

View File

@ -21,12 +21,14 @@ type BarChart struct {
bars []Bar bars []Bar
scale int scale int
maxValue float64 maxValue float64
count int64
} }
type Bar struct { type Bar struct {
label string label string
color ui.Color color ui.Color
value float64 value float64
delta float64
} }
func NewBarChart(title string, scale int) *BarChart { func NewBarChart(title string, scale int) *BarChart {
@ -46,8 +48,9 @@ func (b *BarChart) AddBar(label string, color ui.Color) {
func (b *BarChart) ConsumeSample(sample data.Sample) { func (b *BarChart) ConsumeSample(sample data.Sample) {
float, err := strconv.ParseFloat(sample.Value, 64) b.count++
float, err := strconv.ParseFloat(sample.Value, 64)
if err != nil { if err != nil {
// TODO visual notification + check sample.Error // TODO visual notification + check sample.Error
} }
@ -59,28 +62,50 @@ func (b *BarChart) ConsumeSample(sample data.Sample) {
} }
} }
bar := b.bars[index]
bar.delta = float - bar.value
bar.value = float
b.bars[index] = bar
if float > b.maxValue { if float > b.maxValue {
b.maxValue = float b.maxValue = float
} }
bar := b.bars[index] // normalize bars height once in a while
bar.value = float if b.count%500 == 0 {
b.bars[index] = bar b.reselectMaxValue()
}
}
func (b *BarChart) reselectMaxValue() {
maxValue := -math.MaxFloat64
for _, bar := range b.bars {
if bar.value > maxValue {
maxValue = bar.value
}
}
b.maxValue = maxValue
} }
func (b *BarChart) Draw(buf *ui.Buffer) { func (b *BarChart) Draw(buf *ui.Buffer) {
b.Block.Draw(buf) b.Block.Draw(buf)
barWidth := b.Inner.Dx() / len(b.bars) barWidth := (b.Inner.Dx() - 2*barIndent - len(b.bars)) / len(b.bars)
barXCoordinate := b.Inner.Min.X barXCoordinate := b.Inner.Min.X + barIndent
labelStyle := ui.NewStyle(console.ColorWhite) labelStyle := ui.NewStyle(console.ColorWhite)
for _, bar := range b.bars { for _, bar := range b.bars {
// draw bar // draw bar
height := int((bar.value / b.maxValue) * float64(b.Inner.Dy()-1)) height := int((bar.value / b.maxValue) * float64(b.Inner.Dy()-1))
for x := barXCoordinate; x < ui.MinInt(barXCoordinate+barWidth, b.Inner.Max.X); x++ { if height <= 1 {
for y := b.Inner.Max.Y - 2; y > (b.Inner.Max.Y-2)-height; y-- { height = 2
}
maxYCoordinate := b.Inner.Max.Y - height
for x := barXCoordinate; x < ui.MinInt(barXCoordinate+barWidth, b.Inner.Max.X-barIndent); x++ {
for y := b.Inner.Max.Y - 2; y >= maxYCoordinate; y-- {
c := ui.NewCell(barSymbol, ui.NewStyle(bar.color)) c := ui.NewCell(barSymbol, ui.NewStyle(bar.color))
buf.SetCell(c, image.Pt(x, y)) buf.SetCell(c, image.Pt(x, y))
} }
@ -93,18 +118,20 @@ func (b *BarChart) Draw(buf *ui.Buffer) {
buf.SetString( buf.SetString(
bar.label, bar.label,
labelStyle, labelStyle,
image.Pt(labelXCoordinate, b.Inner.Max.Y-1), image.Pt(labelXCoordinate, b.Inner.Max.Y-1))
)
// draw value // draw value & delta
numberXCoordinate := barXCoordinate + int(float64(barWidth)/2) value := formatValue(bar.value, b.scale)
if numberXCoordinate <= b.Inner.Max.X { if bar.delta != 0 {
buf.SetString( value = fmt.Sprintf("%s / %s", value, formatValueWithSign(bar.delta, b.scale))
formatValue(bar.value, b.scale),
labelStyle,
image.Pt(numberXCoordinate, b.Inner.Max.Y-2),
)
} }
valueXCoordinate := barXCoordinate +
int(float64(barWidth)/2) -
int(float64(rw.StringWidth(value))/2)
buf.SetString(
value,
labelStyle,
image.Pt(valueXCoordinate, maxYCoordinate-1))
barXCoordinate += barWidth + barIndent barXCoordinate += barWidth + barIndent
} }
@ -119,3 +146,14 @@ func formatValue(value float64, scale int) string {
return fmt.Sprintf(format, value) return fmt.Sprintf(format, value)
} }
} }
// TODO extract to utils
func formatValueWithSign(value float64, scale int) string {
if value == 0 {
return " 0"
} else if value > 0 {
return "+" + formatValue(value, scale)
} else {
return formatValue(value, scale)
}
}