浏览代码

stackdriver: reverse points array to be in ascending order

Daniel Lee 7 年之前
父节点
当前提交
df6b430405
共有 2 个文件被更改,包括 16 次插入6 次删除
  1. 4 1
      pkg/tsdb/stackdriver/stackdriver.go
  2. 12 5
      pkg/tsdb/stackdriver/stackdriver_test.go

+ 4 - 1
pkg/tsdb/stackdriver/stackdriver.go

@@ -207,7 +207,10 @@ func (e *StackdriverExecutor) unmarshalResponse(res *http.Response) (StackDriver
 func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data StackDriverResponse) error {
 	for _, series := range data.TimeSeries {
 		points := make([]tsdb.TimePoint, 0)
-		for _, point := range series.Points {
+
+		// reverse the order to be ascending
+		for i := len(series.Points) - 1; i >= 0; i-- {
+			point := series.Points[i]
 			points = append(points, tsdb.NewTimePoint(null.FloatFrom(point.Value.DoubleValue), float64((point.Interval.EndTime).Unix())*1000))
 		}
 		metricName := series.Metric.Type

+ 12 - 5
pkg/tsdb/stackdriver/stackdriver_test.go

@@ -91,9 +91,16 @@ func TestStackdriver(t *testing.T) {
 				So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
 				So(len(res.Series[0].Points), ShouldEqual, 3)
 
-				So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.0666666666667)
-				So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
-				So(res.Series[0].Points[2][0].Float64, ShouldEqual, 0.05)
+				Convey("timestamps should be in ascending order", func() {
+					So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
+					So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1536670020000)
+
+					So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
+					So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1536670080000)
+
+					So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
+					So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1536670260000)
+				})
 			})
 
 			Convey("when data from query with no aggregation", func() {
@@ -116,9 +123,9 @@ func TestStackdriver(t *testing.T) {
 					So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
 
 					So(len(res.Series[0].Points), ShouldEqual, 3)
-					So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.7730520330369)
+					So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
 					So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
-					So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.8566497180145)
+					So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
 				})
 			})
 		})