Version: v6.1.6+incompatible Latest Latest

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

Go to latest
Published: Apr 29, 2019 License: Apache-2.0 Imports: 16 Imported by: 1,063




This section is empty.


View Source
var Interpolate = func(query *Query, timeRange *TimeRange, sql string) (string, error) {
	minInterval, err := GetIntervalFrom(query.DataSource, query.Model, time.Second*60)
	if err != nil {
		return sql, nil
	interval := sqlIntervalCalculator.Calculate(timeRange, minInterval)

	sql = strings.Replace(sql, "$__interval_ms", strconv.FormatInt(interval.Milliseconds(), 10), -1)
	sql = strings.Replace(sql, "$__interval", interval.Text, -1)
	sql = strings.Replace(sql, "$__unixEpochFrom()", fmt.Sprintf("%d", timeRange.GetFromAsSecondsEpoch()), -1)
	sql = strings.Replace(sql, "$__unixEpochTo()", fmt.Sprintf("%d", timeRange.GetToAsSecondsEpoch()), -1)

	return sql, nil

global macros/substitutions for all sql datasources

View Source
var NewSqlQueryEndpoint = func(config *SqlQueryEndpointConfiguration, rowTransformer SqlTableRowTransformer, macroEngine SqlMacroEngine, log log.Logger) (TsdbQueryEndpoint, error) {
	queryEndpoint := sqlQueryEndpoint{
		rowTransformer:  rowTransformer,
		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

	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)
	maxIdleConns := config.Datasource.JsonData.Get("maxIdleConns").MustInt(2)
	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)


func ConvertSqlTimeColumnToEpochMs

func ConvertSqlTimeColumnToEpochMs(values 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.

func EpochPrecisionToMs

func EpochPrecisionToMs(value float64) float64

EpochPrecisionToMs converts epoch precision to millisecond, if needed. Only seconds to milliseconds supported right now

func FormatDuration

func FormatDuration(inter time.Duration) string

FormatDuration converts a duration into the kbn format e.g. 1m 2h or 3d

func GetIntervalFrom

func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error)

func NewIntervalCalculator

func NewIntervalCalculator(opt *IntervalOptions) *intervalCalculator

func RegisterTsdbQueryEndpoint

func RegisterTsdbQueryEndpoint(pluginId string, fn GetTsdbQueryEndpointFn)

func SetupFillmode

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


type GetTsdbQueryEndpointFn

type GetTsdbQueryEndpointFn func(dsInfo *models.DataSource) (TsdbQueryEndpoint, error)

type HandleRequestFunc

type HandleRequestFunc func(ctx context.Context, dsInfo *models.DataSource, req *TsdbQuery) (*Response, error)

type Interval

type Interval struct {
	Text  string
	Value time.Duration

func (*Interval) Milliseconds

func (i *Interval) Milliseconds() int64

type IntervalCalculator

type IntervalCalculator interface {
	Calculate(timeRange *TimeRange, minInterval time.Duration) Interval

type IntervalOptions

type IntervalOptions struct {
	MinInterval time.Duration

type Query

type Query struct {
	RefId         string
	Model         *simplejson.Json
	DataSource    *models.DataSource
	MaxDataPoints int64
	IntervalMs    int64

type QueryResult

type QueryResult struct {
	Error       error            `json:"-"`
	ErrorString string           `json:"error,omitempty"`
	RefId       string           `json:"refId"`
	Meta        *simplejson.Json `json:"meta,omitempty"`
	Series      TimeSeriesSlice  `json:"series"`
	Tables      []*Table         `json:"tables"`

func NewQueryResult

func NewQueryResult() *QueryResult

type Response

type Response struct {
	Results map[string]*QueryResult `json:"results"`
	Message string                  `json:"message,omitempty"`

func HandleRequest

func HandleRequest(ctx context.Context, dsInfo *models.DataSource, req *TsdbQuery) (*Response, error)

type RowValues

type RowValues []interface{}

type SqlMacroEngine

type SqlMacroEngine interface {
	Interpolate(query *Query, timeRange *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 SqlTableRowTransformer

type SqlTableRowTransformer interface {
	Transform(columnTypes []*sql.ColumnType, rows *core.Rows) (RowValues, error)

SqlTableRowTransformer transforms a query result row to RowValues with proper types.

type Table

type Table struct {
	Columns []TableColumn `json:"columns"`
	Rows    []RowValues   `json:"rows"`

type TableColumn

type TableColumn struct {
	Text string `json:"text"`

type TimePoint

type TimePoint [2]null.Float

func NewTimePoint

func NewTimePoint(value null.Float, timestamp float64) TimePoint

type TimeRange

type TimeRange struct {
	From string
	To   string
	// contains filtered or unexported fields

func NewFakeTimeRange

func NewFakeTimeRange(from, to string, now time.Time) *TimeRange

func NewTimeRange

func NewTimeRange(from, to string) *TimeRange

func (*TimeRange) GetFromAsMsEpoch

func (tr *TimeRange) GetFromAsMsEpoch() int64

func (*TimeRange) GetFromAsSecondsEpoch

func (tr *TimeRange) GetFromAsSecondsEpoch() int64

func (*TimeRange) GetFromAsTimeUTC

func (tr *TimeRange) GetFromAsTimeUTC() time.Time

func (*TimeRange) GetToAsMsEpoch

func (tr *TimeRange) GetToAsMsEpoch() int64

func (*TimeRange) GetToAsSecondsEpoch

func (tr *TimeRange) GetToAsSecondsEpoch() int64

func (*TimeRange) GetToAsTimeUTC

func (tr *TimeRange) GetToAsTimeUTC() time.Time

func (*TimeRange) MustGetFrom

func (tr *TimeRange) MustGetFrom() time.Time

func (*TimeRange) MustGetTo

func (tr *TimeRange) MustGetTo() time.Time

func (*TimeRange) ParseFrom

func (tr *TimeRange) ParseFrom() (time.Time, error)

func (*TimeRange) ParseTo

func (tr *TimeRange) ParseTo() (time.Time, error)

type TimeSeries

type TimeSeries struct {
	Name   string            `json:"name"`
	Points TimeSeriesPoints  `json:"points"`
	Tags   map[string]string `json:"tags,omitempty"`

func NewTimeSeries

func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries

type TimeSeriesPoints

type TimeSeriesPoints []TimePoint

func NewTimeSeriesPointsFromArgs

func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints

type TimeSeriesSlice

type TimeSeriesSlice []*TimeSeries

type TsdbQuery

type TsdbQuery struct {
	TimeRange *TimeRange
	Queries   []*Query

type TsdbQueryEndpoint

type TsdbQueryEndpoint interface {
	Query(ctx context.Context, ds *models.DataSource, query *TsdbQuery) (*Response, error)

Jump to

Keyboard shortcuts

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