metrics.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package api
  2. import (
  3. "context"
  4. "sort"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/tsdb"
  10. "github.com/grafana/grafana/pkg/tsdb/testdata"
  11. "github.com/grafana/grafana/pkg/util"
  12. )
  13. // POST /api/tsdb/query
  14. func (hs *HTTPServer) QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) Response {
  15. timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)
  16. if len(reqDto.Queries) == 0 {
  17. return Error(400, "No queries found in query", nil)
  18. }
  19. datasourceId, err := reqDto.Queries[0].Get("datasourceId").Int64()
  20. if err != nil {
  21. return Error(400, "Query missing datasourceId", nil)
  22. }
  23. ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
  24. if err != nil {
  25. if err == m.ErrDataSourceAccessDenied {
  26. return Error(403, "Access denied to datasource", err)
  27. }
  28. return Error(500, "Unable to load datasource meta data", err)
  29. }
  30. request := &tsdb.TsdbQuery{TimeRange: timeRange, Debug: reqDto.Debug}
  31. for _, query := range reqDto.Queries {
  32. request.Queries = append(request.Queries, &tsdb.Query{
  33. RefId: query.Get("refId").MustString("A"),
  34. MaxDataPoints: query.Get("maxDataPoints").MustInt64(100),
  35. IntervalMs: query.Get("intervalMs").MustInt64(1000),
  36. Model: query,
  37. DataSource: ds,
  38. })
  39. }
  40. resp, err := tsdb.HandleRequest(c.Req.Context(), ds, request)
  41. if err != nil {
  42. return Error(500, "Metric request error", err)
  43. }
  44. statusCode := 200
  45. for _, res := range resp.Results {
  46. if res.Error != nil {
  47. res.ErrorString = res.Error.Error()
  48. resp.Message = res.ErrorString
  49. statusCode = 400
  50. }
  51. }
  52. return JSON(statusCode, &resp)
  53. }
  54. // GET /api/tsdb/testdata/scenarios
  55. func GetTestDataScenarios(c *m.ReqContext) Response {
  56. result := make([]interface{}, 0)
  57. scenarioIds := make([]string, 0)
  58. for id := range testdata.ScenarioRegistry {
  59. scenarioIds = append(scenarioIds, id)
  60. }
  61. sort.Strings(scenarioIds)
  62. for _, scenarioId := range scenarioIds {
  63. scenario := testdata.ScenarioRegistry[scenarioId]
  64. result = append(result, map[string]interface{}{
  65. "id": scenario.Id,
  66. "name": scenario.Name,
  67. "description": scenario.Description,
  68. "stringInput": scenario.StringInput,
  69. })
  70. }
  71. return JSON(200, &result)
  72. }
  73. // Generates a index out of range error
  74. func GenerateError(c *m.ReqContext) Response {
  75. var array []string
  76. // nolint: govet
  77. return JSON(200, array[20])
  78. }
  79. // GET /api/tsdb/testdata/gensql
  80. func GenerateSQLTestData(c *m.ReqContext) Response {
  81. if err := bus.Dispatch(&m.InsertSqlTestDataCommand{}); err != nil {
  82. return Error(500, "Failed to insert test data", err)
  83. }
  84. return JSON(200, &util.DynMap{"message": "OK"})
  85. }
  86. // GET /api/tsdb/testdata/random-walk
  87. func GetTestDataRandomWalk(c *m.ReqContext) Response {
  88. from := c.Query("from")
  89. to := c.Query("to")
  90. intervalMs := c.QueryInt64("intervalMs")
  91. timeRange := tsdb.NewTimeRange(from, to)
  92. request := &tsdb.TsdbQuery{TimeRange: timeRange}
  93. dsInfo := &m.DataSource{Type: "testdata"}
  94. request.Queries = append(request.Queries, &tsdb.Query{
  95. RefId: "A",
  96. IntervalMs: intervalMs,
  97. Model: simplejson.NewFromAny(&util.DynMap{
  98. "scenario": "random_walk",
  99. }),
  100. DataSource: dsInfo,
  101. })
  102. resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request)
  103. if err != nil {
  104. return Error(500, "Metric request error", err)
  105. }
  106. return JSON(200, &resp)
  107. }