api_frontendsettings.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package api
  2. import (
  3. "strconv"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. )
  7. func getFrontendSettings(accountId int64) (map[string]interface{}, error) {
  8. query := m.GetDataSourcesQuery{AccountId: accountId}
  9. err := bus.Dispatch(&query)
  10. if err != nil {
  11. return nil, err
  12. }
  13. datasources := make(map[string]interface{})
  14. for i, ds := range query.Result {
  15. url := ds.Url
  16. if ds.Access == m.DS_ACCESS_PROXY {
  17. url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
  18. }
  19. var dsMap = map[string]interface{}{
  20. "type": ds.Type,
  21. "url": url,
  22. }
  23. if ds.Type == m.DS_INFLUXDB {
  24. if ds.Access == m.DS_ACCESS_DIRECT {
  25. dsMap["username"] = ds.User
  26. dsMap["password"] = ds.Password
  27. dsMap["url"] = url + "/db/" + ds.Database
  28. }
  29. }
  30. // temp hack, first is always default
  31. // TODO: implement default ds account setting
  32. if i == 0 {
  33. dsMap["default"] = true
  34. }
  35. datasources[ds.Name] = dsMap
  36. }
  37. // add grafana backend data source
  38. datasources["grafana"] = map[string]interface{}{
  39. "type": "grafana",
  40. "url": "",
  41. "grafanaDB": true,
  42. }
  43. jsonObj := map[string]interface{}{
  44. "datasources": datasources,
  45. }
  46. return jsonObj, nil
  47. }