feat(testdata): worked on testdata app

This commit is contained in:
Torkel Ödegaard
2016-09-27 14:39:51 +02:00
parent 81cb4a740b
commit 34f15d92d0
30 changed files with 512 additions and 169 deletions
+1 -6
View File
@@ -97,12 +97,7 @@ func (slice DataSourceList) Swap(i, j int) {
}
type MetricQueryResultDto struct {
Data []MetricQueryResultDataDto `json:"data"`
}
type MetricQueryResultDataDto struct {
Target string `json:"target"`
DataPoints [][2]float64 `json:"datapoints"`
Data []interface{} `json:"data"`
}
type UserStars struct {
+1 -1
View File
@@ -165,7 +165,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
}
}
if c.OrgRole == m.ROLE_ADMIN {
if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
}
+29 -19
View File
@@ -2,39 +2,49 @@ package api
import (
"encoding/json"
"math/rand"
"net/http"
"strconv"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/util"
)
func GetTestMetrics(c *middleware.Context) Response {
from := c.QueryInt64("from")
to := c.QueryInt64("to")
maxDataPoints := c.QueryInt64("maxDataPoints")
stepInSeconds := (to - from) / maxDataPoints
timeRange := tsdb.NewTimeRange(c.Query("from"), c.Query("to"))
req := &tsdb.Request{
TimeRange: timeRange,
Queries: []*tsdb.Query{
{
RefId: "A",
MaxDataPoints: c.QueryInt64("maxDataPoints"),
IntervalMs: c.QueryInt64("intervalMs"),
DataSource: &tsdb.DataSourceInfo{
Name: "Grafana TestDataDB",
PluginId: "grafana-testdata-datasource",
},
},
},
}
resp, err := tsdb.HandleRequest(req)
if err != nil {
return ApiError(500, "Metric request error", err)
}
result := dtos.MetricQueryResultDto{}
result.Data = make([]dtos.MetricQueryResultDataDto, 1)
for seriesIndex := range result.Data {
points := make([][2]float64, maxDataPoints)
walker := rand.Float64() * 100
time := from
for i := range points {
points[i][0] = walker
points[i][1] = float64(time)
walker += rand.Float64() - 0.5
time += stepInSeconds
for _, v := range resp.Results {
if v.Error != nil {
return ApiError(500, "tsdb.HandleRequest() response error", v.Error)
}
result.Data[seriesIndex].Target = "test-series-" + strconv.Itoa(seriesIndex)
result.Data[seriesIndex].DataPoints = points
for _, series := range v.Series {
result.Data = append(result.Data, series)
}
}
return Json(200, &result)
+6 -1
View File
@@ -43,7 +43,12 @@ func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
appSubPath := strings.Replace(fp.PluginDir, app.PluginDir, "", 1)
fp.IncludedInAppId = app.Id
fp.BaseUrl = app.BaseUrl
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
if isExternalPlugin(app.PluginDir) {
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
} else {
fp.Module = util.JoinUrlFragments("app/plugins/app/"+app.Id, appSubPath) + "/module"
}
}
func (fp *FrontendPluginBase) handleModuleDefaults() {
+4 -4
View File
@@ -34,8 +34,8 @@ type AlertQuery struct {
}
func (c *QueryCondition) Eval(context *alerting.EvalContext) {
timerange := tsdb.NewTimerange(c.Query.From, c.Query.To)
seriesList, err := c.executeQuery(context, timerange)
timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To)
seriesList, err := c.executeQuery(context, timeRange)
if err != nil {
context.Error = err
return
@@ -69,7 +69,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) {
context.Firing = len(context.EvalMatches) > 0
}
func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timerange tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) {
func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) {
getDsInfo := &m.GetDataSourceByIdQuery{
Id: c.Query.DatasourceId,
OrgId: context.Rule.OrgId,
@@ -79,7 +79,7 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timerange t
return nil, fmt.Errorf("Could not find datasource")
}
req := c.getRequestForAlertRule(getDsInfo.Result, timerange)
req := c.getRequestForAlertRule(getDsInfo.Result, timeRange)
result := make(tsdb.TimeSeriesSlice, 0)
resp, err := c.HandleRequest(req)
+1
View File
@@ -7,6 +7,7 @@ import (
"github.com/grafana/grafana/pkg/setting"
_ "github.com/grafana/grafana/pkg/tsdb/graphite"
_ "github.com/grafana/grafana/pkg/tsdb/prometheus"
_ "github.com/grafana/grafana/pkg/tsdb/testdata"
)
var engine *alerting.Engine
+11 -10
View File
@@ -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 {
+54
View File
@@ -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, &timestamp})
walker += rand.Float64() - 0.5
time += stepInSeconds
}
series.Points = points
queryRes.Series = append(queryRes.Series, series)
}
result.QueryResults["A"] = queryRes
return result
}
+10 -1
View File
@@ -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)
}
+17
View File
@@ -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{