frontendsettings.go 4.6 KB

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