From 6c329f76b6537348e6c4019c92dc11f301dd1535 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 7 Apr 2019 14:26:20 -0400 Subject: [PATCH] numbers parsing improvements --- component/barchart/barchart.go | 4 ++-- component/gauge/gauge.go | 3 ++- component/runchart/runchart.go | 4 +--- component/sparkline/sparkline.go | 3 +-- component/util/parse.go | 18 ++++++++++++++++++ component/util/parse_test.go | 31 +++++++++++++++++++++++++++++++ config.yml | 2 +- data/item.go | 2 +- 8 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 component/util/parse.go create mode 100644 component/util/parse_test.go diff --git a/component/barchart/barchart.go b/component/barchart/barchart.go index 02f5620..5916b7e 100644 --- a/component/barchart/barchart.go +++ b/component/barchart/barchart.go @@ -5,6 +5,7 @@ import ( ui "github.com/gizak/termui/v3" rw "github.com/mattn/go-runewidth" "github.com/sqshq/sampler/component" + "github.com/sqshq/sampler/component/util" "github.com/sqshq/sampler/config" "github.com/sqshq/sampler/console" "github.com/sqshq/sampler/data" @@ -68,8 +69,7 @@ func (b *BarChart) consumeSample(sample *data.Sample) { b.count++ - float, err := strconv.ParseFloat(sample.Value, 64) - + float, err := util.ParseFloat(sample.Value) if err != nil { b.AlertChannel <- &data.Alert{ Title: "FAILED TO PARSE A NUMBER", diff --git a/component/gauge/gauge.go b/component/gauge/gauge.go index 91e881e..110fc30 100644 --- a/component/gauge/gauge.go +++ b/component/gauge/gauge.go @@ -4,6 +4,7 @@ import ( "fmt" ui "github.com/gizak/termui/v3" "github.com/sqshq/sampler/component" + "github.com/sqshq/sampler/component/util" "github.com/sqshq/sampler/config" "github.com/sqshq/sampler/console" "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) { - float, err := strconv.ParseFloat(sample.Value, 64) + float, err := util.ParseFloat(sample.Value) if err != nil { g.AlertChannel <- &data.Alert{ Title: "FAILED TO PARSE A NUMBER", diff --git a/component/runchart/runchart.go b/component/runchart/runchart.go index 95cbb51..03bc747 100644 --- a/component/runchart/runchart.go +++ b/component/runchart/runchart.go @@ -8,7 +8,6 @@ import ( "github.com/sqshq/sampler/data" "image" "math" - "strconv" "sync" "time" @@ -157,8 +156,7 @@ func (c *RunChart) AddLine(Label string, color ui.Color) { func (c *RunChart) consumeSample(sample *data.Sample) { - float, err := strconv.ParseFloat(sample.Value, 64) - + float, err := util.ParseFloat(sample.Value) if err != nil { c.AlertChannel <- &data.Alert{ Title: "FAILED TO PARSE A NUMBER", diff --git a/component/sparkline/sparkline.go b/component/sparkline/sparkline.go index 0c02402..ed26966 100644 --- a/component/sparkline/sparkline.go +++ b/component/sparkline/sparkline.go @@ -8,7 +8,6 @@ import ( "github.com/sqshq/sampler/console" "github.com/sqshq/sampler/data" "image" - "strconv" ) type SparkLine struct { @@ -49,8 +48,8 @@ func NewSparkLine(c config.SparkLineConfig, palette console.Palette) *SparkLine } func (s *SparkLine) consumeSample(sample *data.Sample) { - float, err := strconv.ParseFloat(sample.Value, 64) + float, err := util.ParseFloat(sample.Value) if err != nil { s.AlertChannel <- &data.Alert{ Title: "FAILED TO PARSE A NUMBER", diff --git a/component/util/parse.go b/component/util/parse.go new file mode 100644 index 0000000..a3212d3 --- /dev/null +++ b/component/util/parse.go @@ -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) +} diff --git a/component/util/parse_test.go b/component/util/parse_test.go new file mode 100644 index 0000000..b8b6028 --- /dev/null +++ b/component/util/parse_test.go @@ -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) + } + }) + } +} diff --git a/config.yml b/config.yml index 0496030..b48812c 100644 --- a/config.yml +++ b/config.yml @@ -32,8 +32,8 @@ runcharts: sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" barcharts: - title: EVENTS BY STATUS - rate-ms: 1000 position: [[0, 17], [27, 12]] + rate-ms: 300 scale: 0 items: - label: NEW diff --git a/data/item.go b/data/item.go index fee04ac..d588200 100644 --- a/data/item.go +++ b/data/item.go @@ -173,7 +173,7 @@ func (i *Item) transformInteractiveShellCmd(sample string) (string, error) { result = result // TODO } - return strings.TrimSpace(result), nil + return result, nil } func enrichEnvVariables(cmd *exec.Cmd, variables []string) {