frontendsettings.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // add datasources that are built in (meaning they are not added via data sources page, nor have any entry in datasource table)
  83. for _, ds := range plugins.DataSources {
  84. if ds.BuiltIn {
  85. datasources[ds.Name] = map[string]interface{}{
  86. "type": ds.Type,
  87. "name": ds.Name,
  88. "meta": plugins.DataSources[ds.Id],
  89. }
  90. }
  91. }
  92. if defaultDatasource == "" {
  93. defaultDatasource = "-- Grafana --"
  94. }
  95. panels := map[string]interface{}{}
  96. for _, panel := range enabledPlugins.Panels {
  97. panels[panel.Id] = map[string]interface{}{
  98. "module": panel.Module,
  99. "baseUrl": panel.BaseUrl,
  100. "name": panel.Name,
  101. "id": panel.Id,
  102. "info": panel.Info,
  103. "hideFromList": panel.HideFromList,
  104. "sort": getPanelSort(panel.Id),
  105. }
  106. }
  107. jsonObj := map[string]interface{}{
  108. "defaultDatasource": defaultDatasource,
  109. "datasources": datasources,
  110. "panels": panels,
  111. "appSubUrl": setting.AppSubUrl,
  112. "allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
  113. "authProxyEnabled": setting.AuthProxyEnabled,
  114. "ldapEnabled": setting.LdapEnabled,
  115. "alertingEnabled": setting.AlertingEnabled,
  116. "googleAnalyticsId": setting.GoogleAnalyticsId,
  117. "disableLoginForm": setting.DisableLoginForm,
  118. "disableSignoutMenu": setting.DisableSignoutMenu,
  119. "buildInfo": map[string]interface{}{
  120. "version": setting.BuildVersion,
  121. "commit": setting.BuildCommit,
  122. "buildstamp": setting.BuildStamp,
  123. "latestVersion": plugins.GrafanaLatestVersion,
  124. "hasUpdate": plugins.GrafanaHasUpdate,
  125. "env": setting.Env,
  126. },
  127. }
  128. return jsonObj, nil
  129. }
  130. func getPanelSort(id string) int {
  131. sort := 100
  132. switch id {
  133. case "graph":
  134. sort = 1
  135. case "singlestat":
  136. sort = 2
  137. case "table":
  138. sort = 3
  139. case "text":
  140. sort = 4
  141. case "alertlist":
  142. sort = 5
  143. case "dashlist":
  144. sort = 6
  145. }
  146. return sort
  147. }
  148. func GetFrontendSettings(c *middleware.Context) {
  149. settings, err := getFrontendSettingsMap(c)
  150. if err != nil {
  151. c.JsonApiErr(400, "Failed to get frontend settings", err)
  152. return
  153. }
  154. c.JSON(200, settings)
  155. }