Procházet zdrojové kódy

Datasource options are now included in bootData

Torkel Ödegaard před 11 roky
rodič
revize
ec98c201e4
5 změnil soubory, kde provedl 15 přidání a 112 odebrání
  1. 1 1
      grafana
  2. 1 4
      pkg/api/api.go
  3. 0 99
      pkg/api/api_config.go
  4. 13 6
      pkg/api/api_frontendsettings.go
  5. 0 2
      pkg/middleware/middleware.go

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit cfabccc5f29579680dcd186307c431945900c7ce
+Subproject commit 47f226be3b480e037692f30a320c6fcff2b9e01c

+ 1 - 4
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

+ 0 - 99
pkg/api/api_config.go

@@ -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))
-}

+ 13 - 6
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 {

+ 0 - 2
pkg/middleware/middleware.go

@@ -18,8 +18,6 @@ type Context struct {
 
 	Account     *models.Account
 	UserAccount *models.Account
-
-	IsSigned bool
 }
 
 func (c *Context) GetAccountId() int64 {