Fix alerting queries with numeric query type

This commit is contained in:
Alexander Zobnin
2021-06-02 12:47:30 +03:00
parent c546f2143d
commit eb389afc11

View File

@@ -3,9 +3,12 @@ package datasource
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/bitly/go-simplejson"
"github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
) )
const ( const (
@@ -121,5 +124,21 @@ func ReadQuery(query backend.DataQuery) (QueryModel, error) {
return model, fmt.Errorf("could not read query: %w", err) return model, fmt.Errorf("could not read query: %w", err)
} }
if model.QueryType == "" {
queryJSON, err := simplejson.NewJson(query.JSON)
if err != nil {
return model, fmt.Errorf("could not read query JSON: %w", err)
}
queryType, err := queryJSON.Get("queryType").Int64()
if err != nil {
log.DefaultLogger.Warn("could not read query type", "error", err)
log.DefaultLogger.Debug("setting query type to default value")
model.QueryType = "0"
} else {
model.QueryType = strconv.FormatInt(queryType, 10)
}
}
return model, nil return model, nil
} }