metrics.go 3.19 KB
Newer Older
1 2 3
package api

import (
4
	"context"
5 6

	"github.com/grafana/grafana/pkg/api/dtos"
7
	"github.com/grafana/grafana/pkg/bus"
8
	"github.com/grafana/grafana/pkg/components/simplejson"
9
	m "github.com/grafana/grafana/pkg/models"
10
	"github.com/grafana/grafana/pkg/tsdb"
11
	"github.com/grafana/grafana/pkg/tsdb/testdata"
12
	"github.com/grafana/grafana/pkg/util"
13 14
)

15
// POST /api/tsdb/query
16
func (hs *HTTPServer) QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) Response {
17 18
	timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)

19
	if len(reqDto.Queries) == 0 {
20
		return Error(400, "No queries found in query", nil)
21 22
	}

23
	datasourceId, err := reqDto.Queries[0].Get("datasourceId").Int64()
24
	if err != nil {
25
		return Error(400, "Query missing datasourceId", nil)
26 27
	}

28
	ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
29
	if err != nil {
30
		if err == m.ErrDataSourceAccessDenied {
31
			return Error(403, "Access denied to datasource", err)
32
		}
33
		return Error(500, "Unable to load datasource meta data", err)
34 35
	}

bergquist committed
36
	request := &tsdb.TsdbQuery{TimeRange: timeRange}
37 38 39 40 41 42 43

	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,
44
			DataSource:    ds,
45
		})
46 47
	}

48
	resp, err := tsdb.HandleRequest(c.Req.Context(), ds, request)
49
	if err != nil {
50
		return Error(500, "Metric request error", err)
51
	}
52

53
	statusCode := 200
54 55 56
	for _, res := range resp.Results {
		if res.Error != nil {
			res.ErrorString = res.Error.Error()
57
			resp.Message = res.ErrorString
58
			statusCode = 400
59 60 61
		}
	}

62
	return JSON(statusCode, &resp)
63
}
64

65
// GET /api/tsdb/testdata/scenarios
66
func GetTestDataScenarios(c *m.ReqContext) Response {
67
	result := make([]interface{}, 0)
68

69 70 71 72 73
	for _, scenario := range testdata.ScenarioRegistry {
		result = append(result, map[string]interface{}{
			"id":          scenario.Id,
			"name":        scenario.Name,
			"description": scenario.Description,
74
			"stringInput": scenario.StringInput,
75
		})
76 77
	}

78
	return JSON(200, &result)
79
}
80

81
// Generates a index out of range error
82
func GenerateError(c *m.ReqContext) Response {
83
	var array []string
84
	return JSON(200, array[20])
85
}
Torkel Ödegaard committed
86 87

// GET /api/tsdb/testdata/gensql
88
func GenerateSQLTestData(c *m.ReqContext) Response {
89
	if err := bus.Dispatch(&m.InsertSqlTestDataCommand{}); err != nil {
90
		return Error(500, "Failed to insert test data", err)
Torkel Ödegaard committed
91 92
	}

93
	return JSON(200, &util.DynMap{"message": "OK"})
Torkel Ödegaard committed
94
}
95 96

// GET /api/tsdb/testdata/random-walk
97
func GetTestDataRandomWalk(c *m.ReqContext) Response {
98 99 100 101 102
	from := c.Query("from")
	to := c.Query("to")
	intervalMs := c.QueryInt64("intervalMs")

	timeRange := tsdb.NewTimeRange(from, to)
bergquist committed
103
	request := &tsdb.TsdbQuery{TimeRange: timeRange}
104

105
	dsInfo := &m.DataSource{Type: "testdata"}
106 107 108 109 110 111
	request.Queries = append(request.Queries, &tsdb.Query{
		RefId:      "A",
		IntervalMs: intervalMs,
		Model: simplejson.NewFromAny(&util.DynMap{
			"scenario": "random_walk",
		}),
112
		DataSource: dsInfo,
113 114
	})

115
	resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request)
116
	if err != nil {
117
		return Error(500, "Metric request error", err)
118 119
	}

120
	return JSON(200, &resp)
121
}