| 
									
										
										
										
											2019-09-11 03:50:04 +08:00
										 |  |  | package sqleng | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	"database/sql" | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 	"regexp" | 
					
						
							| 
									
										
										
										
											2018-08-12 16:51:58 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2018-03-21 02:40:10 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/data" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tsdb/interval" | 
					
						
							| 
									
										
										
										
											2020-04-01 21:57:21 +08:00
										 |  |  | 	"xorm.io/core" | 
					
						
							|  |  |  | 	"xorm.io/xorm" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-30 02:06:33 +08:00
										 |  |  | // MetaKeyExecutedQueryString is the key where the executed query should get stored
 | 
					
						
							|  |  |  | const MetaKeyExecutedQueryString = "executedQueryString" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | var ErrConnectionFailed = errors.New("failed to connect to server - please inspect Grafana server log for details") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | // SQLMacroEngine interpolates macros into sql. It takes in the Query to have access to query context and
 | 
					
						
							| 
									
										
										
										
											2017-12-09 06:04:17 +08:00
										 |  |  | // timeRange to be able to generate queries that use from and to.
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | type SQLMacroEngine interface { | 
					
						
							|  |  |  | 	Interpolate(query plugins.DataSubQuery, timeRange plugins.DataTimeRange, sql string) (string, error) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | // SqlQueryResultTransformer transforms a query result row to RowValues with proper types.
 | 
					
						
							|  |  |  | type SqlQueryResultTransformer interface { | 
					
						
							|  |  |  | 	// TransformQueryError transforms a query error.
 | 
					
						
							|  |  |  | 	TransformQueryError(err error) error | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	GetConverterList() []sqlutil.StringConverter | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type engineCacheType struct { | 
					
						
							|  |  |  | 	cache    map[int64]*xorm.Engine | 
					
						
							|  |  |  | 	versions map[int64]int | 
					
						
							|  |  |  | 	sync.Mutex | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var engineCache = engineCacheType{ | 
					
						
							|  |  |  | 	cache:    make(map[int64]*xorm.Engine), | 
					
						
							|  |  |  | 	versions: make(map[int64]int), | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | var sqlIntervalCalculator = interval.NewCalculator() | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-16 20:39:01 +08:00
										 |  |  | // NewXormEngine is an xorm.Engine factory, that can be stubbed by tests.
 | 
					
						
							| 
									
										
										
										
											2020-11-19 20:17:00 +08:00
										 |  |  | //nolint:gocritic
 | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | var NewXormEngine = func(driverName string, connectionString string) (*xorm.Engine, error) { | 
					
						
							|  |  |  | 	return xorm.NewEngine(driverName, connectionString) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | type dataPlugin struct { | 
					
						
							|  |  |  | 	macroEngine            SQLMacroEngine | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | 	queryResultTransformer SqlQueryResultTransformer | 
					
						
							|  |  |  | 	engine                 *xorm.Engine | 
					
						
							|  |  |  | 	timeColumnNames        []string | 
					
						
							|  |  |  | 	metricColumnTypes      []string | 
					
						
							|  |  |  | 	log                    log.Logger | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | type DataPluginConfiguration struct { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	DriverName        string | 
					
						
							|  |  |  | 	Datasource        *models.DataSource | 
					
						
							|  |  |  | 	ConnectionString  string | 
					
						
							|  |  |  | 	TimeColumnNames   []string | 
					
						
							|  |  |  | 	MetricColumnTypes []string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func (e *dataPlugin) transformQueryError(err error) error { | 
					
						
							|  |  |  | 	// OpError is the error type usually returned by functions in the net
 | 
					
						
							|  |  |  | 	// package. It describes the operation, network type, and address of
 | 
					
						
							|  |  |  | 	// an error. We log this error rather than return it to the client
 | 
					
						
							|  |  |  | 	// for security purposes.
 | 
					
						
							|  |  |  | 	var opErr *net.OpError | 
					
						
							|  |  |  | 	if errors.As(err, &opErr) { | 
					
						
							|  |  |  | 		e.log.Error("query error", "err", err) | 
					
						
							|  |  |  | 		return ErrConnectionFailed | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return e.queryResultTransformer.TransformQueryError(err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | // NewDataPlugin returns a new plugins.DataPlugin
 | 
					
						
							| 
									
										
										
										
											2021-04-23 09:03:11 +08:00
										 |  |  | //nolint: staticcheck // plugins.DataPlugin deprecated
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func NewDataPlugin(config DataPluginConfiguration, queryResultTransformer SqlQueryResultTransformer, | 
					
						
							|  |  |  | 	macroEngine SQLMacroEngine, log log.Logger) (plugins.DataPlugin, error) { | 
					
						
							|  |  |  | 	plugin := dataPlugin{ | 
					
						
							| 
									
										
										
										
											2019-09-25 02:50:49 +08:00
										 |  |  | 		queryResultTransformer: queryResultTransformer, | 
					
						
							|  |  |  | 		macroEngine:            macroEngine, | 
					
						
							|  |  |  | 		timeColumnNames:        []string{"time"}, | 
					
						
							|  |  |  | 		log:                    log, | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(config.TimeColumnNames) > 0 { | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 		plugin.timeColumnNames = config.TimeColumnNames | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 19:50:18 +08:00
										 |  |  | 	if len(config.MetricColumnTypes) > 0 { | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 		plugin.metricColumnTypes = config.MetricColumnTypes | 
					
						
							| 
									
										
										
										
											2018-07-30 19:50:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	engineCache.Lock() | 
					
						
							|  |  |  | 	defer engineCache.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	if engine, present := engineCache.cache[config.Datasource.Id]; present { | 
					
						
							|  |  |  | 		if version := engineCache.versions[config.Datasource.Id]; version == config.Datasource.Version { | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 			plugin.engine = engine | 
					
						
							|  |  |  | 			return &plugin, nil | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	engine, err := NewXormEngine(config.DriverName, config.ConnectionString) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 20:35:13 +08:00
										 |  |  | 	maxOpenConns := config.Datasource.JsonData.Get("maxOpenConns").MustInt(0) | 
					
						
							|  |  |  | 	engine.SetMaxOpenConns(maxOpenConns) | 
					
						
							|  |  |  | 	maxIdleConns := config.Datasource.JsonData.Get("maxIdleConns").MustInt(2) | 
					
						
							|  |  |  | 	engine.SetMaxIdleConns(maxIdleConns) | 
					
						
							|  |  |  | 	connMaxLifetime := config.Datasource.JsonData.Get("connMaxLifetime").MustInt(14400) | 
					
						
							|  |  |  | 	engine.SetConnMaxLifetime(time.Duration(connMaxLifetime) * time.Second) | 
					
						
							| 
									
										
										
										
											2017-11-15 18:07:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	engineCache.versions[config.Datasource.Id] = config.Datasource.Version | 
					
						
							|  |  |  | 	engineCache.cache[config.Datasource.Id] = engine | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	plugin.engine = engine | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	return &plugin, nil | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 19:21:40 +08:00
										 |  |  | const rowLimit = 1000000 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | // DataQuery queries for data.
 | 
					
						
							| 
									
										
										
										
											2021-04-23 09:03:11 +08:00
										 |  |  | //nolint: staticcheck // plugins.DataPlugin deprecated
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func (e *dataPlugin) DataQuery(ctx context.Context, dsInfo *models.DataSource, | 
					
						
							|  |  |  | 	queryContext plugins.DataQuery) (plugins.DataResponse, error) { | 
					
						
							|  |  |  | 	ch := make(chan plugins.DataQueryResult, len(queryContext.Queries)) | 
					
						
							| 
									
										
										
										
											2018-09-24 20:33:45 +08:00
										 |  |  | 	var wg sync.WaitGroup | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	// Execute each query in a goroutine and wait for them to finish afterwards
 | 
					
						
							|  |  |  | 	for _, query := range queryContext.Queries { | 
					
						
							|  |  |  | 		if query.Model.Get("rawSql").MustString() == "" { | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 		wg.Add(1) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		go e.executeQuery(query, &wg, queryContext, ch) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 20:33:45 +08:00
										 |  |  | 	wg.Wait() | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 	// Read results from channels
 | 
					
						
							|  |  |  | 	close(ch) | 
					
						
							|  |  |  | 	result := plugins.DataResponse{ | 
					
						
							|  |  |  | 		Results: make(map[string]plugins.DataQueryResult), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for queryResult := range ch { | 
					
						
							|  |  |  | 		result.Results[queryResult.RefID] = queryResult | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	return result, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-21 02:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 00:42:54 +08:00
										 |  |  | //nolint: staticcheck,gocyclo // plugins.DataQueryResult deprecated
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func (e *dataPlugin) executeQuery(query plugins.DataSubQuery, wg *sync.WaitGroup, queryContext plugins.DataQuery, | 
					
						
							|  |  |  | 	ch chan plugins.DataQueryResult) { | 
					
						
							|  |  |  | 	defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	queryResult := plugins.DataQueryResult{ | 
					
						
							|  |  |  | 		Meta:  simplejson.New(), | 
					
						
							|  |  |  | 		RefID: query.RefID, | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  | 			e.log.Error("executeQuery panic", "error", r, "stack", log.Stack(1)) | 
					
						
							|  |  |  | 			if theErr, ok := r.(error); ok { | 
					
						
							|  |  |  | 				queryResult.Error = theErr | 
					
						
							|  |  |  | 			} else if theErrString, ok := r.(string); ok { | 
					
						
							|  |  |  | 				queryResult.Error = fmt.Errorf(theErrString) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				queryResult.Error = fmt.Errorf("unexpected error, see the server log for details") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ch <- queryResult | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	rawSQL := query.Model.Get("rawSql").MustString() | 
					
						
							|  |  |  | 	if rawSQL == "" { | 
					
						
							|  |  |  | 		panic("Query model property rawSql should not be empty at this point") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var timeRange plugins.DataTimeRange | 
					
						
							|  |  |  | 	if queryContext.TimeRange != nil { | 
					
						
							|  |  |  | 		timeRange = *queryContext.TimeRange | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 	errAppendDebug := func(frameErr string, err error, query string) { | 
					
						
							|  |  |  | 		var emptyFrame data.Frame | 
					
						
							|  |  |  | 		emptyFrame.SetMeta(&data.FrameMeta{ | 
					
						
							|  |  |  | 			ExecutedQueryString: query, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		queryResult.Error = fmt.Errorf("%s: %w", frameErr, err) | 
					
						
							|  |  |  | 		queryResult.Dataframes = plugins.NewDecodedDataFrames(data.Frames{&emptyFrame}) | 
					
						
							|  |  |  | 		ch <- queryResult | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	// global substitutions
 | 
					
						
							|  |  |  | 	interpolatedQuery, err := Interpolate(query, timeRange, rawSQL) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		errAppendDebug("interpolation failed", e.transformQueryError(err), interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	// data source specific substitutions
 | 
					
						
							|  |  |  | 	interpolatedQuery, err = e.macroEngine.Interpolate(query, timeRange, interpolatedQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		errAppendDebug("interpolation failed", e.transformQueryError(err), interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	session := e.engine.NewSession() | 
					
						
							|  |  |  | 	defer session.Close() | 
					
						
							|  |  |  | 	db := session.DB() | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	rows, err := db.Query(interpolatedQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		errAppendDebug("db query error", e.transformQueryError(err), interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := rows.Close(); err != nil { | 
					
						
							|  |  |  | 			e.log.Warn("Failed to close rows", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	qm, err := e.newProcessCfg(query, queryContext, rows, interpolatedQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		errAppendDebug("failed to get configurations", err, interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	// Convert row.Rows to dataframe
 | 
					
						
							| 
									
										
										
										
											2021-05-11 20:59:33 +08:00
										 |  |  | 	stringConverters := e.queryResultTransformer.GetConverterList() | 
					
						
							|  |  |  | 	frame, err := sqlutil.FrameFromRows(rows.Rows, rowLimit, sqlutil.ToConverters(stringConverters...)...) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		errAppendDebug("convert frame from rows error", err, interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-12-14 00:25:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	frame.SetMeta(&data.FrameMeta{ | 
					
						
							|  |  |  | 		ExecutedQueryString: interpolatedQuery, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If no rows were returned, no point checking anything else.
 | 
					
						
							|  |  |  | 	if frame.Rows() == 0 { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 		queryResult.Dataframes = plugins.NewDecodedDataFrames(data.Frames{frame}) | 
					
						
							|  |  |  | 		ch <- queryResult | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	if qm.timeIndex != -1 { | 
					
						
							|  |  |  | 		if err := convertSQLTimeColumnToEpochMS(frame, qm.timeIndex); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 			errAppendDebug("db convert time column failed", err, interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	if qm.Format == dataQueryFormatSeries { | 
					
						
							|  |  |  | 		// time series has to have time column
 | 
					
						
							|  |  |  | 		if qm.timeIndex == -1 { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 			errAppendDebug("db has no time column", errors.New("no time column found"), interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-14 17:29:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Make sure to name the time field 'Time' to be backward compatible with Grafana pre-v8.
 | 
					
						
							|  |  |  | 		frame.Fields[qm.timeIndex].Name = data.TimeSeriesTimeFieldName | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		for i := range qm.columnNames { | 
					
						
							|  |  |  | 			if i == qm.timeIndex || i == qm.metricIndex { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 00:42:54 +08:00
										 |  |  | 			if t := frame.Fields[i].Type(); t == data.FieldTypeString || t == data.FieldTypeNullableString { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			var err error | 
					
						
							|  |  |  | 			if frame, err = convertSQLValueColumnToFloat(frame, i); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 				errAppendDebug("convert value to float failed", err, interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tsSchema := frame.TimeSeriesSchema() | 
					
						
							|  |  |  | 		if tsSchema.Type == data.TimeSeriesTypeLong { | 
					
						
							|  |  |  | 			var err error | 
					
						
							| 
									
										
										
										
											2021-06-18 14:05:23 +08:00
										 |  |  | 			originalData := frame | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			frame, err = data.LongToWide(frame, qm.FillMissing) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-09 21:14:28 +08:00
										 |  |  | 				errAppendDebug("failed to convert long to wide series when converting from dataframe", err, interpolatedQuery) | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-06-18 14:05:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Before 8x, a special metric column was used to name time series. The LongToWide transforms that into a metric label on the value field.
 | 
					
						
							|  |  |  | 			// But that makes series name have both the value column name AND the metric name. So here we are removing the metric label here and moving it to the
 | 
					
						
							|  |  |  | 			// field name to get the same naming for the series as pre v8
 | 
					
						
							|  |  |  | 			if len(originalData.Fields) == 3 { | 
					
						
							|  |  |  | 				for _, field := range frame.Fields { | 
					
						
							|  |  |  | 					if len(field.Labels) == 1 { // 7x only supported one label
 | 
					
						
							|  |  |  | 						name, ok := field.Labels["metric"] | 
					
						
							|  |  |  | 						if ok { | 
					
						
							|  |  |  | 							field.Name = name | 
					
						
							|  |  |  | 							field.Labels = nil | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if qm.FillMissing != nil { | 
					
						
							|  |  |  | 			var err error | 
					
						
							|  |  |  | 			frame, err = resample(frame, *qm) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				e.log.Error("Failed to resample dataframe", "err", err) | 
					
						
							|  |  |  | 				frame.AppendNotices(data.Notice{Text: "Failed to resample dataframe", Severity: data.NoticeSeverityWarning}) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	queryResult.Dataframes = plugins.NewDecodedDataFrames(data.Frames{frame}) | 
					
						
							|  |  |  | 	ch <- queryResult | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | // Interpolate provides global macros/substitutions for all sql datasources.
 | 
					
						
							|  |  |  | var Interpolate = func(query plugins.DataSubQuery, timeRange plugins.DataTimeRange, sql string) (string, error) { | 
					
						
							|  |  |  | 	minInterval, err := interval.GetIntervalFrom(query.DataSource, query.Model, time.Second*60) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	interval := sqlIntervalCalculator.Calculate(timeRange, minInterval) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	sql = strings.ReplaceAll(sql, "$__interval_ms", strconv.FormatInt(interval.Milliseconds(), 10)) | 
					
						
							|  |  |  | 	sql = strings.ReplaceAll(sql, "$__interval", interval.Text) | 
					
						
							|  |  |  | 	sql = strings.ReplaceAll(sql, "$__unixEpochFrom()", fmt.Sprintf("%d", timeRange.GetFromAsSecondsEpoch())) | 
					
						
							|  |  |  | 	sql = strings.ReplaceAll(sql, "$__unixEpochTo()", fmt.Sprintf("%d", timeRange.GetToAsSecondsEpoch())) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sql, nil | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | //nolint: staticcheck // plugins.DataPlugin deprecated
 | 
					
						
							|  |  |  | func (e *dataPlugin) newProcessCfg(query plugins.DataSubQuery, queryContext plugins.DataQuery, | 
					
						
							|  |  |  | 	rows *core.Rows, interpolatedQuery string) (*dataQueryModel, error) { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	columnNames, err := rows.Columns() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	columnTypes, err := rows.ColumnTypes() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	qm := &dataQueryModel{ | 
					
						
							|  |  |  | 		columnTypes:  columnTypes, | 
					
						
							|  |  |  | 		columnNames:  columnNames, | 
					
						
							|  |  |  | 		rows:         rows, | 
					
						
							|  |  |  | 		timeIndex:    -1, | 
					
						
							|  |  |  | 		metricIndex:  -1, | 
					
						
							|  |  |  | 		metricPrefix: false, | 
					
						
							|  |  |  | 		queryContext: queryContext, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	if query.Model.Get("fill").MustBool(false) { | 
					
						
							|  |  |  | 		qm.FillMissing = &data.FillMissing{} | 
					
						
							|  |  |  | 		qm.Interval = time.Duration(query.Model.Get("fillInterval").MustFloat64() * float64(time.Second)) | 
					
						
							|  |  |  | 		switch strings.ToLower(query.Model.Get("fillMode").MustString()) { | 
					
						
							|  |  |  | 		case "null": | 
					
						
							|  |  |  | 			qm.FillMissing.Mode = data.FillModeNull | 
					
						
							|  |  |  | 		case "previous": | 
					
						
							|  |  |  | 			qm.FillMissing.Mode = data.FillModePrevious | 
					
						
							|  |  |  | 		case "value": | 
					
						
							|  |  |  | 			qm.FillMissing.Mode = data.FillModeValue | 
					
						
							|  |  |  | 			qm.FillMissing.Value = query.Model.Get("fillValue").MustFloat64() | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	//nolint: staticcheck // plugins.DataPlugin deprecated
 | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	if queryContext.TimeRange != nil { | 
					
						
							|  |  |  | 		qm.TimeRange.From = queryContext.TimeRange.GetFromAsTimeUTC() | 
					
						
							|  |  |  | 		qm.TimeRange.To = queryContext.TimeRange.GetToAsTimeUTC() | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	format := query.Model.Get("format").MustString("time_series") | 
					
						
							|  |  |  | 	switch format { | 
					
						
							|  |  |  | 	case "time_series": | 
					
						
							|  |  |  | 		qm.Format = dataQueryFormatSeries | 
					
						
							|  |  |  | 	case "table": | 
					
						
							|  |  |  | 		qm.Format = dataQueryFormatTable | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("Unrecognized query model format: %q", format)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, col := range qm.columnNames { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		for _, tc := range e.timeColumnNames { | 
					
						
							|  |  |  | 			if col == tc { | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 				qm.timeIndex = i | 
					
						
							|  |  |  | 				break | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		switch col { | 
					
						
							|  |  |  | 		case "metric": | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			qm.metricIndex = i | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			if qm.metricIndex == -1 { | 
					
						
							|  |  |  | 				columnType := qm.columnTypes[i].DatabaseTypeName() | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 				for _, mct := range e.metricColumnTypes { | 
					
						
							|  |  |  | 					if columnType == mct { | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 						qm.metricIndex = i | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 						continue | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	qm.InterpolatedQuery = interpolatedQuery | 
					
						
							|  |  |  | 	return qm, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | // dataQueryFormat is the type of query.
 | 
					
						
							|  |  |  | type dataQueryFormat string | 
					
						
							| 
									
										
										
										
											2018-07-25 01:25:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// dataQueryFormatTable identifies a table query (default).
 | 
					
						
							|  |  |  | 	dataQueryFormatTable dataQueryFormat = "table" | 
					
						
							|  |  |  | 	// dataQueryFormatSeries identifies a time series query.
 | 
					
						
							|  |  |  | 	dataQueryFormatSeries dataQueryFormat = "time_series" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type dataQueryModel struct { | 
					
						
							|  |  |  | 	InterpolatedQuery string // property not set until after Interpolate()
 | 
					
						
							|  |  |  | 	Format            dataQueryFormat | 
					
						
							|  |  |  | 	TimeRange         backend.TimeRange | 
					
						
							|  |  |  | 	FillMissing       *data.FillMissing // property not set until after Interpolate()
 | 
					
						
							|  |  |  | 	Interval          time.Duration | 
					
						
							|  |  |  | 	columnNames       []string | 
					
						
							|  |  |  | 	columnTypes       []*sql.ColumnType | 
					
						
							|  |  |  | 	timeIndex         int | 
					
						
							|  |  |  | 	metricIndex       int | 
					
						
							|  |  |  | 	rows              *core.Rows | 
					
						
							|  |  |  | 	metricPrefix      bool | 
					
						
							|  |  |  | 	queryContext      plugins.DataQuery | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func convertInt64ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(int64)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableInt64ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int64) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt64ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(uint64)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt64ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint64) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertInt32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(int32)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableInt32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(uint32)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertInt16ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(int16)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableInt16ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int16) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt16ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(uint16)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-29 19:03:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt16ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint16) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertInt8ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(int8)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableInt8ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int8) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-30 17:04:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt8ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(uint8)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt8ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint8) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUnknownToZero(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(0) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableFloat32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*float32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := float64(*iv) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertFloat32ToFloat64(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := float64(origin.At(i).(float32)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func convertInt64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(float64(origin.At(i).(int64))))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func convertNullableInt64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int64) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(float64(*iv)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(float64(origin.At(i).(uint64))))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint64) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(float64(*iv)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertInt32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(float64(origin.At(i).(int32))))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableInt32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*int32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(float64(*iv)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertUInt32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(float64(origin.At(i).(uint32))))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableUInt32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*uint32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(float64(*iv)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-30 17:04:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertFloat64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(origin.At(i).(float64)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableFloat64ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*float64) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(*iv))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertFloat32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		value := time.Unix(0, int64(epochPrecisionToMS(float64(origin.At(i).(float32))))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 		newField.Append(&value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertNullableFloat32ToEpochMS(origin *data.Field, newField *data.Field) { | 
					
						
							|  |  |  | 	valueLength := origin.Len() | 
					
						
							|  |  |  | 	for i := 0; i < valueLength; i++ { | 
					
						
							|  |  |  | 		iv := origin.At(i).(*float32) | 
					
						
							|  |  |  | 		if iv == nil { | 
					
						
							|  |  |  | 			newField.Append(nil) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			value := time.Unix(0, int64(epochPrecisionToMS(float64(*iv)))*int64(time.Millisecond)) | 
					
						
							|  |  |  | 			newField.Append(&value) | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-27 00:09:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | // convertSQLTimeColumnToEpochMS converts column named time to unix timestamp in milliseconds
 | 
					
						
							| 
									
										
										
										
											2018-03-21 02:40:10 +08:00
										 |  |  | // to make native datetime types and epoch dates work in annotation and table queries.
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertSQLTimeColumnToEpochMS(frame *data.Frame, timeIndex int) error { | 
					
						
							|  |  |  | 	if timeIndex < 0 || timeIndex >= len(frame.Fields) { | 
					
						
							|  |  |  | 		return fmt.Errorf("timeIndex %d is out of range", timeIndex) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	origin := frame.Fields[timeIndex] | 
					
						
							|  |  |  | 	valueType := origin.Type() | 
					
						
							|  |  |  | 	if valueType == data.FieldTypeTime || valueType == data.FieldTypeNullableTime { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newField := data.NewFieldFromFieldType(data.FieldTypeNullableTime, 0) | 
					
						
							|  |  |  | 	newField.Name = origin.Name | 
					
						
							|  |  |  | 	newField.Labels = origin.Labels | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch valueType { | 
					
						
							|  |  |  | 	case data.FieldTypeInt64: | 
					
						
							|  |  |  | 		convertInt64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt64: | 
					
						
							|  |  |  | 		convertNullableInt64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint64: | 
					
						
							|  |  |  | 		convertUInt64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint64: | 
					
						
							|  |  |  | 		convertNullableUInt64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeInt32: | 
					
						
							|  |  |  | 		convertInt32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt32: | 
					
						
							|  |  |  | 		convertNullableInt32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint32: | 
					
						
							|  |  |  | 		convertUInt32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint32: | 
					
						
							|  |  |  | 		convertNullableUInt32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeFloat64: | 
					
						
							|  |  |  | 		convertFloat64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableFloat64: | 
					
						
							|  |  |  | 		convertNullableFloat64ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeFloat32: | 
					
						
							|  |  |  | 		convertFloat32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableFloat32: | 
					
						
							|  |  |  | 		convertNullableFloat32ToEpochMS(frame.Fields[timeIndex], newField) | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return fmt.Errorf("column type %q is not convertible to time.Time", valueType) | 
					
						
							| 
									
										
										
										
											2018-03-21 02:40:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	frame.Fields[timeIndex] = newField | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-03-21 02:40:10 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | // convertSQLValueColumnToFloat converts timeseries value column to float.
 | 
					
						
							| 
									
										
										
										
											2020-08-18 20:43:18 +08:00
										 |  |  | //nolint: gocyclo
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | func convertSQLValueColumnToFloat(frame *data.Frame, Index int) (*data.Frame, error) { | 
					
						
							|  |  |  | 	if Index < 0 || Index >= len(frame.Fields) { | 
					
						
							|  |  |  | 		return frame, fmt.Errorf("metricIndex %d is out of range", Index) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	origin := frame.Fields[Index] | 
					
						
							|  |  |  | 	valueType := origin.Type() | 
					
						
							|  |  |  | 	if valueType == data.FieldTypeFloat64 || valueType == data.FieldTypeNullableFloat64 { | 
					
						
							|  |  |  | 		return frame, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newField := data.NewFieldFromFieldType(data.FieldTypeNullableFloat64, 0) | 
					
						
							|  |  |  | 	newField.Name = origin.Name | 
					
						
							|  |  |  | 	newField.Labels = origin.Labels | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch valueType { | 
					
						
							|  |  |  | 	case data.FieldTypeInt64: | 
					
						
							|  |  |  | 		convertInt64ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt64: | 
					
						
							|  |  |  | 		convertNullableInt64ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint64: | 
					
						
							|  |  |  | 		convertUInt64ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint64: | 
					
						
							|  |  |  | 		convertNullableUInt64ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeInt32: | 
					
						
							|  |  |  | 		convertInt32ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt32: | 
					
						
							|  |  |  | 		convertNullableInt32ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint32: | 
					
						
							|  |  |  | 		convertUInt32ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint32: | 
					
						
							|  |  |  | 		convertNullableUInt32ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeInt16: | 
					
						
							|  |  |  | 		convertInt16ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt16: | 
					
						
							|  |  |  | 		convertNullableInt16ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint16: | 
					
						
							|  |  |  | 		convertUInt16ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint16: | 
					
						
							|  |  |  | 		convertNullableUInt16ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeInt8: | 
					
						
							|  |  |  | 		convertInt8ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableInt8: | 
					
						
							|  |  |  | 		convertNullableInt8ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeUint8: | 
					
						
							|  |  |  | 		convertUInt8ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableUint8: | 
					
						
							|  |  |  | 		convertNullableUInt8ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeFloat32: | 
					
						
							|  |  |  | 		convertFloat32ToFloat64(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 	case data.FieldTypeNullableFloat32: | 
					
						
							|  |  |  | 		convertNullableFloat32ToFloat64(frame.Fields[Index], newField) | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 		convertUnknownToZero(frame.Fields[Index], newField) | 
					
						
							|  |  |  | 		frame.Fields[Index] = newField | 
					
						
							| 
									
										
										
										
											2021-07-21 00:42:54 +08:00
										 |  |  | 		return frame, fmt.Errorf("metricIndex %d type %s can't be converted to float", Index, valueType) | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	frame.Fields[Index] = newField | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 22:46:07 +08:00
										 |  |  | 	return frame, nil | 
					
						
							| 
									
										
										
										
											2018-04-25 01:50:14 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-12 16:51:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func SetupFillmode(query plugins.DataSubQuery, interval time.Duration, fillmode string) error { | 
					
						
							| 
									
										
										
										
											2018-08-12 16:51:58 +08:00
										 |  |  | 	query.Model.Set("fill", true) | 
					
						
							|  |  |  | 	query.Model.Set("fillInterval", interval.Seconds()) | 
					
						
							|  |  |  | 	switch fillmode { | 
					
						
							|  |  |  | 	case "NULL": | 
					
						
							|  |  |  | 		query.Model.Set("fillMode", "null") | 
					
						
							|  |  |  | 	case "previous": | 
					
						
							|  |  |  | 		query.Model.Set("fillMode", "previous") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		query.Model.Set("fillMode", "value") | 
					
						
							|  |  |  | 		floatVal, err := strconv.ParseFloat(fillmode, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("error parsing fill value %v", fillmode) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		query.Model.Set("fillValue", floatVal) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | type SQLMacroEngineBase struct{} | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func NewSQLMacroEngineBase() *SQLMacroEngineBase { | 
					
						
							|  |  |  | 	return &SQLMacroEngineBase{} | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func (m *SQLMacroEngineBase) ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { | 
					
						
							| 
									
										
										
										
											2018-09-13 22:51:00 +08:00
										 |  |  | 	result := "" | 
					
						
							|  |  |  | 	lastIndex := 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { | 
					
						
							|  |  |  | 		groups := []string{} | 
					
						
							|  |  |  | 		for i := 0; i < len(v); i += 2 { | 
					
						
							|  |  |  | 			groups = append(groups, str[v[i]:v[i+1]]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		result += str[lastIndex:v[0]] + repl(groups) | 
					
						
							|  |  |  | 		lastIndex = v[1] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return result + str[lastIndex:] | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // epochPrecisionToMS converts epoch precision to millisecond, if needed.
 | 
					
						
							|  |  |  | // Only seconds to milliseconds supported right now
 | 
					
						
							|  |  |  | func epochPrecisionToMS(value float64) float64 { | 
					
						
							|  |  |  | 	s := strconv.FormatFloat(value, 'e', -1, 64) | 
					
						
							|  |  |  | 	if strings.HasSuffix(s, "e+09") { | 
					
						
							|  |  |  | 		return value * float64(1e3) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if strings.HasSuffix(s, "e+18") { | 
					
						
							|  |  |  | 		return value / float64(time.Millisecond) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return value | 
					
						
							|  |  |  | } |