Documentation ¶
Index ¶
- Constants
- Variables
- func ConvertSqlTimeColumnToEpochMs(values tsdb.RowValues, timeIndex int)
- func ConvertSqlValueColumnToFloat(columnName string, columnValue interface{}) (null.Float, error)
- func SetupFillmode(query *tsdb.Query, interval time.Duration, fillmode string) error
- type SqlMacroEngine
- type SqlMacroEngineBase
- type SqlQueryEndpointConfiguration
- type SqlQueryResultTransformer
Constants ¶
const MetaKeyExecutedQueryString = "executedQueryString"
MetaKeyExecutedQueryString is the key where the executed query should get stored
Variables ¶
var Interpolate = func(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) { minInterval, err := tsdb.GetIntervalFrom(query.DataSource, query.Model, time.Second*60) if err != nil { return sql, nil } interval := sqlIntervalCalculator.Calculate(timeRange, minInterval) 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 }
Interpolate provides global macros/substitutions for all sql datasources.
var NewSqlQueryEndpoint = func(config *SqlQueryEndpointConfiguration, queryResultTransformer SqlQueryResultTransformer, macroEngine SqlMacroEngine, log log.Logger) (tsdb.TsdbQueryEndpoint, error) { queryEndpoint := sqlQueryEndpoint{ queryResultTransformer: queryResultTransformer, macroEngine: macroEngine, timeColumnNames: []string{"time"}, log: log, } if len(config.TimeColumnNames) > 0 { queryEndpoint.timeColumnNames = config.TimeColumnNames } if len(config.MetricColumnTypes) > 0 { queryEndpoint.metricColumnTypes = config.MetricColumnTypes } engineCache.Lock() defer engineCache.Unlock() if engine, present := engineCache.cache[config.Datasource.Id]; present { if version := engineCache.versions[config.Datasource.Id]; version == config.Datasource.Version { queryEndpoint.engine = engine return &queryEndpoint, nil } } engine, err := NewXormEngine(config.DriverName, config.ConnectionString) if err != nil { return nil, err } 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) engineCache.versions[config.Datasource.Id] = config.Datasource.Version engineCache.cache[config.Datasource.Id] = engine queryEndpoint.engine = engine return &queryEndpoint, nil }
var NewXormEngine = func(driverName string, connectionString string) (*xorm.Engine, error) { return xorm.NewEngine(driverName, connectionString) }
NewXormEngine is an xorm.Engine factory, that can be stubbed by tests.
Functions ¶
func ConvertSqlTimeColumnToEpochMs ¶
ConvertSqlTimeColumnToEpochMs converts column named time to unix timestamp in milliseconds to make native datetime types and epoch dates work in annotation and table queries.
func ConvertSqlValueColumnToFloat ¶
ConvertSqlValueColumnToFloat converts timeseries value column to float. nolint: gocyclo
Types ¶
type SqlMacroEngine ¶
type SqlMacroEngine interface {
Interpolate(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error)
}
SqlMacroEngine interpolates macros into sql. It takes in the Query to have access to query context and timeRange to be able to generate queries that use from and to.
type SqlMacroEngineBase ¶
type SqlMacroEngineBase struct{}
func NewSqlMacroEngineBase ¶
func NewSqlMacroEngineBase() *SqlMacroEngineBase
func (*SqlMacroEngineBase) ReplaceAllStringSubmatchFunc ¶
type SqlQueryResultTransformer ¶
type SqlQueryResultTransformer interface { // TransformQueryResult transforms a query result row to RowValues with proper types. TransformQueryResult(columnTypes []*sql.ColumnType, rows *core.Rows) (tsdb.RowValues, error) // TransformQueryError transforms a query error. TransformQueryError(err error) error }
SqlQueryResultTransformer transforms a query result row to RowValues with proper types.