numbers parsing improvements
This commit is contained in:
parent
ebaeeaa1e5
commit
6c329f76b6
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue