From 857b23873f54afa642e4961071c326ea1e00eddf Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Thu, 2 Feb 2023 15:29:13 +1100 Subject: [PATCH] Expose QueryData so that implementations of Codec.CanEncode() can perform a type assertion against Response.Data. Signed-off-by: Charles Korn --- web/api/v1/api.go | 6 +++--- web/api/v1/api_test.go | 28 ++++++++++++++-------------- web/api/v1/json_codec_test.go | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index e0ad76c5c..e9c1182ca 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -384,7 +384,7 @@ func (api *API) Register(r *route.Router) { r.Put("/admin/tsdb/snapshot", wrapAgent(api.snapshot)) } -type queryData struct { +type QueryData struct { ResultType parser.ValueType `json:"resultType"` Result parser.Value `json:"result"` Stats stats.QueryStats `json:"stats,omitempty"` @@ -446,7 +446,7 @@ func (api *API) query(r *http.Request) (result apiFuncResult) { } qs := sr(ctx, qry.Stats(), r.FormValue("stats")) - return apiFuncResult{&queryData{ + return apiFuncResult{&QueryData{ ResultType: res.Value.Type(), Result: res.Value, Stats: qs, @@ -537,7 +537,7 @@ func (api *API) queryRange(r *http.Request) (result apiFuncResult) { } qs := sr(ctx, qry.Stats(), r.FormValue("stats")) - return apiFuncResult{&queryData{ + return apiFuncResult{&QueryData{ ResultType: res.Value.Type(), Result: res.Value, Stats: qs, diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 617a8bdf3..455eae13f 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -831,8 +831,8 @@ func TestStats(t *testing.T) { name: "stats is blank", param: "", expected: func(t *testing.T, i interface{}) { - require.IsType(t, i, &queryData{}) - qd := i.(*queryData) + require.IsType(t, i, &QueryData{}) + qd := i.(*QueryData) require.Nil(t, qd.Stats) }, }, @@ -840,8 +840,8 @@ func TestStats(t *testing.T) { name: "stats is true", param: "true", expected: func(t *testing.T, i interface{}) { - require.IsType(t, i, &queryData{}) - qd := i.(*queryData) + require.IsType(t, i, &QueryData{}) + qd := i.(*QueryData) require.NotNil(t, qd.Stats) qs := qd.Stats.Builtin() require.NotNil(t, qs.Timings) @@ -855,8 +855,8 @@ func TestStats(t *testing.T) { name: "stats is all", param: "all", expected: func(t *testing.T, i interface{}) { - require.IsType(t, i, &queryData{}) - qd := i.(*queryData) + require.IsType(t, i, &QueryData{}) + qd := i.(*QueryData) require.NotNil(t, qd.Stats) qs := qd.Stats.Builtin() require.NotNil(t, qs.Timings) @@ -876,8 +876,8 @@ func TestStats(t *testing.T) { }, param: "known", expected: func(t *testing.T, i interface{}) { - require.IsType(t, i, &queryData{}) - qd := i.(*queryData) + require.IsType(t, i, &QueryData{}) + qd := i.(*QueryData) require.NotNil(t, qd.Stats) j, err := json.Marshal(qd.Stats) require.NoError(t, err) @@ -1037,7 +1037,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E "query": []string{"2"}, "time": []string{"123.4"}, }, - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeScalar, Result: promql.Scalar{ V: 2, @@ -1051,7 +1051,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E "query": []string{"0.333"}, "time": []string{"1970-01-01T00:02:03Z"}, }, - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeScalar, Result: promql.Scalar{ V: 0.333, @@ -1065,7 +1065,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E "query": []string{"0.333"}, "time": []string{"1970-01-01T01:02:03+01:00"}, }, - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeScalar, Result: promql.Scalar{ V: 0.333, @@ -1078,7 +1078,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E query: url.Values{ "query": []string{"0.333"}, }, - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeScalar, Result: promql.Scalar{ V: 0.333, @@ -1094,7 +1094,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E "end": []string{"2"}, "step": []string{"1"}, }, - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeMatrix, Result: promql.Matrix{ promql.Series{ @@ -3180,7 +3180,7 @@ func BenchmarkRespond(b *testing.B) { for i := 0; i < 10000; i++ { points = append(points, promql.Point{V: float64(i * 1000000), T: int64(i)}) } - response := &queryData{ + response := &QueryData{ ResultType: parser.ValueTypeMatrix, Result: promql.Matrix{ promql.Series{ diff --git a/web/api/v1/json_codec_test.go b/web/api/v1/json_codec_test.go index c5b030ff9..20b8ac3a0 100644 --- a/web/api/v1/json_codec_test.go +++ b/web/api/v1/json_codec_test.go @@ -30,7 +30,7 @@ func TestJsonCodec_Encode(t *testing.T) { expected string }{ { - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeMatrix, Result: promql.Matrix{ promql.Series{ @@ -42,7 +42,7 @@ func TestJsonCodec_Encode(t *testing.T) { expected: `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"foo"},"values":[[1,"1"]]}]}}`, }, { - response: &queryData{ + response: &QueryData{ ResultType: parser.ValueTypeMatrix, Result: promql.Matrix{ promql.Series{