Skip to content

Commit a464860

Browse files
committed
feat(testdata): added csv test data scenario
1 parent 8d58576 commit a464860

File tree

12 files changed

+490
-20
lines changed

12 files changed

+490
-20
lines changed

pkg/api/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func GetTestDataScenarios(c *middleware.Context) Response {
4848
"id": scenario.Id,
4949
"name": scenario.Name,
5050
"description": scenario.Description,
51+
"stringInput": scenario.StringInput,
5152
})
5253
}
5354

pkg/tsdb/graphite/graphite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
7979
}
8080

8181
result.QueryResults = make(map[string]*tsdb.QueryResult)
82-
queryRes := &tsdb.QueryResult{}
82+
queryRes := tsdb.NewQueryResult()
8383

8484
for _, series := range data {
8585
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{

pkg/tsdb/models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ type TimePoint [2]null.Float
6666
type TimeSeriesPoints []TimePoint
6767
type TimeSeriesSlice []*TimeSeries
6868

69+
func NewQueryResult() *QueryResult {
70+
return &QueryResult{
71+
Series: make(TimeSeriesSlice, 0),
72+
}
73+
}
74+
6975
func NewTimePoint(value float64, timestamp float64) TimePoint {
7076
return TimePoint{null.FloatFrom(value), null.FloatFrom(timestamp)}
7177
}

pkg/tsdb/prometheus/prometheus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func parseQuery(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) (*Prom
132132

133133
func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb.QueryResult, error) {
134134
queryResults := make(map[string]*tsdb.QueryResult)
135-
queryRes := &tsdb.QueryResult{}
135+
queryRes := tsdb.NewQueryResult()
136136

137137
data, ok := value.(pmodel.Matrix)
138138
if !ok {

pkg/tsdb/testdata/scenarios.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package testdata
22

33
import (
44
"math/rand"
5+
"strconv"
6+
"strings"
57
"time"
68

9+
"github.com/grafana/grafana/pkg/log"
710
"github.com/grafana/grafana/pkg/tsdb"
811
)
912

@@ -12,6 +15,7 @@ type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.Q
1215
type Scenario struct {
1316
Id string `json:"id"`
1417
Name string `json:"name"`
18+
StringInput string `json:"stringOption"`
1519
Description string `json:"description"`
1620
Handler ScenarioHandler `json:"-"`
1721
}
@@ -20,15 +24,17 @@ var ScenarioRegistry map[string]*Scenario
2024

2125
func init() {
2226
ScenarioRegistry = make(map[string]*Scenario)
23-
//logger := log.New("tsdb.testdata")
27+
logger := log.New("tsdb.testdata")
28+
29+
logger.Debug("Initializing TestData Scenario")
2430

2531
registerScenario(&Scenario{
2632
Id: "random_walk",
2733
Name: "Random Walk",
2834

2935
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
30-
to := context.TimeRange.MustGetTo().UnixNano() / int64(time.Millisecond)
31-
timeWalkerMs := context.TimeRange.MustGetFrom().UnixNano() / int64(time.Millisecond)
36+
timeWalkerMs := context.TimeRange.GetFromAsMsEpoch()
37+
to := context.TimeRange.GetToAsMsEpoch()
3238

3339
series := newSeriesForQuery(query)
3440

@@ -44,7 +50,7 @@ func init() {
4450

4551
series.Points = points
4652

47-
queryRes := &tsdb.QueryResult{}
53+
queryRes := tsdb.NewQueryResult()
4854
queryRes.Series = append(queryRes.Series, series)
4955
return queryRes
5056
},
@@ -54,17 +60,15 @@ func init() {
5460
Id: "no_data_points",
5561
Name: "No Data Points",
5662
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
57-
return &tsdb.QueryResult{
58-
Series: make(tsdb.TimeSeriesSlice, 0),
59-
}
63+
return tsdb.NewQueryResult()
6064
},
6165
})
6266

6367
registerScenario(&Scenario{
6468
Id: "datapoints_outside_range",
6569
Name: "Datapoints Outside Range",
6670
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
67-
queryRes := &tsdb.QueryResult{}
71+
queryRes := tsdb.NewQueryResult()
6872

6973
series := newSeriesForQuery(query)
7074
outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000
@@ -75,6 +79,41 @@ func init() {
7579
return queryRes
7680
},
7781
})
82+
83+
registerScenario(&Scenario{
84+
Id: "csv_metric_values",
85+
Name: "CSV Metric Values",
86+
StringInput: "1,20,90,30,5,0",
87+
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
88+
queryRes := tsdb.NewQueryResult()
89+
90+
stringInput := query.Model.Get("stringInput").MustString()
91+
values := []float64{}
92+
for _, strVal := range strings.Split(stringInput, ",") {
93+
if val, err := strconv.ParseFloat(strVal, 64); err == nil {
94+
values = append(values, val)
95+
}
96+
}
97+
98+
if len(values) == 0 {
99+
return queryRes
100+
}
101+
102+
series := newSeriesForQuery(query)
103+
startTime := context.TimeRange.GetFromAsMsEpoch()
104+
endTime := context.TimeRange.GetToAsMsEpoch()
105+
step := (endTime - startTime) / int64(len(values)-1)
106+
107+
for _, val := range values {
108+
series.Points = append(series.Points, tsdb.NewTimePoint(val, float64(startTime)))
109+
startTime += step
110+
}
111+
112+
queryRes.Series = append(queryRes.Series, series)
113+
114+
return queryRes
115+
},
116+
})
78117
}
79118

80119
func registerScenario(scenario *Scenario) {

pkg/tsdb/time_range.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ type TimeRange struct {
2121
Now time.Time
2222
}
2323

24+
func (tr *TimeRange) GetFromAsMsEpoch() int64 {
25+
return tr.MustGetFrom().UnixNano() / int64(time.Millisecond)
26+
}
27+
28+
func (tr *TimeRange) GetToAsMsEpoch() int64 {
29+
return tr.MustGetTo().UnixNano() / int64(time.Millisecond)
30+
}
31+
2432
func (tr *TimeRange) MustGetFrom() time.Time {
2533
if res, err := tr.ParseFrom(); err != nil {
2634
return time.Unix(0, 0)

0 commit comments

Comments
 (0)