update grafana/gafana-plugin-sdk-go dependency, and adjusted code accordingly (#113922)
* update dependencies * query: stop handling _timeRange * fixed unit test
This commit is contained in:
@@ -333,12 +333,9 @@ func (s *ServiceImpl) handleQuerySingleDatasource(ctx context.Context, user iden
|
||||
}
|
||||
|
||||
func getTimeRange(query *simplejson.Json, globalFrom string, globalTo string) (string, string, error) {
|
||||
tr, ok := query.CheckGet("_timeRange")
|
||||
if !ok { // timeRange json node does not exist
|
||||
tr, ok = query.CheckGet("timeRange") // try the old name for backward compatibility
|
||||
if !ok { // timeRange json node does not exist, use global from/to
|
||||
return globalFrom, globalTo, nil
|
||||
}
|
||||
tr, ok := query.CheckGet("timeRange")
|
||||
if !ok { // timeRange json node does not exist, use global from/to
|
||||
return globalFrom, globalTo, nil
|
||||
}
|
||||
from, err := tr.Get("from").String()
|
||||
if err != nil {
|
||||
@@ -352,6 +349,40 @@ func getTimeRange(query *simplejson.Json, globalFrom string, globalTo string) (s
|
||||
return from, to, nil
|
||||
}
|
||||
|
||||
const timeRangeKey = "timeRange"
|
||||
|
||||
func queryToJson(query *simplejson.Json, supportLocalTimeRange bool) ([]byte, error) {
|
||||
if !supportLocalTimeRange {
|
||||
return query.MarshalJSON()
|
||||
}
|
||||
|
||||
// we need to remove the `timeRange` attribute from the JSON, if it exists there,
|
||||
// because it might cause complications with certain data sources.
|
||||
_, has := query.CheckGet(timeRangeKey)
|
||||
if !has {
|
||||
return query.MarshalJSON()
|
||||
}
|
||||
|
||||
qMap, err := query.Map()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// we do not want to modify the query,
|
||||
// so we create a copy without
|
||||
// the timeRange attribute
|
||||
d := make(map[string]any)
|
||||
|
||||
// without the `timeRange` attribute
|
||||
for k, v := range qMap {
|
||||
if k != timeRangeKey {
|
||||
d[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return simplejson.NewFromAny(d).MarshalJSON()
|
||||
}
|
||||
|
||||
// parseRequest parses a request into parsed queries grouped by datasource uid
|
||||
func (s *ServiceImpl) parseMetricRequest(ctx context.Context, user identity.Requester, skipDSCache bool, reqDTO dtos.MetricRequest, supportLocalTimeRange bool) (*parsedRequest, error) {
|
||||
if len(reqDTO.Queries) == 0 {
|
||||
@@ -397,7 +428,7 @@ func (s *ServiceImpl) parseMetricRequest(ctx context.Context, user identity.Requ
|
||||
timeRange = gtime.NewTimeRange(reqDTO.From, reqDTO.To)
|
||||
}
|
||||
|
||||
modelJSON, err := query.MarshalJSON()
|
||||
modelJSON, err := queryToJson(query, supportLocalTimeRange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user