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