grafana/pkg/tsdb/influxdb/fsql/query_model.go
ismail simsek d333c09418
InfluxDB: SQL Query Editor (#72168)
* Add influxdbSqlSupport feature toggle

* Add SQL option to the config page

* Add SQL backend

* Add metadata support in config page

* Implement unified querying

* Fix healthcheck query

* fsql tests

* secure grpc by default

* code cleanup

* Query handing for sql mode

* Implement a placeholder sql editor

* Fix query language dropdown

* drop in SQL editor

* switch to use rawSql, get sql editor working

* fix healthcheck

* WIP

* memoize component to stop unwanted rerender onQuery

* dont reinit datasource on each render of the editor

* remove useless memo

* clean up

* Fix the link

* Alpha state warning

* Remove console.logs

* update model for fsql

* remove unused

---------

Co-authored-by: Galen <galen.kistler@grafana.com>
2023-08-02 19:04:16 +02:00

60 lines
1.5 KiB
Go

package fsql
import (
"encoding/json"
"fmt"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
)
type queryModel struct {
*sqlutil.Query
}
// queryRequest is an inbound query request as part of a batch of queries sent
// to [(*FlightSQLDatasource).QueryData].
type queryRequest struct {
RefID string `json:"refId"`
RawQuery string `json:"rawSql"`
IntervalMilliseconds int `json:"intervalMs"`
MaxDataPoints int64 `json:"maxDataPoints"`
Format string `json:"format"`
}
func getQueryModel(dataQuery backend.DataQuery) (*queryModel, error) {
var q queryRequest
if err := json.Unmarshal(dataQuery.JSON, &q); err != nil {
return nil, fmt.Errorf("unmarshal json: %w", err)
}
var format sqlutil.FormatQueryOption
switch q.Format {
case "time_series":
format = sqlutil.FormatOptionTimeSeries
case "table":
format = sqlutil.FormatOptionTable
default:
format = sqlutil.FormatOptionTimeSeries
}
query := &sqlutil.Query{
RawSQL: q.RawQuery,
RefID: q.RefID,
MaxDataPoints: q.MaxDataPoints,
Interval: time.Duration(q.IntervalMilliseconds) * time.Millisecond,
TimeRange: dataQuery.TimeRange,
Format: format,
}
// Process macros and execute the query.
sql, err := sqlutil.Interpolate(query, macros)
if err != nil {
return nil, fmt.Errorf("macro interpolation: %w", err)
}
query.RawSQL = sql
return &queryModel{query}, nil
}