frontendsettings.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package api
  2. import (
  3. "strconv"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. "github.com/grafana/grafana/pkg/middleware"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/plugins"
  9. "github.com/grafana/grafana/pkg/setting"
  10. "github.com/grafana/grafana/pkg/util"
  11. )
  12. func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, error) {
  13. orgDataSources := make([]*m.DataSource, 0)
  14. if c.OrgId != 0 {
  15. query := m.GetDataSourcesQuery{OrgId: c.OrgId}
  16. err := bus.Dispatch(&query)
  17. if err != nil {
  18. return nil, err
  19. }
  20. orgDataSources = query.Result
  21. }
  22. datasources := make(map[string]interface{})
  23. var defaultDatasource string
  24. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  25. if err != nil {
  26. return nil, err
  27. }
  28. for _, ds := range orgDataSources {
  29. url := ds.Url
  30. if ds.Access == m.DS_ACCESS_PROXY {
  31. url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
  32. }
  33. var dsMap = map[string]interface{}{
  34. "id": ds.Id,
  35. "type": ds.Type,
  36. "name": ds.Name,
  37. "url": url,
  38. }
  39. meta, exists := enabledPlugins.DataSources[ds.Type]
  40. if !exists {
  41. log.Error(3, "Could not find plugin definition for data source: %v", ds.Type)
  42. continue
  43. }
  44. dsMap["meta"] = meta
  45. if ds.IsDefault {
  46. defaultDatasource = ds.Name
  47. }
  48. if ds.JsonData != nil {
  49. dsMap["jsonData"] = ds.JsonData
  50. }
  51. if ds.Access == m.DS_ACCESS_DIRECT {
  52. if ds.BasicAuth {
  53. dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)
  54. }
  55. if ds.WithCredentials {
  56. dsMap["withCredentials"] = ds.WithCredentials
  57. }
  58. if ds.Type == m.DS_INFLUXDB_08 {
  59. dsMap["username"] = ds.User
  60. dsMap["password"] = ds.Password
  61. dsMap["url"] = url + "/db/" + ds.Database
  62. }
  63. if ds.Type == m.DS_INFLUXDB {
  64. dsMap["username"] = ds.User
  65. dsMap["password"] = ds.Password
  66. dsMap["database"] = ds.Database
  67. dsMap["url"] = url
  68. }
  69. }
  70. if ds.Type == m.DS_ES {
  71. dsMap["index"] = ds.Database
  72. }
  73. if ds.Type == m.DS_INFLUXDB {
  74. dsMap["database"] = ds.Database
  75. }
  76. if ds.Type == m.DS_PROMETHEUS {
  77. // add unproxied server URL for link to Prometheus web UI
  78. dsMap["directUrl"] = ds.Url
  79. }
  80. datasources[ds.Name] = dsMap
  81. }
  82. for _, ds := range plugins.DataSources {
  83. if ds.AlwaysDisplay {
  84. datasources[ds.Name] = map[string]interface{}{
  85. "type": ds.Type,
  86. "name": ds.Name,
  87. "meta": plugins.DataSources[ds.Id],
  88. }
  89. }
  90. }
  91. if defaultDatasource == "" {
  92. defaultDatasource = "-- Grafana --"
  93. }
  94. panels := map[string]interface{}{}
  95. for _, panel := range enabledPlugins.Panels {
  96. panels[panel.Id] = map[string]interface{}{
  97. "module": panel.Module,
  98. "baseUrl": panel.BaseUrl,
  99. "name": panel.Name,
  100. "id": panel.Id,
  101. "info": panel.Info,
  102. "hideFromList": panel.HideFromList,
  103. "sort": getPanelSort(panel.Id),
  104. }
  105. }
  106. jsonObj := map[string]interface{}{
  107. "defaultDatasource": defaultDatasource,
  108. "datasources": datasources,
  109. "panels": panels,
  110. "appSubUrl": setting.AppSubUrl,
  111. "allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
  112. "authProxyEnabled": setting.AuthProxyEnabled,
  113. "ldapEnabled": setting.LdapEnabled,
  114. "alertingEnabled": setting.AlertingEnabled,
  115. "googleAnalyticsId": setting.GoogleAnalyticsId,
  116. "disableLoginForm": setting.DisableLoginForm,
  117. "disableSignoutMenu": setting.DisableSignoutMenu,
  118. "buildInfo": map[string]interface{}{
  119. "version": setting.BuildVersion,
  120. "commit": setting.BuildCommit,
  121. "buildstamp": setting.BuildStamp,
  122. "latestVersion": plugins.GrafanaLatestVersion,
  123. "hasUpdate": plugins.GrafanaHasUpdate,
  124. "env": setting.Env,
  125. },
  126. }
  127. return jsonObj, nil
  128. }
  129. func getPanelSort(id string) int {
  130. sort := 100
  131. switch id {
  132. case "graph":
  133. sort = 1
  134. case "singlestat":
  135. sort = 2
  136. case "table":
  137. sort = 3
  138. case "text":
  139. sort = 4
  140. case "alertlist":
  141. sort = 5
  142. case "dashlist":
  143. sort = 6
  144. }
  145. return sort
  146. }
  147. func GetFrontendSettings(c *middleware.Context) {
  148. settings, err := getFrontendSettingsMap(c)
  149. if err != nil {
  150. c.JsonApiErr(400, "Failed to get frontend settings", err)
  151. return
  152. }
  153. c.JSON(200, settings)
  154. }