Skip to content

Commit

Permalink
add labels
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlepp committed Feb 23, 2024
1 parent 9287e1c commit 154b4b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions duck/duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,28 @@ func resultsToFrame(name string, res string, f *sdk.Frame, frames []*sdk.Frame)
f.Meta = resultsFrame.Meta
f.RefID = resultsFrame.RefID

applyLabels(*resultsFrame, frames)

return resultsFrame, nil
}

func applyLabels(resultsFrame sdk.Frame, sourceFrames []*sdk.Frame) {
for _, fld := range resultsFrame.Fields {
for _, f := range sourceFrames {
srcField := find(f, fld)
if srcField != nil {
fld.Labels = srcField.Labels
break
}
}
}
}

func find(f *sdk.Frame, fld *sdk.Field) *sdk.Field {
for _, sfld := range f.Fields {
if sfld.Name == fld.Name {
return sfld
}
}
return nil
}
36 changes: 36 additions & 0 deletions duck/duckdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,39 @@ func TestTimestamps(t *testing.T) {
fmt.Printf("GOT: %s", txt)
assert.Contains(t, txt, "Type: []*time.Time")
}

func TestLabels(t *testing.T) {
db := NewInMemoryDB()

f := new(float64)
*f = 12345

var values = []*float64{f}
labels := map[string]string{
"server": "A",
}
frame := data.NewFrame("foo", data.NewField("value1", labels, values))
frame.RefID = "foo"

var values2 = []*float64{f}
labels2 := map[string]string{
"server": "B",
}
frame2 := data.NewFrame("foo", data.NewField("value2", labels2, values2))
frame2.RefID = "foo"

frames := []*data.Frame{frame, frame2}

model := &data.Frame{}
_, err := db.QueryFramesInto("foo", "select * from foo", frames, model)
assert.Nil(t, err)

assert.Equal(t, 2, model.Rows())
txt, err := model.StringTable(-1, -1)
assert.Nil(t, err)

fmt.Printf("GOT: %s", txt)

assert.Contains(t, txt, "server=A")
assert.Contains(t, txt, "server=B")
}

0 comments on commit 154b4b4

Please sign in to comment.