ソースを参照

mssql: cleanup and minor refactor

Marcus Efraimsson 7 年 前
コミット
d14946a135
3 ファイル変更34 行追加46 行削除
  1. 2 2
      pkg/tsdb/mssql/macros.go
  2. 28 27
      pkg/tsdb/mssql/macros_test.go
  3. 4 17
      pkg/tsdb/mssql/mssql.go

+ 2 - 2
pkg/tsdb/mssql/macros.go

@@ -6,11 +6,11 @@ import (
 	"strings"
 	"time"
 
-	"github.com/grafana/grafana/pkg/tsdb"
 	"strconv"
+
+	"github.com/grafana/grafana/pkg/tsdb"
 )
 
-//const rsString = `(?:"([^"]*)")`;
 const rsIdentifier = `([_a-zA-Z0-9]+)`
 const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
 

+ 28 - 27
pkg/tsdb/mssql/macros_test.go

@@ -3,10 +3,11 @@ package mssql
 import (
 	"testing"
 
+	"time"
+
 	"github.com/grafana/grafana/pkg/components/simplejson"
 	"github.com/grafana/grafana/pkg/tsdb"
 	. "github.com/smartystreets/goconvey/convey"
-	"time"
 )
 
 func TestMacroEngine(t *testing.T) {
@@ -66,6 +67,32 @@ func TestMacroEngine(t *testing.T) {
 			So(sql, ShouldEqual, "GROUP BY cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time_column))/300 as int)*300 as int)")
 		})
 
+		Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
+			_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
+
+			fill := query.Model.Get("fill").MustBool()
+			fillNull := query.Model.Get("fillNull").MustBool()
+			fillInterval := query.Model.Get("fillInterval").MustInt()
+
+			So(err, ShouldBeNil)
+			So(fill, ShouldBeTrue)
+			So(fillNull, ShouldBeTrue)
+			So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
+		})
+
+		Convey("interpolate __timeGroup function with fill (value = float)", func() {
+			_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
+
+			fill := query.Model.Get("fill").MustBool()
+			fillValue := query.Model.Get("fillValue").MustFloat64()
+			fillInterval := query.Model.Get("fillInterval").MustInt()
+
+			So(err, ShouldBeNil)
+			So(fill, ShouldBeTrue)
+			So(fillValue, ShouldEqual, 1.5)
+			So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
+		})
+
 		Convey("interpolate __timeFrom function", func() {
 			sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
 			So(err, ShouldBeNil)
@@ -100,31 +127,5 @@ func TestMacroEngine(t *testing.T) {
 
 			So(sql, ShouldEqual, "select 18446744066914187038")
 		})
-
-		Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
-			_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
-
-			fill := query.Model.Get("fill").MustBool()
-			fillNull := query.Model.Get("fillNull").MustBool()
-			fillInterval := query.Model.Get("fillInterval").MustInt()
-
-			So(err, ShouldBeNil)
-			So(fill, ShouldBeTrue)
-			So(fillNull, ShouldBeTrue)
-			So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
-		})
-
-		Convey("interpolate __timeGroup function with fill (value = float)", func() {
-			_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
-
-			fill := query.Model.Get("fill").MustBool()
-			fillValue := query.Model.Get("fillValue").MustFloat64()
-			fillInterval := query.Model.Get("fillInterval").MustInt()
-
-			So(err, ShouldBeNil)
-			So(fill, ShouldBeTrue)
-			So(fillValue, ShouldEqual, 1.5)
-			So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
-		})
 	})
 }

+ 4 - 17
pkg/tsdb/mssql/mssql.go

@@ -61,7 +61,7 @@ func NewMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoin
 	return endpoint, nil
 }
 
-// Query is the main function for the MssqlExecutor
+// Query is the main function for the MssqlQueryEndpoint
 func (e *MssqlQueryEndpoint) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
 	return e.sqlEngine.Query(ctx, dsInfo, tsdbQuery, e.transformToTimeSeries, e.transformToTable)
 }
@@ -209,7 +209,6 @@ func (e MssqlQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.
 			fillValue.Float64 = query.Model.Get("fillValue").MustFloat64()
 			fillValue.Valid = true
 		}
-
 	}
 
 	for rows.Next() {
@@ -297,8 +296,9 @@ func (e MssqlQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.
 				}
 			}
 
-			e.appendTimePoint(pointsBySeries, seriesByQueryOrder, metric, timestamp, value)
-			rowCount++
+			series.Points = append(series.Points, tsdb.TimePoint{value, null.FloatFrom(timestamp)})
+
+			e.log.Debug("Rows", "metric", metric, "time", timestamp, "value", value)
 		}
 	}
 
@@ -324,16 +324,3 @@ func (e MssqlQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.
 	result.Meta.Set("rowCount", rowCount)
 	return nil
 }
-
-// TODO: look at this, specific to the MS SQL datasource. REMOVE?
-func (e MssqlQueryEndpoint) appendTimePoint(pointsBySeries map[string]*tsdb.TimeSeries, seriesByQueryOrder *list.List, metric string, timestamp float64, value null.Float) {
-	if series, exist := pointsBySeries[metric]; exist {
-		series.Points = append(series.Points, tsdb.TimePoint{value, null.FloatFrom(timestamp)})
-	} else {
-		series := &tsdb.TimeSeries{Name: metric}
-		series.Points = append(series.Points, tsdb.TimePoint{value, null.FloatFrom(timestamp)})
-		pointsBySeries[metric] = series
-		seriesByQueryOrder.PushBack(metric)
-	}
-	e.log.Debug("Rows", "metric", metric, "time", timestamp, "value", value)
-}