@@ -2,6 +2,7 @@ package benchspy
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "reflect"
78 "time"
@@ -18,7 +19,7 @@ type PrometheusQueryExecutor struct {
1819 startTime , endTime time.Time `json:"-"`
1920 client * client.Prometheus `json:"-"`
2021 Queries map [string ]string `json:"queries"`
21- ResourceResults map [string ]interface {} `json:"resources "`
22+ QueryResults map [string ]interface {} `json:"query_results "`
2223 warnings map [string ]v1.Warnings `json:"-"`
2324}
2425
@@ -30,12 +31,12 @@ func NewPrometheusQueryExecutor(url string, startTime, endTime time.Time, querie
3031 }
3132
3233 return & PrometheusQueryExecutor {
33- KindName : string (StandardQueryExecutor_Prometheus ),
34- client : c ,
35- Queries : queries ,
36- startTime : startTime ,
37- endTime : endTime ,
38- ResourceResults : make (map [string ]interface {}),
34+ KindName : string (StandardQueryExecutor_Prometheus ),
35+ client : c ,
36+ Queries : queries ,
37+ startTime : startTime ,
38+ endTime : endTime ,
39+ QueryResults : make (map [string ]interface {}),
3940 }, nil
4041}
4142
@@ -51,11 +52,11 @@ func NewStandardPrometheusQueryExecutor(url string, startTime, endTime time.Time
5152 }
5253
5354 return & PrometheusQueryExecutor {
54- client : c ,
55- Queries : standardQueries ,
56- startTime : startTime ,
57- endTime : endTime ,
58- ResourceResults : make (map [string ]interface {}),
55+ client : c ,
56+ Queries : standardQueries ,
57+ startTime : startTime ,
58+ endTime : endTime ,
59+ QueryResults : make (map [string ]interface {}),
5960 }, nil
6061}
6162
@@ -70,14 +71,14 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
7071 r .warnings [name ] = warnings
7172 }
7273
73- r .ResourceResults [name ] = result
74+ r .QueryResults [name ] = result
7475 }
7576
7677 return nil
7778}
7879
7980func (r * PrometheusQueryExecutor ) Results () map [string ]interface {} {
80- return r .ResourceResults
81+ return r .QueryResults
8182}
8283
8384func (l * PrometheusQueryExecutor ) Kind () string {
@@ -138,12 +139,12 @@ func (r *PrometheusQueryExecutor) Warnings() map[string]v1.Warnings {
138139 return r .warnings
139140}
140141
141- func (r * PrometheusQueryExecutor ) MustResourcesAsValue () map [string ]model.Value {
142- resources := make (map [string ]model.Value )
143- for name , resource := range r .ResourceResults {
144- resources [name ] = resource .(model.Value )
142+ func (r * PrometheusQueryExecutor ) MustResultsAsValue () map [string ]model.Value {
143+ results := make (map [string ]model.Value )
144+ for name , result := range r .QueryResults {
145+ results [name ] = result .(model.Value )
145146 }
146- return resources
147+ return results
147148}
148149
149150func (r * PrometheusQueryExecutor ) TimeRange (startTime , endTime time.Time ) {
@@ -184,3 +185,27 @@ func (r *PrometheusQueryExecutor) generateStandardQueries(nameRegexPattern strin
184185
185186 return standardQueries , nil
186187}
188+
189+ func (r * PrometheusQueryExecutor ) UnmarshalJSON (data []byte ) error {
190+ // helper struct with QueryResults map[string]interface{}
191+ type Alias PrometheusQueryExecutor
192+ var raw struct {
193+ Alias
194+ QueryResults map [string ]interface {} `json:"query_results"`
195+ }
196+
197+ // unmarshal into the helper struct to populate other fields automatically
198+ if err := json .Unmarshal (data , & raw ); err != nil {
199+ return err
200+ }
201+
202+ // convert map[string]interface{} to map[string]actualType
203+ convertedTypes , conversionErr := convertQueryResults (raw .QueryResults )
204+ if conversionErr != nil {
205+ return conversionErr
206+ }
207+
208+ * r = PrometheusQueryExecutor (raw .Alias )
209+ r .QueryResults = convertedTypes
210+ return nil
211+ }
0 commit comments