Просмотр исходного кода

mssql: add integration test to verify stored procedure usage

Marcus Efraimsson 7 лет назад
Родитель
Сommit
192e913b7b
2 измененных файлов с 341 добавлено и 17 удалено
  1. 183 17
      docker/blocks/mssql_tests/dashboard.json
  2. 158 0
      pkg/tsdb/mssql/mssql_test.go

+ 183 - 17
docker/blocks/mssql_tests/dashboard.json

@@ -88,7 +88,7 @@
   "gnetId": null,
   "graphTooltip": 0,
   "id": null,
-  "iteration": 1521475806814,
+  "iteration": 1521481503341,
   "links": [],
   "panels": [
     {
@@ -184,7 +184,7 @@
       "thresholds": [],
       "timeFrom": null,
       "timeShift": null,
-      "title": "Metrics - timeGroup macro 5m without fill",
+      "title": "timeGroup macro 5m without fill",
       "tooltip": {
         "shared": true,
         "sort": 0,
@@ -204,7 +204,7 @@
           "label": null,
           "logBase": 1,
           "max": null,
-          "min": null,
+          "min": "0",
           "show": true
         },
         {
@@ -263,7 +263,7 @@
       "thresholds": [],
       "timeFrom": null,
       "timeShift": null,
-      "title": "Metrics - timeGroup macro 5m with fill(NULL) and null as zero",
+      "title": "timeGroup macro 5m with fill(NULL) and null as zero",
       "tooltip": {
         "shared": true,
         "sort": 0,
@@ -283,7 +283,7 @@
           "label": null,
           "logBase": 1,
           "max": null,
-          "min": null,
+          "min": "0",
           "show": true
         },
         {
@@ -342,7 +342,7 @@
       "thresholds": [],
       "timeFrom": null,
       "timeShift": null,
-      "title": "Metrics - timeGroup macro 5m with fill(10.0)",
+      "title": "timeGroup macro 5m with fill(10.0)",
       "tooltip": {
         "shared": true,
         "sort": 0,
@@ -362,7 +362,7 @@
           "label": null,
           "logBase": 1,
           "max": null,
-          "min": null,
+          "min": "0",
           "show": true
         },
         {
@@ -655,7 +655,7 @@
         {
           "alias": "",
           "format": "time_series",
-          "rawSql": "SELECT \n  $__timeGroup(time, '$summarize') as time, \n  measurement + ' - value one' as metric, \n  avg(valueOne) as valueOne\nFROM\n  metric_values \nGROUP BY \n  $__timeGroup(time, '$summarize'), \n  measurement \nORDER BY 1",
+          "rawSql": "SELECT \n  $__timeGroup(time, '$summarize') as time, \n  measurement + ' - value one' as metric, \n  avg(valueOne) as valueOne\nFROM\n  metric_values \nWHERE\n  $__timeFilter(time)\nGROUP BY \n  $__timeGroup(time, '$summarize'), \n  measurement \nORDER BY 1",
           "refId": "A"
         },
         {
@@ -1292,6 +1292,172 @@
         }
       ]
     },
+    {
+      "aliasColors": {},
+      "bars": false,
+      "dashLength": 10,
+      "dashes": false,
+      "datasource": "${DS_MSSQL_TEST}",
+      "fill": 2,
+      "gridPos": {
+        "h": 8,
+        "w": 12,
+        "x": 0,
+        "y": 54
+      },
+      "id": 29,
+      "legend": {
+        "alignAsTable": true,
+        "avg": true,
+        "current": true,
+        "hideEmpty": false,
+        "hideZero": false,
+        "max": true,
+        "min": true,
+        "rightSide": true,
+        "show": true,
+        "total": true,
+        "values": true
+      },
+      "lines": true,
+      "linewidth": 2,
+      "links": [],
+      "nullPointMode": "null",
+      "percentage": false,
+      "pointradius": 3,
+      "points": false,
+      "renderer": "flot",
+      "seriesOverrides": [],
+      "spaceLength": 10,
+      "stack": false,
+      "steppedLine": false,
+      "targets": [
+        {
+          "alias": "",
+          "format": "time_series",
+          "rawSql": "DECLARE \n  @from int = $__unixEpochFrom(),\n  @to int = $__unixEpochTo()\n  \nEXEC dbo.sp_test_epoch @from, @to",
+          "refId": "A"
+        }
+      ],
+      "thresholds": [],
+      "timeFrom": null,
+      "timeShift": null,
+      "title": "Stored procedure support using epoch",
+      "tooltip": {
+        "shared": true,
+        "sort": 0,
+        "value_type": "individual"
+      },
+      "type": "graph",
+      "xaxis": {
+        "buckets": null,
+        "mode": "time",
+        "name": null,
+        "show": true,
+        "values": []
+      },
+      "yaxes": [
+        {
+          "format": "short",
+          "label": null,
+          "logBase": 1,
+          "max": null,
+          "min": "0",
+          "show": true
+        },
+        {
+          "format": "short",
+          "label": null,
+          "logBase": 1,
+          "max": null,
+          "min": null,
+          "show": true
+        }
+      ]
+    },
+    {
+      "aliasColors": {},
+      "bars": false,
+      "dashLength": 10,
+      "dashes": false,
+      "datasource": "${DS_MSSQL_TEST}",
+      "fill": 2,
+      "gridPos": {
+        "h": 8,
+        "w": 12,
+        "x": 12,
+        "y": 54
+      },
+      "id": 30,
+      "legend": {
+        "alignAsTable": true,
+        "avg": true,
+        "current": true,
+        "hideEmpty": false,
+        "hideZero": false,
+        "max": true,
+        "min": true,
+        "rightSide": true,
+        "show": true,
+        "total": true,
+        "values": true
+      },
+      "lines": true,
+      "linewidth": 2,
+      "links": [],
+      "nullPointMode": "null",
+      "percentage": false,
+      "pointradius": 3,
+      "points": false,
+      "renderer": "flot",
+      "seriesOverrides": [],
+      "spaceLength": 10,
+      "stack": false,
+      "steppedLine": false,
+      "targets": [
+        {
+          "alias": "",
+          "format": "time_series",
+          "rawSql": "DECLARE \n  @from datetime = $__timeFrom(),\n  @to datetime = $__timeTo()\n  \nEXEC dbo.sp_test_datetime @from, @to",
+          "refId": "A"
+        }
+      ],
+      "thresholds": [],
+      "timeFrom": null,
+      "timeShift": null,
+      "title": "Stored procedure support using datetime",
+      "tooltip": {
+        "shared": true,
+        "sort": 0,
+        "value_type": "individual"
+      },
+      "type": "graph",
+      "xaxis": {
+        "buckets": null,
+        "mode": "time",
+        "name": null,
+        "show": true,
+        "values": []
+      },
+      "yaxes": [
+        {
+          "format": "short",
+          "label": null,
+          "logBase": 1,
+          "max": null,
+          "min": "0",
+          "show": true
+        },
+        {
+          "format": "short",
+          "label": null,
+          "logBase": 1,
+          "max": null,
+          "min": null,
+          "show": true
+        }
+      ]
+    },
     {
       "aliasColors": {},
       "bars": true,
@@ -1303,7 +1469,7 @@
         "h": 8,
         "w": 12,
         "x": 0,
-        "y": 54
+        "y": 62
       },
       "id": 14,
       "legend": {
@@ -1393,7 +1559,7 @@
         "h": 8,
         "w": 12,
         "x": 12,
-        "y": 54
+        "y": 62
       },
       "id": 15,
       "legend": {
@@ -1476,7 +1642,7 @@
         "h": 8,
         "w": 12,
         "x": 0,
-        "y": 62
+        "y": 70
       },
       "id": 25,
       "legend": {
@@ -1566,7 +1732,7 @@
         "h": 8,
         "w": 12,
         "x": 12,
-        "y": 62
+        "y": 70
       },
       "id": 22,
       "legend": {
@@ -1649,7 +1815,7 @@
         "h": 8,
         "w": 12,
         "x": 0,
-        "y": 70
+        "y": 78
       },
       "id": 21,
       "legend": {
@@ -1739,7 +1905,7 @@
         "h": 8,
         "w": 12,
         "x": 12,
-        "y": 70
+        "y": 78
       },
       "id": 26,
       "legend": {
@@ -1822,7 +1988,7 @@
         "h": 8,
         "w": 12,
         "x": 0,
-        "y": 78
+        "y": 86
       },
       "id": 23,
       "legend": {
@@ -1912,7 +2078,7 @@
         "h": 8,
         "w": 12,
         "x": 12,
-        "y": 78
+        "y": 86
       },
       "id": 24,
       "legend": {
@@ -2072,5 +2238,5 @@
   "timezone": "",
   "title": "Microsoft SQL Server Data Source Test",
   "uid": "GlAqcPgmz",
-  "version": 35
+  "version": 37
 }

+ 158 - 0
pkg/tsdb/mssql/mssql_test.go

@@ -404,6 +404,164 @@ func TestMSSQL(t *testing.T) {
 				So(queryResult.Series[0].Name, ShouldEqual, "valueOne")
 				So(queryResult.Series[1].Name, ShouldEqual, "valueTwo")
 			})
+
+			Convey("Given a stored procedure that takes @from and @to in epoch time", func() {
+				sql := `
+					IF object_id('sp_test_epoch') IS NOT NULL
+						DROP PROCEDURE sp_test_epoch
+				`
+
+				_, err := sess.Exec(sql)
+				So(err, ShouldBeNil)
+
+				sql = `
+					CREATE PROCEDURE sp_test_epoch(
+						@from int,
+						@to 	int
+					)	AS
+					BEGIN
+						SELECT
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
+							measurement + ' - value one' as metric,
+							avg(valueOne) as value
+						FROM
+							metric_values
+						WHERE
+							time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
+						GROUP BY
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
+							measurement
+						UNION ALL
+						SELECT
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
+							measurement + ' - value two' as metric,
+							avg(valueTwo) as value
+						FROM
+							metric_values
+						WHERE
+							time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
+						GROUP BY
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
+							measurement
+						ORDER BY 1
+					END
+				`
+
+				_, err = sess.Exec(sql)
+				So(err, ShouldBeNil)
+
+				Convey("When doing a metric query using stored procedure should return correct result", func() {
+					query := &tsdb.TsdbQuery{
+						Queries: []*tsdb.Query{
+							{
+								Model: simplejson.NewFromAny(map[string]interface{}{
+									"rawSql": `DECLARE
+										@from int = $__unixEpochFrom(),
+										@to int = $__unixEpochTo()
+
+										EXEC dbo.sp_test_epoch @from, @to`,
+									"format": "time_series",
+								}),
+								RefId: "A",
+							},
+						},
+						TimeRange: &tsdb.TimeRange{
+							From: "1521117000000",
+							To:   "1521122100000",
+						},
+					}
+
+					resp, err := endpoint.Query(nil, nil, query)
+					queryResult := resp.Results["A"]
+					So(err, ShouldBeNil)
+					So(queryResult.Error, ShouldBeNil)
+
+					So(len(queryResult.Series), ShouldEqual, 4)
+					So(queryResult.Series[0].Name, ShouldEqual, "Metric A - value one")
+					So(queryResult.Series[1].Name, ShouldEqual, "Metric B - value one")
+					So(queryResult.Series[2].Name, ShouldEqual, "Metric A - value two")
+					So(queryResult.Series[3].Name, ShouldEqual, "Metric B - value two")
+				})
+			})
+
+			Convey("Given a stored procedure that takes @from and @to in datetime", func() {
+				sql := `
+					IF object_id('sp_test_datetime') IS NOT NULL
+						DROP PROCEDURE sp_test_datetime
+				`
+
+				_, err := sess.Exec(sql)
+				So(err, ShouldBeNil)
+
+				sql = `
+					CREATE PROCEDURE sp_test_datetime(
+						@from datetime,
+						@to 	datetime
+					)	AS
+					BEGIN
+						SELECT
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
+							measurement + ' - value one' as metric,
+							avg(valueOne) as value
+						FROM
+							metric_values
+						WHERE
+							time >= @from AND time <= @to
+						GROUP BY
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
+							measurement
+						UNION ALL
+						SELECT
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
+							measurement + ' - value two' as metric,
+							avg(valueTwo) as value
+						FROM
+							metric_values
+						WHERE
+							time >= @from AND time <= @to
+						GROUP BY
+							cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
+							measurement
+						ORDER BY 1
+					END
+				`
+
+				_, err = sess.Exec(sql)
+				So(err, ShouldBeNil)
+
+				Convey("When doing a metric query using stored procedure should return correct result", func() {
+					query := &tsdb.TsdbQuery{
+						Queries: []*tsdb.Query{
+							{
+								Model: simplejson.NewFromAny(map[string]interface{}{
+									"rawSql": `DECLARE
+										@from int = $__unixEpochFrom(),
+										@to int = $__unixEpochTo()
+
+										EXEC dbo.sp_test_epoch @from, @to`,
+									"format": "time_series",
+								}),
+								RefId: "A",
+							},
+						},
+						TimeRange: &tsdb.TimeRange{
+							From: "1521117000000",
+							To:   "1521122100000",
+						},
+					}
+
+					resp, err := endpoint.Query(nil, nil, query)
+					queryResult := resp.Results["A"]
+					So(err, ShouldBeNil)
+					So(queryResult.Error, ShouldBeNil)
+
+					So(len(queryResult.Series), ShouldEqual, 4)
+					So(queryResult.Series[0].Name, ShouldEqual, "Metric A - value one")
+					So(queryResult.Series[1].Name, ShouldEqual, "Metric B - value one")
+					So(queryResult.Series[2].Name, ShouldEqual, "Metric A - value two")
+					So(queryResult.Series[3].Name, ShouldEqual, "Metric B - value two")
+				})
+			})
 		})
 
 		Convey("Given a table with event data", func() {