mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
mysql: progress
This commit is contained in:
parent
1e29d4fcfa
commit
11806dfa78
@ -45,10 +45,11 @@ func (br *BatchResult) WithError(err error) *BatchResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type QueryResult struct {
|
type QueryResult struct {
|
||||||
Error error `json:"-"`
|
Error error `json:"-"`
|
||||||
ErrorString string `json:"error"`
|
ErrorString string `json:"error,omitempty"`
|
||||||
RefId string `json:"refId"`
|
RefId string `json:"refId"`
|
||||||
Series TimeSeriesSlice `json:"series"`
|
Meta *simplejson.Json `json:"meta,omitempty"`
|
||||||
|
Series TimeSeriesSlice `json:"series"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TimeSeries struct {
|
type TimeSeries struct {
|
||||||
|
@ -30,8 +30,8 @@ func (m *MySqlMacroEngine) Interpolate(sql string) (string, error) {
|
|||||||
var macroError error
|
var macroError error
|
||||||
|
|
||||||
sql = ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
|
sql = ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
|
||||||
res, err := m.EvaluateMacro(groups[1], groups[2:len(groups)])
|
res, err := m.EvaluateMacro(groups[1], groups[2:])
|
||||||
if macroError != nil {
|
if err != nil && macroError == nil {
|
||||||
macroError = err
|
macroError = err
|
||||||
return "macro_error()"
|
return "macro_error()"
|
||||||
}
|
}
|
||||||
@ -68,7 +68,12 @@ func (m *MySqlMacroEngine) EvaluateMacro(name string, args []string) (string, er
|
|||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return "", fmt.Errorf("missing time column argument for macro %v", name)
|
return "", fmt.Errorf("missing time column argument for macro %v", name)
|
||||||
}
|
}
|
||||||
return "UNIX_TIMESTAMP(" + args[0] + ") as time_sec", nil
|
return fmt.Sprintf("UNIX_TIMESTAMP(%s) as time_sec", args[0]), nil
|
||||||
|
case "__timeFilter":
|
||||||
|
if len(args) == 0 {
|
||||||
|
return "", fmt.Errorf("missing time column argument for macro %v", name)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("UNIX_TIMESTAMP(%s) > %d AND UNIX_TIMESTAMP(%s) < %d", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("Unknown macro %v", name)
|
return "", fmt.Errorf("Unknown macro %v", name)
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,14 @@ package mysql
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMacroEngine(t *testing.T) {
|
func TestMacroEngine(t *testing.T) {
|
||||||
Convey("MacroEngine", t, func() {
|
Convey("MacroEngine", t, func() {
|
||||||
|
|
||||||
Convey("interpolate simple function", func() {
|
Convey("interpolate __time function", func() {
|
||||||
engine := &MySqlMacroEngine{}
|
engine := &MySqlMacroEngine{}
|
||||||
|
|
||||||
sql, err := engine.Interpolate("select $__time(time_column)")
|
sql, err := engine.Interpolate("select $__time(time_column)")
|
||||||
@ -18,5 +19,16 @@ func TestMacroEngine(t *testing.T) {
|
|||||||
So(sql, ShouldEqual, "select UNIX_TIMESTAMP(time_column) as time_sec")
|
So(sql, ShouldEqual, "select UNIX_TIMESTAMP(time_column) as time_sec")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("interpolate __timeFilter function", func() {
|
||||||
|
engine := &MySqlMacroEngine{
|
||||||
|
TimeRange: &tsdb.TimeRange{From: "5m", To: "now"},
|
||||||
|
}
|
||||||
|
|
||||||
|
sql, err := engine.Interpolate("WHERE $__timeFilter(time_column)")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
So(sql, ShouldEqual, "WHERE UNIX_TIMESTAMP(time_column) > 18446744066914186738 AND UNIX_TIMESTAMP(time_column) < 18446744066914187038")
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/components/null"
|
"github.com/grafana/grafana/pkg/components/null"
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
@ -81,6 +82,7 @@ func (e *MysqlExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, co
|
|||||||
QueryResults: make(map[string]*tsdb.QueryResult),
|
QueryResults: make(map[string]*tsdb.QueryResult),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macroEngine := NewMysqlMacroEngine(context.TimeRange)
|
||||||
session := e.engine.NewSession()
|
session := e.engine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
db := session.DB()
|
db := session.DB()
|
||||||
@ -91,9 +93,20 @@ func (e *MysqlExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, co
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: query.RefId}
|
||||||
|
result.QueryResults[query.RefId] = queryResult
|
||||||
|
|
||||||
|
rawSql, err := macroEngine.Interpolate(rawSql)
|
||||||
|
if err != nil {
|
||||||
|
queryResult.Error = err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
queryResult.Meta.Set("sql", rawSql)
|
||||||
|
|
||||||
rows, err := db.Query(rawSql)
|
rows, err := db.Query(rawSql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.QueryResults[query.RefId] = &tsdb.QueryResult{Error: err}
|
queryResult.Error = err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,16 +114,24 @@ func (e *MysqlExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, co
|
|||||||
|
|
||||||
res, err := e.TransformToTimeSeries(query, rows)
|
res, err := e.TransformToTimeSeries(query, rows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err
|
queryResult.Error = err
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
result.QueryResults[query.RefId] = &tsdb.QueryResult{RefId: query.RefId, Series: res}
|
queryResult.Series = res
|
||||||
|
queryResult.Meta.Set("rowCount", countPointsInAllSeries(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countPointsInAllSeries(seriesList tsdb.TimeSeriesSlice) (count int) {
|
||||||
|
for _, series := range seriesList {
|
||||||
|
count += len(series.Points)
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
func (e MysqlExecutor) TransformToTimeSeries(query *tsdb.Query, rows *core.Rows) (tsdb.TimeSeriesSlice, error) {
|
func (e MysqlExecutor) TransformToTimeSeries(query *tsdb.Query, rows *core.Rows) (tsdb.TimeSeriesSlice, error) {
|
||||||
pointsBySeries := make(map[string]*tsdb.TimeSeries)
|
pointsBySeries := make(map[string]*tsdb.TimeSeries)
|
||||||
columnNames, err := rows.Columns()
|
columnNames, err := rows.Columns()
|
||||||
|
Loading…
Reference in New Issue
Block a user