mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 10:20:29 -06:00
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:
parent
c6793d4f12
commit
14e55fefbb
@ -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
|
||||
}
|
||||
|
@ -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":
|
||||
|
23
pkg/tsdb/influxdb/influxql/util/util_test.go
Normal file
23
pkg/tsdb/influxdb/influxql/util/util_test.go
Normal 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)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user