added a /api/metrics/test query that returns random walk series

This commit is contained in:
Torkel Ödegaard 2015-01-06 09:11:00 +01:00
parent 0e3f91508e
commit 3e07605260
4 changed files with 49 additions and 20 deletions

@ -1 +1 @@
Subproject commit 34427f34e89fe3913523b5b236a149b88931b71d
Subproject commit 49b18e17d7574540cdbf8d208c968ea4db202954

View File

@ -49,6 +49,9 @@ func Register(m *macaron.Macaron) {
// rendering
m.Get("/render/*", auth, RenderToPng)
// metrics
m.Get("/api/metrics/test", auth, GetTestMetrics)
}
func Index(ctx *middleware.Context) {

View File

@ -19,25 +19,6 @@ type CurrentUser struct {
GravatarUrl string `json:"gravatarUrl"`
}
type AccountInfo struct {
Email string `json:"email"`
Name string `json:"name"`
Collaborators []*Collaborator `json:"collaborators"`
}
type OtherAccount struct {
Id int64 `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
IsUsing bool `json:"isUsing"`
}
type Collaborator struct {
AccountId int64 `json:"accountId"`
Email string `json:"email"`
Role string `json:"role"`
}
type DataSource struct {
Id int64 `json:"id"`
AccountId int64 `json:"accountId"`
@ -51,6 +32,15 @@ type DataSource struct {
BasicAuth bool `json:"basicAuth"`
}
type MetricQueryResultDto struct {
Data []MetricQueryResultDataDto `json:"data"`
}
type MetricQueryResultDataDto struct {
Target string `json:"target"`
DataPoints [][2]float64 `json:"datapoints"`
}
func NewCurrentUser(account *models.Account) *CurrentUser {
model := &CurrentUser{}
if account != nil {

36
pkg/api/metrics.go Normal file
View File

@ -0,0 +1,36 @@
package api
import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware"
"math/rand"
"strconv"
)
func GetTestMetrics(c *middleware.Context) {
from := c.QueryInt64("from")
to := c.QueryInt64("to")
maxDataPoints := c.QueryInt64("maxDataPoints")
stepInSeconds := (to - from) / maxDataPoints
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
}
result.Data[seriesIndex].Target = "test-series-" + strconv.Itoa(seriesIndex)
result.Data[seriesIndex].DataPoints = points
}
c.JSON(200, &result)
}