Chore: Fix Random Walk scenario for Grafana DS (#73889)

This commit is contained in:
Andres Martinez Gotor 2023-08-28 10:53:30 +02:00 committed by GitHub
parent 1a281ac49d
commit aa0d4b3e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 19 deletions

View File

@ -165,7 +165,11 @@ func (s *Service) doReadQuery(ctx context.Context, query backend.DataQuery) back
func (s *Service) doRandomWalk(query backend.DataQuery) backend.DataResponse { func (s *Service) doRandomWalk(query backend.DataQuery) backend.DataResponse {
response := backend.DataResponse{} response := backend.DataResponse{}
model := testdatasource.JSONModel{} model, err := testdatasource.GetJSONModel(json.RawMessage{})
if err != nil {
response.Error = err
return response
}
response.Frames = data.Frames{testdatasource.RandomWalk(query, model, 0)} response.Frames = data.Frames{testdatasource.RandomWalk(query, model, 0)}
return response return response

View File

@ -21,7 +21,7 @@ func (s *Service) handleCsvContentScenario(ctx context.Context, req *backend.Que
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse query json: %v", err) return nil, fmt.Errorf("failed to parse query json: %v", err)
} }
@ -58,7 +58,7 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse query json %v", err) return nil, fmt.Errorf("failed to parse query json %v", err)
} }

View File

@ -263,7 +263,7 @@ type pulseWave struct {
OffValue interface{} `json:"offValue"` OffValue interface{} `json:"offValue"`
} }
func getModel(j json.RawMessage) (JSONModel, error) { func GetJSONModel(j json.RawMessage) (JSONModel, error) {
model := JSONModel{ model := JSONModel{
// Default values // Default values
ScenarioID: string(randomWalkQuery), ScenarioID: string(randomWalkQuery),
@ -272,9 +272,11 @@ func getModel(j json.RawMessage) (JSONModel, error) {
StartValue: rand.Float64() * 100, StartValue: rand.Float64() * 100,
Spread: 1, Spread: 1,
} }
err := json.Unmarshal(j, &model) if len(j) > 0 {
if err != nil { err := json.Unmarshal(j, &model)
return JSONModel{}, err if err != nil {
return JSONModel{}, err
}
} }
return model, nil return model, nil
} }
@ -284,7 +286,7 @@ func (s *Service) handleFallbackScenario(ctx context.Context, req *backend.Query
scenarioQueries := map[string][]backend.DataQuery{} scenarioQueries := map[string][]backend.DataQuery{}
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
s.logger.Error("Failed to unmarshal query model to JSON", "error", err) s.logger.Error("Failed to unmarshal query model to JSON", "error", err)
continue continue
@ -327,7 +329,7 @@ func (s *Service) handleRandomWalkScenario(ctx context.Context, req *backend.Que
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -347,7 +349,7 @@ func (s *Service) handleDatapointsOutsideRangeScenario(ctx context.Context, req
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -371,7 +373,7 @@ func (s *Service) handleCSVMetricValuesScenario(ctx context.Context, req *backen
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -421,7 +423,7 @@ func (s *Service) handleRandomWalkWithErrorScenario(ctx context.Context, req *ba
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -439,7 +441,7 @@ func (s *Service) handleRandomWalkSlowScenario(ctx context.Context, req *backend
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -460,7 +462,7 @@ func (s *Service) handleRandomWalkTableScenario(ctx context.Context, req *backen
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -477,7 +479,7 @@ func (s *Service) handlePredictableCSVWaveScenario(ctx context.Context, req *bac
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -498,7 +500,7 @@ func (s *Service) handlePredictablePulseScenario(ctx context.Context, req *backe
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -517,7 +519,7 @@ func (s *Service) handlePredictablePulseScenario(ctx context.Context, req *backe
func (s *Service) handleServerError500Scenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { func (s *Service) handleServerError500Scenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }
@ -539,7 +541,7 @@ func (s *Service) handleArrowScenario(ctx context.Context, req *backend.QueryDat
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -625,7 +627,7 @@ func (s *Service) handleLogsScenario(ctx context.Context, req *backend.QueryData
from := q.TimeRange.From.UnixNano() / int64(time.Millisecond) from := q.TimeRange.From.UnixNano() / int64(time.Millisecond)
to := q.TimeRange.To.UnixNano() / int64(time.Millisecond) to := q.TimeRange.To.UnixNano() / int64(time.Millisecond)
model, err := getModel(q.JSON) model, err := GetJSONModel(q.JSON)
if err != nil { if err != nil {
continue continue
} }