2016-06-06 03:31:21 -05:00
|
|
|
package tsdb
|
|
|
|
|
2016-09-28 02:15:48 -05:00
|
|
|
import (
|
2020-06-09 06:13:06 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
2017-01-13 05:32:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2016-09-28 02:15:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-12-07 04:10:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-28 02:15:48 -05:00
|
|
|
)
|
2016-09-21 04:17:29 -05:00
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// TsdbQuery contains all information about a query request.
|
2017-09-20 11:31:34 -05:00
|
|
|
type TsdbQuery struct {
|
|
|
|
TimeRange *TimeRange
|
2017-09-20 11:56:33 -05:00
|
|
|
Queries []*Query
|
2020-04-25 15:48:20 -05:00
|
|
|
Headers map[string]string
|
2019-06-25 01:52:17 -05:00
|
|
|
Debug bool
|
2020-03-06 07:37:36 -06:00
|
|
|
User *models.SignedInUser
|
2017-09-20 11:31:34 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 00:01:53 -05:00
|
|
|
type Query struct {
|
2016-09-27 07:39:51 -05:00
|
|
|
RefId string
|
|
|
|
Model *simplejson.Json
|
2016-12-07 04:10:42 -06:00
|
|
|
DataSource *models.DataSource
|
2016-09-27 07:39:51 -05:00
|
|
|
MaxDataPoints int64
|
|
|
|
IntervalMs int64
|
2020-05-05 03:32:34 -05:00
|
|
|
QueryType string
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2017-09-21 11:04:06 -05:00
|
|
|
Results map[string]*QueryResult `json:"results"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 03:31:21 -05:00
|
|
|
type QueryResult struct {
|
2017-04-19 10:26:29 -05:00
|
|
|
Error error `json:"-"`
|
|
|
|
ErrorString string `json:"error,omitempty"`
|
|
|
|
RefId string `json:"refId"`
|
|
|
|
Meta *simplejson.Json `json:"meta,omitempty"`
|
|
|
|
Series TimeSeriesSlice `json:"series"`
|
2017-04-21 08:07:43 -05:00
|
|
|
Tables []*Table `json:"tables"`
|
2020-06-09 06:13:06 -05:00
|
|
|
Dataframes DataFrames `json:"dataframes"`
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type TimeSeries struct {
|
2017-02-06 07:59:29 -06:00
|
|
|
Name string `json:"name"`
|
|
|
|
Points TimeSeriesPoints `json:"points"`
|
2017-04-21 08:07:43 -05:00
|
|
|
Tags map[string]string `json:"tags,omitempty"`
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2017-04-21 08:07:43 -05:00
|
|
|
type Table struct {
|
|
|
|
Columns []TableColumn `json:"columns"`
|
|
|
|
Rows []RowValues `json:"rows"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TableColumn struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RowValues []interface{}
|
2016-09-28 02:15:48 -05:00
|
|
|
type TimePoint [2]null.Float
|
|
|
|
type TimeSeriesPoints []TimePoint
|
2016-06-06 03:31:21 -05:00
|
|
|
type TimeSeriesSlice []*TimeSeries
|
|
|
|
|
2016-09-28 03:37:30 -05:00
|
|
|
func NewQueryResult() *QueryResult {
|
|
|
|
return &QueryResult{
|
|
|
|
Series: make(TimeSeriesSlice, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 05:51:45 -05:00
|
|
|
func NewTimePoint(value null.Float, timestamp float64) TimePoint {
|
|
|
|
return TimePoint{value, null.FloatFrom(timestamp)}
|
2016-09-28 02:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
|
|
|
|
points := make(TimeSeriesPoints, 0)
|
|
|
|
|
|
|
|
for i := 0; i < len(values); i += 2 {
|
2016-10-06 05:51:45 -05:00
|
|
|
points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
|
2016-09-28 02:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
|
2016-06-06 03:31:21 -05:00
|
|
|
return &TimeSeries{
|
|
|
|
Name: name,
|
|
|
|
Points: points,
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 06:13:06 -05:00
|
|
|
|
|
|
|
// DataFrames interface for retrieving encoded and decoded data frames.
|
|
|
|
//
|
|
|
|
// See NewDecodedDataFrames and NewEncodedDataFrames for more information.
|
|
|
|
type DataFrames interface {
|
|
|
|
// Encoded encodes Frames into a slice of []byte.
|
|
|
|
// If an error occurs [][]byte will be nil.
|
|
|
|
// The encoded result, if any, will be cached and returned next time Encoded is called.
|
|
|
|
Encoded() ([][]byte, error)
|
|
|
|
|
|
|
|
// Decoded decodes a slice of Arrow encoded frames to data.Frames ([]*data.Frame).
|
|
|
|
// If an error occurs Frames will be nil.
|
|
|
|
// The decoded result, if any, will be cached and returned next time Decoded is called.
|
|
|
|
Decoded() (data.Frames, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dataFrames struct {
|
|
|
|
decoded data.Frames
|
|
|
|
encoded [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDecodedDataFrames create new DataFrames from decoded frames.
|
|
|
|
//
|
|
|
|
// This should be the primary function for creating DataFrames if your implementing a plugin.
|
|
|
|
// In Grafana alerting scenario it needs to operate on decoded frames why this function is
|
|
|
|
// preferrable. When encoded data frames is needed, e.g. returned from Grafana HTTP API, it will
|
|
|
|
// happen automatically when MarshalJSON() is called.
|
|
|
|
func NewDecodedDataFrames(decodedFrames data.Frames) DataFrames {
|
|
|
|
return &dataFrames{
|
|
|
|
decoded: decodedFrames,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncodedDataFrames create new DataFrames from encoded frames.
|
|
|
|
//
|
|
|
|
// This one is primarily used for creating DataFrames when receiving encoded data frames from an external
|
|
|
|
// plugin or similar. This may allow the encoded data frames to be returned to Grafana UI without any additional
|
|
|
|
// decoding/encoding required. In Grafana alerting scenario it needs to operate on decoded data frames why encoded
|
|
|
|
// frames needs to be decoded before usage.
|
|
|
|
func NewEncodedDataFrames(encodedFrames [][]byte) DataFrames {
|
|
|
|
return &dataFrames{
|
|
|
|
encoded: encodedFrames,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *dataFrames) Encoded() ([][]byte, error) {
|
|
|
|
if df.encoded == nil {
|
|
|
|
encoded, err := df.decoded.MarshalArrow()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
df.encoded = encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
return df.encoded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *dataFrames) Decoded() (data.Frames, error) {
|
|
|
|
if df.decoded == nil {
|
|
|
|
decoded, err := data.UnmarshalArrowFrames(df.encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
df.decoded = decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
return df.decoded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *dataFrames) MarshalJSON() ([]byte, error) {
|
|
|
|
encoded, err := df.Encoded()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(encoded)
|
|
|
|
}
|