| 
									
										
										
										
											2020-06-30 18:47:13 +03:00
										 |  |  | package cloudmonitoring | 
					
						
							| 
									
										
										
										
											2018-09-11 14:06:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-03-16 01:37:48 -07:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-09-11 15:52:37 +02:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2022-03-16 01:37:48 -07:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-09-11 14:06:46 +02:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							| 
									
										
										
										
											2022-03-16 01:37:48 -07:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend/datasource" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" | 
					
						
							| 
									
										
										
										
											2022-11-09 09:38:06 +01:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/httpclient" | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2018-09-11 14:06:46 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 09:38:06 +01:00
										 |  |  | func TestNewInstanceSettings(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("should create a new instance with empty settings", func(t *testing.T) { | 
					
						
							|  |  |  | 		cli := httpclient.NewProvider() | 
					
						
							|  |  |  | 		f := newInstanceSettings(cli) | 
					
						
							|  |  |  | 		dsInfo, err := f(backend.DataSourceInstanceSettings{ | 
					
						
							|  |  |  | 			JSONData: json.RawMessage(`{}`), | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.NotNil(t, dsInfo) | 
					
						
							|  |  |  | 		assert.Equal(t, jwtAuthentication, dsInfo.(*datasourceInfo).authenticationType) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("should create a new instance parsing settings", func(t *testing.T) { | 
					
						
							|  |  |  | 		cli := httpclient.NewProvider() | 
					
						
							|  |  |  | 		f := newInstanceSettings(cli) | 
					
						
							|  |  |  | 		dsInfo, err := f(backend.DataSourceInstanceSettings{ | 
					
						
							|  |  |  | 			JSONData: json.RawMessage(`{"authenticationType": "test", "defaultProject": "test", "clientEmail": "test", "tokenUri": "test"}`), | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.NotNil(t, dsInfo) | 
					
						
							|  |  |  | 		dsInfoCasted := dsInfo.(*datasourceInfo) | 
					
						
							|  |  |  | 		assert.Equal(t, "test", dsInfoCasted.authenticationType) | 
					
						
							|  |  |  | 		assert.Equal(t, "test", dsInfoCasted.defaultProject) | 
					
						
							|  |  |  | 		assert.Equal(t, "test", dsInfoCasted.clientEmail) | 
					
						
							|  |  |  | 		assert.Equal(t, "test", dsInfoCasted.tokenUri) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-30 18:47:13 +03:00
										 |  |  | func TestCloudMonitoring(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 	service := &Service{} | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 	t.Run("parses a time series list query", func(t *testing.T) { | 
					
						
							|  |  |  | 		req := baseTimeSeriesList() | 
					
						
							|  |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Len(t, queries, 1) | 
					
						
							|  |  |  | 		assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 		assert.Equal(t, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL", queries[0].params.Encode()) | 
					
						
							|  |  |  | 		assert.Equal(t, 7, len(queries[0].params)) | 
					
						
							|  |  |  | 		assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "metric.type=\"a/metric/type\"", queries[0].params["filter"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "testalias", queries[0].aliasBy) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("parses a time series query", func(t *testing.T) { | 
					
						
							|  |  |  | 		req := baseTimeSeriesQuery() | 
					
						
							|  |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		queries := getCloudMonitoringQueryFromInterface(t, qes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Len(t, queries, 1) | 
					
						
							|  |  |  | 		assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 		assert.Equal(t, "foo", queries[0].parameters.Query) | 
					
						
							|  |  |  | 		assert.Equal(t, "testalias", queries[0].aliasBy) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 12:39:45 +01:00
										 |  |  | 	t.Run("parses a time series list with secondary inputs", func(t *testing.T) { | 
					
						
							|  |  |  | 		req := baseTimeSeriesList() | 
					
						
							|  |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 			"timeSeriesList": { | 
					
						
							|  |  |  | 				"filters": ["metric.type=\"a/metric/type\""], | 
					
						
							|  |  |  | 				"view":       "FULL", | 
					
						
							|  |  |  | 				"secondaryAlignmentPeriod": "60s", | 
					
						
							|  |  |  | 				"secondaryCrossSeriesReducer": "REDUCE_NONE", | 
					
						
							|  |  |  | 				"secondaryPerSeriesAligner": "ALIGN_MEAN", | 
					
						
							|  |  |  | 				"secondaryGroupBys": ["metric.label.group"] | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			"aliasBy":    "testalias" | 
					
						
							|  |  |  | 		}`) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Len(t, queries, 1) | 
					
						
							|  |  |  | 		assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["secondaryAggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_NONE", queries[0].params["secondaryAggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_MEAN", queries[0].params["secondaryAggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "metric.label.group", queries[0].params["secondaryAggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "testalias", queries[0].aliasBy) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	t.Run("Parse migrated queries from frontend and build Google Cloud Monitoring API queries", func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Run("and query has no aggregation set", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 			err := migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			require.Len(t, queries, 1) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL", queries[0].params.Encode()) | 
					
						
							|  |  |  | 			assert.Equal(t, 7, len(queries[0].params)) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.type=\"a/metric/type\"", queries[0].params["filter"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "testalias", queries[0].aliasBy) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			t.Run("and generated deep link has correct parameters", func(t *testing.T) { | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							|  |  |  | 					"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 					"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"perSeriesAligner": "ALIGN_MEAN", | 
					
						
							|  |  |  | 					"filter":           "resource.type=\"a/resource/type\" metric.type=\"a/metric/type\"", | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-12 00:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and query has filters", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			query := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			query.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				"metricType": "a/metric/type", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"filters":    ["key", "=", "value", "AND", "key2", "=", "value2", "AND", "resource.type", "=", "another/resource/type"] | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 			err := migrateRequest(query) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-12 00:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err := service.buildQueryExecutors(slog, query) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-29 12:39:45 +01:00
										 |  |  | 			assert.Equal(t, `key="value" key2="value2" resource.type="another/resource/type" metric.type="a/metric/type"`, queries[0].params["filter"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// assign a resource type to query parameters | 
					
						
							|  |  |  | 			// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							|  |  |  | 			// the deep link should not contain this resource type since another resource type is included in the query filters | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 				"timeRange": "custom", | 
					
						
							|  |  |  | 				"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 				"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2022-11-29 12:39:45 +01:00
										 |  |  | 				"filter": `key="value" key2="value2" resource.type="another/resource/type" metric.type="a/metric/type"`, | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("and alignmentPeriod is set to grafana-auto", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and IntervalMS is larger than 60000", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].Interval = 1000000 * time.Millisecond | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"alignmentPeriod": "grafana-auto", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"filters":    ["key", "=", "value", "AND", "key2", "=", "value2"] | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-14 16:20:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+1000s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							|  |  |  | 					"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 					"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `1000s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2018-09-14 16:20:51 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and IntervalMS is less than 60000", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].Interval = 30000 * time.Millisecond | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"alignmentPeriod": "grafana-auto", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"filters":    ["key", "=", "value", "AND", "key2", "=", "value2"] | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							|  |  |  | 					"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 					"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2018-09-17 16:30:06 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-17 16:30:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and alignmentPeriod is set to cloud-monitoring-auto", func(t *testing.T) { // legacy | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			now := time.Now().UTC() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is two hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 2)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "cloud-monitoring-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2020-06-30 18:47:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 22 hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 22)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "cloud-monitoring-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2020-06-30 18:47:13 +03:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 23 hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 23)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "cloud-monitoring-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+300s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-09-27 10:20:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 7 days", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now.AddDate(0, 0, 7) | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "cloud-monitoring-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+3600s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2018-09-27 10:20:01 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-27 10:20:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and alignmentPeriod is set to stackdriver-auto", func(t *testing.T) { // legacy | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			now := time.Now().UTC() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is two hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 2)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "stackdriver-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"start":     req.Queries[0].TimeRange.From.Format(time.RFC3339), | 
					
						
							|  |  |  | 					"end":       req.Queries[0].TimeRange.To.Format(time.RFC3339), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2018-09-17 17:13:06 +02:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 22 hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 22)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "stackdriver-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"start":     req.Queries[0].TimeRange.From.Format(time.RFC3339), | 
					
						
							|  |  |  | 					"end":       req.Queries[0].TimeRange.To.Format(time.RFC3339), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2018-09-12 00:24:59 +02:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 23 hours", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.Add(-(time.Hour * 23)) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "stackdriver-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+300s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"start":     req.Queries[0].TimeRange.From.Format(time.RFC3339), | 
					
						
							|  |  |  | 					"end":       req.Queries[0].TimeRange.To.Format(time.RFC3339), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `300s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:02:31 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2020-03-27 12:01:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and range is 7 days", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].TimeRange.From = now.AddDate(0, 0, -7) | 
					
						
							|  |  |  | 				req.Queries[0].TimeRange.To = now | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"target": "target", | 
					
						
							|  |  |  | 					"alignmentPeriod": "stackdriver-auto" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+3600s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"start":     req.Queries[0].TimeRange.From.Format(time.RFC3339), | 
					
						
							|  |  |  | 					"end":       req.Queries[0].TimeRange.To.Format(time.RFC3339), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `3600s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2020-03-27 12:01:16 +01:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-03-27 12:01:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and alignmentPeriod is set in frontend", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and alignment period is within accepted range", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				req.Queries[0].Interval = 1000 | 
					
						
							|  |  |  | 				req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 					"alignmentPeriod": "+600s" | 
					
						
							|  |  |  | 				}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 				err := migrateRequest(req) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-03-27 12:01:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 				qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							|  |  |  | 				assert.Equal(t, `+600s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 				// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 					"timeRange": "custom", | 
					
						
							|  |  |  | 					"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 					"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 					"minAlignmentPeriod": `600s`, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							| 
									
										
										
										
											2020-03-27 12:01:16 +01:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and query has aggregation mean set", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 				"crossSeriesReducer": "REDUCE_SUM", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"view":               "FULL" | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 			err := migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-11 15:52:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_SUM&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL", queries[0].params.Encode()) | 
					
						
							|  |  |  | 			assert.Equal(t, 7, len(queries[0].params)) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "REDUCE_SUM", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.type=\"a/metric/type\"", queries[0].params["filter"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 			// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 				"timeRange": "custom", | 
					
						
							|  |  |  | 				"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 				"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 				"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				"crossSeriesReducer": "REDUCE_SUM", | 
					
						
							|  |  |  | 				"perSeriesAligner":   "ALIGN_MEAN", | 
					
						
							|  |  |  | 				"filter":             "resource.type=\"a/resource/type\" metric.type=\"a/metric/type\"", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-11 15:52:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and query has group bys", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 				"crossSeriesReducer": "REDUCE_NONE", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"groupBys":           ["metric.label.group1", "metric.label.group2"], | 
					
						
							|  |  |  | 				"view":               "FULL" | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 			err := migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-12 01:03:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.groupByFields=metric.label.group1&aggregation.groupByFields=metric.label.group2&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL", queries[0].params.Encode()) | 
					
						
							|  |  |  | 			assert.Equal(t, 8, len(queries[0].params)) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.label.group1", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.label.group2", queries[0].params["aggregation.groupByFields"][1]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.type=\"a/metric/type\"", queries[0].params["filter"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 			// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 				"timeRange": "custom", | 
					
						
							|  |  |  | 				"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 				"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 				"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				"perSeriesAligner":   "ALIGN_MEAN", | 
					
						
							|  |  |  | 				"filter":             "resource.type=\"a/resource/type\" metric.type=\"a/metric/type\"", | 
					
						
							|  |  |  | 				"groupByFields":      []interface{}{"metric.label.group1", "metric.label.group2"}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-09-12 01:03:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	t.Run("Parse queries from frontend and build Google Cloud Monitoring API queries", func(t *testing.T) { | 
					
						
							|  |  |  | 		fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req := &backend.QueryDataRequest{ | 
					
						
							|  |  |  | 			Queries: []backend.DataQuery{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					RefID: "A", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					TimeRange: backend.TimeRange{ | 
					
						
							|  |  |  | 						From: fromStart, | 
					
						
							|  |  |  | 						To:   fromStart.Add(34 * time.Minute), | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					JSON: json.RawMessage(`{ | 
					
						
							|  |  |  | 		 				 "queryType": "metrics", | 
					
						
							|  |  |  |                          "metricQuery": { | 
					
						
							|  |  |  | 		 					"metricType": "a/metric/type", | 
					
						
							|  |  |  | 		 					"view":       "FULL", | 
					
						
							|  |  |  | 		 					"aliasBy":    "testalias", | 
					
						
							|  |  |  | 		 					"type":       "timeSeriesQuery", | 
					
						
							|  |  |  | 		 					"groupBys":   ["metric.label.group1", "metric.label.group2"] | 
					
						
							|  |  |  | 		 				} | 
					
						
							|  |  |  | 		 			}`), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and query type is metrics", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.groupByFields=metric.label.group1&aggregation.groupByFields=metric.label.group2&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL", queries[0].params.Encode()) | 
					
						
							|  |  |  | 			assert.Equal(t, 8, len(queries[0].params)) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.label.group1", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.label.group2", queries[0].params["aggregation.groupByFields"][1]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "metric.type=\"a/metric/type\"", queries[0].params["filter"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "FULL", queries[0].params["view"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "testalias", queries[0].aliasBy) | 
					
						
							|  |  |  | 			assert.Equal(t, []string{"metric.label.group1", "metric.label.group2"}, queries[0].parameters.GroupBys) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// assign resource type to query parameters to be included in the deep link filter | 
					
						
							|  |  |  | 			// in the actual workflow this information comes from the response of the Monitoring API | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			queries[0].params.Set("resourceType", "a/resource/type") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			dl := queries[0].buildDeepLink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedTimeSelection := map[string]string{ | 
					
						
							|  |  |  | 				"timeRange": "custom", | 
					
						
							|  |  |  | 				"start":     "2018-03-15T13:00:00Z", | 
					
						
							|  |  |  | 				"end":       "2018-03-15T13:34:00Z", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			expectedTimeSeriesFilter := map[string]interface{}{ | 
					
						
							|  |  |  | 				"minAlignmentPeriod": `60s`, | 
					
						
							|  |  |  | 				"perSeriesAligner":   "ALIGN_MEAN", | 
					
						
							|  |  |  | 				"filter":             "resource.type=\"a/resource/type\" metric.type=\"a/metric/type\"", | 
					
						
							|  |  |  | 				"groupByFields":      []interface{}{"metric.label.group1", "metric.label.group2"}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			verifyDeepLink(t, dl, expectedTimeSelection, expectedTimeSeriesFilter) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 				"queryType": "metrics", | 
					
						
							|  |  |  | 				 "metricQuery": { | 
					
						
							|  |  |  | 					"editorMode":  "mql", | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"projectName": "test-proj", | 
					
						
							|  |  |  | 					"query":       "test-query", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"aliasBy":     "test-alias" | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"sloQuery": {} | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			err = migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-11 22:41:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err = service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-11 22:41:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			tqueries := getCloudMonitoringQueryFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			assert.Equal(t, 1, len(tqueries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", tqueries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "test-proj", tqueries[0].parameters.ProjectName) | 
					
						
							|  |  |  | 			assert.Equal(t, "test-query", tqueries[0].parameters.Query) | 
					
						
							|  |  |  | 			assert.Equal(t, "test-alias", tqueries[0].aliasBy) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:02:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and query type is SLOs", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 				"queryType": "slo", | 
					
						
							|  |  |  | 				 "sloQuery": { | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"projectName":      "test-proj", | 
					
						
							|  |  |  | 					"alignmentPeriod":  "stackdriver-auto", | 
					
						
							|  |  |  | 					"perSeriesAligner": "ALIGN_NEXT_OLDER", | 
					
						
							| 
									
										
										
										
											2022-12-20 12:47:49 +01:00
										 |  |  | 					"aliasBy":          "test-alias", | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"selectorName":     "select_slo_health", | 
					
						
							|  |  |  | 					"serviceId":        "test-service", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"sloId":            "test-slo" | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"metricQuery": {} | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			err := migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-20 00:52:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-30 17:23:05 +01:00
										 |  |  | 			queries := getCloudMonitoringSLOFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "A", queries[0].refID) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:00:00Z", queries[0].params["interval.startTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, "2018-03-15T13:34:00Z", queries[0].params["interval.endTime"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, `+60s`, queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2022-12-20 12:47:49 +01:00
										 |  |  | 			assert.Equal(t, "test-alias", queries[0].aliasBy) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "ALIGN_MEAN", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 			assert.Equal(t, `aggregation.alignmentPeriod=%2B60s&aggregation.perSeriesAligner=ALIGN_MEAN&filter=select_slo_health%28%22projects%2Ftest-proj%2Fservices%2Ftest-service%2FserviceLevelObjectives%2Ftest-slo%22%29&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z`, queries[0].params.Encode()) | 
					
						
							|  |  |  | 			assert.Equal(t, 5, len(queries[0].params)) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 				"queryType": "slo", | 
					
						
							|  |  |  | 				 "sloQuery": { | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"projectName":      "test-proj", | 
					
						
							|  |  |  | 					"alignmentPeriod":  "stackdriver-auto", | 
					
						
							|  |  |  | 					"perSeriesAligner": "ALIGN_NEXT_OLDER", | 
					
						
							|  |  |  | 					"aliasBy":          "", | 
					
						
							|  |  |  | 					"selectorName":     "select_slo_compliance", | 
					
						
							|  |  |  | 					"serviceId":        "test-service", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"sloId":            "test-slo" | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				"metricQuery": {} | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			err = migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-20 11:44:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err = service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-30 17:23:05 +01:00
										 |  |  | 			qqueries := getCloudMonitoringSLOFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, "ALIGN_NEXT_OLDER", qqueries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							| 
									
										
										
										
											2018-09-23 22:04:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			dl := qqueries[0].buildDeepLink() | 
					
						
							|  |  |  | 			assert.Empty(t, dl) | 
					
						
							| 
									
										
										
										
											2022-08-22 17:11:54 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							|  |  |  | 				"queryType": "slo", | 
					
						
							|  |  |  | 				 "sloQuery": { | 
					
						
							|  |  |  | 					"projectName":      "test-proj", | 
					
						
							|  |  |  | 					"alignmentPeriod":  "stackdriver-auto", | 
					
						
							|  |  |  | 					"perSeriesAligner": "ALIGN_NEXT_OLDER", | 
					
						
							|  |  |  | 					"aliasBy":          "", | 
					
						
							|  |  |  | 					"selectorName":     "select_slo_burn_rate", | 
					
						
							|  |  |  | 					"serviceId":        "test-service", | 
					
						
							|  |  |  | 					"sloId":            "test-slo", | 
					
						
							|  |  |  | 					"lookbackPeriod":   "1h" | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				"metricQuery": {} | 
					
						
							|  |  |  | 			}`) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			err = migrateRequest(req) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-08-22 17:11:54 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 			qes, err = service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2022-08-22 17:11:54 +09:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-30 17:23:05 +01:00
										 |  |  | 			qqqueries := getCloudMonitoringSLOFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 			assert.Equal(t, `aggregation.alignmentPeriod=%2B60s&aggregation.perSeriesAligner=ALIGN_NEXT_OLDER&filter=select_slo_burn_rate%28%22projects%2Ftest-proj%2Fservices%2Ftest-service%2FserviceLevelObjectives%2Ftest-slo%22%2C+%221h%22%29&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z`, qqqueries[0].params.Encode()) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-10-02 22:45:54 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	t.Run("when interpolating filter wildcards", func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Run("and wildcard is used in the beginning and the end of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and there's no wildcard in the middle of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("*-central1*") | 
					
						
							|  |  |  | 				assert.Equal(t, `has_substring("-central1")`, value) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and there is a wildcard in the middle of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("*-cent*ral1*") | 
					
						
							|  |  |  | 				assert.False(t, strings.HasPrefix(value, `has_substring`)) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and wildcard is used in the beginning of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and there is not a wildcard elsewhere in the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("*-central1") | 
					
						
							|  |  |  | 				assert.Equal(t, `ends_with("-central1")`, value) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			t.Run("and there is a wildcard elsewhere in the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("*-cent*al1") | 
					
						
							|  |  |  | 				assert.False(t, strings.HasPrefix(value, `ends_with`)) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and wildcard is used at the end of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and there is not a wildcard elsewhere in the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("us-central*") | 
					
						
							|  |  |  | 				assert.Equal(t, `starts_with("us-central")`, value) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			t.Run("and there is a wildcard elsewhere in the word", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("*us-central*") | 
					
						
							|  |  |  | 				assert.False(t, strings.HasPrefix(value, `starts_with`)) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and wildcard is used in the middle of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Run("and there is only one wildcard", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("us-ce*tral1-b") | 
					
						
							|  |  |  | 				assert.Equal(t, `monitoring.regex.full_match("^us\\-ce.*tral1\\-b$")`, value) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:52:26 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:58:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			t.Run("and there is more than one wildcard", func(t *testing.T) { | 
					
						
							|  |  |  | 				value := interpolateFilterWildcards("us-ce*tra*1-b") | 
					
						
							|  |  |  | 				assert.Equal(t, `monitoring.regex.full_match("^us\\-ce.*tra.*1\\-b$")`, value) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:58:31 +02:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:29:51 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-02 17:58:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and wildcard is used in the middle of the word and in the beginning of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			value := interpolateFilterWildcards("*s-ce*tral1-b") | 
					
						
							|  |  |  | 			assert.Equal(t, `monitoring.regex.full_match("^.*s\\-ce.*tral1\\-b$")`, value) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-08 11:08:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		t.Run("and wildcard is used in the middle of the word and in the ending of the word", func(t *testing.T) { | 
					
						
							|  |  |  | 			value := interpolateFilterWildcards("us-ce*tral1-*") | 
					
						
							|  |  |  | 			assert.Equal(t, `monitoring.regex.full_match("^us\\-ce.*tral1\\-.*$")`, value) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		t.Run("and no wildcard is used", func(t *testing.T) { | 
					
						
							|  |  |  | 			value := interpolateFilterWildcards("us-central1-a}") | 
					
						
							|  |  |  | 			assert.Equal(t, `us-central1-a}`, value) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-10-02 18:09:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	t.Run("and query preprocessor is not defined", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_MIN", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_SUM", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           ["labelname"], | 
					
						
							|  |  |  | 			"view":               "FULL" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.NotContains(t, queries[0].params, "secondaryAggregation.crossSeriesReducer") | 
					
						
							|  |  |  | 		assert.NotContains(t, "REDUCE_SUM", queries[0].params, "secondaryAggregation.perSeriesAligner") | 
					
						
							|  |  |  | 		assert.NotContains(t, "+60s", queries[0].params, "secondaryAggregation.alignmentPeriod") | 
					
						
							|  |  |  | 		assert.NotContains(t, "labelname", queries[0].params, "secondaryAggregation.groupByFields") | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("and query preprocessor is set to none", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_MIN", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_SUM", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           ["labelname"], | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"view":               "FULL", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"preprocessor":       "none" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.NotContains(t, queries[0].params, "secondaryAggregation.crossSeriesReducer") | 
					
						
							|  |  |  | 		assert.NotContains(t, "REDUCE_SUM", queries[0].params, "secondaryAggregation.perSeriesAligner") | 
					
						
							|  |  |  | 		assert.NotContains(t, "+60s", queries[0].params, "secondaryAggregation.alignmentPeriod") | 
					
						
							|  |  |  | 		assert.NotContains(t, "labelname", queries[0].params, "secondaryAggregation.groupByFields") | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("and query preprocessor is set to rate and there's no group bys", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_SUM", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_MIN", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           [], | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"view":               "FULL", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"preprocessor":       "rate" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_NONE", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_RATE", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["secondaryAggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["secondaryAggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["secondaryAggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("and query preprocessor is set to rate and group bys exist", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_SUM", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_MIN", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           ["labelname"], | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"view":               "FULL", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"preprocessor":       "rate" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_RATE", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["secondaryAggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["secondaryAggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["secondaryAggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["secondaryAggregation.groupByFields"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("and query preprocessor is set to delta and there's no group bys", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_MIN", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_SUM", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           [], | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"view":               "FULL", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"preprocessor":       "delta" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_NONE", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_DELTA", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["secondaryAggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["secondaryAggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["secondaryAggregation.alignmentPeriod"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("and query preprocessor is set to delta and group bys exist", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		req := deprecatedReq() | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 		req.Queries[0].JSON = json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"metricType":         "a/metric/type", | 
					
						
							|  |  |  | 			"crossSeriesReducer": "REDUCE_MIN", | 
					
						
							|  |  |  | 			"perSeriesAligner":   "REDUCE_SUM", | 
					
						
							|  |  |  | 			"alignmentPeriod":    "+60s", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"groupBys":           ["labelname"], | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 			"view":               "FULL", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 			"preprocessor":       "delta" | 
					
						
							|  |  |  | 		}`) | 
					
						
							| 
									
										
										
										
											2022-11-10 09:30:47 +01:00
										 |  |  | 		err := migrateRequest(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:28:38 -04:00
										 |  |  | 		qes, err := service.buildQueryExecutors(slog, req) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		queries := getCloudMonitoringListFromInterface(t, qes) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 1, len(queries)) | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["aggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "ALIGN_DELTA", queries[0].params["aggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["aggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["aggregation.groupByFields"][0]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_MIN", queries[0].params["secondaryAggregation.crossSeriesReducer"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "REDUCE_SUM", queries[0].params["secondaryAggregation.perSeriesAligner"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "+60s", queries[0].params["secondaryAggregation.alignmentPeriod"][0]) | 
					
						
							|  |  |  | 		assert.Equal(t, "labelname", queries[0].params["secondaryAggregation.groupByFields"][0]) | 
					
						
							| 
									
										
										
										
											2021-05-19 08:16:05 +02:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-09-11 14:06:46 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-20 00:52:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | func getCloudMonitoringListFromInterface(t *testing.T, qes []cloudMonitoringQueryExecutor) []*cloudMonitoringTimeSeriesList { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	queries := make([]*cloudMonitoringTimeSeriesList, 0) | 
					
						
							|  |  |  | 	for _, qi := range qes { | 
					
						
							|  |  |  | 		q, ok := qi.(*cloudMonitoringTimeSeriesList) | 
					
						
							|  |  |  | 		require.Truef(t, ok, "Received wrong type %T", qi) | 
					
						
							|  |  |  | 		queries = append(queries, q) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return queries | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-30 17:23:05 +01:00
										 |  |  | func getCloudMonitoringSLOFromInterface(t *testing.T, qes []cloudMonitoringQueryExecutor) []*cloudMonitoringSLO { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	queries := make([]*cloudMonitoringSLO, 0) | 
					
						
							|  |  |  | 	for _, qi := range qes { | 
					
						
							|  |  |  | 		q, ok := qi.(*cloudMonitoringSLO) | 
					
						
							|  |  |  | 		require.Truef(t, ok, "Received wrong type %T", qi) | 
					
						
							|  |  |  | 		queries = append(queries, q) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return queries | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | func getCloudMonitoringQueryFromInterface(t *testing.T, qes []cloudMonitoringQueryExecutor) []*cloudMonitoringTimeSeriesQuery { | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 	queries := make([]*cloudMonitoringTimeSeriesQuery, 0) | 
					
						
							| 
									
										
										
										
											2021-01-18 21:48:43 +09:00
										 |  |  | 	for _, qi := range qes { | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 		q, ok := qi.(*cloudMonitoringTimeSeriesQuery) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		require.Truef(t, ok, "Received wrong type %T", qi) | 
					
						
							| 
									
										
										
										
											2021-01-18 21:48:43 +09:00
										 |  |  | 		queries = append(queries, q) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return queries | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | func verifyDeepLink(t *testing.T, dl string, expectedTimeSelection map[string]string, | 
					
						
							|  |  |  | 	expectedTimeSeriesFilter map[string]interface{}) { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	u, err := url.Parse(dl) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	assert.Equal(t, "https", u.Scheme) | 
					
						
							|  |  |  | 	assert.Equal(t, "accounts.google.com", u.Host) | 
					
						
							|  |  |  | 	assert.Equal(t, "/AccountChooser", u.Path) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	params, err := url.ParseQuery(u.RawQuery) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	continueParam := params.Get("continue") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.NotEmpty(t, continueParam) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	u, err = url.Parse(continueParam) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	params, err = url.ParseQuery(u.RawQuery) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 19:31:17 +03:00
										 |  |  | 	deepLinkParam := params.Get("Grafana_deeplink") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.NotEmpty(t, deepLinkParam) | 
					
						
							| 
									
										
										
										
											2020-07-15 19:31:17 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	pageStateStr := params.Get("pageState") | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.NotEmpty(t, pageStateStr) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var pageState map[string]map[string]interface{} | 
					
						
							|  |  |  | 	err = json.Unmarshal([]byte(pageStateStr), &pageState) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	timeSelection, ok := pageState["timeSelection"] | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.True(t, ok) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	for k, v := range expectedTimeSelection { | 
					
						
							|  |  |  | 		s, ok := timeSelection[k].(string) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		assert.True(t, ok) | 
					
						
							|  |  |  | 		assert.Equal(t, v, s) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dataSets, ok := pageState["xyChart"]["dataSets"].([]interface{}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.True(t, ok) | 
					
						
							|  |  |  | 	assert.Equal(t, 1, len(dataSets)) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	dataSet, ok := dataSets[0].(map[string]interface{}) | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.True(t, ok) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	i, ok := dataSet["timeSeriesFilter"] | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	assert.True(t, ok) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 	timeSeriesFilter := i.(map[string]interface{}) | 
					
						
							|  |  |  | 	for k, v := range expectedTimeSeriesFilter { | 
					
						
							|  |  |  | 		s, ok := timeSeriesFilter[k] | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 		assert.True(t, ok) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 		rt := reflect.TypeOf(v) | 
					
						
							|  |  |  | 		switch rt.Kind() { | 
					
						
							|  |  |  | 		case reflect.Slice, reflect.Array: | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			assert.Equal(t, v, s) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			assert.Equal(t, v, s) | 
					
						
							| 
									
										
										
										
											2020-07-01 01:16:20 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | func deprecatedReq() *backend.QueryDataRequest { | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 	fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local) | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 	query := &backend.QueryDataRequest{ | 
					
						
							|  |  |  | 		Queries: []backend.DataQuery{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 				RefID: "A", | 
					
						
							|  |  |  | 				TimeRange: backend.TimeRange{ | 
					
						
							|  |  |  | 					From: fromStart, | 
					
						
							|  |  |  | 					To:   fromStart.Add(34 * time.Minute), | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				JSON: json.RawMessage(`{ | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 					"metricType": "a/metric/type", | 
					
						
							|  |  |  | 					"view":       "FULL", | 
					
						
							|  |  |  | 					"aliasBy":    "testalias", | 
					
						
							| 
									
										
										
										
											2021-10-08 14:46:35 +02:00
										 |  |  | 					"type":       "timeSeriesQuery" | 
					
						
							|  |  |  | 				}`), | 
					
						
							| 
									
										
										
										
											2021-03-08 07:02:49 +01:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return query | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-03-16 01:37:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | func baseTimeSeriesList() *backend.QueryDataRequest { | 
					
						
							|  |  |  | 	fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local) | 
					
						
							|  |  |  | 	query := &backend.QueryDataRequest{ | 
					
						
							|  |  |  | 		Queries: []backend.DataQuery{ | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				RefID: "A", | 
					
						
							|  |  |  | 				TimeRange: backend.TimeRange{ | 
					
						
							|  |  |  | 					From: fromStart, | 
					
						
							|  |  |  | 					To:   fromStart.Add(34 * time.Minute), | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2022-12-20 12:47:49 +01:00
										 |  |  | 				QueryType: timeSeriesListQueryType, | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				JSON: json.RawMessage(`{ | 
					
						
							|  |  |  | 					"timeSeriesList": { | 
					
						
							| 
									
										
										
										
											2022-11-29 12:39:45 +01:00
										 |  |  | 						"filters": ["metric.type=\"a/metric/type\""], | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 						"view":       "FULL" | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					"aliasBy":    "testalias" | 
					
						
							|  |  |  | 				}`), | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return query | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func baseTimeSeriesQuery() *backend.QueryDataRequest { | 
					
						
							|  |  |  | 	fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local) | 
					
						
							|  |  |  | 	query := &backend.QueryDataRequest{ | 
					
						
							|  |  |  | 		Queries: []backend.DataQuery{ | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				RefID: "A", | 
					
						
							|  |  |  | 				TimeRange: backend.TimeRange{ | 
					
						
							|  |  |  | 					From: fromStart, | 
					
						
							|  |  |  | 					To:   fromStart.Add(34 * time.Minute), | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2022-12-20 12:47:49 +01:00
										 |  |  | 				QueryType: timeSeriesQueryQueryType, | 
					
						
							| 
									
										
										
										
											2022-11-28 09:17:01 +01:00
										 |  |  | 				JSON: json.RawMessage(`{ | 
					
						
							|  |  |  | 					"queryType": "metrics", | 
					
						
							|  |  |  | 					"timeSeriesQuery": { | 
					
						
							|  |  |  | 						"query": "foo" | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					"aliasBy":    "testalias" | 
					
						
							|  |  |  | 				}`), | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return query | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 01:37:48 -07:00
										 |  |  | func TestCheckHealth(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("and using GCE authentation should return proper error", func(t *testing.T) { | 
					
						
							|  |  |  | 		im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { | 
					
						
							|  |  |  | 			return &datasourceInfo{ | 
					
						
							|  |  |  | 				authenticationType: gceAuthentication, | 
					
						
							|  |  |  | 			}, nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		service := &Service{ | 
					
						
							|  |  |  | 			im: im, | 
					
						
							|  |  |  | 			gceDefaultProjectGetter: func(ctx context.Context) (string, error) { | 
					
						
							|  |  |  | 				return "", fmt.Errorf("not found!") | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res, err := service.CheckHealth(context.Background(), &backend.CheckHealthRequest{ | 
					
						
							|  |  |  | 			PluginContext: backend.PluginContext{ | 
					
						
							|  |  |  | 				DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		assert.Nil(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, &backend.CheckHealthResult{ | 
					
						
							|  |  |  | 			Status:  backend.HealthStatusError, | 
					
						
							|  |  |  | 			Message: "not found!", | 
					
						
							|  |  |  | 		}, res) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |