InfluxDB: SQL Support (#72167)
* 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 * go mod updates * make lint-go * more make lint-go * remove unused runQuery * switch statements with default case * linting again
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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:"query"`
|
||||
IntervalMilliseconds int `json:"intervalMs"`
|
||||
MaxDataPoints int64 `json:"maxDataPoints"`
|
||||
Format string `json:"resultFormat"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user