| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 23:14:15 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/pluginproxy" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2016-06-03 15:17:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/metrics" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-08-23 16:52:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2017-01-16 19:16:41 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | const HeaderNameNoBackendCache = "X-Grafana-NoCache" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | func (hs *HTTPServer) getDatasourceByID(id int64, orgID int64, nocache bool) (*m.DataSource, error) { | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | 	cacheKey := fmt.Sprintf("ds-%d", id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !nocache { | 
					
						
							|  |  |  | 		if cached, found := hs.cache.Get(cacheKey); found { | 
					
						
							|  |  |  | 			ds := cached.(*m.DataSource) | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 			if ds.OrgId == orgID { | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | 				return ds, nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	query := m.GetDataSourceByIdQuery{Id: id, OrgId: orgID} | 
					
						
							| 
									
										
										
										
											2015-03-12 00:34:11 +08:00
										 |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2015-10-08 23:30:13 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | 	hs.cache.Set(cacheKey, query.Result, time.Second*5) | 
					
						
							| 
									
										
										
										
											2016-06-06 23:11:46 +08:00
										 |  |  | 	return query.Result, nil | 
					
						
							| 
									
										
										
										
											2015-10-08 23:30:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) { | 
					
						
							| 
									
										
										
										
											2016-06-03 15:17:36 +08:00
										 |  |  | 	c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 19:31:26 +08:00
										 |  |  | 	nocache := c.Req.Header.Get(HeaderNameNoBackendCache) == "true" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	ds, err := hs.getDatasourceByID(c.ParamsInt64(":id"), c.OrgId, nocache) | 
					
						
							| 
									
										
										
										
											2016-06-03 15:17:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 23:30:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		c.JsonApiErr(500, "Unable to load datasource meta data", err) | 
					
						
							| 
									
										
										
										
											2015-03-12 00:34:11 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 16:52:31 +08:00
										 |  |  | 	// find plugin
 | 
					
						
							|  |  |  | 	plugin, ok := plugins.DataSources[ds.Type] | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Unable to find datasource plugin", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-22 19:15:18 +08:00
										 |  |  | 	proxyPath := c.Params("*") | 
					
						
							| 
									
										
										
										
											2017-08-23 16:52:31 +08:00
										 |  |  | 	proxy := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath) | 
					
						
							| 
									
										
										
										
											2017-08-22 23:14:15 +08:00
										 |  |  | 	proxy.HandleRequest() | 
					
						
							| 
									
										
										
										
											2016-08-17 13:33:59 +08:00
										 |  |  | } |