frontendsettings.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. } else {
  51. dsMap["jsonData"] = make(map[string]string)
  52. }
  53. if ds.Access == m.DS_ACCESS_DIRECT {
  54. if ds.BasicAuth {
  55. dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)
  56. }
  57. if ds.WithCredentials {
  58. dsMap["withCredentials"] = ds.WithCredentials
  59. }
  60. if ds.Type == m.DS_INFLUXDB_08 {
  61. dsMap["username"] = ds.User
  62. dsMap["password"] = ds.Password
  63. dsMap["url"] = url + "/db/" + ds.Database
  64. }
  65. if ds.Type == m.DS_INFLUXDB {
  66. dsMap["username"] = ds.User
  67. dsMap["password"] = ds.Password
  68. dsMap["database"] = ds.Database
  69. dsMap["url"] = url
  70. }
  71. }
  72. if ds.Type == m.DS_ES {
  73. dsMap["index"] = ds.Database
  74. }
  75. if ds.Type == m.DS_INFLUXDB {
  76. dsMap["database"] = ds.Database
  77. }
  78. if ds.Type == m.DS_PROMETHEUS {
  79. // add unproxied server URL for link to Prometheus web UI
  80. dsMap["directUrl"] = ds.Url
  81. }
  82. datasources[ds.Name] = dsMap
  83. }
  84. // add datasources that are built in (meaning they are not added via data sources page, nor have any entry in datasource table)
  85. for _, ds := range plugins.DataSources {
  86. if ds.BuiltIn {
  87. datasources[ds.Name] = map[string]interface{}{
  88. "type": ds.Type,
  89. "name": ds.Name,
  90. "meta": plugins.DataSources[ds.Id],
  91. }
  92. }
  93. }
  94. if defaultDatasource == "" {
  95. defaultDatasource = "-- Grafana --"
  96. }
  97. panels := map[string]interface{}{}
  98. for _, panel := range enabledPlugins.Panels {
  99. panels[panel.Id] = map[string]interface{}{
  100. "module": panel.Module,
  101. "baseUrl": panel.BaseUrl,
  102. "name": panel.Name,
  103. "id": panel.Id,
  104. "info": panel.Info,
  105. "hideFromList": panel.HideFromList,
  106. "sort": getPanelSort(panel.Id),
  107. }
  108. }
  109. jsonObj := map[string]interface{}{
  110. "defaultDatasource": defaultDatasource,
  111. "datasources": datasources,
  112. "panels": panels,
  113. "appSubUrl": setting.AppSubUrl,
  114. "allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
  115. "authProxyEnabled": setting.AuthProxyEnabled,
  116. "ldapEnabled": setting.LdapEnabled,
  117. "alertingEnabled": setting.AlertingEnabled,
  118. "googleAnalyticsId": setting.GoogleAnalyticsId,
  119. "disableLoginForm": setting.DisableLoginForm,
  120. "disableSignoutMenu": setting.DisableSignoutMenu,
  121. "externalUserMngInfo": setting.ExternalUserMngInfo,
  122. "externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
  123. "externalUserMngLinkName": setting.ExternalUserMngLinkName,
  124. "buildInfo": map[string]interface{}{
  125. "version": setting.BuildVersion,
  126. "commit": setting.BuildCommit,
  127. "buildstamp": setting.BuildStamp,
  128. "latestVersion": plugins.GrafanaLatestVersion,
  129. "hasUpdate": plugins.GrafanaHasUpdate,
  130. "env": setting.Env,
  131. },
  132. }
  133. return jsonObj, nil
  134. }
  135. func getPanelSort(id string) int {
  136. sort := 100
  137. switch id {
  138. case "graph":
  139. sort = 1
  140. case "singlestat":
  141. sort = 2
  142. case "table":
  143. sort = 3
  144. case "text":
  145. sort = 4
  146. case "heatmap":
  147. sort = 5
  148. case "alertlist":
  149. sort = 6
  150. case "dashlist":
  151. sort = 7
  152. }
  153. return sort
  154. }
  155. func GetFrontendSettings(c *middleware.Context) {
  156. settings, err := getFrontendSettingsMap(c)
  157. if err != nil {
  158. c.JsonApiErr(400, "Failed to get frontend settings", err)
  159. return
  160. }
  161. c.JSON(200, settings)
  162. }