diff --git a/grafana b/grafana index dede578c7d5..cfabccc5f29 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit dede578c7d569f87c35724f74a72216743bf9508 +Subproject commit cfabccc5f29579680dcd186307c431945900c7ce diff --git a/pkg/api/api.go b/pkg/api/api.go index f83efca3558..73482f2886f 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -54,7 +54,14 @@ func Register(m *macaron.Macaron) { } func Index(ctx *middleware.Context) { - ctx.Data["User"] = dtos.NewCurrentUser(ctx.UserAccount) + settings, err := getFrontendSettings(ctx.GetAccountId()) + if err != nil { + ctx.Handle(500, "Failed to get settings", err) + return + } + + ctx.Data["user"] = dtos.NewCurrentUser(ctx.UserAccount) + ctx.Data["settings"] = settings ctx.HTML(200, "index") } diff --git a/pkg/api/api_frontendsettings.go b/pkg/api/api_frontendsettings.go new file mode 100644 index 00000000000..ea24ae16c87 --- /dev/null +++ b/pkg/api/api_frontendsettings.go @@ -0,0 +1,61 @@ +package api + +import ( + "strconv" + + "github.com/torkelo/grafana-pro/pkg/bus" + 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) + + if err != nil { + return nil, err + } + + datasources := make(map[string]interface{}) + + for i, ds := range query.Result { + 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, + } + + return jsonObj, nil +} diff --git a/views/index.html b/views/index.html index 40045c7ea58..4c5d61bd69f 100644 --- a/views/index.html +++ b/views/index.html @@ -46,7 +46,8 @@