| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | package es | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2022-08-10 21:37:51 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 22:45:59 +08:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2019-10-22 22:16:53 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2023-01-30 16:50:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2023-09-07 19:54:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2023-09-18 16:49:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/tracing" | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | func TestClient_ExecuteMultisearch(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 	t.Run("Given a fake http client and a client with response", func(t *testing.T) { | 
					
						
							|  |  |  | 		var request *http.Request | 
					
						
							|  |  |  | 		var requestBody *bytes.Buffer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 			request = r | 
					
						
							|  |  |  | 			buf, err := io.ReadAll(r.Body) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			requestBody = bytes.NewBuffer(buf) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			rw.Header().Set("Content-Type", "application/x-ndjson") | 
					
						
							|  |  |  | 			_, err = rw.Write([]byte( | 
					
						
							|  |  |  | 				`{ | 
					
						
							| 
									
										
										
										
											2019-05-06 21:12:18 +08:00
										 |  |  | 				"responses": [ | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						"hits": {	"hits": [], "max_score": 0,	"total": { "value": 4656, "relation": "eq"}	}, | 
					
						
							|  |  |  | 						"status": 200 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				] | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 			}`)) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			rw.WriteHeader(200) | 
					
						
							|  |  |  | 		})) | 
					
						
							| 
									
										
										
										
											2022-09-26 20:27:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:50:56 +08:00
										 |  |  | 		configuredFields := ConfiguredFields{ | 
					
						
							|  |  |  | 			TimeField:       "testtime", | 
					
						
							|  |  |  | 			LogMessageField: "line", | 
					
						
							|  |  |  | 			LogLevelField:   "lvl", | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-26 20:27:46 +08:00
										 |  |  | 		ds := DatasourceInfo{ | 
					
						
							|  |  |  | 			URL:                        ts.URL, | 
					
						
							|  |  |  | 			HTTPClient:                 ts.Client(), | 
					
						
							|  |  |  | 			Database:                   "[metrics-]YYYY.MM.DD", | 
					
						
							| 
									
										
										
										
											2023-03-01 18:50:56 +08:00
										 |  |  | 			ConfiguredFields:           configuredFields, | 
					
						
							| 
									
										
										
										
											2022-09-26 20:27:46 +08:00
										 |  |  | 			Interval:                   "Daily", | 
					
						
							|  |  |  | 			MaxConcurrentShardRequests: 6, | 
					
						
							|  |  |  | 			IncludeFrozen:              true, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-05-06 21:12:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		timeRange := backend.TimeRange{ | 
					
						
							|  |  |  | 			From: from, | 
					
						
							|  |  |  | 			To:   to, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 		c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest()) | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.NotNil(t, c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		t.Cleanup(func() { | 
					
						
							|  |  |  | 			ts.Close() | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 		ms, err := createMultisearchForTest(t, c, timeRange) | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		res, err := c.ExecuteMultisearch(ms) | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		require.NotNil(t, request) | 
					
						
							|  |  |  | 		assert.Equal(t, http.MethodPost, request.Method) | 
					
						
							|  |  |  | 		assert.Equal(t, "/_msearch", request.URL.Path) | 
					
						
							|  |  |  | 		assert.Equal(t, "max_concurrent_shard_requests=6&ignore_throttled=false", request.URL.RawQuery) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		require.NotNil(t, requestBody) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		headerBytes, err := requestBody.ReadBytes('\n') | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-09-14 20:59:35 +08:00
										 |  |  | 		bodyBytes := requestBody.Bytes() | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		jHeader, err := simplejson.NewJson(headerBytes) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		jBody, err := simplejson.NewJson(bodyBytes) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 		assert.Equal(t, "metrics-2018.05.15", jHeader.Get("index").MustString()) | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		assert.True(t, jHeader.Get("ignore_unavailable").MustBool(false)) | 
					
						
							|  |  |  | 		assert.Equal(t, "query_then_fetch", jHeader.Get("search_type").MustString()) | 
					
						
							|  |  |  | 		assert.Empty(t, jHeader.Get("max_concurrent_shard_requests")) | 
					
						
							| 
									
										
										
										
											2021-07-15 21:52:02 +08:00
										 |  |  | 		assert.False(t, jHeader.Get("ignore_throttled").MustBool()) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString()) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 16:28:29 +08:00
										 |  |  | 		assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "fixed_interval").MustString()) | 
					
						
							| 
									
										
										
										
											2019-04-25 15:41:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 		assert.Equal(t, 200, res.Status) | 
					
						
							|  |  |  | 		require.Len(t, res.Responses, 1) | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Given a fake http client, 2 queries and a client with response", func(t *testing.T) { | 
					
						
							|  |  |  | 		var requestBody *bytes.Buffer | 
					
						
							|  |  |  | 		ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 			buf, err := io.ReadAll(r.Body) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			requestBody = bytes.NewBuffer(buf) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			rw.Header().Set("Content-Type", "application/x-ndjson") | 
					
						
							|  |  |  | 			_, err = rw.Write([]byte( | 
					
						
							|  |  |  | 				`{ | 
					
						
							|  |  |  | 				"responses": [ | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						"hits": {	"hits": [], "max_score": 0,	"total": { "value": 4656, "relation": "eq"}	}, | 
					
						
							|  |  |  | 						"status": 200 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				] | 
					
						
							|  |  |  | 			}`)) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			rw.WriteHeader(200) | 
					
						
							|  |  |  | 		})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		configuredFields := ConfiguredFields{ | 
					
						
							|  |  |  | 			TimeField:       "testtime", | 
					
						
							|  |  |  | 			LogMessageField: "line", | 
					
						
							|  |  |  | 			LogLevelField:   "lvl", | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ds := DatasourceInfo{ | 
					
						
							|  |  |  | 			URL:                        ts.URL, | 
					
						
							|  |  |  | 			HTTPClient:                 ts.Client(), | 
					
						
							|  |  |  | 			Database:                   "[metrics-]YYYY.MM.DD", | 
					
						
							|  |  |  | 			ConfiguredFields:           configuredFields, | 
					
						
							|  |  |  | 			Interval:                   "Daily", | 
					
						
							|  |  |  | 			MaxConcurrentShardRequests: 6, | 
					
						
							|  |  |  | 			IncludeFrozen:              true, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		timeRange := backend.TimeRange{ | 
					
						
							|  |  |  | 			From: from, | 
					
						
							|  |  |  | 			To:   to, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		from2 := time.Date(2018, 5, 17, 17, 50, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		to2 := time.Date(2018, 5, 17, 17, 55, 0, 0, time.UTC) | 
					
						
							|  |  |  | 		timeRange2 := backend.TimeRange{ | 
					
						
							|  |  |  | 			From: from2, | 
					
						
							|  |  |  | 			To:   to2, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest()) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.NotNil(t, c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		t.Cleanup(func() { | 
					
						
							|  |  |  | 			ts.Close() | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ms, err := createMultisearchWithMultipleQueriesForTest(t, c, timeRange, timeRange2) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = c.ExecuteMultisearch(ms) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.NotNil(t, requestBody) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bodyString := requestBody.String() | 
					
						
							|  |  |  | 		require.Contains(t, bodyString, "metrics-2018.05.15") | 
					
						
							|  |  |  | 		require.Contains(t, bodyString, "metrics-2018.05.17") | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | func TestClient_Index(t *testing.T) { | 
					
						
							|  |  |  | 	tt := []struct { | 
					
						
							|  |  |  | 		name                string | 
					
						
							|  |  |  | 		indexInDatasource   string | 
					
						
							|  |  |  | 		patternInDatasource string | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 		indexInRequest      string | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2023-05-17 21:24:46 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			name:                "empty string", | 
					
						
							|  |  |  | 			indexInDatasource:   "", | 
					
						
							|  |  |  | 			patternInDatasource: "", | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 			indexInRequest:      "", | 
					
						
							| 
									
										
										
										
											2023-05-17 21:24:46 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			name:                "single string", | 
					
						
							|  |  |  | 			indexInDatasource:   "logs-*", | 
					
						
							|  |  |  | 			patternInDatasource: "", | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 			indexInRequest:      "logs-*", | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:                "daily pattern", | 
					
						
							|  |  |  | 			indexInDatasource:   "[logs-]YYYY.MM.DD", | 
					
						
							|  |  |  | 			patternInDatasource: "Daily", | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 			indexInRequest:      "logs-2018.05.10,logs-2018.05.11,logs-2018.05.12", | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tt { | 
					
						
							|  |  |  | 		t.Run(test.name, func(t *testing.T) { | 
					
						
							|  |  |  | 			var request *http.Request | 
					
						
							|  |  |  | 			var requestBody *bytes.Buffer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 				request = r | 
					
						
							|  |  |  | 				buf, err := io.ReadAll(r.Body) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				requestBody = bytes.NewBuffer(buf) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				rw.Header().Set("Content-Type", "application/x-ndjson") | 
					
						
							|  |  |  | 				_, err = rw.Write([]byte( | 
					
						
							|  |  |  | 					`{ | 
					
						
							|  |  |  | 				"responses": [ | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						"hits": {	"hits": [], "max_score": 0,	"total": { "value": 4656, "relation": "eq"}	}, | 
					
						
							|  |  |  | 						"status": 200 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				] | 
					
						
							|  |  |  | 			}`)) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 				rw.WriteHeader(200) | 
					
						
							|  |  |  | 			})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			configuredFields := ConfiguredFields{ | 
					
						
							|  |  |  | 				TimeField:       "testtime", | 
					
						
							|  |  |  | 				LogMessageField: "line", | 
					
						
							|  |  |  | 				LogLevelField:   "lvl", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ds := DatasourceInfo{ | 
					
						
							|  |  |  | 				URL:                        ts.URL, | 
					
						
							|  |  |  | 				HTTPClient:                 ts.Client(), | 
					
						
							|  |  |  | 				Database:                   test.indexInDatasource, | 
					
						
							|  |  |  | 				ConfiguredFields:           configuredFields, | 
					
						
							|  |  |  | 				Interval:                   test.patternInDatasource, | 
					
						
							|  |  |  | 				MaxConcurrentShardRequests: 6, | 
					
						
							|  |  |  | 				IncludeFrozen:              true, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			from := time.Date(2018, 5, 10, 17, 50, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			to := time.Date(2018, 5, 12, 17, 55, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			timeRange := backend.TimeRange{ | 
					
						
							|  |  |  | 				From: from, | 
					
						
							|  |  |  | 				To:   to, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 			c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest()) | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.NotNil(t, c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			t.Cleanup(func() { | 
					
						
							|  |  |  | 				ts.Close() | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 			ms, err := createMultisearchForTest(t, c, timeRange) | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			_, err = c.ExecuteMultisearch(ms) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			require.NotNil(t, request) | 
					
						
							|  |  |  | 			require.NotNil(t, requestBody) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			headerBytes, err := requestBody.ReadBytes('\n') | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			jHeader, err := simplejson.NewJson(headerBytes) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:47:16 +08:00
										 |  |  | 			assert.Equal(t, test.indexInRequest, jHeader.Get("index").MustString()) | 
					
						
							| 
									
										
										
										
											2023-05-16 23:09:09 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | func createMultisearchForTest(t *testing.T, c Client, timeRange backend.TimeRange) (*MultiSearchRequest, error) { | 
					
						
							| 
									
										
										
										
											2021-01-07 16:35:56 +08:00
										 |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 	msb := c.MultiSearch() | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 	s := msb.Search(15*time.Second, timeRange) | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 	s.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) { | 
					
						
							| 
									
										
										
										
											2022-06-13 16:28:29 +08:00
										 |  |  | 		a.FixedInterval = "$__interval" | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 01:02:20 +08:00
										 |  |  | 		ab.Metric("1", "avg", "@hostname", func(a *MetricAggregation) { | 
					
						
							|  |  |  | 			a.Settings["script"] = "$__interval_ms*@hostname" | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	return msb.Build() | 
					
						
							| 
									
										
										
										
											2018-05-23 20:59:12 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-13 18:49:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func createMultisearchWithMultipleQueriesForTest(t *testing.T, c Client, firstTimeRange backend.TimeRange, secondTimeRange backend.TimeRange) (*MultiSearchRequest, error) { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	msb := c.MultiSearch() | 
					
						
							|  |  |  | 	s1 := msb.Search(15*time.Second, firstTimeRange) | 
					
						
							|  |  |  | 	s1.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) { | 
					
						
							|  |  |  | 		a.FixedInterval = "$__interval" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ab.Metric("1", "avg", "@hostname", func(a *MetricAggregation) { | 
					
						
							|  |  |  | 			a.Settings["script"] = "$__interval_ms*@hostname" | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s2 := msb.Search(15*time.Second, secondTimeRange) | 
					
						
							|  |  |  | 	s2.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) { | 
					
						
							|  |  |  | 		a.FixedInterval = "$__interval" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ab.Metric("1", "avg", "@hostname", func(a *MetricAggregation) { | 
					
						
							|  |  |  | 			a.Settings["script"] = "$__interval_ms*@hostname" | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return msb.Build() | 
					
						
							|  |  |  | } |