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)