@@ -39,15 +39,20 @@ func NewDatasource(ctx context.Context, settings backend.DataSourceInstanceSetti
39
39
40
40
return & Datasource {
41
41
engine : engine ,
42
- queries : make ( map [string ]string ) ,
42
+ queries : map [string ]queryReq {} ,
43
43
}, nil
44
44
}
45
45
46
+ type queryReq struct {
47
+ SQL string
48
+ RefID string
49
+ }
50
+
46
51
// Datasource is an example datasource which can respond to data queries, reports
47
52
// its health and has streaming skills.
48
53
type Datasource struct {
49
54
engine timeplus.Engine
50
- queries map [string ]string
55
+ queries map [string ]queryReq
51
56
}
52
57
53
58
func (d * Datasource ) QueryData (ctx context.Context , req * backend.QueryDataRequest ) (* backend.QueryDataResponse , error ) {
@@ -77,7 +82,10 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
77
82
78
83
if isStreaming {
79
84
id := uuid .NewString ()
80
- d .queries [id ] = q .SQL
85
+ d .queries [id ] = queryReq {
86
+ SQL : q .SQL ,
87
+ RefID : query .RefID ,
88
+ }
81
89
channel := live.Channel {
82
90
Scope : live .ScopeDatasource ,
83
91
Namespace : req .PluginContext .DataSourceInstanceSettings .UID ,
@@ -165,12 +173,12 @@ func (d *Datasource) RunStream(ctx context.Context, req *backend.RunStreamReques
165
173
logger := log .DefaultLogger .FromContext (ctx )
166
174
167
175
path := req .Path
168
- sql , ok := d .queries [path ]
176
+ queryReq , ok := d .queries [path ]
169
177
if ! ok {
170
178
return nil
171
179
}
172
180
173
- columnTypes , ch , err := d .engine .RunQuery (ctx , sql )
181
+ columnTypes , ch , err := d .engine .RunQuery (ctx , queryReq . SQL )
174
182
if err != nil {
175
183
return err
176
184
}
@@ -193,6 +201,9 @@ func (d *Datasource) RunStream(ctx context.Context, req *backend.RunStreamReques
193
201
if frame == nil {
194
202
frame = data .NewFrame ("response" )
195
203
204
+ // RefID is needed for some grafana features. (e.g. Transformations -> Config from query results)
205
+ frame .RefID = queryReq .RefID
206
+
196
207
for _ , c := range columnTypes {
197
208
frame .Fields = append (frame .Fields , timeplus .NewDataFieldByType (c .Name (), c .DatabaseTypeName ()))
198
209
}
0 commit comments