Skip to content

Commit 73bc46e

Browse files
Merge pull request #30 from NeedleInAJayStack/feature/eval-colDis
Sets field display name to id dis if present
2 parents 3fc80fc + 0d460d7 commit 73bc46e

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

pkg/plugin/datasource.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func dataFrameFromGrid(grid haystack.Grid) (*data.Frame, error) {
477477

478478
// Set Grafana field info from Haystack grid info
479479
config := &data.FieldConfig{}
480-
config.DisplayName = col.Name()
480+
config.DisplayName = disFromGrid(grid, col)
481481
config.Unit = unitFromGrid(grid, col)
482482
field.Config = config
483483
fields = append(fields, field)
@@ -494,6 +494,21 @@ func dataFrameFromGrid(grid haystack.Grid) (*data.Frame, error) {
494494
return frame, nil
495495
}
496496

497+
// disFromGrid returns the display name of a column in a haystack grid
498+
func disFromGrid(grid haystack.Grid, col haystack.Col) string {
499+
// If column has 'id' meta, use the Ref name
500+
id := col.Meta().Get("id")
501+
if id != nil {
502+
switch id := id.(type) {
503+
case haystack.Ref:
504+
return id.Dis()
505+
default:
506+
return col.Name()
507+
}
508+
}
509+
return col.Name()
510+
}
511+
497512
// unitFromGrid returns the unit of a column in a haystack grid
498513
// It is expected that the column is from the grid
499514
// The unit is determined in the following order:

pkg/plugin/datasource_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ func TestQueryData_Eval(t *testing.T) {
4343
}
4444
}
4545

46+
func TestQueryData_Eval_Dis(t *testing.T) {
47+
response := haystack.NewGridBuilder()
48+
response.AddCol("a", map[string]haystack.Val{"id": haystack.NewRef("abc", "dis")})
49+
response.AddRow([]haystack.Val{haystack.NewStr("a")})
50+
51+
client := &testHaystackClient{
52+
evalResponse: response.ToGrid(),
53+
}
54+
55+
actual := getResponse(
56+
client,
57+
&QueryModel{
58+
Type: "eval",
59+
Eval: "test()",
60+
},
61+
t,
62+
)
63+
64+
aVal := "a"
65+
expected := data.NewFrame("",
66+
data.NewField("a", nil, []*string{&aVal}).SetConfig(&data.FieldConfig{DisplayName: "dis"}),
67+
)
68+
69+
if !cmp.Equal(actual, expected, data.FrameTestCompareOptions()...) {
70+
t.Error(cmp.Diff(actual, expected, data.FrameTestCompareOptions()...))
71+
}
72+
}
73+
4674
func TestQueryData_HisRead(t *testing.T) {
4775
response := haystack.NewGridBuilder()
4876
response.AddCol("ts", map[string]haystack.Val{})

0 commit comments

Comments
 (0)