2015-01-06 02:11:00 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"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"
|
2017-05-29 03:48:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2018-03-06 16:59:45 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-09-27 07:39:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2016-09-27 11:17:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/testdata"
|
2016-06-03 14:22:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-01-06 02:11:00 -06:00
|
|
|
)
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
// POST /api/tsdb/query
|
2018-03-06 16:59:45 -06:00
|
|
|
func QueryMetrics(c *m.Context, reqDto dtos.MetricRequest) Response {
|
2016-09-27 11:17:39 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
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}
|
2016-09-27 11:17:39 -05:00
|
|
|
|
|
|
|
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 11:17:39 -05:00
|
|
|
})
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -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)
|
|
|
|
}
|
2015-01-06 02:11:00 -06:00
|
|
|
|
2017-04-20 10:10:09 -05:00
|
|
|
statusCode := 200
|
2017-03-31 04:45:25 -05:00
|
|
|
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-03-31 04:45:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 10:10:09 -05:00
|
|
|
return Json(statusCode, &resp)
|
2016-09-27 11:17:39 -05:00
|
|
|
}
|
2016-09-27 07:39:51 -05:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
// GET /api/tsdb/testdata/scenarios
|
2018-03-06 16:59:45 -06:00
|
|
|
func GetTestDataScenarios(c *m.Context) Response {
|
2016-09-27 11:17:39 -05:00
|
|
|
result := make([]interface{}, 0)
|
2015-01-06 02:11:00 -06:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
for _, scenario := range testdata.ScenarioRegistry {
|
|
|
|
result = append(result, map[string]interface{}{
|
|
|
|
"id": scenario.Id,
|
|
|
|
"name": scenario.Name,
|
|
|
|
"description": scenario.Description,
|
2016-09-28 03:37:30 -05:00
|
|
|
"stringInput": scenario.StringInput,
|
2016-09-27 11:17:39 -05:00
|
|
|
})
|
2015-01-06 02:11:00 -06:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:45:13 -05:00
|
|
|
return Json(200, &result)
|
2015-01-06 02:11:00 -06:00
|
|
|
}
|
2016-06-03 08:06:57 -05:00
|
|
|
|
2016-06-07 03:05:10 -05:00
|
|
|
// Genereates a index out of range error
|
2018-03-06 16:59:45 -06:00
|
|
|
func GenerateError(c *m.Context) Response {
|
2016-06-07 03:05:10 -05:00
|
|
|
var array []string
|
|
|
|
return Json(200, array[20])
|
|
|
|
}
|
2017-03-30 13:23:40 -05:00
|
|
|
|
|
|
|
// GET /api/tsdb/testdata/gensql
|
2018-03-06 16:59:45 -06:00
|
|
|
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"})
|
|
|
|
}
|
2017-05-29 03:48:38 -05:00
|
|
|
|
|
|
|
// GET /api/tsdb/testdata/random-walk
|
2018-03-06 16:59:45 -06:00
|
|
|
func GetTestDataRandomWalk(c *m.Context) Response {
|
2017-05-29 03:48:38 -05:00
|
|
|
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}
|
2017-05-29 03:48:38 -05:00
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
dsInfo := &m.DataSource{Type: "grafana-testdata-datasource"}
|
2017-05-29 03:48:38 -05:00
|
|
|
request.Queries = append(request.Queries, &tsdb.Query{
|
|
|
|
RefId: "A",
|
|
|
|
IntervalMs: intervalMs,
|
|
|
|
Model: simplejson.NewFromAny(&util.DynMap{
|
|
|
|
"scenario": "random_walk",
|
|
|
|
}),
|
2017-09-21 11:04:06 -05:00
|
|
|
DataSource: dsInfo,
|
2017-05-29 03:48:38 -05:00
|
|
|
})
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request)
|
2017-05-29 03:48:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return ApiError(500, "Metric request error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, &resp)
|
|
|
|
}
|