feat(testdata): worked on testdata app
This commit is contained in:
+11
-10
@@ -3,21 +3,22 @@ package tsdb
|
||||
import "github.com/grafana/grafana/pkg/components/simplejson"
|
||||
|
||||
type Query struct {
|
||||
RefId string
|
||||
Query string
|
||||
Model *simplejson.Json
|
||||
Depends []string
|
||||
DataSource *DataSourceInfo
|
||||
Results []*TimeSeries
|
||||
Exclude bool
|
||||
RefId string
|
||||
Query string
|
||||
Model *simplejson.Json
|
||||
Depends []string
|
||||
DataSource *DataSourceInfo
|
||||
Results []*TimeSeries
|
||||
Exclude bool
|
||||
MaxDataPoints int64
|
||||
IntervalMs int64
|
||||
}
|
||||
|
||||
type QuerySlice []*Query
|
||||
|
||||
type Request struct {
|
||||
TimeRange TimeRange
|
||||
MaxDataPoints int
|
||||
Queries QuerySlice
|
||||
TimeRange TimeRange
|
||||
Queries QuerySlice
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
|
||||
Vendored
+54
@@ -0,0 +1,54 @@
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
)
|
||||
|
||||
type TestDataExecutor struct {
|
||||
*tsdb.DataSourceInfo
|
||||
}
|
||||
|
||||
func NewTestDataExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor {
|
||||
return &TestDataExecutor{dsInfo}
|
||||
}
|
||||
|
||||
func init() {
|
||||
tsdb.RegisterExecutor("grafana-testdata-datasource", NewTestDataExecutor)
|
||||
}
|
||||
|
||||
func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
|
||||
result := &tsdb.BatchResult{}
|
||||
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
||||
|
||||
from, _ := context.TimeRange.FromTime()
|
||||
to, _ := context.TimeRange.ToTime()
|
||||
|
||||
queryRes := &tsdb.QueryResult{}
|
||||
|
||||
for _, query := range queries {
|
||||
// scenario := query.Model.Get("scenario").MustString("random_walk")
|
||||
series := &tsdb.TimeSeries{Name: "test-series-0"}
|
||||
|
||||
stepInSeconds := (to.Unix() - from.Unix()) / query.MaxDataPoints
|
||||
points := make([][2]*float64, 0)
|
||||
walker := rand.Float64() * 100
|
||||
time := from.Unix()
|
||||
|
||||
for i := int64(0); i < query.MaxDataPoints; i++ {
|
||||
timestamp := float64(time)
|
||||
val := float64(walker)
|
||||
points = append(points, [2]*float64{&val, ×tamp})
|
||||
|
||||
walker += rand.Float64() - 0.5
|
||||
time += stepInSeconds
|
||||
}
|
||||
|
||||
series.Points = points
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
}
|
||||
|
||||
result.QueryResults["A"] = queryRes
|
||||
return result
|
||||
}
|
||||
+10
-1
@@ -2,11 +2,12 @@ package tsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewTimerange(from, to string) TimeRange {
|
||||
func NewTimeRange(from, to string) TimeRange {
|
||||
return TimeRange{
|
||||
From: from,
|
||||
To: to,
|
||||
@@ -21,6 +22,10 @@ type TimeRange struct {
|
||||
}
|
||||
|
||||
func (tr TimeRange) FromTime() (time.Time, error) {
|
||||
if val, err := strconv.ParseInt(tr.From, 10, 64); err == nil {
|
||||
return time.Unix(val, 0), nil
|
||||
}
|
||||
|
||||
fromRaw := strings.Replace(tr.From, "now-", "", 1)
|
||||
|
||||
diff, err := time.ParseDuration("-" + fromRaw)
|
||||
@@ -45,5 +50,9 @@ func (tr TimeRange) ToTime() (time.Time, error) {
|
||||
return tr.Now.Add(diff), nil
|
||||
}
|
||||
|
||||
if val, err := strconv.ParseInt(tr.To, 10, 64); err == nil {
|
||||
return time.Unix(val, 0), nil
|
||||
}
|
||||
|
||||
return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To)
|
||||
}
|
||||
|
||||
@@ -60,6 +60,23 @@ func TestTimeRange(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
Convey("can parse unix epocs", func() {
|
||||
var err error
|
||||
tr := TimeRange{
|
||||
From: "1474973725473",
|
||||
To: "1474975757930",
|
||||
Now: now,
|
||||
}
|
||||
|
||||
res, err := tr.FromTime()
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Unix(), ShouldEqual, 1474973725473)
|
||||
|
||||
res, err = tr.ToTime()
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Unix(), ShouldEqual, 1474975757930)
|
||||
})
|
||||
|
||||
Convey("Cannot parse asdf", func() {
|
||||
var err error
|
||||
tr := TimeRange{
|
||||
|
||||
Reference in New Issue
Block a user