mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SQL data sources: Convert to return data frames (#32257)
Convert SQL data sources to return data frames. Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Will Browne <will.browne@grafana.com> Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>
This commit is contained in:
parent
06c24476dc
commit
bd66c8dde3
@ -510,6 +510,14 @@ func IsTestDbPostgres() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsTestDBMSSQL() bool {
|
||||||
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
||||||
|
return db == migrator.MSSQL
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type DatabaseConfig struct {
|
type DatabaseConfig struct {
|
||||||
Type string
|
Type string
|
||||||
Host string
|
Host string
|
||||||
|
@ -69,7 +69,7 @@ func (ic *intervalCalculator) Calculate(timerange plugins.DataTimeRange, minInte
|
|||||||
func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error) {
|
func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error) {
|
||||||
interval := queryModel.Get("interval").MustString("")
|
interval := queryModel.Get("interval").MustString("")
|
||||||
|
|
||||||
if interval == "" && dsInfo.JsonData != nil {
|
if interval == "" && dsInfo != nil && dsInfo.JsonData != nil {
|
||||||
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
|
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
|
||||||
if dsInterval != "" {
|
if dsInterval != "" {
|
||||||
interval = dsInterval
|
interval = dsInterval
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package mssql
|
package mssql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
|
|
||||||
@ -16,7 +18,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
||||||
"xorm.io/core"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = log.New("tsdb.mssql")
|
var logger = log.New("tsdb.mssql")
|
||||||
@ -115,49 +116,6 @@ type mssqlQueryResultTransformer struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *mssqlQueryResultTransformer) TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) (
|
|
||||||
plugins.DataRowValues, error) {
|
|
||||||
values := make([]interface{}, len(columnTypes))
|
|
||||||
valuePtrs := make([]interface{}, len(columnTypes))
|
|
||||||
|
|
||||||
for i := range columnTypes {
|
|
||||||
// debug output on large tables causes high memory utilization/leak
|
|
||||||
// t.log.Debug("type", "type", stype)
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert types not handled by denisenkom/go-mssqldb
|
|
||||||
// unhandled types are returned as []byte
|
|
||||||
for i := 0; i < len(columnTypes); i++ {
|
|
||||||
if value, ok := values[i].([]byte); ok {
|
|
||||||
switch columnTypes[i].DatabaseTypeName() {
|
|
||||||
case "MONEY", "SMALLMONEY", "DECIMAL":
|
|
||||||
if v, err := strconv.ParseFloat(string(value), 64); err == nil {
|
|
||||||
values[i] = v
|
|
||||||
} else {
|
|
||||||
t.log.Debug("Rows", "Error converting numeric to float", value)
|
|
||||||
}
|
|
||||||
case "UNIQUEIDENTIFIER":
|
|
||||||
uuid := &mssql.UniqueIdentifier{}
|
|
||||||
if err := uuid.Scan(value); err == nil {
|
|
||||||
values[i] = uuid.String()
|
|
||||||
} else {
|
|
||||||
t.log.Debug("Rows", "Error converting uniqueidentifier to string", value)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.log.Debug("Rows", "Unknown database type", columnTypes[i].DatabaseTypeName(), "value", value)
|
|
||||||
values[i] = string(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return values, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *mssqlQueryResultTransformer) TransformQueryError(err error) error {
|
func (t *mssqlQueryResultTransformer) TransformQueryError(err error) error {
|
||||||
// go-mssql overrides source error, so we currently match on string
|
// go-mssql overrides source error, so we currently match on string
|
||||||
// ref https://github.com/denisenkom/go-mssqldb/blob/045585d74f9069afe2e115b6235eb043c8047043/tds.go#L904
|
// ref https://github.com/denisenkom/go-mssqldb/blob/045585d74f9069afe2e115b6235eb043c8047043/tds.go#L904
|
||||||
@ -168,3 +126,85 @@ func (t *mssqlQueryResultTransformer) TransformQueryError(err error) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *mssqlQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
||||||
|
return []sqlutil.StringConverter{
|
||||||
|
{
|
||||||
|
Name: "handle MONEY",
|
||||||
|
InputScanKind: reflect.Slice,
|
||||||
|
InputTypeName: "MONEY",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle SMALLMONEY",
|
||||||
|
InputScanKind: reflect.Slice,
|
||||||
|
InputTypeName: "SMALLMONEY",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle DECIMAL",
|
||||||
|
InputScanKind: reflect.Slice,
|
||||||
|
InputTypeName: "DECIMAL",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle UNIQUEIDENTIFIER",
|
||||||
|
InputScanKind: reflect.Slice,
|
||||||
|
InputTypeName: "UNIQUEIDENTIFIER",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableString,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
uuid := &mssql.UniqueIdentifier{}
|
||||||
|
if err := uuid.Scan([]byte(*in)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
v := uuid.String()
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,17 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/VividCortex/mysqlerr"
|
"github.com/VividCortex/mysqlerr"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
"github.com/go-sql-driver/mysql"
|
||||||
@ -17,7 +19,12 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
||||||
"xorm.io/core"
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
dateFormat = "2006-01-02"
|
||||||
|
dateTimeFormat1 = "2006-01-02 15:04:05"
|
||||||
|
dateTimeFormat2 = "2006-01-02T15:04:05Z"
|
||||||
)
|
)
|
||||||
|
|
||||||
func characterEscape(s string, escapeChar string) string {
|
func characterEscape(s string, escapeChar string) string {
|
||||||
@ -77,66 +84,6 @@ type mysqlQueryResultTransformer struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *mysqlQueryResultTransformer) TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) (
|
|
||||||
plugins.DataRowValues, error) {
|
|
||||||
values := make([]interface{}, len(columnTypes))
|
|
||||||
|
|
||||||
for i := range values {
|
|
||||||
scanType := columnTypes[i].ScanType()
|
|
||||||
values[i] = reflect.New(scanType).Interface()
|
|
||||||
|
|
||||||
if columnTypes[i].DatabaseTypeName() == "BIT" {
|
|
||||||
values[i] = new([]byte)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(values...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(columnTypes); i++ {
|
|
||||||
typeName := reflect.ValueOf(values[i]).Type().String()
|
|
||||||
|
|
||||||
switch typeName {
|
|
||||||
case "*sql.RawBytes":
|
|
||||||
values[i] = string(*values[i].(*sql.RawBytes))
|
|
||||||
case "*mysql.NullTime":
|
|
||||||
sqlTime := (*values[i].(*mysql.NullTime))
|
|
||||||
if sqlTime.Valid {
|
|
||||||
values[i] = sqlTime.Time
|
|
||||||
} else {
|
|
||||||
values[i] = nil
|
|
||||||
}
|
|
||||||
case "*sql.NullInt64":
|
|
||||||
nullInt64 := (*values[i].(*sql.NullInt64))
|
|
||||||
if nullInt64.Valid {
|
|
||||||
values[i] = nullInt64.Int64
|
|
||||||
} else {
|
|
||||||
values[i] = nil
|
|
||||||
}
|
|
||||||
case "*sql.NullFloat64":
|
|
||||||
nullFloat64 := (*values[i].(*sql.NullFloat64))
|
|
||||||
if nullFloat64.Valid {
|
|
||||||
values[i] = nullFloat64.Float64
|
|
||||||
} else {
|
|
||||||
values[i] = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if columnTypes[i].DatabaseTypeName() == "DECIMAL" {
|
|
||||||
f, err := strconv.ParseFloat(values[i].(string), 64)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
values[i] = f
|
|
||||||
} else {
|
|
||||||
values[i] = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return values, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *mysqlQueryResultTransformer) TransformQueryError(err error) error {
|
func (t *mysqlQueryResultTransformer) TransformQueryError(err error) error {
|
||||||
var driverErr *mysql.MySQLError
|
var driverErr *mysql.MySQLError
|
||||||
if errors.As(err, &driverErr) {
|
if errors.As(err, &driverErr) {
|
||||||
@ -151,3 +98,199 @@ func (t *mysqlQueryResultTransformer) TransformQueryError(err error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var errQueryFailed = errors.New("query failed - please inspect Grafana server log for details")
|
var errQueryFailed = errors.New("query failed - please inspect Grafana server log for details")
|
||||||
|
|
||||||
|
func (t *mysqlQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
||||||
|
// For the MySQL driver , we have these possible data types:
|
||||||
|
// https://www.w3schools.com/sql/sql_datatypes.asp#:~:text=In%20MySQL%20there%20are%20three,numeric%2C%20and%20date%20and%20time.
|
||||||
|
// Since by default, we convert all into String, we need only to handle the Numeric data types
|
||||||
|
return []sqlutil.StringConverter{
|
||||||
|
{
|
||||||
|
Name: "handle DOUBLE",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "DOUBLE",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle BIGINT",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "BIGINT",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableInt64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseInt(*in, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle DECIMAL",
|
||||||
|
InputScanKind: reflect.Slice,
|
||||||
|
InputTypeName: "DECIMAL",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle DATETIME",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "DATETIME",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableTime,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := time.Parse(dateTimeFormat1, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
v, err = time.Parse(dateTimeFormat2, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle DATE",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "DATE",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableTime,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := time.Parse(dateFormat, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
v, err = time.Parse(dateTimeFormat1, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
v, err = time.Parse(dateTimeFormat2, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle TIMESTAMP",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "TIMESTAMP",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableTime,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := time.Parse(dateTimeFormat1, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
v, err = time.Parse(dateTimeFormat2, *in)
|
||||||
|
if err == nil {
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle YEAR",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "YEAR",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableInt64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseInt(*in, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle INT",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "INT",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableInt64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseInt(*in, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle FLOAT",
|
||||||
|
InputScanKind: reflect.Struct,
|
||||||
|
InputTypeName: "FLOAT",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,13 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/util/errutil"
|
"github.com/grafana/grafana/pkg/util/errutil"
|
||||||
@ -14,7 +16,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
||||||
"xorm.io/core"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -114,7 +115,6 @@ func (s *PostgresService) generateConnectionString(datasource *models.DataSource
|
|||||||
|
|
||||||
connStr += fmt.Sprintf(" sslmode='%s'", escape(tlsSettings.Mode))
|
connStr += fmt.Sprintf(" sslmode='%s'", escape(tlsSettings.Mode))
|
||||||
|
|
||||||
// Attach root certificate if provided
|
|
||||||
// Attach root certificate if provided
|
// Attach root certificate if provided
|
||||||
if tlsSettings.RootCertFile != "" {
|
if tlsSettings.RootCertFile != "" {
|
||||||
s.logger.Debug("Setting server root certificate", "tlsRootCert", tlsSettings.RootCertFile)
|
s.logger.Debug("Setting server root certificate", "tlsRootCert", tlsSettings.RootCertFile)
|
||||||
@ -137,43 +137,68 @@ type postgresQueryResultTransformer struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *postgresQueryResultTransformer) TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) (
|
|
||||||
plugins.DataRowValues, error) {
|
|
||||||
values := make([]interface{}, len(columnTypes))
|
|
||||||
valuePtrs := make([]interface{}, len(columnTypes))
|
|
||||||
|
|
||||||
for i := 0; i < len(columnTypes); i++ {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert types not handled by lib/pq
|
|
||||||
// unhandled types are returned as []byte
|
|
||||||
for i := 0; i < len(columnTypes); i++ {
|
|
||||||
if value, ok := values[i].([]byte); ok {
|
|
||||||
switch columnTypes[i].DatabaseTypeName() {
|
|
||||||
case "NUMERIC":
|
|
||||||
if v, err := strconv.ParseFloat(string(value), 64); err == nil {
|
|
||||||
values[i] = v
|
|
||||||
} else {
|
|
||||||
t.log.Debug("Rows", "Error converting numeric to float", value)
|
|
||||||
}
|
|
||||||
case "UNKNOWN", "CIDR", "INET", "MACADDR":
|
|
||||||
// char literals have type UNKNOWN
|
|
||||||
values[i] = string(value)
|
|
||||||
default:
|
|
||||||
t.log.Debug("Rows", "Unknown database type", columnTypes[i].DatabaseTypeName(), "value", value)
|
|
||||||
values[i] = string(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return values, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *postgresQueryResultTransformer) TransformQueryError(err error) error {
|
func (t *postgresQueryResultTransformer) TransformQueryError(err error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *postgresQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
||||||
|
return []sqlutil.StringConverter{
|
||||||
|
{
|
||||||
|
Name: "handle FLOAT4",
|
||||||
|
InputScanKind: reflect.Interface,
|
||||||
|
InputTypeName: "FLOAT4",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle FLOAT8",
|
||||||
|
InputScanKind: reflect.Interface,
|
||||||
|
InputTypeName: "FLOAT8",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "handle NUMERIC",
|
||||||
|
InputScanKind: reflect.Interface,
|
||||||
|
InputTypeName: "NUMERIC",
|
||||||
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
||||||
|
Replacer: &sqlutil.StringFieldReplacer{
|
||||||
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
||||||
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
||||||
|
if in == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(*in, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
"github.com/grafana/grafana/pkg/components/securejsondata"
|
"github.com/grafana/grafana/pkg/components/securejsondata"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
@ -151,10 +152,9 @@ func TestGenerateConnectionString(t *testing.T) {
|
|||||||
// devenv/README.md for setup instructions.
|
// devenv/README.md for setup instructions.
|
||||||
func TestPostgres(t *testing.T) {
|
func TestPostgres(t *testing.T) {
|
||||||
// change to true to run the PostgreSQL tests
|
// change to true to run the PostgreSQL tests
|
||||||
runPostgresTests := false
|
const runPostgresTests = false
|
||||||
// runPostgresTests := true
|
|
||||||
|
|
||||||
if !sqlstore.IsTestDbPostgres() && !runPostgresTests {
|
if !(sqlstore.IsTestDbPostgres() || runPostgresTests) {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,7 +213,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
c12_date date,
|
c12_date date,
|
||||||
c13_time time without time zone,
|
c13_time time without time zone,
|
||||||
c14_timetz time with time zone,
|
c14_timetz time with time zone,
|
||||||
|
time date,
|
||||||
c15_interval interval
|
c15_interval interval
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
@ -226,7 +226,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
4.5,6.7,1.1,1.2,
|
4.5,6.7,1.1,1.2,
|
||||||
'char10','varchar10','text',
|
'char10','varchar10','text',
|
||||||
|
|
||||||
now(),now(),now(),now(),now(),'15m'::interval
|
now(),now(),now(),now(),now(),now(),'15m'::interval
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
@ -250,32 +250,36 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
column := queryResult.Tables[0].Rows[0]
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, int64(1), column[0].(int64))
|
require.Len(t, frames, 1)
|
||||||
require.Equal(t, int64(2), column[1].(int64))
|
require.Len(t, frames[0].Fields, 17)
|
||||||
require.Equal(t, int64(3), column[2].(int64))
|
|
||||||
|
|
||||||
require.Equal(t, float64(4.5), column[3].(float64))
|
require.Equal(t, int16(1), *frames[0].Fields[0].At(0).(*int16))
|
||||||
require.Equal(t, float64(6.7), column[4].(float64))
|
require.Equal(t, int32(2), *frames[0].Fields[1].At(0).(*int32))
|
||||||
require.Equal(t, float64(1.1), column[5].(float64))
|
require.Equal(t, int64(3), *frames[0].Fields[2].At(0).(*int64))
|
||||||
require.Equal(t, float64(1.2), column[6].(float64))
|
|
||||||
|
|
||||||
require.Equal(t, "char10 ", column[7].(string))
|
require.Equal(t, float64(4.5), *frames[0].Fields[3].At(0).(*float64))
|
||||||
require.Equal(t, "varchar10", column[8].(string))
|
require.Equal(t, float64(6.7), *frames[0].Fields[4].At(0).(*float64))
|
||||||
require.Equal(t, "text", column[9].(string))
|
require.Equal(t, float64(1.1), *frames[0].Fields[5].At(0).(*float64))
|
||||||
|
require.Equal(t, float64(1.2), *frames[0].Fields[6].At(0).(*float64))
|
||||||
|
|
||||||
_, ok := column[10].(time.Time)
|
require.Equal(t, "char10 ", *frames[0].Fields[7].At(0).(*string))
|
||||||
require.True(t, ok)
|
require.Equal(t, "varchar10", *frames[0].Fields[8].At(0).(*string))
|
||||||
_, ok = column[11].(time.Time)
|
require.Equal(t, "text", *frames[0].Fields[9].At(0).(*string))
|
||||||
require.True(t, ok)
|
|
||||||
_, ok = column[12].(time.Time)
|
|
||||||
require.True(t, ok)
|
|
||||||
_, ok = column[13].(time.Time)
|
|
||||||
require.True(t, ok)
|
|
||||||
_, ok = column[14].(time.Time)
|
|
||||||
require.True(t, ok)
|
|
||||||
|
|
||||||
require.Equal(t, "00:15:00", column[15].(string))
|
_, ok := frames[0].Fields[10].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
_, ok = frames[0].Fields[11].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
_, ok = frames[0].Fields[12].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
_, ok = frames[0].Fields[13].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
_, ok = frames[0].Fields[14].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
_, ok = frames[0].Fields[15].At(0).(*time.Time)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, "00:15:00", *frames[0].Fields[16].At(0).(*string))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -335,26 +339,27 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
points := queryResult.Series[0].Points
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
|
require.Len(t, frames, 1)
|
||||||
|
require.Equal(t, 4, frames[0].Fields[0].Len())
|
||||||
|
|
||||||
// without fill this should result in 4 buckets
|
// without fill this should result in 4 buckets
|
||||||
require.Len(t, points, 4)
|
|
||||||
|
|
||||||
dt := fromStart
|
dt := fromStart
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := *frames[0].Fields[1].At(i).(*float64)
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := *frames[0].Fields[0].At(i).(*time.Time)
|
||||||
require.Equal(t, float64(15), aValue)
|
require.Equal(t, float64(15), aValue)
|
||||||
require.Equal(t, dt, aTime)
|
require.Equal(t, dt, aTime)
|
||||||
require.Equal(t, int64(0), aTime.Unix()%300)
|
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjust for 10 minute gap between first and second set of points
|
// adjust for 10 minute gap between first and second set of points
|
||||||
dt = dt.Add(10 * time.Minute)
|
dt = dt.Add(10 * time.Minute)
|
||||||
for i := 2; i < 4; i++ {
|
for i := 2; i < 4; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := *frames[0].Fields[1].At(i).(*float64)
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := *frames[0].Fields[0].At(i).(*time.Time)
|
||||||
require.Equal(t, float64(20), aValue)
|
require.Equal(t, float64(20), aValue)
|
||||||
require.Equal(t, dt, aTime)
|
require.Equal(t, dt, aTime)
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
@ -388,10 +393,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
resp, err := exe.DataQuery(context.Background(), nil, query)
|
resp, err := exe.DataQuery(context.Background(), nil, query)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
|
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Equal(t,
|
require.Equal(t,
|
||||||
"SELECT floor(extract(epoch from time)/60)*60 AS time, avg(value) as value FROM metric GROUP BY 1 ORDER BY 1",
|
"SELECT floor(extract(epoch from time)/60)*60 AS time, avg(value) as value FROM metric GROUP BY 1 ORDER BY 1",
|
||||||
queryResult.Meta.Get(sqleng.MetaKeyExecutedQueryString).MustString())
|
frames[0].Meta.ExecutedQueryString)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using timeGroup with NULL fill enabled", func(t *testing.T) {
|
t.Run("When doing a metric query using timeGroup with NULL fill enabled", func(t *testing.T) {
|
||||||
@ -416,35 +423,36 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
points := queryResult.Series[0].Points
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Len(t, points, 7)
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 7, frames[0].Fields[0].Len())
|
||||||
|
|
||||||
dt := fromStart
|
dt := fromStart
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := *frames[0].Fields[1].At(i).(*float64)
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := *frames[0].Fields[0].At(i).(*time.Time)
|
||||||
require.Equal(t, float64(15), aValue)
|
require.Equal(t, float64(15), aValue)
|
||||||
require.Equal(t, dt, aTime)
|
require.True(t, aTime.Equal(dt))
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for NULL values inserted by fill
|
// check for NULL values inserted by fill
|
||||||
require.False(t, points[2][0].Valid)
|
require.Nil(t, frames[0].Fields[1].At(2))
|
||||||
require.False(t, points[3][0].Valid)
|
require.Nil(t, frames[0].Fields[1].At(3))
|
||||||
|
|
||||||
// adjust for 10 minute gap between first and second set of points
|
// adjust for 10 minute gap between first and second set of points
|
||||||
dt = dt.Add(10 * time.Minute)
|
dt = dt.Add(10 * time.Minute)
|
||||||
for i := 4; i < 6; i++ {
|
for i := 4; i < 6; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := *frames[0].Fields[1].At(i).(*float64)
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := *frames[0].Fields[0].At(i).(*time.Time)
|
||||||
require.Equal(t, float64(20), aValue)
|
require.Equal(t, float64(20), aValue)
|
||||||
require.Equal(t, dt, aTime)
|
require.True(t, aTime.Equal(dt))
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for NULL values inserted by fill
|
// check for NULL values inserted by fill
|
||||||
require.False(t, points[6][0].Valid)
|
require.Nil(t, frames[0].Fields[1].At(6))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using timeGroup with value fill enabled", func(t *testing.T) {
|
t.Run("When doing a metric query using timeGroup with value fill enabled", func(t *testing.T) {
|
||||||
@ -469,8 +477,9 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
points := queryResult.Series[0].Points
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(1.5), points[3][0].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 1.5, *frames[0].Fields[1].At(3).(*float64))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -496,10 +505,11 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
points := queryResult.Series[0].Points
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(15.0), points[2][0].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
require.Equal(t, float64(15.0), points[3][0].Float64)
|
require.Equal(t, float64(15.0), *frames[0].Fields[1].At(2).(*float64))
|
||||||
require.Equal(t, float64(20.0), points[6][0].Float64)
|
require.Equal(t, float64(15.0), *frames[0].Fields[1].At(3).(*float64))
|
||||||
|
require.Equal(t, float64(20.0), *frames[0].Fields[1].At(6).(*float64))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given a table with metrics having multiple values and measurements", func(t *testing.T) {
|
t.Run("Given a table with metrics having multiple values and measurements", func(t *testing.T) {
|
||||||
@ -570,7 +580,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Run(
|
t.Run(
|
||||||
"When doing a metric query using epoch (int64) as time column and value column (int64) should return metric with time in milliseconds",
|
"When doing a metric query using epoch (int64) as time column and value column (int64) should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -589,11 +599,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Equal(t, 1, len(queryResult.Series))
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Len(t, frames, 1)
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (int64 nullable) as time column and value column (int64 nullable,) should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (int64 nullable) as time column and value column (int64 nullable,) should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -612,11 +623,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Len(t, frames, 1)
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (float64) as time column and value column (float64), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (float64) as time column and value column (float64), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -635,11 +647,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Len(t, frames, 1)
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (float64 nullable) as time column and value column (float64 nullable), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (float64 nullable) as time column and value column (float64 nullable), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -658,11 +671,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (int32) as time column and value column (int32), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (int32) as time column and value column (int32), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -681,11 +695,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (int32 nullable) as time column and value column (int32 nullable), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (int32 nullable) as time column and value column (int32 nullable), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -704,11 +719,12 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(tInitial.UnixNano()/1e6), queryResult.Series[0].Points[0][1].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.True(t, tInitial.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (float32) as time column and value column (float32), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (float32) as time column and value column (float32), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -727,11 +743,13 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(float32(tInitial.Unix()))*1e3, queryResult.Series[0].Points[0][1].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
aTime := time.Unix(0, int64(float64(float32(tInitial.Unix()))*1e3)*int64(time.Millisecond))
|
||||||
|
require.True(t, aTime.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable), should return metric with time in milliseconds",
|
t.Run("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable), should return metric with time in time.Time",
|
||||||
func(t *testing.T) {
|
func(t *testing.T) {
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
Queries: []plugins.DataSubQuery{
|
Queries: []plugins.DataSubQuery{
|
||||||
@ -750,8 +768,10 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, float64(float32(tInitial.Unix()))*1e3, queryResult.Series[0].Points[0][1].Float64)
|
require.Equal(t, 1, len(frames))
|
||||||
|
aTime := time.Unix(0, int64(float64(float32(tInitial.Unix()))*1e3)*int64(time.Millisecond))
|
||||||
|
require.True(t, aTime.Equal(*frames[0].Fields[0].At(0).(*time.Time)))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query grouping by time and select metric column should return correct series", func(t *testing.T) {
|
t.Run("When doing a metric query grouping by time and select metric column should return correct series", func(t *testing.T) {
|
||||||
@ -772,9 +792,11 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 2)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, "Metric A - value one", queryResult.Series[0].Name)
|
require.Equal(t, 1, len(frames))
|
||||||
require.Equal(t, "Metric B - value one", queryResult.Series[1].Name)
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric A - value one"}, frames[0].Fields[1].Labels)
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric B - value one"}, frames[0].Fields[2].Labels)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query with metric column and multiple value columns", func(t *testing.T) {
|
t.Run("When doing a metric query with metric column and multiple value columns", func(t *testing.T) {
|
||||||
@ -795,11 +817,18 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 4)
|
frames, err := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, "Metric A valueOne", queryResult.Series[0].Name)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "Metric A valueTwo", queryResult.Series[1].Name)
|
require.Equal(t, 1, len(frames))
|
||||||
require.Equal(t, "Metric B valueOne", queryResult.Series[2].Name)
|
require.Equal(t, 5, len(frames[0].Fields))
|
||||||
require.Equal(t, "Metric B valueTwo", queryResult.Series[3].Name)
|
require.Equal(t, "valueOne", frames[0].Fields[1].Name)
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric A"}, frames[0].Fields[1].Labels)
|
||||||
|
require.Equal(t, "valueOne", frames[0].Fields[2].Name)
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric B"}, frames[0].Fields[2].Labels)
|
||||||
|
require.Equal(t, "valueTwo", frames[0].Fields[3].Name)
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric A"}, frames[0].Fields[3].Labels)
|
||||||
|
require.Equal(t, "valueTwo", frames[0].Fields[4].Name)
|
||||||
|
require.Equal(t, data.Labels{"metric": "Metric B"}, frames[0].Fields[4].Labels)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a metric query grouping by time should return correct series", func(t *testing.T) {
|
t.Run("When doing a metric query grouping by time should return correct series", func(t *testing.T) {
|
||||||
@ -820,9 +849,11 @@ func TestPostgres(t *testing.T) {
|
|||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
|
||||||
require.Len(t, queryResult.Series, 2)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, "valueOne", queryResult.Series[0].Name)
|
require.Equal(t, 1, len(frames))
|
||||||
require.Equal(t, "valueTwo", queryResult.Series[1].Name)
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
require.Equal(t, "valueOne", frames[0].Fields[1].Name)
|
||||||
|
require.Equal(t, "valueTwo", frames[0].Fields[2].Name)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing a query with timeFrom,timeTo,unixEpochFrom,unixEpochTo macros", func(t *testing.T) {
|
t.Run("When doing a query with timeFrom,timeTo,unixEpochFrom,unixEpochTo macros", func(t *testing.T) {
|
||||||
@ -850,9 +881,11 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
|
require.Len(t, frames, 1)
|
||||||
require.Equal(t,
|
require.Equal(t,
|
||||||
"SELECT time FROM metric_values WHERE time > '2018-03-15T12:55:00Z' OR time < '2018-03-15T12:55:00Z' OR 1 < 1521118500 OR 1521118800 > 1 ORDER BY 1",
|
"SELECT time FROM metric_values WHERE time > '2018-03-15T12:55:00Z' OR time < '2018-03-15T12:55:00Z' OR 1 < 1521118500 OR 1521118800 > 1 ORDER BY 1",
|
||||||
queryResult.Meta.Get(sqleng.MetaKeyExecutedQueryString).MustString())
|
frames[0].Meta.ExecutedQueryString)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -910,7 +943,10 @@ func TestPostgres(t *testing.T) {
|
|||||||
resp, err := exe.DataQuery(context.Background(), nil, query)
|
resp, err := exe.DataQuery(context.Background(), nil, query)
|
||||||
queryResult := resp.Results["Deploys"]
|
queryResult := resp.Results["Deploys"]
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 3)
|
|
||||||
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
|
require.Len(t, frames, 1)
|
||||||
|
require.Len(t, frames[0].Fields, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query of ticket events should return expected result", func(t *testing.T) {
|
t.Run("When doing an annotation query of ticket events should return expected result", func(t *testing.T) {
|
||||||
@ -933,7 +969,10 @@ func TestPostgres(t *testing.T) {
|
|||||||
resp, err := exe.DataQuery(context.Background(), nil, query)
|
resp, err := exe.DataQuery(context.Background(), nil, query)
|
||||||
queryResult := resp.Results["Tickets"]
|
queryResult := resp.Results["Tickets"]
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 3)
|
|
||||||
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column in datetime format", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column in datetime format", func(t *testing.T) {
|
||||||
@ -960,14 +999,15 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
//Should be in milliseconds
|
// Should be in time.Time
|
||||||
require.Equal(t, float64(dt.UnixNano()/1e6), columns[0].(float64))
|
require.Equal(t, dt.Unix(), (*frames[0].Fields[0].At(0).(*time.Time)).Unix())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column in epoch second format should return ms", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column in epoch second format should return time.Time", func(t *testing.T) {
|
||||||
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
||||||
|
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
@ -975,7 +1015,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Model: simplejson.NewFromAny(map[string]interface{}{
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"rawSql": fmt.Sprintf(`SELECT
|
"rawSql": fmt.Sprintf(`SELECT
|
||||||
%d as time,
|
%d as time,
|
||||||
'message' as text,
|
'message' as text,
|
||||||
'tag1,tag2' as tags
|
'tag1,tag2' as tags
|
||||||
`, dt.Unix()),
|
`, dt.Unix()),
|
||||||
@ -990,14 +1030,16 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
|
||||||
|
|
||||||
//Should be in milliseconds
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, dt.Unix()*1000, columns[0].(int64))
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
|
// Should be in time.Time
|
||||||
|
require.Equal(t, dt.Unix(), (*frames[0].Fields[0].At(0).(*time.Time)).Unix())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column in epoch second format (t *testing.Tint) should return ms", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column in epoch second format (t *testing.Tint) should return time.Time", func(t *testing.T) {
|
||||||
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
||||||
|
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
@ -1005,7 +1047,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Model: simplejson.NewFromAny(map[string]interface{}{
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"rawSql": fmt.Sprintf(`SELECT
|
"rawSql": fmt.Sprintf(`SELECT
|
||||||
cast(%d as bigint) as time,
|
cast(%d as bigint) as time,
|
||||||
'message' as text,
|
'message' as text,
|
||||||
'tag1,tag2' as tags
|
'tag1,tag2' as tags
|
||||||
`, dt.Unix()),
|
`, dt.Unix()),
|
||||||
@ -1020,14 +1062,16 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
|
||||||
|
|
||||||
//Should be in milliseconds
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, dt.Unix()*1000, columns[0].(int64))
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
|
// Should be in time.Time
|
||||||
|
require.Equal(t, dt.Unix(), (*frames[0].Fields[0].At(0).(*time.Time)).Unix())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column in epoch millisecond format should return ms", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column in epoch millisecond format should return time.Time", func(t *testing.T) {
|
||||||
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
dt := time.Date(2018, 3, 14, 21, 20, 6, 527e6, time.UTC)
|
||||||
|
|
||||||
query := plugins.DataQuery{
|
query := plugins.DataQuery{
|
||||||
@ -1050,11 +1094,13 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
|
||||||
|
|
||||||
//Should be in milliseconds
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Equal(t, dt.Unix()*1000, columns[0].(int64))
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
|
// Should be in time.Time
|
||||||
|
require.Equal(t, dt.Unix(), (*frames[0].Fields[0].At(0).(*time.Time)).Unix())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column holding a bigint null value should return nil", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column holding a bigint null value should return nil", func(t *testing.T) {
|
||||||
@ -1078,11 +1124,13 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
|
||||||
|
|
||||||
//Should be in milliseconds
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
require.Nil(t, columns[0])
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
|
// Should be in time.Time
|
||||||
|
require.Nil(t, frames[0].Fields[0].At(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When doing an annotation query with a time column holding a timestamp null value should return nil", func(t *testing.T) {
|
t.Run("When doing an annotation query with a time column holding a timestamp null value should return nil", func(t *testing.T) {
|
||||||
@ -1091,10 +1139,10 @@ func TestPostgres(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Model: simplejson.NewFromAny(map[string]interface{}{
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"rawSql": `SELECT
|
"rawSql": `SELECT
|
||||||
cast(null as timestamp) as time,
|
cast(null as timestamp) as time,
|
||||||
'message' as text,
|
'message' as text,
|
||||||
'tag1,tag2' as tags
|
'tag1,tag2' as tags
|
||||||
`,
|
`,
|
||||||
"format": "table",
|
"format": "table",
|
||||||
}),
|
}),
|
||||||
RefID: "A",
|
RefID: "A",
|
||||||
@ -1106,11 +1154,13 @@ func TestPostgres(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
queryResult := resp.Results["A"]
|
queryResult := resp.Results["A"]
|
||||||
require.NoError(t, queryResult.Error)
|
require.NoError(t, queryResult.Error)
|
||||||
require.Len(t, queryResult.Tables[0].Rows, 1)
|
|
||||||
columns := queryResult.Tables[0].Rows[0]
|
|
||||||
|
|
||||||
//Should be in milliseconds
|
frames, _ := queryResult.Dataframes.Decoded()
|
||||||
assert.Nil(t, columns[0])
|
require.Equal(t, 1, len(frames))
|
||||||
|
require.Equal(t, 3, len(frames[0].Fields))
|
||||||
|
|
||||||
|
// Should be in time.Time
|
||||||
|
assert.Nil(t, frames[0].Fields[0].At(0))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
136
pkg/tsdb/sqleng/resample.go
Normal file
136
pkg/tsdb/sqleng/resample.go
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package sqleng
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getRowFillValues populates a slice of values corresponding to the provided data.Frame fields.
|
||||||
|
// Uses data.FillMissing settings to fill in values that are missing. Values are normally missing
|
||||||
|
// due to that the selected query interval doesn't match the intervals of the data returned from
|
||||||
|
// the query and therefore needs to be resampled.
|
||||||
|
func getRowFillValues(f *data.Frame, tsSchema data.TimeSeriesSchema, currentTime time.Time,
|
||||||
|
fillMissing *data.FillMissing, intermediateRows []int, lastSeenRowIdx int) []interface{} {
|
||||||
|
vals := make([]interface{}, 0, len(f.Fields))
|
||||||
|
for i, field := range f.Fields {
|
||||||
|
// if the current field is the time index of the series
|
||||||
|
// set the new value to be added to the new timestamp
|
||||||
|
if i == tsSchema.TimeIndex {
|
||||||
|
switch f.Fields[tsSchema.TimeIndex].Type() {
|
||||||
|
case data.FieldTypeTime:
|
||||||
|
vals = append(vals, currentTime)
|
||||||
|
default:
|
||||||
|
vals = append(vals, ¤tTime)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
isValueField := false
|
||||||
|
for _, idx := range tsSchema.ValueIndices {
|
||||||
|
if i == idx {
|
||||||
|
isValueField = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the current field is value Field
|
||||||
|
// set the new value to the last seen field value (if such exists)
|
||||||
|
// otherwise set the appropriate value according to the fillMissing mode
|
||||||
|
// if the current field is string field)
|
||||||
|
// set the new value to be added to the last seen value (if such exists)
|
||||||
|
// if the Frame is wide then there should not be any string fields
|
||||||
|
var newVal interface{}
|
||||||
|
if isValueField {
|
||||||
|
if len(intermediateRows) > 0 {
|
||||||
|
// instead of setting the last seen
|
||||||
|
// we could set avg, sum, min or max
|
||||||
|
// of the intermediate values for each field
|
||||||
|
newVal = f.At(i, intermediateRows[len(intermediateRows)-1])
|
||||||
|
} else {
|
||||||
|
val, err := data.GetMissing(fillMissing, field, lastSeenRowIdx)
|
||||||
|
if err == nil {
|
||||||
|
newVal = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if lastSeenRowIdx >= 0 {
|
||||||
|
newVal = f.At(i, lastSeenRowIdx)
|
||||||
|
}
|
||||||
|
vals = append(vals, newVal)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
}
|
||||||
|
|
||||||
|
// resample resample provided time-series data.Frame.
|
||||||
|
// This is needed in the case of the selected query interval doesn't
|
||||||
|
// match the intervals of the time-series field in the data.Frame and
|
||||||
|
// therefore needs to be resampled.
|
||||||
|
func resample(f *data.Frame, qm dataQueryModel) (*data.Frame, error) {
|
||||||
|
tsSchema := f.TimeSeriesSchema()
|
||||||
|
if tsSchema.Type == data.TimeSeriesTypeNot {
|
||||||
|
return f, fmt.Errorf("can not fill missing, not timeseries frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
if qm.Interval == 0 {
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
newFields := make([]*data.Field, 0, len(f.Fields))
|
||||||
|
for _, field := range f.Fields {
|
||||||
|
newField := data.NewFieldFromFieldType(field.Type(), 0)
|
||||||
|
newField.Name = field.Name
|
||||||
|
newField.Labels = field.Labels
|
||||||
|
newFields = append(newFields, newField)
|
||||||
|
}
|
||||||
|
resampledFrame := data.NewFrame(f.Name, newFields...)
|
||||||
|
resampledFrame.Meta = f.Meta
|
||||||
|
|
||||||
|
resampledRowidx := 0
|
||||||
|
lastSeenRowIdx := -1
|
||||||
|
timeField := f.Fields[tsSchema.TimeIndex]
|
||||||
|
|
||||||
|
for currentTime := qm.TimeRange.From; !currentTime.After(qm.TimeRange.To); currentTime = currentTime.Add(qm.Interval) {
|
||||||
|
initialRowIdx := 0
|
||||||
|
if lastSeenRowIdx > 0 {
|
||||||
|
initialRowIdx = lastSeenRowIdx + 1
|
||||||
|
}
|
||||||
|
intermediateRows := make([]int, 0)
|
||||||
|
for {
|
||||||
|
rowLen, err := f.RowLen()
|
||||||
|
if err != nil {
|
||||||
|
return f, err
|
||||||
|
}
|
||||||
|
if initialRowIdx == rowLen {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
t, ok := timeField.ConcreteAt(initialRowIdx)
|
||||||
|
if !ok {
|
||||||
|
return f, fmt.Errorf("time point is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.(time.Time).After(currentTime) {
|
||||||
|
nextTime := currentTime.Add(qm.Interval)
|
||||||
|
if t.(time.Time).Before(nextTime) {
|
||||||
|
intermediateRows = append(intermediateRows, initialRowIdx)
|
||||||
|
lastSeenRowIdx = initialRowIdx
|
||||||
|
initialRowIdx++
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
intermediateRows = append(intermediateRows, initialRowIdx)
|
||||||
|
lastSeenRowIdx = initialRowIdx
|
||||||
|
initialRowIdx++
|
||||||
|
}
|
||||||
|
|
||||||
|
// no intermediate points; set values following fill missing mode
|
||||||
|
fieldVals := getRowFillValues(f, tsSchema, currentTime, qm.FillMissing, intermediateRows, lastSeenRowIdx)
|
||||||
|
|
||||||
|
resampledFrame.InsertRow(resampledRowidx, fieldVals...)
|
||||||
|
resampledRowidx++
|
||||||
|
}
|
||||||
|
|
||||||
|
return resampledFrame, nil
|
||||||
|
}
|
309
pkg/tsdb/sqleng/resample_test.go
Normal file
309
pkg/tsdb/sqleng/resample_test.go
Normal file
@ -0,0 +1,309 @@
|
|||||||
|
package sqleng
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/xorcare/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResampleWide(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *data.Frame
|
||||||
|
fillMissing *data.FillMissing
|
||||||
|
timeRange backend.TimeRange
|
||||||
|
interval time.Duration
|
||||||
|
output *data.Frame
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "interval 1s; fill null",
|
||||||
|
fillMissing: &data.FillMissing{Mode: data.FillModeNull},
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
},
|
||||||
|
interval: time.Second,
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
nil,
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
nil,
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "interval 1s; fill value",
|
||||||
|
fillMissing: &data.FillMissing{Mode: data.FillModeValue, Value: -1},
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
},
|
||||||
|
interval: time.Second,
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(-1),
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(-1),
|
||||||
|
pointer.Int64(-1),
|
||||||
|
pointer.Int64(-1),
|
||||||
|
pointer.Int64(15),
|
||||||
|
pointer.Int64(-1),
|
||||||
|
pointer.Int64(-1),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(-1),
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(-1),
|
||||||
|
pointer.Float64(-1),
|
||||||
|
pointer.Float64(-1),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
pointer.Float64(-1),
|
||||||
|
pointer.Float64(-1),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "interval 1s; fill previous",
|
||||||
|
fillMissing: &data.FillMissing{Mode: data.FillModePrevious},
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
},
|
||||||
|
interval: time.Second,
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
nil,
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
pointer.Int64(15),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
nil,
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "interval 2s; fill null",
|
||||||
|
fillMissing: &data.FillMissing{Mode: data.FillModeNull},
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
},
|
||||||
|
interval: 2 * time.Second,
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
nil,
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
nil,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "interval 1s; fill null; rows outside timerange window",
|
||||||
|
fillMissing: &data.FillMissing{Mode: data.FillModeNull},
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
},
|
||||||
|
interval: time.Second,
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
frame, err := resample(tt.input, dataQueryModel{
|
||||||
|
FillMissing: tt.fillMissing,
|
||||||
|
TimeRange: tt.timeRange,
|
||||||
|
Interval: tt.interval,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
if diff := cmp.Diff(tt.output, frame, data.FrameTestCompareOptions()...); diff != "" {
|
||||||
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -7,19 +7,20 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/null"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/xorcare/pointer"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSQLEngine(t *testing.T) {
|
func TestSQLEngine(t *testing.T) {
|
||||||
dt := time.Date(2018, 3, 14, 21, 20, 6, int(527345*time.Microsecond), time.UTC)
|
dt := time.Date(2018, 3, 14, 21, 20, 6, int(527345*time.Microsecond), time.UTC)
|
||||||
earlyDt := time.Date(1970, 3, 14, 21, 20, 6, int(527345*time.Microsecond), time.UTC)
|
|
||||||
|
|
||||||
t.Run("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func(t *testing.T) {
|
t.Run("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func(t *testing.T) {
|
||||||
from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
|
from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
|
||||||
@ -58,56 +59,48 @@ func TestSQLEngine(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with time.Time as time columns", func(t *testing.T) {
|
|
||||||
var nilPointer *time.Time
|
|
||||||
|
|
||||||
fixtures := make([]interface{}, 5)
|
|
||||||
fixtures[0] = dt
|
|
||||||
fixtures[1] = &dt
|
|
||||||
fixtures[2] = earlyDt
|
|
||||||
fixtures[3] = &earlyDt
|
|
||||||
fixtures[4] = nilPointer
|
|
||||||
|
|
||||||
for i := range fixtures {
|
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := float64(dt.UnixNano()) / float64(time.Millisecond)
|
|
||||||
expectedEarly := float64(earlyDt.UnixNano()) / float64(time.Millisecond)
|
|
||||||
|
|
||||||
require.Equal(t, expected, fixtures[0].(float64))
|
|
||||||
require.Equal(t, expected, fixtures[1].(float64))
|
|
||||||
require.Equal(t, expectedEarly, fixtures[2].(float64))
|
|
||||||
require.Equal(t, expectedEarly, fixtures[3].(float64))
|
|
||||||
require.Nil(t, fixtures[4])
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Given row values with int64 as time columns", func(t *testing.T) {
|
t.Run("Given row values with int64 as time columns", func(t *testing.T) {
|
||||||
tSeconds := dt.Unix()
|
tSeconds := dt.Unix()
|
||||||
tMilliseconds := dt.UnixNano() / 1e6
|
tMilliseconds := dt.UnixNano() / 1e6
|
||||||
tNanoSeconds := dt.UnixNano()
|
tNanoSeconds := dt.UnixNano()
|
||||||
var nilPointer *int64
|
var nilPointer *int64
|
||||||
|
|
||||||
fixtures := make([]interface{}, 7)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []int64{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = tMilliseconds
|
}),
|
||||||
fixtures[3] = &tMilliseconds
|
data.NewField("time2", nil, []*int64{
|
||||||
fixtures[4] = tNanoSeconds
|
pointer.Int64(tSeconds),
|
||||||
fixtures[5] = &tNanoSeconds
|
}),
|
||||||
fixtures[6] = nilPointer
|
data.NewField("time3", nil, []int64{
|
||||||
|
tMilliseconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time4", nil, []*int64{
|
||||||
|
pointer.Int64(tMilliseconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time5", nil, []int64{
|
||||||
|
tNanoSeconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time6", nil, []*int64{
|
||||||
|
pointer.Int64(tNanoSeconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time7", nil, []*int64{
|
||||||
|
nilPointer,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
for i := range fixtures {
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, tSeconds*1e3, fixtures[0].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tSeconds*1e3, fixtures[1].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[2].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[2].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[3].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[3].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[4].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[4].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[5].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[5].At(0).(*time.Time)).Unix())
|
||||||
require.Nil(t, fixtures[6])
|
require.Nil(t, originFrame.Fields[6].At(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with uint64 as time columns", func(t *testing.T) {
|
t.Run("Given row values with uint64 as time columns", func(t *testing.T) {
|
||||||
@ -116,62 +109,91 @@ func TestSQLEngine(t *testing.T) {
|
|||||||
tNanoSeconds := uint64(dt.UnixNano())
|
tNanoSeconds := uint64(dt.UnixNano())
|
||||||
var nilPointer *uint64
|
var nilPointer *uint64
|
||||||
|
|
||||||
fixtures := make([]interface{}, 7)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []uint64{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = tMilliseconds
|
}),
|
||||||
fixtures[3] = &tMilliseconds
|
data.NewField("time2", nil, []*uint64{
|
||||||
fixtures[4] = tNanoSeconds
|
pointer.Uint64(tSeconds),
|
||||||
fixtures[5] = &tNanoSeconds
|
}),
|
||||||
fixtures[6] = nilPointer
|
data.NewField("time3", nil, []uint64{
|
||||||
|
tMilliseconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time4", nil, []*uint64{
|
||||||
|
pointer.Uint64(tMilliseconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time5", nil, []uint64{
|
||||||
|
tNanoSeconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time6", nil, []*uint64{
|
||||||
|
pointer.Uint64(tNanoSeconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time7", nil, []*uint64{
|
||||||
|
nilPointer,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
for i := range fixtures {
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, int64(tSeconds*1e3), fixtures[0].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, int64(tSeconds*1e3), fixtures[1].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, int64(tMilliseconds), fixtures[2].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[2].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, int64(tMilliseconds), fixtures[3].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[3].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, int64(tMilliseconds), fixtures[4].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[4].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, int64(tMilliseconds), fixtures[5].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[5].At(0).(*time.Time)).Unix())
|
||||||
require.Nil(t, fixtures[6])
|
require.Nil(t, originFrame.Fields[6].At(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with int32 as time columns", func(t *testing.T) {
|
t.Run("Given row values with int32 as time columns", func(t *testing.T) {
|
||||||
tSeconds := int32(dt.Unix())
|
tSeconds := int32(dt.Unix())
|
||||||
var nilInt *int32
|
var nilInt *int32
|
||||||
|
|
||||||
fixtures := make([]interface{}, 3)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []int32{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = nilInt
|
}),
|
||||||
|
data.NewField("time2", nil, []*int32{
|
||||||
for i := range fixtures {
|
pointer.Int32(tSeconds),
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
}),
|
||||||
|
data.NewField("time7", nil, []*int32{
|
||||||
|
nilInt,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, dt.Unix()*1e3, fixtures[0].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, dt.Unix()*1e3, fixtures[1].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Nil(t, fixtures[2])
|
require.Nil(t, originFrame.Fields[2].At(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with uint32 as time columns", func(t *testing.T) {
|
t.Run("Given row values with uint32 as time columns", func(t *testing.T) {
|
||||||
tSeconds := uint32(dt.Unix())
|
tSeconds := uint32(dt.Unix())
|
||||||
var nilInt *uint32
|
var nilInt *uint32
|
||||||
|
|
||||||
fixtures := make([]interface{}, 3)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []uint32{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = nilInt
|
}),
|
||||||
|
data.NewField("time2", nil, []*uint32{
|
||||||
for i := range fixtures {
|
pointer.Uint32(tSeconds),
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
}),
|
||||||
|
data.NewField("time7", nil, []*uint32{
|
||||||
|
nilInt,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, dt.Unix()*1e3, fixtures[0].(int64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, dt.Unix()*1e3, fixtures[1].(int64))
|
require.Nil(t, originFrame.Fields[2].At(0))
|
||||||
require.Nil(t, fixtures[2])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with float64 as time columns", func(t *testing.T) {
|
t.Run("Given row values with float64 as time columns", func(t *testing.T) {
|
||||||
@ -180,137 +202,192 @@ func TestSQLEngine(t *testing.T) {
|
|||||||
tNanoSeconds := float64(dt.UnixNano())
|
tNanoSeconds := float64(dt.UnixNano())
|
||||||
var nilPointer *float64
|
var nilPointer *float64
|
||||||
|
|
||||||
fixtures := make([]interface{}, 7)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []float64{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = tMilliseconds
|
}),
|
||||||
fixtures[3] = &tMilliseconds
|
data.NewField("time2", nil, []*float64{
|
||||||
fixtures[4] = tNanoSeconds
|
pointer.Float64(tSeconds),
|
||||||
fixtures[5] = &tNanoSeconds
|
}),
|
||||||
fixtures[6] = nilPointer
|
data.NewField("time3", nil, []float64{
|
||||||
|
tMilliseconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time4", nil, []*float64{
|
||||||
|
pointer.Float64(tMilliseconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time5", nil, []float64{
|
||||||
|
tNanoSeconds,
|
||||||
|
}),
|
||||||
|
data.NewField("time6", nil, []*float64{
|
||||||
|
pointer.Float64(tNanoSeconds),
|
||||||
|
}),
|
||||||
|
data.NewField("time7", nil, []*float64{
|
||||||
|
nilPointer,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
for i := range fixtures {
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, tMilliseconds, fixtures[0].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[1].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[2].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[2].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[3].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[3].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[4].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[4].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, tMilliseconds, fixtures[5].(float64))
|
require.Equal(t, dt.Unix(), (*originFrame.Fields[5].At(0).(*time.Time)).Unix())
|
||||||
require.Nil(t, fixtures[6])
|
require.Nil(t, originFrame.Fields[6].At(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row values with float32 as time columns", func(t *testing.T) {
|
t.Run("Given row values with float32 as time columns", func(t *testing.T) {
|
||||||
tSeconds := float32(dt.Unix())
|
tSeconds := float32(dt.Unix())
|
||||||
var nilInt *float32
|
var nilInt *float32
|
||||||
|
|
||||||
fixtures := make([]interface{}, 3)
|
originFrame := data.NewFrame("",
|
||||||
fixtures[0] = tSeconds
|
data.NewField("time1", nil, []float32{
|
||||||
fixtures[1] = &tSeconds
|
tSeconds,
|
||||||
fixtures[2] = nilInt
|
}),
|
||||||
|
data.NewField("time2", nil, []*float32{
|
||||||
for i := range fixtures {
|
pointer.Float32(tSeconds),
|
||||||
ConvertSqlTimeColumnToEpochMs(fixtures, i)
|
}),
|
||||||
|
data.NewField("time7", nil, []*float32{
|
||||||
|
nilInt,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
|
err := convertSQLTimeColumnToEpochMS(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
require.Equal(t, int64(tSeconds), (*originFrame.Fields[0].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, float64(tSeconds)*1e3, fixtures[0].(float64))
|
require.Equal(t, int64(tSeconds), (*originFrame.Fields[1].At(0).(*time.Time)).Unix())
|
||||||
require.Equal(t, float64(tSeconds)*1e3, fixtures[1].(float64))
|
require.Nil(t, originFrame.Fields[2].At(0))
|
||||||
require.Nil(t, fixtures[2])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row with value columns", func(t *testing.T) {
|
t.Run("Given row with value columns, would be converted to float64", func(t *testing.T) {
|
||||||
intValue := 1
|
originFrame := data.NewFrame("",
|
||||||
int64Value := int64(1)
|
data.NewField("value1", nil, []int64{
|
||||||
int32Value := int32(1)
|
int64(1),
|
||||||
int16Value := int16(1)
|
}),
|
||||||
int8Value := int8(1)
|
data.NewField("value2", nil, []*int64{
|
||||||
float64Value := float64(1)
|
pointer.Int64(1),
|
||||||
float32Value := float32(1)
|
}),
|
||||||
uintValue := uint(1)
|
data.NewField("value3", nil, []int32{
|
||||||
uint64Value := uint64(1)
|
int32(1),
|
||||||
uint32Value := uint32(1)
|
}),
|
||||||
uint16Value := uint16(1)
|
data.NewField("value4", nil, []*int32{
|
||||||
uint8Value := uint8(1)
|
pointer.Int32(1),
|
||||||
|
}),
|
||||||
testCases := []struct {
|
data.NewField("value5", nil, []int16{
|
||||||
name string
|
int16(1),
|
||||||
value interface{}
|
}),
|
||||||
}{
|
data.NewField("value6", nil, []*int16{
|
||||||
{"intValue", intValue},
|
pointer.Int16(1),
|
||||||
{"&intValue", &intValue},
|
}),
|
||||||
{"int64Value", int64Value},
|
data.NewField("value7", nil, []int8{
|
||||||
{"&int64Value", &int64Value},
|
int8(1),
|
||||||
{"int32Value", int32Value},
|
}),
|
||||||
{"&int32Value", &int32Value},
|
data.NewField("value8", nil, []*int8{
|
||||||
{"int16Value", int16Value},
|
pointer.Int8(1),
|
||||||
{"&int16Value", &int16Value},
|
}),
|
||||||
{"int8Value", int8Value},
|
data.NewField("value9", nil, []float64{
|
||||||
{"&int8Value", &int8Value},
|
float64(1),
|
||||||
{"float64Value", float64Value},
|
}),
|
||||||
{"&float64Value", &float64Value},
|
data.NewField("value10", nil, []*float64{
|
||||||
{"float32Value", float32Value},
|
pointer.Float64(1),
|
||||||
{"&float32Value", &float32Value},
|
}),
|
||||||
{"uintValue", uintValue},
|
data.NewField("value11", nil, []float32{
|
||||||
{"&uintValue", &uintValue},
|
float32(1),
|
||||||
{"uint64Value", uint64Value},
|
}),
|
||||||
{"&uint64Value", &uint64Value},
|
data.NewField("value12", nil, []*float32{
|
||||||
{"uint32Value", uint32Value},
|
pointer.Float32(1),
|
||||||
{"&uint32Value", &uint32Value},
|
}),
|
||||||
{"uint16Value", uint16Value},
|
data.NewField("value13", nil, []uint64{
|
||||||
{"&uint16Value", &uint16Value},
|
uint64(1),
|
||||||
{"uint8Value", uint8Value},
|
}),
|
||||||
{"&uint8Value", &uint8Value},
|
data.NewField("value14", nil, []*uint64{
|
||||||
}
|
pointer.Uint64(1),
|
||||||
|
}),
|
||||||
for _, tc := range testCases {
|
data.NewField("value15", nil, []uint32{
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
uint32(1),
|
||||||
value, err := ConvertSqlValueColumnToFloat("col", tc.value)
|
}),
|
||||||
require.NoError(t, err)
|
data.NewField("value16", nil, []*uint32{
|
||||||
require.True(t, value.Valid)
|
pointer.Uint32(1),
|
||||||
require.Equal(t, null.FloatFrom(1).Float64, value.Float64)
|
}),
|
||||||
})
|
data.NewField("value17", nil, []uint16{
|
||||||
|
uint16(1),
|
||||||
|
}),
|
||||||
|
data.NewField("value18", nil, []*uint16{
|
||||||
|
pointer.Uint16(1),
|
||||||
|
}),
|
||||||
|
data.NewField("value19", nil, []uint8{
|
||||||
|
uint8(1),
|
||||||
|
}),
|
||||||
|
data.NewField("value20", nil, []*uint8{
|
||||||
|
pointer.Uint8(1),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
|
_, err := convertSQLValueColumnToFloat(originFrame, i)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if i == 8 {
|
||||||
|
require.Equal(t, float64(1), originFrame.Fields[i].At(0).(float64))
|
||||||
|
} else {
|
||||||
|
require.NotNil(t, originFrame.Fields[i].At(0).(*float64))
|
||||||
|
require.Equal(t, float64(1), *originFrame.Fields[i].At(0).(*float64))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Given row with nil value columns", func(t *testing.T) {
|
t.Run("Given row with nil value columns", func(t *testing.T) {
|
||||||
var intNilPointer *int
|
|
||||||
var int64NilPointer *int64
|
var int64NilPointer *int64
|
||||||
var int32NilPointer *int32
|
var int32NilPointer *int32
|
||||||
var int16NilPointer *int16
|
var int16NilPointer *int16
|
||||||
var int8NilPointer *int8
|
var int8NilPointer *int8
|
||||||
var float64NilPointer *float64
|
var float64NilPointer *float64
|
||||||
var float32NilPointer *float32
|
var float32NilPointer *float32
|
||||||
var uintNilPointer *uint
|
|
||||||
var uint64NilPointer *uint64
|
var uint64NilPointer *uint64
|
||||||
var uint32NilPointer *uint32
|
var uint32NilPointer *uint32
|
||||||
var uint16NilPointer *uint16
|
var uint16NilPointer *uint16
|
||||||
var uint8NilPointer *uint8
|
var uint8NilPointer *uint8
|
||||||
|
|
||||||
testCases := []struct {
|
originFrame := data.NewFrame("",
|
||||||
name string
|
data.NewField("value1", nil, []*int64{
|
||||||
value interface{}
|
int64NilPointer,
|
||||||
}{
|
}),
|
||||||
{"intNilPointer", intNilPointer},
|
data.NewField("value2", nil, []*int32{
|
||||||
{"int64NilPointer", int64NilPointer},
|
int32NilPointer,
|
||||||
{"int32NilPointer", int32NilPointer},
|
}),
|
||||||
{"int16NilPointer", int16NilPointer},
|
data.NewField("value3", nil, []*int16{
|
||||||
{"int8NilPointer", int8NilPointer},
|
int16NilPointer,
|
||||||
{"float64NilPointer", float64NilPointer},
|
}),
|
||||||
{"float32NilPointer", float32NilPointer},
|
data.NewField("value4", nil, []*int8{
|
||||||
{"uintNilPointer", uintNilPointer},
|
int8NilPointer,
|
||||||
{"uint64NilPointer", uint64NilPointer},
|
}),
|
||||||
{"uint32NilPointer", uint32NilPointer},
|
data.NewField("value5", nil, []*float64{
|
||||||
{"uint16NilPointer", uint16NilPointer},
|
float64NilPointer,
|
||||||
{"uint8NilPointer", uint8NilPointer},
|
}),
|
||||||
}
|
data.NewField("value6", nil, []*float32{
|
||||||
|
float32NilPointer,
|
||||||
for _, tc := range testCases {
|
}),
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
data.NewField("value7", nil, []*uint64{
|
||||||
value, err := ConvertSqlValueColumnToFloat("col", tc.value)
|
uint64NilPointer,
|
||||||
|
}),
|
||||||
|
data.NewField("value8", nil, []*uint32{
|
||||||
|
uint32NilPointer,
|
||||||
|
}),
|
||||||
|
data.NewField("value9", nil, []*uint16{
|
||||||
|
uint16NilPointer,
|
||||||
|
}),
|
||||||
|
data.NewField("value10", nil, []*uint8{
|
||||||
|
uint8NilPointer,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
for i := 0; i < len(originFrame.Fields); i++ {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
_, err := convertSQLValueColumnToFloat(originFrame, i)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.False(t, value.Valid)
|
require.Nil(t, originFrame.Fields[i].At(0))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -352,3 +429,7 @@ func (t *testQueryResultTransformer) TransformQueryError(err error) error {
|
|||||||
t.transformQueryErrorWasCalled = true
|
t.transformQueryErrorWasCalled = true
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *testQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
51
pkg/tsdb/sqleng/trim.go
Normal file
51
pkg/tsdb/sqleng/trim.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package sqleng
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// trim trims rows that are outside the qm.TimeRange.
|
||||||
|
func trim(f *data.Frame, qm dataQueryModel) error {
|
||||||
|
tsSchema := f.TimeSeriesSchema()
|
||||||
|
if tsSchema.Type == data.TimeSeriesTypeNot {
|
||||||
|
return fmt.Errorf("can not trim non-timeseries frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
timeField := f.Fields[tsSchema.TimeIndex]
|
||||||
|
if timeField.Len() == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim rows after end
|
||||||
|
for i := timeField.Len() - 1; i >= 0; i-- {
|
||||||
|
t, ok := timeField.ConcreteAt(i)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("time point is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !t.(time.Time).After(qm.TimeRange.To) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
f.DeleteRow(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim rows before start
|
||||||
|
for timeField.Len() > 0 {
|
||||||
|
t, ok := timeField.ConcreteAt(0)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("time point is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !t.(time.Time).Before(qm.TimeRange.From) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
f.DeleteRow(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
171
pkg/tsdb/sqleng/trim_test.go
Normal file
171
pkg/tsdb/sqleng/trim_test.go
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
package sqleng
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/xorcare/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTrimWide(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *data.Frame
|
||||||
|
timeRange backend.TimeRange
|
||||||
|
output *data.Frame
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "needs trimming",
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
},
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
nil,
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
nil,
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not need trimming",
|
||||||
|
timeRange: backend.TimeRange{
|
||||||
|
From: time.Date(2020, 1, 2, 3, 4, 15, 0, time.UTC),
|
||||||
|
To: time.Date(2020, 1, 2, 3, 4, 30, 0, time.UTC),
|
||||||
|
},
|
||||||
|
input: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
nil,
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
nil,
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
})),
|
||||||
|
output: data.NewFrame("wide_test",
|
||||||
|
data.NewField("Time", nil, []time.Time{
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 18, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 19, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 20, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 21, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 22, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 23, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 24, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 25, 0, time.UTC),
|
||||||
|
time.Date(2020, 1, 2, 3, 4, 26, 0, time.UTC),
|
||||||
|
}),
|
||||||
|
data.NewField("Values Ints", nil, []*int64{
|
||||||
|
nil,
|
||||||
|
pointer.Int64(10),
|
||||||
|
pointer.Int64(12),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Int64(15),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}),
|
||||||
|
data.NewField(`Values Floats`, data.Labels{"Animal Factor": "sloth"}, []*float64{
|
||||||
|
nil,
|
||||||
|
pointer.Float64(10.5),
|
||||||
|
pointer.Float64(12.5),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
pointer.Float64(15.0),
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := trim(tt.input, dataQueryModel{
|
||||||
|
TimeRange: tt.timeRange,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
if diff := cmp.Diff(tt.output, tt.input, data.FrameTestCompareOptions()...); diff != "" {
|
||||||
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +1,31 @@
|
|||||||
import { map as _map, filter } from 'lodash';
|
import { map as _map } from 'lodash';
|
||||||
import { Observable, of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { catchError, map, mapTo } from 'rxjs/operators';
|
import { catchError, map, mapTo } from 'rxjs/operators';
|
||||||
import { getBackendSrv } from '@grafana/runtime';
|
import { BackendDataSourceResponse, DataSourceWithBackend, FetchResponse, getBackendSrv } from '@grafana/runtime';
|
||||||
import { ScopedVars } from '@grafana/data';
|
import { AnnotationEvent, DataSourceInstanceSettings, ScopedVars, MetricFindValue } from '@grafana/data';
|
||||||
|
|
||||||
import ResponseParser, { MssqlResponse } from './response_parser';
|
import ResponseParser from './response_parser';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
|
import { MssqlQueryForInterpolation, MssqlQuery, MssqlOptions } from './types';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||||
import { MssqlQueryForInterpolation } from './types';
|
|
||||||
|
|
||||||
export class MssqlDatasource {
|
export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOptions> {
|
||||||
id: any;
|
id: any;
|
||||||
name: any;
|
name: any;
|
||||||
responseParser: ResponseParser;
|
responseParser: ResponseParser;
|
||||||
interval: string;
|
interval: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: any,
|
instanceSettings: DataSourceInstanceSettings<MssqlOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
private readonly timeSrv: TimeSrv = getTimeSrv()
|
||||||
) {
|
) {
|
||||||
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
this.id = instanceSettings.id;
|
this.id = instanceSettings.id;
|
||||||
this.responseParser = new ResponseParser();
|
this.responseParser = new ResponseParser();
|
||||||
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
|
const settingsData = instanceSettings.jsonData || ({} as MssqlOptions);
|
||||||
|
this.interval = settingsData.timeInterval || '1m';
|
||||||
}
|
}
|
||||||
|
|
||||||
interpolateVariable(value: any, variable: any) {
|
interpolateVariable(value: any, variable: any) {
|
||||||
@ -68,38 +70,16 @@ export class MssqlDatasource {
|
|||||||
return expandedQueries;
|
return expandedQueries;
|
||||||
}
|
}
|
||||||
|
|
||||||
query(options: any): Observable<MssqlResponse> {
|
applyTemplateVariables(target: MssqlQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||||
const queries = filter(options.targets, (item) => {
|
return {
|
||||||
return item.hide !== true;
|
refId: target.refId,
|
||||||
}).map((item) => {
|
datasourceId: this.id,
|
||||||
return {
|
rawSql: this.templateSrv.replace(target.rawSql, scopedVars, this.interpolateVariable),
|
||||||
refId: item.refId,
|
format: target.format,
|
||||||
intervalMs: options.intervalMs,
|
};
|
||||||
maxDataPoints: options.maxDataPoints,
|
|
||||||
datasourceId: this.id,
|
|
||||||
rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
|
|
||||||
format: item.format,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (queries.length === 0) {
|
|
||||||
return of({ data: [] });
|
|
||||||
}
|
|
||||||
|
|
||||||
return getBackendSrv()
|
|
||||||
.fetch({
|
|
||||||
url: '/api/tsdb/query',
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
from: options.range.from.valueOf().toString(),
|
|
||||||
to: options.range.to.valueOf().toString(),
|
|
||||||
queries: queries,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.pipe(map(this.responseParser.processQueryResult));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
annotationQuery(options: any) {
|
async annotationQuery(options: any): Promise<AnnotationEvent[]> {
|
||||||
if (!options.annotation.rawQuery) {
|
if (!options.annotation.rawQuery) {
|
||||||
return Promise.reject({ message: 'Query missing in annotation definition' });
|
return Promise.reject({ message: 'Query missing in annotation definition' });
|
||||||
}
|
}
|
||||||
@ -112,25 +92,33 @@ export class MssqlDatasource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: options.range.from.valueOf().toString(),
|
from: options.range.from.valueOf().toString(),
|
||||||
to: options.range.to.valueOf().toString(),
|
to: options.range.to.valueOf().toString(),
|
||||||
queries: [query],
|
queries: [query],
|
||||||
},
|
},
|
||||||
|
requestId: options.annotation.name,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.transformAnnotationResponse(options, data)))
|
.pipe(
|
||||||
|
map(
|
||||||
|
async (res: FetchResponse<BackendDataSourceResponse>) =>
|
||||||
|
await this.responseParser.transformAnnotationResponse(options, res.data)
|
||||||
|
)
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
metricFindQuery(query: string, optionalOptions: { variable: { name: string } }) {
|
metricFindQuery(query: string, optionalOptions: any): Promise<MetricFindValue[]> {
|
||||||
let refId = 'tempvar';
|
let refId = 'tempvar';
|
||||||
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
||||||
refId = optionalOptions.variable.name;
|
refId = optionalOptions.variable.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const range = this.timeSrv.timeRange();
|
||||||
|
|
||||||
const interpolatedQuery = {
|
const interpolatedQuery = {
|
||||||
refId: refId,
|
refId: refId,
|
||||||
datasourceId: this.id,
|
datasourceId: this.id,
|
||||||
@ -138,27 +126,29 @@ export class MssqlDatasource {
|
|||||||
format: 'table',
|
format: 'table',
|
||||||
};
|
};
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
|
||||||
const data = {
|
|
||||||
queries: [interpolatedQuery],
|
|
||||||
from: range.from.valueOf().toString(),
|
|
||||||
to: range.to.valueOf().toString(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: data,
|
data: {
|
||||||
|
from: range.from.valueOf().toString(),
|
||||||
|
to: range.to.valueOf().toString(),
|
||||||
|
queries: [interpolatedQuery],
|
||||||
|
},
|
||||||
|
requestId: refId,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data)))
|
.pipe(
|
||||||
|
map((rsp) => {
|
||||||
|
return this.responseParser.transformMetricFindResponse(rsp);
|
||||||
|
})
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
testDatasource() {
|
testDatasource(): Promise<any> {
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: '5m',
|
from: '5m',
|
||||||
@ -189,8 +179,8 @@ export class MssqlDatasource {
|
|||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
targetContainsTemplate(target: any) {
|
targetContainsTemplate(query: MssqlQuery): boolean {
|
||||||
const rawSql = target.rawSql.replace('$__', '');
|
const rawSql = query.rawSql.replace('$__', '');
|
||||||
return this.templateSrv.variableExists(rawSql);
|
return this.templateSrv.variableExists(rawSql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { MssqlDatasource } from './datasource';
|
import { MssqlDatasource } from './datasource';
|
||||||
import { MssqlQueryCtrl } from './query_ctrl';
|
import { MssqlQueryCtrl } from './query_ctrl';
|
||||||
import { MssqlConfigCtrl } from './config_ctrl';
|
import { MssqlConfigCtrl } from './config_ctrl';
|
||||||
|
import { MssqlQuery } from './types';
|
||||||
|
import { DataSourcePlugin } from '@grafana/data';
|
||||||
|
|
||||||
const defaultQuery = `SELECT
|
const defaultQuery = `SELECT
|
||||||
<time_column> as time,
|
<time_column> as time,
|
||||||
@ -16,18 +18,16 @@ const defaultQuery = `SELECT
|
|||||||
class MssqlAnnotationsQueryCtrl {
|
class MssqlAnnotationsQueryCtrl {
|
||||||
static templateUrl = 'partials/annotations.editor.html';
|
static templateUrl = 'partials/annotations.editor.html';
|
||||||
|
|
||||||
annotation: any;
|
declare annotation: any;
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor() {
|
constructor($scope: any) {
|
||||||
|
this.annotation = $scope.ctrl.annotation;
|
||||||
this.annotation.rawQuery = this.annotation.rawQuery || defaultQuery;
|
this.annotation.rawQuery = this.annotation.rawQuery || defaultQuery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export const plugin = new DataSourcePlugin<MssqlDatasource, MssqlQuery>(MssqlDatasource)
|
||||||
MssqlDatasource,
|
.setQueryCtrl(MssqlQueryCtrl)
|
||||||
MssqlDatasource as Datasource,
|
.setConfigCtrl(MssqlConfigCtrl)
|
||||||
MssqlQueryCtrl as QueryCtrl,
|
.setAnnotationQueryCtrl(MssqlAnnotationsQueryCtrl);
|
||||||
MssqlConfigCtrl as ConfigCtrl,
|
|
||||||
MssqlAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
|
||||||
};
|
|
||||||
|
@ -1,13 +1,7 @@
|
|||||||
import { QueryCtrl } from 'app/plugins/sdk';
|
import { QueryCtrl } from 'app/plugins/sdk';
|
||||||
import { auto } from 'angular';
|
import { auto } from 'angular';
|
||||||
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
||||||
|
import { MssqlQuery } from './types';
|
||||||
export interface MssqlQuery {
|
|
||||||
refId: string;
|
|
||||||
format: string;
|
|
||||||
alias: string;
|
|
||||||
rawSql: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultQuery = `SELECT
|
const defaultQuery = `SELECT
|
||||||
$__timeEpoch(<time_column>),
|
$__timeEpoch(<time_column>),
|
||||||
|
@ -1,73 +1,42 @@
|
|||||||
import { map } from 'lodash';
|
import { map } from 'lodash';
|
||||||
import { MetricFindValue } from '@grafana/data';
|
import { AnnotationEvent, DataFrame, FieldType, MetricFindValue } from '@grafana/data';
|
||||||
|
import { BackendDataSourceResponse, toDataQueryResponse, FetchResponse } from '@grafana/runtime';
|
||||||
interface TableResponse extends Record<string, any> {
|
|
||||||
type: string;
|
|
||||||
refId: string;
|
|
||||||
meta: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SeriesResponse extends Record<string, any> {
|
|
||||||
target: string;
|
|
||||||
refId: string;
|
|
||||||
meta: any;
|
|
||||||
datapoints: [any[]];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MssqlResponse {
|
|
||||||
data: Array<TableResponse | SeriesResponse>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class ResponseParser {
|
export default class ResponseParser {
|
||||||
processQueryResult(res: any): MssqlResponse {
|
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
|
||||||
const data: any[] = [];
|
const frames = toDataQueryResponse(raw).data as DataFrame[];
|
||||||
|
|
||||||
if (!res.data.results) {
|
if (!frames || !frames.length) {
|
||||||
return { data };
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in res.data.results) {
|
|
||||||
const queryRes = res.data.results[key];
|
|
||||||
|
|
||||||
if (queryRes.series) {
|
|
||||||
for (const series of queryRes.series) {
|
|
||||||
data.push({
|
|
||||||
target: series.name,
|
|
||||||
datapoints: series.points,
|
|
||||||
refId: queryRes.refId,
|
|
||||||
meta: queryRes.meta,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queryRes.tables) {
|
|
||||||
for (const table of queryRes.tables) {
|
|
||||||
table.type = 'table';
|
|
||||||
table.refId = queryRes.refId;
|
|
||||||
table.meta = queryRes.meta;
|
|
||||||
data.push(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { data: data };
|
|
||||||
}
|
|
||||||
|
|
||||||
parseMetricFindQueryResult(refId: string, results: any): MetricFindValue[] {
|
|
||||||
if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) {
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const columns = results.data.results[refId].tables[0].columns;
|
const frame = frames[0];
|
||||||
const rows = results.data.results[refId].tables[0].rows;
|
|
||||||
const textColIndex = this.findColIndex(columns, '__text');
|
|
||||||
const valueColIndex = this.findColIndex(columns, '__value');
|
|
||||||
|
|
||||||
if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) {
|
const values: MetricFindValue[] = [];
|
||||||
return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
|
const textField = frame.fields.find((f) => f.name === '__text');
|
||||||
|
const valueField = frame.fields.find((f) => f.name === '__value');
|
||||||
|
|
||||||
|
if (textField && valueField) {
|
||||||
|
for (let i = 0; i < textField.values.length; i++) {
|
||||||
|
values.push({ text: '' + textField.values.get(i), value: '' + valueField.values.get(i) });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const textFields = frame.fields.filter((f) => f.type === FieldType.string);
|
||||||
|
if (textFields) {
|
||||||
|
values.push(
|
||||||
|
...textFields
|
||||||
|
.flatMap((f) => f.values.toArray())
|
||||||
|
.map((v) => ({
|
||||||
|
text: '' + v,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.transformToSimpleList(rows);
|
return Array.from(new Set(values.map((v) => v.text))).map((text) => ({
|
||||||
|
text,
|
||||||
|
value: values.find((v) => v.text === text)?.value,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number): MetricFindValue[] {
|
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number): MetricFindValue[] {
|
||||||
@ -117,41 +86,34 @@ export default class ResponseParser {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
transformAnnotationResponse(options: any, data: any) {
|
async transformAnnotationResponse(options: any, data: BackendDataSourceResponse): Promise<AnnotationEvent[]> {
|
||||||
const table = data.data.results[options.annotation.name].tables[0];
|
const frames = toDataQueryResponse({ data: data }).data as DataFrame[];
|
||||||
|
const frame = frames[0];
|
||||||
|
const timeField = frame.fields.find((f) => f.name === 'time');
|
||||||
|
|
||||||
let timeColumnIndex = -1;
|
if (!timeField) {
|
||||||
let timeEndColumnIndex = -1;
|
|
||||||
let textColumnIndex = -1;
|
|
||||||
let tagsColumnIndex = -1;
|
|
||||||
|
|
||||||
for (let i = 0; i < table.columns.length; i++) {
|
|
||||||
if (table.columns[i].text === 'time') {
|
|
||||||
timeColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'timeend') {
|
|
||||||
timeEndColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'text') {
|
|
||||||
textColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'tags') {
|
|
||||||
tagsColumnIndex = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timeColumnIndex === -1) {
|
|
||||||
return Promise.reject({ message: 'Missing mandatory time column (with time column alias) in annotation query.' });
|
return Promise.reject({ message: 'Missing mandatory time column (with time column alias) in annotation query.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const list = [];
|
const timeEndField = frame.fields.find((f) => f.name === 'timeend');
|
||||||
for (let i = 0; i < table.rows.length; i++) {
|
const textField = frame.fields.find((f) => f.name === 'text');
|
||||||
const row = table.rows[i];
|
const tagsField = frame.fields.find((f) => f.name === 'tags');
|
||||||
const timeEnd =
|
|
||||||
timeEndColumnIndex !== -1 && row[timeEndColumnIndex] ? Math.floor(row[timeEndColumnIndex]) : undefined;
|
const list: AnnotationEvent[] = [];
|
||||||
|
for (let i = 0; i < frame.length; i++) {
|
||||||
|
const timeEnd = timeEndField && timeEndField.values.get(i) ? Math.floor(timeEndField.values.get(i)) : undefined;
|
||||||
list.push({
|
list.push({
|
||||||
annotation: options.annotation,
|
annotation: options.annotation,
|
||||||
time: Math.floor(row[timeColumnIndex]),
|
time: Math.floor(timeField.values.get(i)),
|
||||||
timeEnd,
|
timeEnd,
|
||||||
text: row[textColumnIndex],
|
text: textField && textField.values.get(i) ? textField.values.get(i) : '',
|
||||||
tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
|
tags:
|
||||||
|
tagsField && tagsField.values.get(i)
|
||||||
|
? tagsField.values
|
||||||
|
.get(i)
|
||||||
|
.trim()
|
||||||
|
.split(/\s*,\s*/)
|
||||||
|
: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import { of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { dateTime } from '@grafana/data';
|
import { dataFrameToJSON, dateTime, MetricFindValue, MutableDataFrame } from '@grafana/data';
|
||||||
|
|
||||||
import { MssqlDatasource } from '../datasource';
|
import { MssqlDatasource } from '../datasource';
|
||||||
import { TimeSrvStub } from 'test/specs/helpers';
|
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { backendSrv } from 'app/core/services/backend_srv';
|
import { backendSrv } from 'app/core/services/backend_srv';
|
||||||
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
||||||
import { createFetchResponse } from 'test/helpers/createFetchResponse';
|
import { createFetchResponse } from 'test/helpers/createFetchResponse';
|
||||||
|
import { TimeSrvStub } from 'test/specs/helpers';
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
||||||
@ -47,16 +47,16 @@ describe('MSSQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
MyAnno: {
|
MyAnno: {
|
||||||
refId: annotationName,
|
frames: [
|
||||||
tables: [
|
dataFrameToJSON(
|
||||||
{
|
new MutableDataFrame({
|
||||||
columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
|
fields: [
|
||||||
rows: [
|
{ name: 'time', values: [1521545610656, 1521546251185, 1521546501378] },
|
||||||
[1521545610656, 'some text', 'TagA,TagB'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
[1521546251185, 'some text2', ' TagB , TagC'],
|
{ name: 'tags', values: ['TagA,TagB', ' TagB , TagC', null] },
|
||||||
[1521546501378, 'some text3'],
|
],
|
||||||
],
|
})
|
||||||
},
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -85,24 +85,20 @@ describe('MSSQLDatasource', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing metricFindQuery', () => {
|
describe('When performing metricFindQuery', () => {
|
||||||
let results: any;
|
let results: MetricFindValue[];
|
||||||
const query = 'select * from atable';
|
const query = 'select * from atable';
|
||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
frames: [
|
||||||
rowCount: 3,
|
dataFrameToJSON(
|
||||||
},
|
new MutableDataFrame({
|
||||||
refId: 'tempvar',
|
fields: [
|
||||||
tables: [
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
{
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
],
|
||||||
rows: [
|
})
|
||||||
['aTitle', 'some text'],
|
),
|
||||||
['aTitle2', 'some text2'],
|
|
||||||
['aTitle3', 'some text3'],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -111,7 +107,7 @@ describe('MSSQLDatasource', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
||||||
|
|
||||||
return ctx.ds.metricFindQuery(query).then((data: any) => {
|
return ctx.ds.metricFindQuery(query).then((data: MetricFindValue[]) => {
|
||||||
results = data;
|
results = data;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -129,19 +125,15 @@ describe('MSSQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
frames: [
|
||||||
rowCount: 3,
|
dataFrameToJSON(
|
||||||
},
|
new MutableDataFrame({
|
||||||
refId: 'tempvar',
|
fields: [
|
||||||
tables: [
|
{ name: '__value', values: ['value1', 'value2', 'value3'] },
|
||||||
{
|
{ name: '__text', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
columns: [{ text: '__value' }, { text: '__text' }],
|
],
|
||||||
rows: [
|
})
|
||||||
['value1', 'aTitle'],
|
),
|
||||||
['value2', 'aTitle2'],
|
|
||||||
['value3', 'aTitle3'],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -170,19 +162,15 @@ describe('MSSQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
frames: [
|
||||||
rowCount: 3,
|
dataFrameToJSON(
|
||||||
},
|
new MutableDataFrame({
|
||||||
refId: 'tempvar',
|
fields: [
|
||||||
tables: [
|
{ name: '__text', values: ['aTitle', 'aTitle', 'aTitle'] },
|
||||||
{
|
{ name: '__value', values: ['same', 'same', 'diff'] },
|
||||||
columns: [{ text: '__text' }, { text: '__value' }],
|
],
|
||||||
rows: [
|
})
|
||||||
['aTitle', 'same'],
|
),
|
||||||
['aTitle', 'same'],
|
|
||||||
['aTitle', 'diff'],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -207,15 +195,12 @@ describe('MSSQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
frames: [
|
||||||
rowCount: 1,
|
dataFrameToJSON(
|
||||||
},
|
new MutableDataFrame({
|
||||||
refId: 'tempvar',
|
fields: [{ name: 'test', values: ['aTitle'] }],
|
||||||
tables: [
|
})
|
||||||
{
|
),
|
||||||
columns: [{ text: 'title' }],
|
|
||||||
rows: [['aTitle']],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -227,10 +212,9 @@ describe('MSSQLDatasource', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.timeSrv.setTime(time);
|
ctx.timeSrv.setTime(time);
|
||||||
|
|
||||||
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
fetchMock.mockImplementation(() => of(createFetchResponse(response)));
|
||||||
|
|
||||||
return ctx.ds.metricFindQuery(query);
|
return ctx.ds.metricFindQuery(query, { range: time });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass timerange to datasourceRequest', () => {
|
it('should pass timerange to datasourceRequest', () => {
|
||||||
|
@ -1,7 +1,21 @@
|
|||||||
|
import { DataQuery, DataSourceJsonData } from '@grafana/data';
|
||||||
|
|
||||||
export interface MssqlQueryForInterpolation {
|
export interface MssqlQueryForInterpolation {
|
||||||
alias?: any;
|
alias?: any;
|
||||||
format?: any;
|
format?: any;
|
||||||
rawSql?: any;
|
rawSql?: any;
|
||||||
refId?: any;
|
refId: any;
|
||||||
hide?: any;
|
hide?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ResultFormat = 'time_series' | 'table';
|
||||||
|
|
||||||
|
export interface MssqlQuery extends DataQuery {
|
||||||
|
alias?: string;
|
||||||
|
format?: ResultFormat;
|
||||||
|
rawSql?: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MssqlOptions extends DataSourceJsonData {
|
||||||
|
timeInterval: string;
|
||||||
|
}
|
||||||
|
@ -1,32 +1,34 @@
|
|||||||
import { map as _map, filter } from 'lodash';
|
import { map as _map } from 'lodash';
|
||||||
import { Observable, of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { catchError, map, mapTo } from 'rxjs/operators';
|
import { catchError, map, mapTo } from 'rxjs/operators';
|
||||||
import { getBackendSrv } from '@grafana/runtime';
|
import { getBackendSrv, DataSourceWithBackend, FetchResponse, BackendDataSourceResponse } from '@grafana/runtime';
|
||||||
import { ScopedVars } from '@grafana/data';
|
import { DataSourceInstanceSettings, ScopedVars, MetricFindValue, AnnotationEvent } from '@grafana/data';
|
||||||
import MysqlQuery from 'app/plugins/datasource/mysql/mysql_query';
|
import MySQLQueryModel from 'app/plugins/datasource/mysql/mysql_query_model';
|
||||||
import ResponseParser, { MysqlResponse } from './response_parser';
|
import ResponseParser from './response_parser';
|
||||||
import { MysqlMetricFindValue, MysqlQueryForInterpolation } from './types';
|
import { MysqlQueryForInterpolation, MySQLOptions, MySQLQuery } from './types';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
||||||
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
||||||
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||||
|
|
||||||
export class MysqlDatasource {
|
export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOptions> {
|
||||||
id: any;
|
id: any;
|
||||||
name: any;
|
name: any;
|
||||||
responseParser: ResponseParser;
|
responseParser: ResponseParser;
|
||||||
queryModel: MysqlQuery;
|
queryModel: MySQLQueryModel;
|
||||||
interval: string;
|
interval: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: any,
|
instanceSettings: DataSourceInstanceSettings<MySQLOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
private readonly timeSrv: TimeSrv = getTimeSrv()
|
||||||
) {
|
) {
|
||||||
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
this.id = instanceSettings.id;
|
this.id = instanceSettings.id;
|
||||||
this.responseParser = new ResponseParser();
|
this.responseParser = new ResponseParser();
|
||||||
this.queryModel = new MysqlQuery({});
|
this.queryModel = new MySQLQueryModel({});
|
||||||
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
|
const settingsData = instanceSettings.jsonData || ({} as MySQLOptions);
|
||||||
|
this.interval = settingsData.timeInterval || '1m';
|
||||||
}
|
}
|
||||||
|
|
||||||
interpolateVariable = (value: string | string[] | number, variable: any) => {
|
interpolateVariable = (value: string | string[] | number, variable: any) => {
|
||||||
@ -68,40 +70,24 @@ export class MysqlDatasource {
|
|||||||
return expandedQueries;
|
return expandedQueries;
|
||||||
}
|
}
|
||||||
|
|
||||||
query(options: any): Observable<MysqlResponse> {
|
filterQuery(query: MySQLQuery): boolean {
|
||||||
const queries = filter(options.targets, (target) => {
|
if (query.hide) {
|
||||||
return target.hide !== true;
|
return false;
|
||||||
}).map((target) => {
|
|
||||||
const queryModel = new MysqlQuery(target, this.templateSrv, options.scopedVars);
|
|
||||||
|
|
||||||
return {
|
|
||||||
refId: target.refId,
|
|
||||||
intervalMs: options.intervalMs,
|
|
||||||
maxDataPoints: options.maxDataPoints,
|
|
||||||
datasourceId: this.id,
|
|
||||||
rawSql: queryModel.render(this.interpolateVariable as any),
|
|
||||||
format: target.format,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (queries.length === 0) {
|
|
||||||
return of({ data: [] });
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
return getBackendSrv()
|
|
||||||
.fetch({
|
|
||||||
url: '/api/tsdb/query',
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
from: options.range.from.valueOf().toString(),
|
|
||||||
to: options.range.to.valueOf().toString(),
|
|
||||||
queries: queries,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.pipe(map(this.responseParser.processQueryResult));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
annotationQuery(options: any) {
|
applyTemplateVariables(target: MySQLQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||||
|
const queryModel = new MySQLQueryModel(target, this.templateSrv, scopedVars);
|
||||||
|
return {
|
||||||
|
refId: target.refId,
|
||||||
|
datasourceId: this.id,
|
||||||
|
rawSql: queryModel.render(this.interpolateVariable as any),
|
||||||
|
format: target.format,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async annotationQuery(options: any): Promise<AnnotationEvent[]> {
|
||||||
if (!options.annotation.rawQuery) {
|
if (!options.annotation.rawQuery) {
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
message: 'Query missing in annotation definition',
|
message: 'Query missing in annotation definition',
|
||||||
@ -116,20 +102,26 @@ export class MysqlDatasource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: options.range.from.valueOf().toString(),
|
from: options.range.from.valueOf().toString(),
|
||||||
to: options.range.to.valueOf().toString(),
|
to: options.range.to.valueOf().toString(),
|
||||||
queries: [query],
|
queries: [query],
|
||||||
},
|
},
|
||||||
|
requestId: options.annotation.name,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.transformAnnotationResponse(options, data)))
|
.pipe(
|
||||||
|
map(
|
||||||
|
async (res: FetchResponse<BackendDataSourceResponse>) =>
|
||||||
|
await this.responseParser.transformAnnotationResponse(options, res.data)
|
||||||
|
)
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
metricFindQuery(query: string, optionalOptions: any): Promise<MysqlMetricFindValue[]> {
|
metricFindQuery(query: string, optionalOptions: any): Promise<MetricFindValue[]> {
|
||||||
let refId = 'tempvar';
|
let refId = 'tempvar';
|
||||||
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
||||||
refId = optionalOptions.variable.name;
|
refId = optionalOptions.variable.name;
|
||||||
@ -149,33 +141,30 @@ export class MysqlDatasource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
const range = this.timeSrv.timeRange();
|
||||||
const data = {
|
|
||||||
queries: [interpolatedQuery],
|
|
||||||
from: range.from.valueOf().toString(),
|
|
||||||
to: range.to.valueOf().toString(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
|
|
||||||
data['from'] = optionalOptions.range.from.valueOf().toString();
|
|
||||||
}
|
|
||||||
if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
|
|
||||||
data['to'] = optionalOptions.range.to.valueOf().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: data,
|
data: {
|
||||||
|
from: range.from.valueOf().toString(),
|
||||||
|
to: range.to.valueOf().toString(),
|
||||||
|
queries: [interpolatedQuery],
|
||||||
|
},
|
||||||
|
requestId: refId,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data)))
|
.pipe(
|
||||||
|
map((rsp) => {
|
||||||
|
return this.responseParser.transformMetricFindResponse(rsp);
|
||||||
|
})
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
testDatasource() {
|
testDatasource(): Promise<any> {
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: '5m',
|
from: '5m',
|
||||||
@ -212,7 +201,7 @@ export class MysqlDatasource {
|
|||||||
if (target.rawQuery) {
|
if (target.rawQuery) {
|
||||||
rawSql = target.rawSql;
|
rawSql = target.rawSql;
|
||||||
} else {
|
} else {
|
||||||
const query = new MysqlQuery(target);
|
const query = new MySQLQueryModel(target);
|
||||||
rawSql = query.buildQuery();
|
rawSql = query.buildQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@ import {
|
|||||||
createResetHandler,
|
createResetHandler,
|
||||||
PasswordFieldEnum,
|
PasswordFieldEnum,
|
||||||
} from '../../../features/datasources/utils/passwordHandlers';
|
} from '../../../features/datasources/utils/passwordHandlers';
|
||||||
|
import { MySQLQuery } from './types';
|
||||||
|
import { DataSourcePlugin } from '@grafana/data';
|
||||||
|
|
||||||
class MysqlConfigCtrl {
|
class MysqlConfigCtrl {
|
||||||
static templateUrl = 'partials/config.html';
|
static templateUrl = 'partials/config.html';
|
||||||
@ -31,10 +33,11 @@ const defaultQuery = `SELECT
|
|||||||
class MysqlAnnotationsQueryCtrl {
|
class MysqlAnnotationsQueryCtrl {
|
||||||
static templateUrl = 'partials/annotations.editor.html';
|
static templateUrl = 'partials/annotations.editor.html';
|
||||||
|
|
||||||
annotation: any;
|
declare annotation: any;
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor() {
|
constructor($scope: any) {
|
||||||
|
this.annotation = $scope.ctrl.annotation;
|
||||||
this.annotation.rawQuery = this.annotation.rawQuery || defaultQuery;
|
this.annotation.rawQuery = this.annotation.rawQuery || defaultQuery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,3 +49,8 @@ export {
|
|||||||
MysqlConfigCtrl as ConfigCtrl,
|
MysqlConfigCtrl as ConfigCtrl,
|
||||||
MysqlAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
MysqlAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const plugin = new DataSourcePlugin<MysqlDatasource, MySQLQuery>(MysqlDatasource)
|
||||||
|
.setQueryCtrl(MysqlQueryCtrl)
|
||||||
|
.setConfigCtrl(MysqlConfigCtrl)
|
||||||
|
.setAnnotationQueryCtrl(MysqlAnnotationsQueryCtrl);
|
||||||
|
@ -2,7 +2,7 @@ import { find, map } from 'lodash';
|
|||||||
import { TemplateSrv } from '@grafana/runtime';
|
import { TemplateSrv } from '@grafana/runtime';
|
||||||
import { ScopedVars } from '@grafana/data';
|
import { ScopedVars } from '@grafana/data';
|
||||||
|
|
||||||
export default class MysqlQuery {
|
export default class MySQLQueryModel {
|
||||||
target: any;
|
target: any;
|
||||||
templateSrv: any;
|
templateSrv: any;
|
||||||
scopedVars: any;
|
scopedVars: any;
|
@ -3,7 +3,7 @@ import appEvents from 'app/core/app_events';
|
|||||||
import { MysqlMetaQuery } from './meta_query';
|
import { MysqlMetaQuery } from './meta_query';
|
||||||
import { QueryCtrl } from 'app/plugins/sdk';
|
import { QueryCtrl } from 'app/plugins/sdk';
|
||||||
import { SqlPart } from 'app/core/components/sql_part/sql_part';
|
import { SqlPart } from 'app/core/components/sql_part/sql_part';
|
||||||
import MysqlQuery from './mysql_query';
|
import MySQLQueryModel from './mysql_query_model';
|
||||||
import sqlPart from './sql_part';
|
import sqlPart from './sql_part';
|
||||||
import { auto } from 'angular';
|
import { auto } from 'angular';
|
||||||
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
||||||
@ -27,7 +27,7 @@ export class MysqlQueryCtrl extends QueryCtrl {
|
|||||||
lastQueryError?: string;
|
lastQueryError?: string;
|
||||||
showHelp!: boolean;
|
showHelp!: boolean;
|
||||||
|
|
||||||
queryModel: MysqlQuery;
|
queryModel: MySQLQueryModel;
|
||||||
metaBuilder: MysqlMetaQuery;
|
metaBuilder: MysqlMetaQuery;
|
||||||
lastQueryMeta?: QueryResultMeta;
|
lastQueryMeta?: QueryResultMeta;
|
||||||
tableSegment: any;
|
tableSegment: any;
|
||||||
@ -50,7 +50,7 @@ export class MysqlQueryCtrl extends QueryCtrl {
|
|||||||
super($scope, $injector);
|
super($scope, $injector);
|
||||||
|
|
||||||
this.target = this.target;
|
this.target = this.target;
|
||||||
this.queryModel = new MysqlQuery(this.target, templateSrv, this.panel.scopedVars);
|
this.queryModel = new MySQLQueryModel(this.target, templateSrv, this.panel.scopedVars);
|
||||||
this.metaBuilder = new MysqlMetaQuery(this.target, this.queryModel);
|
this.metaBuilder = new MysqlMetaQuery(this.target, this.queryModel);
|
||||||
this.updateProjection();
|
this.updateProjection();
|
||||||
|
|
||||||
|
@ -1,91 +1,57 @@
|
|||||||
import { map } from 'lodash';
|
import { map } from 'lodash';
|
||||||
import { MysqlMetricFindValue } from './types';
|
import { AnnotationEvent, DataFrame, FieldType, MetricFindValue } from '@grafana/data';
|
||||||
|
import { BackendDataSourceResponse, FetchResponse, toDataQueryResponse } from '@grafana/runtime';
|
||||||
interface TableResponse extends Record<string, any> {
|
|
||||||
type: string;
|
|
||||||
refId: string;
|
|
||||||
meta: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SeriesResponse extends Record<string, any> {
|
|
||||||
target: string;
|
|
||||||
refId: string;
|
|
||||||
meta: any;
|
|
||||||
datapoints: [any[]];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MysqlResponse {
|
|
||||||
data: Array<TableResponse | SeriesResponse>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class ResponseParser {
|
export default class ResponseParser {
|
||||||
processQueryResult(res: any): MysqlResponse {
|
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
|
||||||
const data: any[] = [];
|
const frames = toDataQueryResponse(raw).data as DataFrame[];
|
||||||
|
|
||||||
if (!res.data.results) {
|
if (!frames || !frames.length) {
|
||||||
return { data: data };
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in res.data.results) {
|
|
||||||
const queryRes = res.data.results[key];
|
|
||||||
|
|
||||||
if (queryRes.series) {
|
|
||||||
for (const series of queryRes.series) {
|
|
||||||
data.push({
|
|
||||||
target: series.name,
|
|
||||||
datapoints: series.points,
|
|
||||||
refId: queryRes.refId,
|
|
||||||
meta: queryRes.meta,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queryRes.tables) {
|
|
||||||
for (const table of queryRes.tables) {
|
|
||||||
table.type = 'table';
|
|
||||||
table.refId = queryRes.refId;
|
|
||||||
table.meta = queryRes.meta;
|
|
||||||
data.push(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { data: data };
|
|
||||||
}
|
|
||||||
|
|
||||||
parseMetricFindQueryResult(refId: string, results: any): MysqlMetricFindValue[] {
|
|
||||||
if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) {
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const columns = results.data.results[refId].tables[0].columns;
|
const frame = frames[0];
|
||||||
const rows = results.data.results[refId].tables[0].rows;
|
|
||||||
const textColIndex = this.findColIndex(columns, '__text');
|
|
||||||
const valueColIndex = this.findColIndex(columns, '__value');
|
|
||||||
|
|
||||||
if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) {
|
const values: MetricFindValue[] = [];
|
||||||
return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
|
const textField = frame.fields.find((f) => f.name === '__text');
|
||||||
|
const valueField = frame.fields.find((f) => f.name === '__value');
|
||||||
|
|
||||||
|
if (textField && valueField) {
|
||||||
|
for (let i = 0; i < textField.values.length; i++) {
|
||||||
|
values.push({ text: '' + textField.values.get(i), value: '' + valueField.values.get(i) });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const textFields = frame.fields.filter((f) => f.type === FieldType.string);
|
||||||
|
if (textFields) {
|
||||||
|
values.push(
|
||||||
|
...textFields
|
||||||
|
.flatMap((f) => f.values.toArray())
|
||||||
|
.map((v) => ({
|
||||||
|
text: '' + v,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.transformToSimpleList(rows);
|
return Array.from(new Set(values.map((v) => v.text))).map((text) => ({
|
||||||
|
text,
|
||||||
|
value: values.find((v) => v.text === text)?.value,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
|
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number): MetricFindValue[] {
|
||||||
const res = [];
|
const res = [];
|
||||||
|
|
||||||
for (let i = 0; i < rows.length; i++) {
|
for (let i = 0; i < rows.length; i++) {
|
||||||
if (!this.containsKey(res, rows[i][textColIndex])) {
|
if (!this.containsKey(res, rows[i][textColIndex])) {
|
||||||
res.push({
|
res.push({ text: rows[i][textColIndex], value: rows[i][valueColIndex] });
|
||||||
text: rows[i][textColIndex],
|
|
||||||
value: rows[i][valueColIndex],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
transformToSimpleList(rows: any) {
|
transformToSimpleList(rows: any): MetricFindValue[] {
|
||||||
const res = [];
|
const res = [];
|
||||||
|
|
||||||
for (let i = 0; i < rows.length; i++) {
|
for (let i = 0; i < rows.length; i++) {
|
||||||
@ -120,47 +86,38 @@ export default class ResponseParser {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
transformAnnotationResponse(options: any, data: any) {
|
async transformAnnotationResponse(options: any, data: BackendDataSourceResponse): Promise<AnnotationEvent[]> {
|
||||||
const table = data.data.results[options.annotation.name].tables[0];
|
const frames = toDataQueryResponse({ data: data }).data as DataFrame[];
|
||||||
|
const frame = frames[0];
|
||||||
|
const timeField = frame.fields.find((f) => f.name === 'time' || f.name === 'time_sec');
|
||||||
|
|
||||||
let timeColumnIndex = -1;
|
if (!timeField) {
|
||||||
let timeEndColumnIndex = -1;
|
throw new Error('Missing mandatory time column (with time column alias) in annotation query');
|
||||||
let textColumnIndex = -1;
|
|
||||||
let tagsColumnIndex = -1;
|
|
||||||
|
|
||||||
for (let i = 0; i < table.columns.length; i++) {
|
|
||||||
if (table.columns[i].text === 'time_sec' || table.columns[i].text === 'time') {
|
|
||||||
timeColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'timeend') {
|
|
||||||
timeEndColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'title') {
|
|
||||||
throw {
|
|
||||||
message: 'The title column for annotations is deprecated, now only a column named text is returned',
|
|
||||||
};
|
|
||||||
} else if (table.columns[i].text === 'text') {
|
|
||||||
textColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'tags') {
|
|
||||||
tagsColumnIndex = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeColumnIndex === -1) {
|
if (frame.fields.find((f) => f.name === 'title')) {
|
||||||
throw {
|
throw new Error('The title column for annotations is deprecated, now only a column named text is returned');
|
||||||
message: 'Missing mandatory time column (with time_sec column alias) in annotation query.',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const list = [];
|
const timeEndField = frame.fields.find((f) => f.name === 'timeend');
|
||||||
for (let i = 0; i < table.rows.length; i++) {
|
const textField = frame.fields.find((f) => f.name === 'text');
|
||||||
const row = table.rows[i];
|
const tagsField = frame.fields.find((f) => f.name === 'tags');
|
||||||
const timeEnd =
|
|
||||||
timeEndColumnIndex !== -1 && row[timeEndColumnIndex] ? Math.floor(row[timeEndColumnIndex]) : undefined;
|
const list: AnnotationEvent[] = [];
|
||||||
|
for (let i = 0; i < frame.length; i++) {
|
||||||
|
const timeEnd = timeEndField && timeEndField.values.get(i) ? Math.floor(timeEndField.values.get(i)) : undefined;
|
||||||
list.push({
|
list.push({
|
||||||
annotation: options.annotation,
|
annotation: options.annotation,
|
||||||
time: Math.floor(row[timeColumnIndex]),
|
time: Math.floor(timeField.values.get(i)),
|
||||||
timeEnd,
|
timeEnd,
|
||||||
text: row[textColumnIndex] ? row[textColumnIndex].toString() : '',
|
text: textField && textField.values.get(i) ? textField.values.get(i) : '',
|
||||||
tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
|
tags:
|
||||||
|
tagsField && tagsField.values.get(i)
|
||||||
|
? tagsField.values
|
||||||
|
.get(i)
|
||||||
|
.trim()
|
||||||
|
.split(/\s*,\s*/)
|
||||||
|
: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,22 +1,32 @@
|
|||||||
import { of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { dateTime, toUtc } from '@grafana/data';
|
import {
|
||||||
|
dataFrameToJSON,
|
||||||
|
DataQueryRequest,
|
||||||
|
DataSourceInstanceSettings,
|
||||||
|
dateTime,
|
||||||
|
MutableDataFrame,
|
||||||
|
toUtc,
|
||||||
|
} from '@grafana/data';
|
||||||
|
|
||||||
import { MysqlDatasource } from '../datasource';
|
import { MysqlDatasource } from '../datasource';
|
||||||
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
||||||
import { FetchResponse } from '@grafana/runtime';
|
import { FetchResponse, setBackendSrv } from '@grafana/runtime';
|
||||||
|
import { MySQLOptions, MySQLQuery } from './../types';
|
||||||
jest.mock('@grafana/runtime', () => ({
|
|
||||||
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
|
||||||
getBackendSrv: () => backendSrv,
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe('MySQLDatasource', () => {
|
describe('MySQLDatasource', () => {
|
||||||
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
|
||||||
const setupTextContext = (response: any) => {
|
const setupTextContext = (response: any) => {
|
||||||
const instanceSettings = { name: 'mysql' };
|
jest.clearAllMocks();
|
||||||
|
setBackendSrv(backendSrv);
|
||||||
|
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
||||||
|
const instanceSettings = ({
|
||||||
|
jsonData: {
|
||||||
|
defaultProject: 'testproject',
|
||||||
|
},
|
||||||
|
} as unknown) as DataSourceInstanceSettings<MySQLOptions>;
|
||||||
const templateSrv: TemplateSrv = new TemplateSrv();
|
const templateSrv: TemplateSrv = new TemplateSrv();
|
||||||
|
const variable = { ...initialCustomVariableModelState };
|
||||||
const raw = {
|
const raw = {
|
||||||
from: toUtc('2018-04-25 10:00'),
|
from: toUtc('2018-04-25 10:00'),
|
||||||
to: toUtc('2018-04-25 11:00'),
|
to: toUtc('2018-04-25 11:00'),
|
||||||
@ -28,19 +38,44 @@ describe('MySQLDatasource', () => {
|
|||||||
raw: raw,
|
raw: raw,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
const variable = { ...initialCustomVariableModelState };
|
|
||||||
|
|
||||||
jest.clearAllMocks();
|
|
||||||
fetchMock.mockImplementation((options) => of(createFetchResponse(response)));
|
fetchMock.mockImplementation((options) => of(createFetchResponse(response)));
|
||||||
|
|
||||||
const ds = new MysqlDatasource(instanceSettings, templateSrv, timeSrvMock);
|
const ds = new MysqlDatasource(instanceSettings, templateSrv, timeSrvMock);
|
||||||
|
|
||||||
return { ds, variable, templateSrv };
|
return { ds, variable, templateSrv, fetchMock };
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('When performing annotationQuery', () => {
|
describe('When performing a query with hidden target', () => {
|
||||||
const annotationName = 'MyAnno';
|
it('should return empty result and backendSrv.fetch should not be called', async () => {
|
||||||
|
const options = ({
|
||||||
|
range: {
|
||||||
|
from: dateTime(1432288354),
|
||||||
|
to: dateTime(1432288401),
|
||||||
|
},
|
||||||
|
targets: [
|
||||||
|
{
|
||||||
|
format: 'table',
|
||||||
|
rawQuery: true,
|
||||||
|
rawSql: 'select time, metric, value from grafana_metric',
|
||||||
|
refId: 'A',
|
||||||
|
datasource: 'gdev-ds',
|
||||||
|
hide: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as unknown) as DataQueryRequest<MySQLQuery>;
|
||||||
|
|
||||||
|
const { ds, fetchMock } = setupTextContext({});
|
||||||
|
|
||||||
|
await expect(ds.query(options)).toEmitValuesWith((received) => {
|
||||||
|
expect(received[0]).toEqual({ data: [] });
|
||||||
|
expect(fetchMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When performing annotationQuery', () => {
|
||||||
|
let results: any;
|
||||||
|
const annotationName = 'MyAnno';
|
||||||
const options = {
|
const options = {
|
||||||
annotation: {
|
annotation: {
|
||||||
name: annotationName,
|
name: annotationName,
|
||||||
@ -51,38 +86,37 @@ describe('MySQLDatasource', () => {
|
|||||||
to: dateTime(1432288401),
|
to: dateTime(1432288401),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
MyAnno: {
|
MyAnno: {
|
||||||
refId: annotationName,
|
frames: [
|
||||||
tables: [
|
dataFrameToJSON(
|
||||||
{
|
new MutableDataFrame({
|
||||||
columns: [{ text: 'time_sec' }, { text: 'text' }, { text: 'tags' }],
|
fields: [
|
||||||
rows: [
|
{ name: 'time_sec', values: [1432288355, 1432288390, 1432288400] },
|
||||||
[1432288355, 'some text', 'TagA,TagB'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
[1432288390, 'some text2', ' TagB , TagC'],
|
{ name: 'tags', values: ['TagA,TagB', ' TagB , TagC', null] },
|
||||||
[1432288400, 'some text3'],
|
],
|
||||||
],
|
})
|
||||||
},
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should return annotation list', async () => {
|
beforeEach(async () => {
|
||||||
const { ds } = setupTextContext(response);
|
const { ds } = setupTextContext(response);
|
||||||
const results = await ds.annotationQuery(options);
|
const data = await ds.annotationQuery(options);
|
||||||
|
results = data;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return annotation list', async () => {
|
||||||
expect(results.length).toBe(3);
|
expect(results.length).toBe(3);
|
||||||
|
|
||||||
expect(results[0].text).toBe('some text');
|
expect(results[0].text).toBe('some text');
|
||||||
expect(results[0].tags[0]).toBe('TagA');
|
expect(results[0].tags[0]).toBe('TagA');
|
||||||
expect(results[0].tags[1]).toBe('TagB');
|
expect(results[0].tags[1]).toBe('TagB');
|
||||||
|
|
||||||
expect(results[1].tags[0]).toBe('TagB');
|
expect(results[1].tags[0]).toBe('TagB');
|
||||||
expect(results[1].tags[1]).toBe('TagC');
|
expect(results[1].tags[1]).toBe('TagC');
|
||||||
|
|
||||||
expect(results[2].tags.length).toBe(0);
|
expect(results[2].tags.length).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -92,19 +126,19 @@ describe('MySQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -125,26 +159,26 @@ describe('MySQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should return list of all column values', async () => {
|
it('should return list of all column values', async () => {
|
||||||
const { ds } = setupTextContext(response);
|
const { ds, fetchMock } = setupTextContext(response);
|
||||||
const results = await ds.metricFindQuery(query, { searchFilter: 'aTit' });
|
const results = await ds.metricFindQuery(query, { searchFilter: 'aTit' });
|
||||||
|
|
||||||
expect(fetchMock).toBeCalledTimes(1);
|
expect(fetchMock).toBeCalledTimes(1);
|
||||||
@ -160,26 +194,26 @@ describe('MySQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should return list of all column values', async () => {
|
it('should return list of all column values', async () => {
|
||||||
const { ds } = setupTextContext(response);
|
const { ds, fetchMock } = setupTextContext(response);
|
||||||
const results = await ds.metricFindQuery(query, {});
|
const results = await ds.metricFindQuery(query, {});
|
||||||
|
|
||||||
expect(fetchMock).toBeCalledTimes(1);
|
expect(fetchMock).toBeCalledTimes(1);
|
||||||
@ -193,19 +227,19 @@ describe('MySQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: '__value' }, { text: '__text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['value1', 'aTitle'],
|
{ name: '__value', values: ['value1', 'value2', 'value3'] },
|
||||||
['value2', 'aTitle2'],
|
{ name: '__text', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['value3', 'aTitle3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -228,19 +262,19 @@ describe('MySQLDatasource', () => {
|
|||||||
const response = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: '__text' }, { text: '__value' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'same'],
|
{ name: '__text', values: ['aTitle', 'aTitle', 'aTitle'] },
|
||||||
['aTitle', 'same'],
|
{ name: '__value', values: ['same', 'same', 'diff'] },
|
||||||
['aTitle', 'diff'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
import { MetricFindValue } from '@grafana/data';
|
import { DataQuery, DataSourceJsonData } from '@grafana/data';
|
||||||
|
|
||||||
export interface MysqlQueryForInterpolation {
|
export interface MysqlQueryForInterpolation {
|
||||||
alias?: any;
|
alias?: any;
|
||||||
format?: any;
|
format?: any;
|
||||||
rawSql?: any;
|
rawSql?: any;
|
||||||
refId?: any;
|
refId: any;
|
||||||
hide?: any;
|
hide?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MysqlMetricFindValue extends MetricFindValue {
|
export interface MySQLOptions extends DataSourceJsonData {
|
||||||
value?: string;
|
timeInterval: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResultFormat = 'time_series' | 'table';
|
||||||
|
|
||||||
|
export interface MySQLQuery extends DataQuery {
|
||||||
|
alias?: string;
|
||||||
|
format?: ResultFormat;
|
||||||
|
rawSql?: any;
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,37 @@
|
|||||||
import { map as _map, filter } from 'lodash';
|
import { map as _map } from 'lodash';
|
||||||
import { Observable, of } from 'rxjs';
|
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { getBackendSrv } from '@grafana/runtime';
|
import { BackendDataSourceResponse, DataSourceWithBackend, FetchResponse, getBackendSrv } from '@grafana/runtime';
|
||||||
import { DataQueryResponse, ScopedVars } from '@grafana/data';
|
import { AnnotationEvent, DataSourceInstanceSettings, MetricFindValue, ScopedVars } from '@grafana/data';
|
||||||
|
|
||||||
import ResponseParser from './response_parser';
|
import ResponseParser from './response_parser';
|
||||||
import PostgresQuery from 'app/plugins/datasource/postgres/postgres_query';
|
import PostgresQueryModel from 'app/plugins/datasource/postgres/postgres_query_model';
|
||||||
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||||
//Types
|
//Types
|
||||||
import { PostgresMetricFindValue, PostgresQueryForInterpolation } from './types';
|
import { PostgresOptions, PostgresQuery, PostgresQueryForInterpolation } from './types';
|
||||||
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
||||||
|
|
||||||
export class PostgresDatasource {
|
export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, PostgresOptions> {
|
||||||
id: any;
|
id: any;
|
||||||
name: any;
|
name: any;
|
||||||
jsonData: any;
|
jsonData: any;
|
||||||
responseParser: ResponseParser;
|
responseParser: ResponseParser;
|
||||||
queryModel: PostgresQuery;
|
queryModel: PostgresQueryModel;
|
||||||
interval: string;
|
interval: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
instanceSettings: { name: any; id?: any; jsonData?: any },
|
instanceSettings: DataSourceInstanceSettings<PostgresOptions>,
|
||||||
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
||||||
private readonly timeSrv: TimeSrv = getTimeSrv()
|
private readonly timeSrv: TimeSrv = getTimeSrv()
|
||||||
) {
|
) {
|
||||||
|
super(instanceSettings);
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
this.id = instanceSettings.id;
|
this.id = instanceSettings.id;
|
||||||
this.jsonData = instanceSettings.jsonData;
|
this.jsonData = instanceSettings.jsonData;
|
||||||
this.responseParser = new ResponseParser();
|
this.responseParser = new ResponseParser();
|
||||||
this.queryModel = new PostgresQuery({});
|
this.queryModel = new PostgresQueryModel({});
|
||||||
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
|
const settingsData = instanceSettings.jsonData || ({} as PostgresOptions);
|
||||||
|
this.interval = settingsData.timeInterval || '1m';
|
||||||
}
|
}
|
||||||
|
|
||||||
interpolateVariable = (value: string | string[], variable: { multi: any; includeAll: any }) => {
|
interpolateVariable = (value: string | string[], variable: { multi: any; includeAll: any }) => {
|
||||||
@ -71,40 +72,21 @@ export class PostgresDatasource {
|
|||||||
return expandedQueries;
|
return expandedQueries;
|
||||||
}
|
}
|
||||||
|
|
||||||
query(options: any): Observable<DataQueryResponse> {
|
filterQuery(query: PostgresQuery): boolean {
|
||||||
const queries = filter(options.targets, (target) => {
|
return !query.hide;
|
||||||
return target.hide !== true;
|
|
||||||
}).map((target) => {
|
|
||||||
const queryModel = new PostgresQuery(target, this.templateSrv, options.scopedVars);
|
|
||||||
|
|
||||||
return {
|
|
||||||
refId: target.refId,
|
|
||||||
intervalMs: options.intervalMs,
|
|
||||||
maxDataPoints: options.maxDataPoints,
|
|
||||||
datasourceId: this.id,
|
|
||||||
rawSql: queryModel.render(this.interpolateVariable),
|
|
||||||
format: target.format,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (queries.length === 0) {
|
|
||||||
return of({ data: [] });
|
|
||||||
}
|
|
||||||
|
|
||||||
return getBackendSrv()
|
|
||||||
.fetch({
|
|
||||||
url: '/api/tsdb/query',
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
from: options.range.from.valueOf().toString(),
|
|
||||||
to: options.range.to.valueOf().toString(),
|
|
||||||
queries: queries,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.pipe(map(this.responseParser.processQueryResult));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
annotationQuery(options: any) {
|
applyTemplateVariables(target: PostgresQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||||
|
const queryModel = new PostgresQueryModel(target, this.templateSrv, scopedVars);
|
||||||
|
return {
|
||||||
|
refId: target.refId,
|
||||||
|
datasourceId: this.id,
|
||||||
|
rawSql: queryModel.render(this.interpolateVariable as any),
|
||||||
|
format: target.format,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async annotationQuery(options: any): Promise<AnnotationEvent[]> {
|
||||||
if (!options.annotation.rawQuery) {
|
if (!options.annotation.rawQuery) {
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
message: 'Query missing in annotation definition',
|
message: 'Query missing in annotation definition',
|
||||||
@ -119,23 +101,26 @@ export class PostgresDatasource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
from: options.range.from.valueOf().toString(),
|
from: options.range.from.valueOf().toString(),
|
||||||
to: options.range.to.valueOf().toString(),
|
to: options.range.to.valueOf().toString(),
|
||||||
queries: [query],
|
queries: [query],
|
||||||
},
|
},
|
||||||
|
requestId: options.annotation.name,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.transformAnnotationResponse(options, data)))
|
.pipe(
|
||||||
|
map(
|
||||||
|
async (res: FetchResponse<BackendDataSourceResponse>) =>
|
||||||
|
await this.responseParser.transformAnnotationResponse(options, res.data)
|
||||||
|
)
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
metricFindQuery(
|
metricFindQuery(query: string, optionalOptions: any): Promise<MetricFindValue[]> {
|
||||||
query: string,
|
|
||||||
optionalOptions: { variable?: any; searchFilter?: string }
|
|
||||||
): Promise<PostgresMetricFindValue[]> {
|
|
||||||
let refId = 'tempvar';
|
let refId = 'tempvar';
|
||||||
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
||||||
refId = optionalOptions.variable.name;
|
refId = optionalOptions.variable.name;
|
||||||
@ -155,33 +140,37 @@ export class PostgresDatasource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const range = this.timeSrv.timeRange();
|
const range = this.timeSrv.timeRange();
|
||||||
const data = {
|
|
||||||
queries: [interpolatedQuery],
|
|
||||||
from: range.from.valueOf().toString(),
|
|
||||||
to: range.to.valueOf().toString(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return getBackendSrv()
|
return getBackendSrv()
|
||||||
.fetch({
|
.fetch<BackendDataSourceResponse>({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/ds/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: data,
|
data: {
|
||||||
|
from: range.from.valueOf().toString(),
|
||||||
|
to: range.to.valueOf().toString(),
|
||||||
|
queries: [interpolatedQuery],
|
||||||
|
},
|
||||||
|
requestId: refId,
|
||||||
})
|
})
|
||||||
.pipe(map((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data)))
|
.pipe(
|
||||||
|
map((rsp) => {
|
||||||
|
return this.responseParser.transformMetricFindResponse(rsp);
|
||||||
|
})
|
||||||
|
)
|
||||||
.toPromise();
|
.toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
getVersion() {
|
getVersion(): Promise<any> {
|
||||||
return this.metricFindQuery("SELECT current_setting('server_version_num')::int/100", {});
|
return this.metricFindQuery("SELECT current_setting('server_version_num')::int/100", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
getTimescaleDBVersion() {
|
getTimescaleDBVersion(): Promise<any> {
|
||||||
return this.metricFindQuery("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'", {});
|
return this.metricFindQuery("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
testDatasource() {
|
testDatasource(): Promise<any> {
|
||||||
return this.metricFindQuery('SELECT 1', {})
|
return this.metricFindQuery('SELECT 1', {})
|
||||||
.then((res: any) => {
|
.then(() => {
|
||||||
return { status: 'success', message: 'Database Connection OK' };
|
return { status: 'success', message: 'Database Connection OK' };
|
||||||
})
|
})
|
||||||
.catch((err: any) => {
|
.catch((err: any) => {
|
||||||
@ -200,7 +189,7 @@ export class PostgresDatasource {
|
|||||||
if (target.rawQuery) {
|
if (target.rawQuery) {
|
||||||
rawSql = target.rawSql;
|
rawSql = target.rawSql;
|
||||||
} else {
|
} else {
|
||||||
const query = new PostgresQuery(target);
|
const query = new PostgresQueryModel(target);
|
||||||
rawSql = query.buildQuery();
|
rawSql = query.buildQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import QueryModel from './postgres_query';
|
import QueryModel from './postgres_query_model';
|
||||||
|
|
||||||
export class PostgresMetaQuery {
|
export class PostgresMetaQuery {
|
||||||
constructor(private target: { table: string; timeColumn: string }, private queryModel: QueryModel) {}
|
constructor(private target: { table: string; timeColumn: string }, private queryModel: QueryModel) {}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { PostgresDatasource } from './datasource';
|
import { PostgresDatasource } from './datasource';
|
||||||
import { PostgresQueryCtrl } from './query_ctrl';
|
import { PostgresQueryCtrl } from './query_ctrl';
|
||||||
import { PostgresConfigCtrl } from './config_ctrl';
|
import { PostgresConfigCtrl } from './config_ctrl';
|
||||||
|
import { PostgresQuery } from './types';
|
||||||
|
import { DataSourcePlugin } from '@grafana/data';
|
||||||
|
|
||||||
const defaultQuery = `SELECT
|
const defaultQuery = `SELECT
|
||||||
extract(epoch from time_column) AS time,
|
extract(epoch from time_column) AS time,
|
||||||
@ -24,10 +26,7 @@ class PostgresAnnotationsQueryCtrl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export const plugin = new DataSourcePlugin<PostgresDatasource, PostgresQuery>(PostgresDatasource)
|
||||||
PostgresDatasource,
|
.setQueryCtrl(PostgresQueryCtrl)
|
||||||
PostgresDatasource as Datasource,
|
.setConfigCtrl(PostgresConfigCtrl)
|
||||||
PostgresQueryCtrl as QueryCtrl,
|
.setAnnotationQueryCtrl(PostgresAnnotationsQueryCtrl);
|
||||||
PostgresConfigCtrl as ConfigCtrl,
|
|
||||||
PostgresAnnotationsQueryCtrl as AnnotationsQueryCtrl,
|
|
||||||
};
|
|
||||||
|
@ -2,7 +2,7 @@ import { find, map } from 'lodash';
|
|||||||
import { TemplateSrv } from '@grafana/runtime';
|
import { TemplateSrv } from '@grafana/runtime';
|
||||||
import { ScopedVars } from '@grafana/data';
|
import { ScopedVars } from '@grafana/data';
|
||||||
|
|
||||||
export default class PostgresQuery {
|
export default class PostgresQueryModel {
|
||||||
target: any;
|
target: any;
|
||||||
templateSrv: any;
|
templateSrv: any;
|
||||||
scopedVars: any;
|
scopedVars: any;
|
@ -3,7 +3,7 @@ import appEvents from 'app/core/app_events';
|
|||||||
import { PostgresMetaQuery } from './meta_query';
|
import { PostgresMetaQuery } from './meta_query';
|
||||||
import { QueryCtrl } from 'app/plugins/sdk';
|
import { QueryCtrl } from 'app/plugins/sdk';
|
||||||
import { SqlPart } from 'app/core/components/sql_part/sql_part';
|
import { SqlPart } from 'app/core/components/sql_part/sql_part';
|
||||||
import PostgresQuery from './postgres_query';
|
import PostgresQueryModel from './postgres_query_model';
|
||||||
import sqlPart from './sql_part';
|
import sqlPart from './sql_part';
|
||||||
import { auto } from 'angular';
|
import { auto } from 'angular';
|
||||||
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
import { PanelEvents, QueryResultMeta } from '@grafana/data';
|
||||||
@ -24,7 +24,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
|||||||
static templateUrl = 'partials/query.editor.html';
|
static templateUrl = 'partials/query.editor.html';
|
||||||
|
|
||||||
formats: any[];
|
formats: any[];
|
||||||
queryModel: PostgresQuery;
|
queryModel: PostgresQueryModel;
|
||||||
metaBuilder: PostgresMetaQuery;
|
metaBuilder: PostgresMetaQuery;
|
||||||
lastQueryMeta?: QueryResultMeta;
|
lastQueryMeta?: QueryResultMeta;
|
||||||
lastQueryError?: string;
|
lastQueryError?: string;
|
||||||
@ -48,7 +48,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
|||||||
) {
|
) {
|
||||||
super($scope, $injector);
|
super($scope, $injector);
|
||||||
this.target = this.target;
|
this.target = this.target;
|
||||||
this.queryModel = new PostgresQuery(this.target, templateSrv, this.panel.scopedVars);
|
this.queryModel = new PostgresQueryModel(this.target, templateSrv, this.panel.scopedVars);
|
||||||
this.metaBuilder = new PostgresMetaQuery(this.target, this.queryModel);
|
this.metaBuilder = new PostgresMetaQuery(this.target, this.queryModel);
|
||||||
this.updateProjection();
|
this.updateProjection();
|
||||||
|
|
||||||
|
@ -1,55 +1,42 @@
|
|||||||
|
import { AnnotationEvent, DataFrame, FieldType, MetricFindValue } from '@grafana/data';
|
||||||
|
import { BackendDataSourceResponse, FetchResponse, toDataQueryResponse } from '@grafana/runtime';
|
||||||
import { map } from 'lodash';
|
import { map } from 'lodash';
|
||||||
|
|
||||||
export default class ResponseParser {
|
export default class ResponseParser {
|
||||||
processQueryResult(res: any) {
|
transformMetricFindResponse(raw: FetchResponse<BackendDataSourceResponse>): MetricFindValue[] {
|
||||||
const data: any[] = [];
|
const frames = toDataQueryResponse(raw).data as DataFrame[];
|
||||||
|
|
||||||
if (!res.data.results) {
|
if (!frames || !frames.length) {
|
||||||
return { data: data };
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in res.data.results) {
|
|
||||||
const queryRes = res.data.results[key];
|
|
||||||
|
|
||||||
if (queryRes.series) {
|
|
||||||
for (const series of queryRes.series) {
|
|
||||||
data.push({
|
|
||||||
target: series.name,
|
|
||||||
datapoints: series.points,
|
|
||||||
refId: queryRes.refId,
|
|
||||||
meta: queryRes.meta,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queryRes.tables) {
|
|
||||||
for (const table of queryRes.tables) {
|
|
||||||
table.type = 'table';
|
|
||||||
table.refId = queryRes.refId;
|
|
||||||
table.meta = queryRes.meta;
|
|
||||||
data.push(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { data: data };
|
|
||||||
}
|
|
||||||
|
|
||||||
parseMetricFindQueryResult(refId: string, results: any) {
|
|
||||||
if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) {
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const columns = results.data.results[refId].tables[0].columns;
|
const frame = frames[0];
|
||||||
const rows = results.data.results[refId].tables[0].rows;
|
|
||||||
const textColIndex = this.findColIndex(columns, '__text');
|
|
||||||
const valueColIndex = this.findColIndex(columns, '__value');
|
|
||||||
|
|
||||||
if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) {
|
const values: MetricFindValue[] = [];
|
||||||
return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
|
const textField = frame.fields.find((f) => f.name === '__text');
|
||||||
|
const valueField = frame.fields.find((f) => f.name === '__value');
|
||||||
|
|
||||||
|
if (textField && valueField) {
|
||||||
|
for (let i = 0; i < textField.values.length; i++) {
|
||||||
|
values.push({ text: '' + textField.values.get(i), value: '' + valueField.values.get(i) });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const textFields = frame.fields.filter((f) => f.type === FieldType.string);
|
||||||
|
if (textFields) {
|
||||||
|
values.push(
|
||||||
|
...textFields
|
||||||
|
.flatMap((f) => f.values.toArray())
|
||||||
|
.map((v) => ({
|
||||||
|
text: '' + v,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.transformToSimpleList(rows);
|
return Array.from(new Set(values.map((v) => v.text))).map((text) => ({
|
||||||
|
text,
|
||||||
|
value: values.find((v) => v.text === text)?.value,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
|
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
|
||||||
@ -102,45 +89,34 @@ export default class ResponseParser {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
transformAnnotationResponse(options: any, data: any) {
|
async transformAnnotationResponse(options: any, data: BackendDataSourceResponse): Promise<AnnotationEvent[]> {
|
||||||
const table = data.data.results[options.annotation.name].tables[0];
|
const frames = toDataQueryResponse({ data: data }).data as DataFrame[];
|
||||||
|
const frame = frames[0];
|
||||||
|
const timeField = frame.fields.find((f) => f.name === 'time');
|
||||||
|
|
||||||
let timeColumnIndex = -1;
|
if (!timeField) {
|
||||||
let timeEndColumnIndex = -1;
|
throw new Error('Missing mandatory time column (with time column alias) in annotation query');
|
||||||
const titleColumnIndex = -1;
|
|
||||||
let textColumnIndex = -1;
|
|
||||||
let tagsColumnIndex = -1;
|
|
||||||
|
|
||||||
for (let i = 0; i < table.columns.length; i++) {
|
|
||||||
if (table.columns[i].text === 'time') {
|
|
||||||
timeColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'timeend') {
|
|
||||||
timeEndColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'text') {
|
|
||||||
textColumnIndex = i;
|
|
||||||
} else if (table.columns[i].text === 'tags') {
|
|
||||||
tagsColumnIndex = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeColumnIndex === -1) {
|
const timeEndField = frame.fields.find((f) => f.name === 'timeend');
|
||||||
return Promise.reject({
|
const textField = frame.fields.find((f) => f.name === 'text');
|
||||||
message: 'Missing mandatory time column in annotation query.',
|
const tagsField = frame.fields.find((f) => f.name === 'tags');
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const list = [];
|
const list: AnnotationEvent[] = [];
|
||||||
for (let i = 0; i < table.rows.length; i++) {
|
for (let i = 0; i < frame.length; i++) {
|
||||||
const row = table.rows[i];
|
const timeEnd = timeEndField && timeEndField.values.get(i) ? Math.floor(timeEndField.values.get(i)) : undefined;
|
||||||
const timeEnd =
|
|
||||||
timeEndColumnIndex !== -1 && row[timeEndColumnIndex] ? Math.floor(row[timeEndColumnIndex]) : undefined;
|
|
||||||
list.push({
|
list.push({
|
||||||
annotation: options.annotation,
|
annotation: options.annotation,
|
||||||
time: Math.floor(row[timeColumnIndex]),
|
time: Math.floor(timeField.values.get(i)),
|
||||||
timeEnd,
|
timeEnd,
|
||||||
title: row[titleColumnIndex],
|
text: textField && textField.values.get(i) ? textField.values.get(i) : '',
|
||||||
text: row[textColumnIndex],
|
tags:
|
||||||
tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
|
tagsField && tagsField.values.get(i)
|
||||||
|
? tagsField.values
|
||||||
|
.get(i)
|
||||||
|
.trim()
|
||||||
|
.split(/\s*,\s*/)
|
||||||
|
: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,25 +1,47 @@
|
|||||||
import { of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { TestScheduler } from 'rxjs/testing';
|
import { TestScheduler } from 'rxjs/testing';
|
||||||
import { FetchResponse } from '@grafana/runtime';
|
import { FetchResponse } from '@grafana/runtime';
|
||||||
import { dateTime, toUtc } from '@grafana/data';
|
import {
|
||||||
|
dataFrameToJSON,
|
||||||
|
DataQueryRequest,
|
||||||
|
DataSourceInstanceSettings,
|
||||||
|
dateTime,
|
||||||
|
MutableDataFrame,
|
||||||
|
toUtc,
|
||||||
|
} from '@grafana/data';
|
||||||
|
|
||||||
import { PostgresDatasource } from '../datasource';
|
import { PostgresDatasource } from '../datasource';
|
||||||
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
|
||||||
import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv';
|
import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv';
|
||||||
|
import { PostgresOptions, PostgresQuery } from '../types';
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
||||||
getBackendSrv: () => backendSrv,
|
getBackendSrv: () => backendSrv,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('@grafana/runtime/src/services', () => ({
|
||||||
|
...((jest.requireActual('@grafana/runtime/src/services') as unknown) as object),
|
||||||
|
getBackendSrv: () => backendSrv,
|
||||||
|
getDataSourceSrv: () => {
|
||||||
|
return {
|
||||||
|
getInstanceSettings: () => ({ id: 8674 }),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
describe('PostgreSQLDatasource', () => {
|
describe('PostgreSQLDatasource', () => {
|
||||||
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
||||||
const setupTestContext = (data: any) => {
|
const setupTestContext = (data: any) => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
fetchMock.mockImplementation(() => of(createFetchResponse(data)));
|
fetchMock.mockImplementation(() => of(createFetchResponse(data)));
|
||||||
|
const instanceSettings = ({
|
||||||
|
jsonData: {
|
||||||
|
defaultProject: 'testproject',
|
||||||
|
},
|
||||||
|
} as unknown) as DataSourceInstanceSettings<PostgresOptions>;
|
||||||
const templateSrv: TemplateSrv = new TemplateSrv();
|
const templateSrv: TemplateSrv = new TemplateSrv();
|
||||||
const raw = {
|
const raw = {
|
||||||
from: toUtc('2018-04-25 10:00'),
|
from: toUtc('2018-04-25 10:00'),
|
||||||
@ -33,7 +55,7 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
}),
|
}),
|
||||||
} as unknown) as TimeSrv;
|
} as unknown) as TimeSrv;
|
||||||
const variable = { ...initialCustomVariableModelState };
|
const variable = { ...initialCustomVariableModelState };
|
||||||
const ds = new PostgresDatasource({ name: 'dsql' }, templateSrv, timeSrvMock);
|
const ds = new PostgresDatasource(instanceSettings, templateSrv, timeSrvMock);
|
||||||
|
|
||||||
return { ds, templateSrv, timeSrvMock, variable };
|
return { ds, templateSrv, timeSrvMock, variable };
|
||||||
};
|
};
|
||||||
@ -80,42 +102,66 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
const response = {
|
||||||
const data = {
|
|
||||||
results: {
|
results: {
|
||||||
A: {
|
A: {
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
meta: {
|
frames: [
|
||||||
executedQueryString: 'select time, metric from grafana_metric',
|
dataFrameToJSON(
|
||||||
rowCount: 0,
|
new MutableDataFrame({
|
||||||
},
|
fields: [
|
||||||
series: [
|
{ name: 'time', values: [1599643351085] },
|
||||||
{
|
{ name: 'metric', values: [30.226249741223704], labels: { metric: 'America' } },
|
||||||
name: 'America',
|
],
|
||||||
points: [[30.226249741223704, 1599643351085]],
|
meta: {
|
||||||
},
|
executedQueryString: 'select time, metric from grafana_metric',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
tables: null,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const values = { a: createFetchResponse(data) };
|
const values = { a: createFetchResponse(response) };
|
||||||
const marble = '-a|';
|
const marble = '-a|';
|
||||||
const expectedMarble = '-a|';
|
const expectedMarble = '-a|';
|
||||||
const expectedValues = {
|
const expectedValues = {
|
||||||
a: {
|
a: {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
datapoints: [[30.226249741223704, 1599643351085]],
|
fields: [
|
||||||
|
{
|
||||||
|
config: {},
|
||||||
|
entities: {},
|
||||||
|
name: 'time',
|
||||||
|
type: 'time',
|
||||||
|
values: {
|
||||||
|
buffer: [1599643351085],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config: {},
|
||||||
|
entities: {},
|
||||||
|
labels: {
|
||||||
|
metric: 'America',
|
||||||
|
},
|
||||||
|
name: 'metric',
|
||||||
|
type: 'number',
|
||||||
|
values: {
|
||||||
|
buffer: [30.226249741223704],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
length: 1,
|
||||||
meta: {
|
meta: {
|
||||||
executedQueryString: 'select time, metric from grafana_metric',
|
executedQueryString: 'select time, metric from grafana_metric',
|
||||||
rowCount: 0,
|
|
||||||
},
|
},
|
||||||
|
name: undefined,
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
target: 'America',
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
state: 'Done',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -140,63 +186,73 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
const response = {
|
||||||
const data = {
|
|
||||||
results: {
|
results: {
|
||||||
A: {
|
A: {
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
meta: {
|
frames: [
|
||||||
executedQueryString: 'select time, metric, value from grafana_metric',
|
dataFrameToJSON(
|
||||||
rowCount: 1,
|
new MutableDataFrame({
|
||||||
},
|
fields: [
|
||||||
series: null,
|
{ name: 'time', values: [1599643351085] },
|
||||||
tables: [
|
{ name: 'metric', values: ['America'] },
|
||||||
{
|
{ name: 'value', values: [30.226249741223704] },
|
||||||
columns: [
|
],
|
||||||
{
|
meta: {
|
||||||
text: 'time',
|
executedQueryString: 'select time, metric, value from grafana_metric',
|
||||||
},
|
},
|
||||||
{
|
})
|
||||||
text: 'metric',
|
),
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'value',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
rows: [[1599643351085, 'America', 30.226249741223704]],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const values = { a: createFetchResponse(data) };
|
const values = { a: createFetchResponse(response) };
|
||||||
const marble = '-a|';
|
const marble = '-a|';
|
||||||
const expectedMarble = '-a|';
|
const expectedMarble = '-a|';
|
||||||
const expectedValues = {
|
const expectedValues = {
|
||||||
a: {
|
a: {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
columns: [
|
fields: [
|
||||||
{
|
{
|
||||||
text: 'time',
|
config: {},
|
||||||
|
entities: {},
|
||||||
|
name: 'time',
|
||||||
|
type: 'time',
|
||||||
|
values: {
|
||||||
|
buffer: [1599643351085],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'metric',
|
config: {},
|
||||||
|
entities: {},
|
||||||
|
name: 'metric',
|
||||||
|
type: 'string',
|
||||||
|
values: {
|
||||||
|
buffer: ['America'],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'value',
|
config: {},
|
||||||
|
entities: {},
|
||||||
|
name: 'value',
|
||||||
|
type: 'number',
|
||||||
|
values: {
|
||||||
|
buffer: [30.226249741223704],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
rows: [[1599643351085, 'America', 30.226249741223704]],
|
length: 1,
|
||||||
type: 'table',
|
|
||||||
refId: 'A',
|
|
||||||
meta: {
|
meta: {
|
||||||
executedQueryString: 'select time, metric, value from grafana_metric',
|
executedQueryString: 'select time, metric, value from grafana_metric',
|
||||||
rowCount: 1,
|
|
||||||
},
|
},
|
||||||
|
name: undefined,
|
||||||
|
refId: 'A',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
state: 'Done',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -206,7 +262,7 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
|
|
||||||
describe('When performing a query with hidden target', () => {
|
describe('When performing a query with hidden target', () => {
|
||||||
it('should return empty result and backendSrv.fetch should not be called', async () => {
|
it('should return empty result and backendSrv.fetch should not be called', async () => {
|
||||||
const options = {
|
const options = ({
|
||||||
range: {
|
range: {
|
||||||
from: dateTime(1432288354),
|
from: dateTime(1432288354),
|
||||||
to: dateTime(1432288401),
|
to: dateTime(1432288401),
|
||||||
@ -221,7 +277,7 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
hide: true,
|
hide: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
} as unknown) as DataQueryRequest<PostgresQuery>;
|
||||||
|
|
||||||
const { ds } = setupTestContext({});
|
const { ds } = setupTestContext({});
|
||||||
|
|
||||||
@ -233,40 +289,42 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing annotationQuery', () => {
|
describe('When performing annotationQuery', () => {
|
||||||
it('should return annotation list', async () => {
|
let results: any;
|
||||||
const annotationName = 'MyAnno';
|
const annotationName = 'MyAnno';
|
||||||
const options = {
|
const options = {
|
||||||
annotation: {
|
annotation: {
|
||||||
name: annotationName,
|
name: annotationName,
|
||||||
rawQuery: 'select time, title, text, tags from table;',
|
rawQuery: 'select time, title, text, tags from table;',
|
||||||
},
|
},
|
||||||
range: {
|
range: {
|
||||||
from: dateTime(1432288354),
|
from: dateTime(1432288354),
|
||||||
to: dateTime(1432288401),
|
to: dateTime(1432288401),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
MyAnno: {
|
MyAnno: {
|
||||||
refId: annotationName,
|
frames: [
|
||||||
tables: [
|
dataFrameToJSON(
|
||||||
{
|
new MutableDataFrame({
|
||||||
columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
|
fields: [
|
||||||
rows: [
|
{ name: 'time', values: [1432288355, 1432288390, 1432288400] },
|
||||||
[1432288355, 'some text', 'TagA,TagB'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
[1432288390, 'some text2', ' TagB , TagC'],
|
{ name: 'tags', values: ['TagA,TagB', ' TagB , TagC', null] },
|
||||||
[1432288400, 'some text3'],
|
|
||||||
],
|
],
|
||||||
},
|
})
|
||||||
],
|
),
|
||||||
},
|
],
|
||||||
},
|
},
|
||||||
};
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const { ds } = setupTestContext(data);
|
beforeEach(async () => {
|
||||||
|
const { ds } = setupTestContext(response);
|
||||||
const results = await ds.annotationQuery(options);
|
results = await ds.annotationQuery(options);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return annotation list', async () => {
|
||||||
expect(results.length).toBe(3);
|
expect(results.length).toBe(3);
|
||||||
|
|
||||||
expect(results[0].text).toBe('some text');
|
expect(results[0].text).toBe('some text');
|
||||||
@ -283,29 +341,28 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
describe('When performing metricFindQuery', () => {
|
describe('When performing metricFindQuery', () => {
|
||||||
it('should return list of all column values', async () => {
|
it('should return list of all column values', async () => {
|
||||||
const query = 'select * from atable';
|
const query = 'select * from atable';
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { ds } = setupTestContext(data);
|
const { ds } = setupTestContext(response);
|
||||||
|
|
||||||
const results = await ds.metricFindQuery(query, {});
|
const results = await ds.metricFindQuery(query, {});
|
||||||
|
|
||||||
expect(results.length).toBe(6);
|
expect(results.length).toBe(6);
|
||||||
@ -317,29 +374,28 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
describe('When performing metricFindQuery with $__searchFilter and a searchFilter is given', () => {
|
describe('When performing metricFindQuery with $__searchFilter and a searchFilter is given', () => {
|
||||||
it('should return list of all column values', async () => {
|
it('should return list of all column values', async () => {
|
||||||
const query = "select title from atable where title LIKE '$__searchFilter'";
|
const query = "select title from atable where title LIKE '$__searchFilter'";
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { ds } = setupTestContext(data);
|
const { ds } = setupTestContext(response);
|
||||||
|
|
||||||
const results = await ds.metricFindQuery(query, { searchFilter: 'aTit' });
|
const results = await ds.metricFindQuery(query, { searchFilter: 'aTit' });
|
||||||
|
|
||||||
expect(fetchMock).toBeCalledTimes(1);
|
expect(fetchMock).toBeCalledTimes(1);
|
||||||
@ -348,10 +404,10 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
);
|
);
|
||||||
expect(results).toEqual([
|
expect(results).toEqual([
|
||||||
{ text: 'aTitle' },
|
{ text: 'aTitle' },
|
||||||
{ text: 'some text' },
|
|
||||||
{ text: 'aTitle2' },
|
{ text: 'aTitle2' },
|
||||||
{ text: 'some text2' },
|
|
||||||
{ text: 'aTitle3' },
|
{ text: 'aTitle3' },
|
||||||
|
{ text: 'some text' },
|
||||||
|
{ text: 'some text2' },
|
||||||
{ text: 'some text3' },
|
{ text: 'some text3' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -360,39 +416,38 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
describe('When performing metricFindQuery with $__searchFilter but no searchFilter is given', () => {
|
describe('When performing metricFindQuery with $__searchFilter but no searchFilter is given', () => {
|
||||||
it('should return list of all column values', async () => {
|
it('should return list of all column values', async () => {
|
||||||
const query = "select title from atable where title LIKE '$__searchFilter'";
|
const query = "select title from atable where title LIKE '$__searchFilter'";
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: 'title' }, { text: 'text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'some text'],
|
{ name: 'title', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['aTitle2', 'some text2'],
|
{ name: 'text', values: ['some text', 'some text2', 'some text3'] },
|
||||||
['aTitle3', 'some text3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { ds } = setupTestContext(data);
|
const { ds } = setupTestContext(response);
|
||||||
|
|
||||||
const results = await ds.metricFindQuery(query, {});
|
const results = await ds.metricFindQuery(query, {});
|
||||||
|
|
||||||
expect(fetchMock).toBeCalledTimes(1);
|
expect(fetchMock).toBeCalledTimes(1);
|
||||||
expect(fetchMock.mock.calls[0][0].data.queries[0].rawSql).toBe("select title from atable where title LIKE '%'");
|
expect(fetchMock.mock.calls[0][0].data.queries[0].rawSql).toBe("select title from atable where title LIKE '%'");
|
||||||
expect(results).toEqual([
|
expect(results).toEqual([
|
||||||
{ text: 'aTitle' },
|
{ text: 'aTitle' },
|
||||||
{ text: 'some text' },
|
|
||||||
{ text: 'aTitle2' },
|
{ text: 'aTitle2' },
|
||||||
{ text: 'some text2' },
|
|
||||||
{ text: 'aTitle3' },
|
{ text: 'aTitle3' },
|
||||||
|
{ text: 'some text' },
|
||||||
|
{ text: 'some text2' },
|
||||||
{ text: 'some text3' },
|
{ text: 'some text3' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -401,29 +456,27 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
describe('When performing metricFindQuery with key, value columns', () => {
|
describe('When performing metricFindQuery with key, value columns', () => {
|
||||||
it('should return list of as text, value', async () => {
|
it('should return list of as text, value', async () => {
|
||||||
const query = 'select * from atable';
|
const query = 'select * from atable';
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: '__value' }, { text: '__text' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['value1', 'aTitle'],
|
{ name: '__value', values: ['value1', 'value2', 'value3'] },
|
||||||
['value2', 'aTitle2'],
|
{ name: '__text', values: ['aTitle', 'aTitle2', 'aTitle3'] },
|
||||||
['value3', 'aTitle3'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const { ds } = setupTestContext(response);
|
||||||
const { ds } = setupTestContext(data);
|
|
||||||
|
|
||||||
const results = await ds.metricFindQuery(query, {});
|
const results = await ds.metricFindQuery(query, {});
|
||||||
|
|
||||||
expect(results).toEqual([
|
expect(results).toEqual([
|
||||||
@ -437,29 +490,27 @@ describe('PostgreSQLDatasource', () => {
|
|||||||
describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
|
describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
|
||||||
it('should return list of unique keys', async () => {
|
it('should return list of unique keys', async () => {
|
||||||
const query = 'select * from atable';
|
const query = 'select * from atable';
|
||||||
const data = {
|
const response = {
|
||||||
results: {
|
results: {
|
||||||
tempvar: {
|
tempvar: {
|
||||||
meta: {
|
|
||||||
rowCount: 3,
|
|
||||||
},
|
|
||||||
refId: 'tempvar',
|
refId: 'tempvar',
|
||||||
tables: [
|
frames: [
|
||||||
{
|
dataFrameToJSON(
|
||||||
columns: [{ text: '__text' }, { text: '__value' }],
|
new MutableDataFrame({
|
||||||
rows: [
|
fields: [
|
||||||
['aTitle', 'same'],
|
{ name: '__text', values: ['aTitle', 'aTitle', 'aTitle'] },
|
||||||
['aTitle', 'same'],
|
{ name: '__value', values: ['same', 'same', 'diff'] },
|
||||||
['aTitle', 'diff'],
|
],
|
||||||
],
|
meta: {
|
||||||
},
|
executedQueryString: 'select * from atable',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const { ds } = setupTestContext(response);
|
||||||
const { ds } = setupTestContext(data);
|
|
||||||
|
|
||||||
const results = await ds.metricFindQuery(query, {});
|
const results = await ds.metricFindQuery(query, {});
|
||||||
|
|
||||||
expect(results).toEqual([{ text: 'aTitle', value: 'same' }]);
|
expect(results).toEqual([{ text: 'aTitle', value: 'same' }]);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import PostgresQuery from '../postgres_query';
|
import PostgresQueryModel from '../postgres_query_model';
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
|
|
||||||
describe('PostgresQuery', () => {
|
describe('PostgresQuery', () => {
|
||||||
@ -9,17 +9,17 @@ describe('PostgresQuery', () => {
|
|||||||
|
|
||||||
describe('When initializing', () => {
|
describe('When initializing', () => {
|
||||||
it('should not be in SQL mode', () => {
|
it('should not be in SQL mode', () => {
|
||||||
const query = new PostgresQuery({}, templateSrv);
|
const query = new PostgresQueryModel({}, templateSrv);
|
||||||
expect(query.target.rawQuery).toBe(false);
|
expect(query.target.rawQuery).toBe(false);
|
||||||
});
|
});
|
||||||
it('should be in SQL mode for pre query builder queries', () => {
|
it('should be in SQL mode for pre query builder queries', () => {
|
||||||
const query = new PostgresQuery({ rawSql: 'SELECT 1' }, templateSrv);
|
const query = new PostgresQueryModel({ rawSql: 'SELECT 1' }, templateSrv);
|
||||||
expect(query.target.rawQuery).toBe(true);
|
expect(query.target.rawQuery).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating time column SQL', () => {
|
describe('When generating time column SQL', () => {
|
||||||
const query = new PostgresQuery({}, templateSrv);
|
const query = new PostgresQueryModel({}, templateSrv);
|
||||||
|
|
||||||
query.target.timeColumn = 'time';
|
query.target.timeColumn = 'time';
|
||||||
expect(query.buildTimeColumn()).toBe('time AS "time"');
|
expect(query.buildTimeColumn()).toBe('time AS "time"');
|
||||||
@ -28,17 +28,20 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating time column SQL with group by time', () => {
|
describe('When generating time column SQL with group by time', () => {
|
||||||
let query = new PostgresQuery(
|
let query = new PostgresQueryModel(
|
||||||
{ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'none'] }] },
|
{ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'none'] }] },
|
||||||
templateSrv
|
templateSrv
|
||||||
);
|
);
|
||||||
expect(query.buildTimeColumn()).toBe('$__timeGroupAlias(time,5m)');
|
expect(query.buildTimeColumn()).toBe('$__timeGroupAlias(time,5m)');
|
||||||
expect(query.buildTimeColumn(false)).toBe('$__timeGroup(time,5m)');
|
expect(query.buildTimeColumn(false)).toBe('$__timeGroup(time,5m)');
|
||||||
|
|
||||||
query = new PostgresQuery({ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'NULL'] }] }, templateSrv);
|
query = new PostgresQueryModel(
|
||||||
|
{ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'NULL'] }] },
|
||||||
|
templateSrv
|
||||||
|
);
|
||||||
expect(query.buildTimeColumn()).toBe('$__timeGroupAlias(time,5m,NULL)');
|
expect(query.buildTimeColumn()).toBe('$__timeGroupAlias(time,5m,NULL)');
|
||||||
|
|
||||||
query = new PostgresQuery(
|
query = new PostgresQueryModel(
|
||||||
{ timeColumn: 'time', timeColumnType: 'int4', group: [{ type: 'time', params: ['5m', 'none'] }] },
|
{ timeColumn: 'time', timeColumnType: 'int4', group: [{ type: 'time', params: ['5m', 'none'] }] },
|
||||||
templateSrv
|
templateSrv
|
||||||
);
|
);
|
||||||
@ -47,7 +50,7 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating metric column SQL', () => {
|
describe('When generating metric column SQL', () => {
|
||||||
const query = new PostgresQuery({}, templateSrv);
|
const query = new PostgresQueryModel({}, templateSrv);
|
||||||
|
|
||||||
query.target.metricColumn = 'host';
|
query.target.metricColumn = 'host';
|
||||||
expect(query.buildMetricColumn()).toBe('host AS metric');
|
expect(query.buildMetricColumn()).toBe('host AS metric');
|
||||||
@ -56,7 +59,7 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating value column SQL', () => {
|
describe('When generating value column SQL', () => {
|
||||||
const query = new PostgresQuery({}, templateSrv);
|
const query = new PostgresQueryModel({}, templateSrv);
|
||||||
|
|
||||||
let column = [{ type: 'column', params: ['value'] }];
|
let column = [{ type: 'column', params: ['value'] }];
|
||||||
expect(query.buildValueColumn(column)).toBe('value');
|
expect(query.buildValueColumn(column)).toBe('value');
|
||||||
@ -84,7 +87,7 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating value column SQL with metric column', () => {
|
describe('When generating value column SQL with metric column', () => {
|
||||||
const query = new PostgresQuery({}, templateSrv);
|
const query = new PostgresQueryModel({}, templateSrv);
|
||||||
query.target.metricColumn = 'host';
|
query.target.metricColumn = 'host';
|
||||||
|
|
||||||
let column = [{ type: 'column', params: ['value'] }];
|
let column = [{ type: 'column', params: ['value'] }];
|
||||||
@ -124,7 +127,7 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating WHERE clause', () => {
|
describe('When generating WHERE clause', () => {
|
||||||
const query = new PostgresQuery({ where: [] }, templateSrv);
|
const query = new PostgresQueryModel({ where: [] }, templateSrv);
|
||||||
|
|
||||||
expect(query.buildWhereClause()).toBe('');
|
expect(query.buildWhereClause()).toBe('');
|
||||||
|
|
||||||
@ -143,7 +146,7 @@ describe('PostgresQuery', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When generating GROUP BY clause', () => {
|
describe('When generating GROUP BY clause', () => {
|
||||||
const query = new PostgresQuery({ group: [], metricColumn: 'none' }, templateSrv);
|
const query = new PostgresQueryModel({ group: [], metricColumn: 'none' }, templateSrv);
|
||||||
|
|
||||||
expect(query.buildGroupClause()).toBe('');
|
expect(query.buildGroupClause()).toBe('');
|
||||||
query.target.group = [{ type: 'time', params: ['5m'] }];
|
query.target.group = [{ type: 'time', params: ['5m'] }];
|
||||||
@ -160,7 +163,7 @@ describe('PostgresQuery', () => {
|
|||||||
where: [],
|
where: [],
|
||||||
};
|
};
|
||||||
let result = 'SELECT\n t AS "time",\n value\nFROM table\nORDER BY 1';
|
let result = 'SELECT\n t AS "time",\n value\nFROM table\nORDER BY 1';
|
||||||
const query = new PostgresQuery(target, templateSrv);
|
const query = new PostgresQueryModel(target, templateSrv);
|
||||||
|
|
||||||
expect(query.buildQuery()).toBe(result);
|
expect(query.buildQuery()).toBe(result);
|
||||||
|
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
import { MetricFindValue } from '@grafana/data';
|
import { DataQuery, DataSourceJsonData } from '@grafana/data';
|
||||||
|
|
||||||
export interface PostgresQueryForInterpolation {
|
export interface PostgresQueryForInterpolation {
|
||||||
alias?: any;
|
alias?: any;
|
||||||
format?: any;
|
format?: any;
|
||||||
rawSql?: any;
|
rawSql?: any;
|
||||||
refId?: any;
|
refId: any;
|
||||||
hide?: any;
|
hide?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PostgresMetricFindValue extends MetricFindValue {
|
export interface PostgresOptions extends DataSourceJsonData {
|
||||||
value?: string;
|
timeInterval: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResultFormat = 'time_series' | 'table';
|
||||||
|
|
||||||
|
export interface PostgresQuery extends DataQuery {
|
||||||
|
alias?: string;
|
||||||
|
format?: ResultFormat;
|
||||||
|
rawSql?: any;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user