numbers parsing improvements

This commit is contained in:
sqshq 2019-04-07 14:26:20 -04:00
parent ebaeeaa1e5
commit 6c329f76b6
8 changed files with 57 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import (
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
rw "github.com/mattn/go-runewidth" rw "github.com/mattn/go-runewidth"
"github.com/sqshq/sampler/component" "github.com/sqshq/sampler/component"
"github.com/sqshq/sampler/component/util"
"github.com/sqshq/sampler/config" "github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console" "github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data" "github.com/sqshq/sampler/data"
@ -68,8 +69,7 @@ func (b *BarChart) consumeSample(sample *data.Sample) {
b.count++ b.count++
float, err := strconv.ParseFloat(sample.Value, 64) float, err := util.ParseFloat(sample.Value)
if err != nil { if err != nil {
b.AlertChannel <- &data.Alert{ b.AlertChannel <- &data.Alert{
Title: "FAILED TO PARSE A NUMBER", Title: "FAILED TO PARSE A NUMBER",

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/component" "github.com/sqshq/sampler/component"
"github.com/sqshq/sampler/component/util"
"github.com/sqshq/sampler/config" "github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console" "github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data" "github.com/sqshq/sampler/data"
@ -56,7 +57,7 @@ func NewGauge(c config.GaugeConfig, palette console.Palette) *Gauge {
func (g *Gauge) ConsumeSample(sample *data.Sample) { func (g *Gauge) ConsumeSample(sample *data.Sample) {
float, err := strconv.ParseFloat(sample.Value, 64) float, err := util.ParseFloat(sample.Value)
if err != nil { if err != nil {
g.AlertChannel <- &data.Alert{ g.AlertChannel <- &data.Alert{
Title: "FAILED TO PARSE A NUMBER", Title: "FAILED TO PARSE A NUMBER",

View File

@ -8,7 +8,6 @@ import (
"github.com/sqshq/sampler/data" "github.com/sqshq/sampler/data"
"image" "image"
"math" "math"
"strconv"
"sync" "sync"
"time" "time"
@ -157,8 +156,7 @@ func (c *RunChart) AddLine(Label string, color ui.Color) {
func (c *RunChart) consumeSample(sample *data.Sample) { func (c *RunChart) consumeSample(sample *data.Sample) {
float, err := strconv.ParseFloat(sample.Value, 64) float, err := util.ParseFloat(sample.Value)
if err != nil { if err != nil {
c.AlertChannel <- &data.Alert{ c.AlertChannel <- &data.Alert{
Title: "FAILED TO PARSE A NUMBER", Title: "FAILED TO PARSE A NUMBER",

View File

@ -8,7 +8,6 @@ import (
"github.com/sqshq/sampler/console" "github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data" "github.com/sqshq/sampler/data"
"image" "image"
"strconv"
) )
type SparkLine struct { type SparkLine struct {
@ -49,8 +48,8 @@ func NewSparkLine(c config.SparkLineConfig, palette console.Palette) *SparkLine
} }
func (s *SparkLine) consumeSample(sample *data.Sample) { func (s *SparkLine) consumeSample(sample *data.Sample) {
float, err := strconv.ParseFloat(sample.Value, 64)
float, err := util.ParseFloat(sample.Value)
if err != nil { if err != nil {
s.AlertChannel <- &data.Alert{ s.AlertChannel <- &data.Alert{
Title: "FAILED TO PARSE A NUMBER", Title: "FAILED TO PARSE A NUMBER",

18
component/util/parse.go Normal file
View File

@ -0,0 +1,18 @@
package util
import (
"strconv"
"strings"
)
func ParseFloat(input string) (float64, error) {
clean := strings.TrimSpace(input)
if strings.Contains(clean, "\n") {
lastIndex := strings.LastIndex(clean, "\n")
clean = clean[lastIndex+1:]
}
return strconv.ParseFloat(clean, 64)
}

View File

@ -0,0 +1,31 @@
package util
import "testing"
func TestParseFloat(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want float64
wantErr bool
}{
{"should parse a regular number", args{"123"}, 123, false},
{"should parse a regular number with spaces", args{" 123 "}, 123, false},
{"should parse a last line in the given string", args{"123\n456"}, 456, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseFloat(tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseFloat() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseFloat() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -32,8 +32,8 @@ runcharts:
sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
barcharts: barcharts:
- title: EVENTS BY STATUS - title: EVENTS BY STATUS
rate-ms: 1000
position: [[0, 17], [27, 12]] position: [[0, 17], [27, 12]]
rate-ms: 300
scale: 0 scale: 0
items: items:
- label: NEW - label: NEW

View File

@ -173,7 +173,7 @@ func (i *Item) transformInteractiveShellCmd(sample string) (string, error) {
result = result // TODO result = result // TODO
} }
return strings.TrimSpace(result), nil return result, nil
} }
func enrichEnvVariables(cmd *exec.Cmd, variables []string) { func enrichEnvVariables(cmd *exec.Cmd, variables []string) {