metrics.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) Response {
  14. timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)
  15. if len(reqDto.Queries) == 0 {
  16. return ApiError(400, "No queries found in query", nil)
  17. }
  18. dsID, err := reqDto.Queries[0].Get("datasourceId").Int64()
  19. if err != nil {
  20. return ApiError(400, "Query missing datasourceId", nil)
  21. }
  22. dsQuery := m.GetDataSourceByIdQuery{Id: dsID, OrgId: c.OrgId}
  23. if err := bus.Dispatch(&dsQuery); err != nil {
  24. return ApiError(500, "failed to fetch data source", err)
  25. }
  26. request := &tsdb.TsdbQuery{TimeRange: timeRange}
  27. for _, query := range reqDto.Queries {
  28. request.Queries = append(request.Queries, &tsdb.Query{
  29. RefId: query.Get("refId").MustString("A"),
  30. MaxDataPoints: query.Get("maxDataPoints").MustInt64(100),
  31. IntervalMs: query.Get("intervalMs").MustInt64(1000),
  32. Model: query,
  33. DataSource: dsQuery.Result,
  34. })
  35. }
  36. resp, err := tsdb.HandleRequest(context.Background(), dsQuery.Result, request)
  37. if err != nil {
  38. return ApiError(500, "Metric request error", err)
  39. }
  40. statusCode := 200
  41. for _, res := range resp.Results {
  42. if res.Error != nil {
  43. res.ErrorString = res.Error.Error()
  44. resp.Message = res.ErrorString
  45. statusCode = 500
  46. }
  47. }
  48. return Json(statusCode, &resp)
  49. }
  50. // GET /api/tsdb/testdata/scenarios
  51. func GetTestDataScenarios(c *m.ReqContext) Response {
  52. result := make([]interface{}, 0)
  53. for _, scenario := range testdata.ScenarioRegistry {
  54. result = append(result, map[string]interface{}{
  55. "id": scenario.Id,
  56. "name": scenario.Name,
  57. "description": scenario.Description,
  58. "stringInput": scenario.StringInput,
  59. })
  60. }
  61. return Json(200, &result)
  62. }
  63. // Genereates a index out of range error
  64. func GenerateError(c *m.ReqContext) Response {
  65. var array []string
  66. return Json(200, array[20])
  67. }
  68. // GET /api/tsdb/testdata/gensql
  69. func GenerateSQLTestData(c *m.ReqContext) Response {
  70. if err := bus.Dispatch(&m.InsertSqlTestDataCommand{}); err != nil {
  71. return ApiError(500, "Failed to insert test data", err)
  72. }
  73. return Json(200, &util.DynMap{"message": "OK"})
  74. }
  75. // GET /api/tsdb/testdata/random-walk
  76. func GetTestDataRandomWalk(c *m.ReqContext) Response {
  77. from := c.Query("from")
  78. to := c.Query("to")
  79. intervalMs := c.QueryInt64("intervalMs")
  80. timeRange := tsdb.NewTimeRange(from, to)
  81. request := &tsdb.TsdbQuery{TimeRange: timeRange}
  82. dsInfo := &m.DataSource{Type: "grafana-testdata-datasource"}
  83. request.Queries = append(request.Queries, &tsdb.Query{
  84. RefId: "A",
  85. IntervalMs: intervalMs,
  86. Model: simplejson.NewFromAny(&util.DynMap{
  87. "scenario": "random_walk",
  88. }),
  89. DataSource: dsInfo,
  90. })
  91. resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request)
  92. if err != nil {
  93. return ApiError(500, "Metric request error", err)
  94. }
  95. return Json(200, &resp)
  96. }