Browse Source

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

Torkel Ödegaard 11 years ago
parent
commit
3e07605260
4 changed files with 49 additions and 20 deletions
  1. 1 1
      grafana
  2. 3 0
      pkg/api/api.go
  3. 9 19
      pkg/api/dtos/models.go
  4. 36 0
      pkg/api/metrics.go

+ 1 - 1
grafana

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

+ 3 - 0
pkg/api/api.go

@@ -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) {

+ 9 - 19
pkg/api/dtos/models.go

@@ -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 - 0
pkg/api/metrics.go

@@ -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)
+}