metrics.go 3.2 KB

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