InfluxDB: Check the value type before casting it to the string (#80986)

* Check the value type before casting it to the string

* set visualization as table by default

* append all values for show diagnostics

* golangci-lint

* append metadata only to first frame
This commit is contained in:
ismail simsek 2024-01-25 11:28:25 +01:00 committed by GitHub
parent c6793d4f12
commit 14e55fefbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 6 deletions

View File

@ -241,6 +241,12 @@ func transformRowsForTimeSeries(rows []models.Row, query models.Query) data.Fram
if !hasTimeCol {
newFrame := newFrameWithoutTimeField(row, query)
if len(frames) == 0 {
newFrame.Meta = &data.FrameMeta{
ExecutedQueryString: query.RawQuery,
PreferredVisualization: util.GetVisType(query.ResultFormat),
}
}
frames = append(frames, newFrame)
} else {
for colIndex, column := range row.Columns {
@ -326,20 +332,21 @@ func newFrameWithoutTimeField(row models.Row, query models.Query) *data.Frame {
for _, valuePair := range row.Values {
if strings.Contains(strings.ToLower(query.RawQuery), strings.ToLower("SHOW TAG VALUES")) {
if len(valuePair) >= 2 {
values = append(values, util.ToPtr(valuePair[1].(string)))
values = append(values, util.ParseString(valuePair[1]))
}
} else if strings.Contains(strings.ToLower(query.RawQuery), strings.ToLower("SHOW DIAGNOSTICS")) {
// https://docs.influxdata.com/platform/monitoring/influxdata-platform/tools/show-diagnostics/
for _, vp := range valuePair {
values = append(values, util.ParseString(vp))
}
} else {
if len(valuePair) >= 1 {
values = append(values, util.ToPtr(valuePair[0].(string)))
values = append(values, util.ParseString(valuePair[0]))
}
}
}
field := data.NewField("Value", nil, values)
frame := data.NewFrame(row.Name, field)
frame.Meta = &data.FrameMeta{
ExecutedQueryString: query.RawQuery,
PreferredVisualization: util.GetVisType(query.ResultFormat),
}
return frame
}

View File

@ -139,6 +139,15 @@ func ParseNumber(value any) *float64 {
return &fvalue
}
func ParseString(value any) *string {
switch val := value.(type) {
case string:
return ToPtr(val)
default:
return ToPtr(fmt.Sprintf("%v", value))
}
}
func GetVisType(resFormat string) data.VisType {
switch resFormat {
case "table":

View File

@ -0,0 +1,23 @@
package util
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParseString(t *testing.T) {
t.Run("parse bool value to string", func(t *testing.T) {
val := true
expected := ToPtr("true")
result := ParseString(val)
require.Equal(t, expected, result)
})
t.Run("parse number value to string", func(t *testing.T) {
val := 123
expected := ToPtr("123")
result := ParseString(val)
require.Equal(t, expected, result)
})
}