2020-06-10 14:26:24 -05:00
|
|
|
package flux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2023-01-30 02:38:51 -06:00
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/influxdb/models"
|
2020-06-10 14:26:24 -05:00
|
|
|
)
|
|
|
|
|
2020-08-19 02:23:11 -05:00
|
|
|
// queryOptions represents datasource configuration options
|
|
|
|
type queryOptions struct {
|
2020-06-10 14:26:24 -05:00
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
DefaultBucket string `json:"defaultBucket"`
|
|
|
|
Organization string `json:"organization"`
|
|
|
|
}
|
|
|
|
|
2020-08-19 02:23:11 -05:00
|
|
|
// queryModel represents a query.
|
|
|
|
type queryModel struct {
|
2020-06-10 14:26:24 -05:00
|
|
|
RawQuery string `json:"query"`
|
2020-08-19 02:23:11 -05:00
|
|
|
Options queryOptions `json:"options"`
|
2020-06-10 14:26:24 -05:00
|
|
|
|
|
|
|
// Not from JSON
|
|
|
|
TimeRange backend.TimeRange `json:"-"`
|
|
|
|
MaxDataPoints int64 `json:"-"`
|
|
|
|
Interval time.Duration `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func getQueryModel(query backend.DataQuery, timeRange backend.TimeRange,
|
|
|
|
dsInfo *models.DatasourceInfo) (*queryModel, error) {
|
2020-08-19 02:23:11 -05:00
|
|
|
model := &queryModel{}
|
2021-07-19 04:32:33 -05:00
|
|
|
if err := json.Unmarshal(query.JSON, model); err != nil {
|
2020-08-13 10:50:53 -05:00
|
|
|
return nil, fmt.Errorf("error reading query: %w", err)
|
2020-06-10 14:26:24 -05:00
|
|
|
}
|
|
|
|
if model.Options.DefaultBucket == "" {
|
2021-07-19 04:32:33 -05:00
|
|
|
model.Options.DefaultBucket = dsInfo.DefaultBucket
|
2020-06-10 14:26:24 -05:00
|
|
|
}
|
|
|
|
if model.Options.Bucket == "" {
|
|
|
|
model.Options.Bucket = model.Options.DefaultBucket
|
|
|
|
}
|
|
|
|
if model.Options.Organization == "" {
|
2021-07-19 04:32:33 -05:00
|
|
|
model.Options.Organization = dsInfo.Organization
|
2020-06-10 14:26:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy directly from the well typed query
|
2021-07-19 04:32:33 -05:00
|
|
|
model.TimeRange = timeRange
|
2020-06-10 14:26:24 -05:00
|
|
|
model.MaxDataPoints = query.MaxDataPoints
|
2020-09-09 02:34:17 -05:00
|
|
|
if model.MaxDataPoints == 0 {
|
|
|
|
model.MaxDataPoints = 10000 // 10k/series should be a reasonable place to abort!
|
|
|
|
}
|
2021-07-19 04:32:33 -05:00
|
|
|
model.Interval = query.Interval
|
2020-09-09 02:34:17 -05:00
|
|
|
if model.Interval.Milliseconds() == 0 {
|
|
|
|
model.Interval = time.Millisecond // 1ms
|
|
|
|
}
|
2020-06-10 14:26:24 -05:00
|
|
|
return model, nil
|
|
|
|
}
|