| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-10-03 15:38:03 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-06-03 21:06:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb" | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb/testdata" | 
					
						
							| 
									
										
										
										
											2016-06-04 03:22:34 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | // POST /api/tsdb/query
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) Response { | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 	timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	if len(reqDto.Queries) == 0 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(400, "No queries found in query", nil) | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dsID, err := reqDto.Queries[0].Get("datasourceId").Int64() | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(400, "Query missing datasourceId", nil) | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dsQuery := m.GetDataSourceByIdQuery{Id: dsID, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	if err := bus.Dispatch(&dsQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "failed to fetch data source", err) | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 00:56:33 +08:00
										 |  |  | 	request := &tsdb.TsdbQuery{TimeRange: timeRange} | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08: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, | 
					
						
							| 
									
										
										
										
											2017-03-30 04:54:07 +08:00
										 |  |  | 			DataSource:    dsQuery.Result, | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 00:04:06 +08:00
										 |  |  | 	resp, err := tsdb.HandleRequest(context.Background(), dsQuery.Result, request) | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Metric request error", err) | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-20 23:10:09 +08:00
										 |  |  | 	statusCode := 200 | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 	for _, res := range resp.Results { | 
					
						
							|  |  |  | 		if res.Error != nil { | 
					
						
							|  |  |  | 			res.ErrorString = res.Error.Error() | 
					
						
							| 
									
										
										
										
											2017-04-20 23:10:09 +08:00
										 |  |  | 			resp.Message = res.ErrorString | 
					
						
							|  |  |  | 			statusCode = 500 | 
					
						
							| 
									
										
										
										
											2017-03-31 17:45:25 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(statusCode, &resp) | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | // GET /api/tsdb/testdata/scenarios
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetTestDataScenarios(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 	result := make([]interface{}, 0) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 	for _, scenario := range testdata.ScenarioRegistry { | 
					
						
							|  |  |  | 		result = append(result, map[string]interface{}{ | 
					
						
							|  |  |  | 			"id":          scenario.Id, | 
					
						
							|  |  |  | 			"name":        scenario.Name, | 
					
						
							|  |  |  | 			"description": scenario.Description, | 
					
						
							| 
									
										
										
										
											2016-09-28 16:37:30 +08:00
										 |  |  | 			"stringInput": scenario.StringInput, | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, &result) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-03 21:06:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 00:40:14 +08:00
										 |  |  | // Generates a index out of range error
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GenerateError(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2016-06-07 16:05:10 +08:00
										 |  |  | 	var array []string | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, array[20]) | 
					
						
							| 
									
										
										
										
											2016-06-07 16:05:10 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-31 02:23:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GET /api/tsdb/testdata/gensql
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | func GenerateSQLTestData(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	if err := bus.Dispatch(&m.InsertSqlTestDataCommand{}); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to insert test data", err) | 
					
						
							| 
									
										
										
										
											2017-03-31 02:23:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, &util.DynMap{"message": "OK"}) | 
					
						
							| 
									
										
										
										
											2017-03-31 02:23:40 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GET /api/tsdb/testdata/random-walk
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetTestDataRandomWalk(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	from := c.Query("from") | 
					
						
							|  |  |  | 	to := c.Query("to") | 
					
						
							|  |  |  | 	intervalMs := c.QueryInt64("intervalMs") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	timeRange := tsdb.NewTimeRange(from, to) | 
					
						
							| 
									
										
										
										
											2017-09-21 00:56:33 +08:00
										 |  |  | 	request := &tsdb.TsdbQuery{TimeRange: timeRange} | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	dsInfo := &m.DataSource{Type: "grafana-testdata-datasource"} | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	request.Queries = append(request.Queries, &tsdb.Query{ | 
					
						
							|  |  |  | 		RefId:      "A", | 
					
						
							|  |  |  | 		IntervalMs: intervalMs, | 
					
						
							|  |  |  | 		Model: simplejson.NewFromAny(&util.DynMap{ | 
					
						
							|  |  |  | 			"scenario": "random_walk", | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2017-09-22 00:04:06 +08:00
										 |  |  | 		DataSource: dsInfo, | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 00:04:06 +08:00
										 |  |  | 	resp, err := tsdb.HandleRequest(context.Background(), dsInfo, request) | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Metric request error", err) | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, &resp) | 
					
						
							| 
									
										
										
										
											2017-05-29 16:48:38 +08:00
										 |  |  | } |