From ec98c201e4ea518a06d92549f79ac898e6320833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 28 Dec 2014 19:30:14 +0100 Subject: [PATCH] Datasource options are now included in bootData --- grafana | 2 +- pkg/api/api.go | 5 +- pkg/api/api_config.go | 99 --------------------------------- pkg/api/api_frontendsettings.go | 19 +++++-- pkg/middleware/middleware.go | 2 - 5 files changed, 15 insertions(+), 112 deletions(-) delete mode 100644 pkg/api/api_config.go diff --git a/grafana b/grafana index cfabccc5f29..47f226be3b4 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit cfabccc5f29579680dcd186307c431945900c7ce +Subproject commit 47f226be3b480e037692f30a320c6fcff2b9e01c diff --git a/pkg/api/api.go b/pkg/api/api.go index 73482f2886f..42a04796574 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -46,15 +46,12 @@ func Register(m *macaron.Macaron) { m.Post("/api/dashboard/", auth, PostDashboard) m.Delete("/api/dashboard/:slug", auth, DeleteDashboard) - // frontend config - m.Get("/frontend/config", auth, GetConfigJS) - // rendering m.Get("/render/*", auth, RenderToPng) } func Index(ctx *middleware.Context) { - settings, err := getFrontendSettings(ctx.GetAccountId()) + settings, err := getFrontendSettings(ctx) if err != nil { ctx.Handle(500, "Failed to get settings", err) return diff --git a/pkg/api/api_config.go b/pkg/api/api_config.go deleted file mode 100644 index 0487c09a7ff..00000000000 --- a/pkg/api/api_config.go +++ /dev/null @@ -1,99 +0,0 @@ -package api - -import ( - "encoding/json" - "strconv" - "strings" - - "github.com/torkelo/grafana-pro/pkg/bus" - "github.com/torkelo/grafana-pro/pkg/middleware" - m "github.com/torkelo/grafana-pro/pkg/models" -) - -const configTemplate = ` - define(['settings'], - function (Settings) { - "use strict"; - return new Settings(%json%); - }); - ` - -type configJsTmplModel struct { - DataSources []*m.DataSource -} - -// TODO: cleanup this ugly code -func renderConfig(data *configJsTmplModel) string { - datasources := make(map[string]interface{}) - - for i, ds := range data.DataSources { - url := ds.Url - - if ds.Access == m.DS_ACCESS_PROXY { - url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10) - } - - var dsMap = map[string]interface{}{ - "type": ds.Type, - "url": url, - } - - if ds.Type == m.DS_INFLUXDB { - if ds.Access == m.DS_ACCESS_DIRECT { - dsMap["username"] = ds.User - dsMap["password"] = ds.Password - dsMap["url"] = url + "/db/" + ds.Database - } - } - - // temp hack, first is always default - // TODO: implement default ds account setting - if i == 0 { - dsMap["default"] = true - } - - datasources[ds.Name] = dsMap - } - - // add grafana backend data source - datasources["grafana"] = map[string]interface{}{ - "type": "grafana", - "url": "", - "grafanaDB": true, - } - - jsonObj := map[string]interface{}{ - "datasources": datasources, - } - - buff, _ := json.Marshal(jsonObj) - - return strings.Replace(configTemplate, "%json%", string(buff), 1) -} - -func GetConfigJS(c *middleware.Context) { - - query := m.GetDataSourcesQuery{AccountId: c.GetAccountId()} - err := bus.Dispatch(&query) - - if err != nil { - c.Handle(500, "cold not load data sources", err) - return - } - - vm := configJsTmplModel{DataSources: query.Result} - configStr := renderConfig(&vm) - - if err != nil { - c.Handle(500, "Failed to generate config.js", err) - return - } - - c.Header().Set("Content-Type", "text/javascript; charset=UTF-8") - c.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - c.Header().Set("Pragma", "no-cache") - c.Header().Set("Expires", "0") - c.WriteHeader(200) - - c.Write([]byte(configStr)) -} diff --git a/pkg/api/api_frontendsettings.go b/pkg/api/api_frontendsettings.go index ea24ae16c87..b4854039685 100644 --- a/pkg/api/api_frontendsettings.go +++ b/pkg/api/api_frontendsettings.go @@ -4,20 +4,27 @@ import ( "strconv" "github.com/torkelo/grafana-pro/pkg/bus" + "github.com/torkelo/grafana-pro/pkg/middleware" m "github.com/torkelo/grafana-pro/pkg/models" ) -func getFrontendSettings(accountId int64) (map[string]interface{}, error) { - query := m.GetDataSourcesQuery{AccountId: accountId} - err := bus.Dispatch(&query) +func getFrontendSettings(c *middleware.Context) (map[string]interface{}, error) { + accountDataSources := make([]*m.DataSource, 0) - if err != nil { - return nil, err + if c.Account != nil { + query := m.GetDataSourcesQuery{AccountId: c.Account.Id} + err := bus.Dispatch(&query) + + if err != nil { + return nil, err + } + + accountDataSources = query.Result } datasources := make(map[string]interface{}) - for i, ds := range query.Result { + for i, ds := range accountDataSources { url := ds.Url if ds.Access == m.DS_ACCESS_PROXY { diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index d2db4bc7722..e18ae675604 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -18,8 +18,6 @@ type Context struct { Account *models.Account UserAccount *models.Account - - IsSigned bool } func (c *Context) GetAccountId() int64 {