Commit ec98c201 by Torkel Ödegaard

Datasource options are now included in bootData

parent f3132b45
Subproject commit cfabccc5f29579680dcd186307c431945900c7ce
Subproject commit 47f226be3b480e037692f30a320c6fcff2b9e01c
......@@ -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
......
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))
}
......@@ -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 {
......
......@@ -18,8 +18,6 @@ type Context struct {
Account *models.Account
UserAccount *models.Account
IsSigned bool
}
func (c *Context) GetAccountId() int64 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment