2015-01-06 02:11:00 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2020-07-24 05:34:56 -05:00
|
|
|
"errors"
|
2019-05-21 02:20:57 -05:00
|
|
|
"sort"
|
2016-06-03 08:06:57 -05:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-10-30 13:38:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2019-10-25 08:28:26 -05:00
|
|
|
|
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"
|
2016-09-27 07:39:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2019-09-27 09:18:24 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/testdatasource"
|
2016-06-03 14:22:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-01-06 02:11:00 -06:00
|
|
|
)
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// QueryMetricsV2 returns query metrics
|
2019-10-31 18:22:00 -05:00
|
|
|
// POST /api/ds/query DataSource query w/ expressions
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricRequest) Response {
|
2019-10-25 08:28:26 -05:00
|
|
|
if len(reqDto.Queries) == 0 {
|
2020-07-24 05:34:56 -05:00
|
|
|
return Error(400, "No queries found in query", nil)
|
2019-10-30 13:38:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
request := &tsdb.TsdbQuery{
|
|
|
|
TimeRange: tsdb.NewTimeRange(reqDto.From, reqDto.To),
|
|
|
|
Debug: reqDto.Debug,
|
2020-03-06 07:37:36 -06:00
|
|
|
User: c.SignedInUser,
|
2019-10-25 08:28:26 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
expr := false
|
2020-03-04 05:57:20 -06:00
|
|
|
var ds *models.DataSource
|
2019-10-30 13:38:28 -05:00
|
|
|
for i, query := range reqDto.Queries {
|
2020-07-24 05:34:56 -05:00
|
|
|
hs.log.Debug("Processing metrics query", "query", query)
|
2020-05-14 22:05:16 -05:00
|
|
|
name := query.Get("datasource").MustString("")
|
2019-10-30 13:38:28 -05:00
|
|
|
if name == "__expr__" {
|
|
|
|
expr = true
|
2019-10-25 08:28:26 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
datasourceID, err := query.Get("datasourceId").Int64()
|
|
|
|
if err != nil {
|
2020-07-24 05:34:56 -05:00
|
|
|
hs.log.Debug("Can't process query since it's missing data source ID")
|
|
|
|
return Error(400, "Query missing data source ID", nil)
|
2019-10-30 13:38:28 -05:00
|
|
|
}
|
2019-10-25 08:28:26 -05:00
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
if i == 0 && !expr {
|
|
|
|
ds, err = hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
|
|
|
if err != nil {
|
2020-07-24 05:34:56 -05:00
|
|
|
hs.log.Debug("Encountered error getting data source", "err", err)
|
|
|
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
|
|
|
return Error(403, "Access denied to data source", err)
|
2019-10-30 13:38:28 -05:00
|
|
|
}
|
2020-07-24 05:34:56 -05:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
|
|
|
return Error(400, "Invalid data source ID", err)
|
|
|
|
}
|
|
|
|
return Error(500, "Unable to load data source metadata", err)
|
2019-10-30 13:38:28 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 05:57:20 -06:00
|
|
|
|
2019-10-25 08:28:26 -05:00
|
|
|
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),
|
2020-05-05 03:32:34 -05:00
|
|
|
QueryType: query.Get("queryType").MustString(""),
|
2019-10-25 08:28:26 -05:00
|
|
|
Model: query,
|
|
|
|
DataSource: ds,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
var resp *tsdb.Response
|
|
|
|
var err error
|
|
|
|
if !expr {
|
|
|
|
resp, err = tsdb.HandleRequest(c.Req.Context(), ds, request)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Metric request error", err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-27 01:36:36 -05:00
|
|
|
if !hs.Cfg.IsExpressionsEnabled() {
|
2020-03-13 09:43:25 -05:00
|
|
|
return Error(404, "Expressions feature toggle is not enabled", nil)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
resp, err = plugins.Transform.Transform(c.Req.Context(), request)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Transform request error", err)
|
|
|
|
}
|
2019-10-25 08:28:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
statusCode := 200
|
|
|
|
for _, res := range resp.Results {
|
|
|
|
if res.Error != nil {
|
|
|
|
res.ErrorString = res.Error.Error()
|
|
|
|
resp.Message = res.ErrorString
|
|
|
|
statusCode = 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON(statusCode, &resp)
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// QueryMetrics returns query metrics
|
2016-09-27 11:17:39 -05:00
|
|
|
// POST /api/tsdb/query
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) QueryMetrics(c *models.ReqContext, 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 {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "No queries found in query", nil)
|
2017-03-29 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:30:32 -05:00
|
|
|
datasourceId, err := reqDto.Queries[0].Get("datasourceId").Int64()
|
2017-03-29 15:54:07 -05:00
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Query missing datasourceId", nil)
|
2017-03-29 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2018-10-26 03:40:33 -05:00
|
|
|
ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
|
2018-09-14 02:30:32 -05:00
|
|
|
if err != nil {
|
2020-03-04 05:57:20 -06:00
|
|
|
if err == models.ErrDataSourceAccessDenied {
|
2018-10-26 11:34:10 -05:00
|
|
|
return Error(403, "Access denied to datasource", err)
|
2018-10-26 03:40:33 -05:00
|
|
|
}
|
2018-09-14 02:30:32 -05:00
|
|
|
return Error(500, "Unable to load datasource meta data", err)
|
2017-03-29 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
request := &tsdb.TsdbQuery{
|
|
|
|
TimeRange: timeRange,
|
|
|
|
Debug: reqDto.Debug,
|
|
|
|
User: c.SignedInUser,
|
|
|
|
}
|
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,
|
2018-09-14 02:30:32 -05:00
|
|
|
DataSource: ds,
|
2016-09-27 11:17:39 -05:00
|
|
|
})
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:30:32 -05:00
|
|
|
resp, err := tsdb.HandleRequest(c.Req.Context(), ds, request)
|
2016-09-27 07:39:51 -05:00
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Metric request error", err)
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
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
|
2018-07-24 02:58:48 -05:00
|
|
|
statusCode = 400
|
2017-03-31 04:45:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -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
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetTestDataScenarios(c *models.ReqContext) Response {
|
2016-09-27 11:17:39 -05:00
|
|
|
result := make([]interface{}, 0)
|
2015-01-06 02:11:00 -06:00
|
|
|
|
2019-05-21 02:20:57 -05:00
|
|
|
scenarioIds := make([]string, 0)
|
2019-09-27 09:18:24 -05:00
|
|
|
for id := range testdatasource.ScenarioRegistry {
|
2019-05-21 02:20:57 -05:00
|
|
|
scenarioIds = append(scenarioIds, id)
|
|
|
|
}
|
|
|
|
sort.Strings(scenarioIds)
|
|
|
|
|
|
|
|
for _, scenarioId := range scenarioIds {
|
2019-09-27 09:18:24 -05:00
|
|
|
scenario := testdatasource.ScenarioRegistry[scenarioId]
|
2016-09-27 11:17:39 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &result)
|
2015-01-06 02:11:00 -06:00
|
|
|
}
|
2016-06-03 08:06:57 -05:00
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// GenerateError generates a index out of range error
|
2020-03-04 05:57:20 -06:00
|
|
|
func GenerateError(c *models.ReqContext) Response {
|
2016-06-07 03:05:10 -05:00
|
|
|
var array []string
|
2019-06-04 15:00:05 -05:00
|
|
|
// nolint: govet
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, array[20])
|
2016-06-07 03:05:10 -05:00
|
|
|
}
|
2017-03-30 13:23:40 -05:00
|
|
|
|
|
|
|
// GET /api/tsdb/testdata/gensql
|
2020-03-04 05:57:20 -06:00
|
|
|
func GenerateSQLTestData(c *models.ReqContext) Response {
|
|
|
|
if err := bus.Dispatch(&models.InsertSqlTestDataCommand{}); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to insert test data", err)
|
2017-03-30 13:23:40 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &util.DynMap{"message": "OK"})
|
2017-03-30 13:23:40 -05:00
|
|
|
}
|
2017-05-29 03:48:38 -05:00
|
|
|
|
|
|
|
// GET /api/tsdb/testdata/random-walk
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetTestDataRandomWalk(c *models.ReqContext) 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
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
dsInfo := &models.DataSource{Type: "testdata"}
|
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 {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Metric request error", err)
|
2017-05-29 03:48:38 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &resp)
|
2017-05-29 03:48:38 -05:00
|
|
|
}
|