grafana/pkg/api/metrics.go

119 lines
3.1 KiB
Go
Raw Normal View History

package api
import (
"context"
2016-06-03 08:06:57 -05:00
"github.com/grafana/grafana/pkg/api/dtos"
2017-03-29 15:54:07 -05:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
2016-09-27 07:39:51 -05:00
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb/testdata"
"github.com/grafana/grafana/pkg/util"
)
// POST /api/tsdb/query
func QueryMetrics(c *m.Context, reqDto dtos.MetricRequest) Response {
timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)
2017-03-29 15:54:07 -05:00
if len(reqDto.Queries) == 0 {
return ApiError(400, "No queries found in query", nil)
}
dsId, err := reqDto.Queries[0].Get("datasourceId").Int64()
if err != nil {
return ApiError(400, "Query missing datasourceId", nil)
}
dsQuery := m.GetDataSourceByIdQuery{Id: dsId, OrgId: c.OrgId}
2017-03-29 15:54:07 -05:00
if err := bus.Dispatch(&dsQuery); err != nil {
return ApiError(500, "failed to fetch data source", err)
}
2017-09-20 11:56:33 -05:00
request := &tsdb.TsdbQuery{TimeRange: timeRange}
for _, query := range reqDto.Queries {
request.Queries = append(request.Queries, &tsdb.Query{
RefId: query.Get("refId").MustString("A"),
MaxDataPoints: query.Get("maxDataPoints").MustInt64(100),
IntervalMs: query.Get("intervalMs").MustInt64(1000),
Model: query,
2017-03-29 15:54:07 -05:00
DataSource: dsQuery.Result,
})
2016-09-27 07:39:51 -05:00
}
resp, err := tsdb.HandleRequest(context.Background(), dsQuery.Result, request)
2016-09-27 07:39:51 -05:00
if err != nil {
return ApiError(500, "Metric request error", err)
}
2017-04-20 10:10:09 -05:00
statusCode := 200
for _, res := range resp.Results {
if res.Error != nil {
res.ErrorString = res.Error.Error()
2017-04-20 10:10:09 -05:00
resp.Message = res.ErrorString
statusCode = 500
}
}
2017-04-20 10:10:09 -05:00
return Json(statusCode, &resp)
}
2016-09-27 07:39:51 -05:00
// GET /api/tsdb/testdata/scenarios
func GetTestDataScenarios(c *m.Context) Response {
result := make([]interface{}, 0)
for _, scenario := range testdata.ScenarioRegistry {
result = append(result, map[string]interface{}{
"id": scenario.Id,
"name": scenario.Name,
"description": scenario.Description,
"stringInput": scenario.StringInput,
})
}
return Json(200, &result)
}
2016-06-03 08:06:57 -05:00
// Genereates a index out of range error
func GenerateError(c *m.Context) Response {
var array []string
return Json(200, array[20])
}
2017-03-30 13:23:40 -05:00
// GET /api/tsdb/testdata/gensql
func GenerateSqlTestData(c *m.Context) Response {
if err := bus.Dispatch(&m.InsertSqlTestDataCommand{}); err != nil {
2017-03-30 13:23:40 -05:00
return ApiError(500, "Failed to insert test data", err)
}
return Json(200, &util.DynMap{"message": "OK"})
}
// GET /api/tsdb/testdata/random-walk
func GetTestDataRandomWalk(c *m.Context) Response {
from := c.Query("from")
to := c.Query("to")
intervalMs := c.QueryInt64("intervalMs")
timeRange := tsdb.NewTimeRange(from, to)
2017-09-20 11:56:33 -05:00
request := &tsdb.TsdbQuery{TimeRange: timeRange}
dsInfo := &m.DataSource{Type: "grafana-testdata-datasource"}
request.Queries = append(request.Queries, &tsdb.Query{
RefId: "A",
IntervalMs: intervalMs,
Model: simplejson.NewFromAny(&util.DynMap{
"scenario": "random_walk",
}),
DataSource: dsInfo,
})
resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request)
if err != nil {
return ApiError(500, "Metric request error", err)
}
return Json(200, &resp)
}