Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set ref id for data frame #21

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ func NewDatasource(ctx context.Context, settings backend.DataSourceInstanceSetti

return &Datasource{
engine: engine,
queries: make(map[string]string),
queries: map[string]queryReq{},
}, nil
}

type queryReq struct {
SQL string
RefID string
}

// Datasource is an example datasource which can respond to data queries, reports
// its health and has streaming skills.
type Datasource struct {
engine timeplus.Engine
queries map[string]string
queries map[string]queryReq
}

func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
Expand Down Expand Up @@ -77,7 +82,10 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques

if isStreaming {
id := uuid.NewString()
d.queries[id] = q.SQL
d.queries[id] = queryReq{
SQL: q.SQL,
RefID: query.RefID,
}
channel := live.Channel{
Scope: live.ScopeDatasource,
Namespace: req.PluginContext.DataSourceInstanceSettings.UID,
Expand Down Expand Up @@ -165,12 +173,12 @@ func (d *Datasource) RunStream(ctx context.Context, req *backend.RunStreamReques
logger := log.DefaultLogger.FromContext(ctx)

path := req.Path
sql, ok := d.queries[path]
queryReq, ok := d.queries[path]
if !ok {
return nil
}

columnTypes, ch, err := d.engine.RunQuery(ctx, sql)
columnTypes, ch, err := d.engine.RunQuery(ctx, queryReq.SQL)
if err != nil {
return err
}
Expand All @@ -193,6 +201,9 @@ func (d *Datasource) RunStream(ctx context.Context, req *backend.RunStreamReques
if frame == nil {
frame = data.NewFrame("response")

// RefID is needed for some grafana features. (e.g. Transformations -> Config from query results)
frame.RefID = queryReq.RefID

for _, c := range columnTypes {
frame.Fields = append(frame.Fields, timeplus.NewDataFieldByType(c.Name(), c.DatabaseTypeName()))
}
Expand Down
Loading