sqleng

package
v0.0.0-...-34898ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const MetaKeyExecutedQueryString = "executedQueryString"

MetaKeyExecutedQueryString is the key where the executed query should get stored

Variables

View Source
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.

View Source
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
}
View Source
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

func ConvertSqlTimeColumnToEpochMs(values tsdb.RowValues, timeIndex int)

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

func ConvertSqlValueColumnToFloat(columnName string, columnValue interface{}) (null.Float, error)

ConvertSqlValueColumnToFloat converts timeseries value column to float. nolint: gocyclo

func SetupFillmode

func SetupFillmode(query *tsdb.Query, interval time.Duration, fillmode string) error

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

func (m *SqlMacroEngineBase) ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string

type SqlQueryEndpointConfiguration

type SqlQueryEndpointConfiguration struct {
	DriverName        string
	Datasource        *models.DataSource
	ConnectionString  string
	TimeColumnNames   []string
	MetricColumnTypes []string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL