frontendsettings.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 (hs *HTTPServer) 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. if panel.State == "alpha" && !hs.Cfg.EnableAlphaPanels {
  99. continue
  100. }
  101. panels[panel.Id] = map[string]interface{}{
  102. "module": panel.Module,
  103. "baseUrl": panel.BaseUrl,
  104. "name": panel.Name,
  105. "id": panel.Id,
  106. "info": panel.Info,
  107. "hideFromList": panel.HideFromList,
  108. "sort": getPanelSort(panel.Id),
  109. }
  110. }
  111. jsonObj := map[string]interface{}{
  112. "defaultDatasource": defaultDatasource,
  113. "datasources": datasources,
  114. "panels": panels,
  115. "appSubUrl": setting.AppSubUrl,
  116. "allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
  117. "authProxyEnabled": setting.AuthProxyEnabled,
  118. "ldapEnabled": setting.LdapEnabled,
  119. "alertingEnabled": setting.AlertingEnabled,
  120. "alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
  121. "alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
  122. "exploreEnabled": setting.ExploreEnabled,
  123. "googleAnalyticsId": setting.GoogleAnalyticsId,
  124. "disableLoginForm": setting.DisableLoginForm,
  125. "externalUserMngInfo": setting.ExternalUserMngInfo,
  126. "externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
  127. "externalUserMngLinkName": setting.ExternalUserMngLinkName,
  128. "buildInfo": map[string]interface{}{
  129. "version": setting.BuildVersion,
  130. "commit": setting.BuildCommit,
  131. "buildstamp": setting.BuildStamp,
  132. "latestVersion": plugins.GrafanaLatestVersion,
  133. "hasUpdate": plugins.GrafanaHasUpdate,
  134. "env": setting.Env,
  135. "isEnterprise": setting.IsEnterprise,
  136. },
  137. }
  138. return jsonObj, nil
  139. }
  140. func getPanelSort(id string) int {
  141. sort := 100
  142. switch id {
  143. case "graph":
  144. sort = 1
  145. case "singlestat":
  146. sort = 2
  147. case "table":
  148. sort = 3
  149. case "text":
  150. sort = 4
  151. case "heatmap":
  152. sort = 5
  153. case "alertlist":
  154. sort = 6
  155. case "dashlist":
  156. sort = 7
  157. }
  158. return sort
  159. }
  160. func (hs *HTTPServer) GetFrontendSettings(c *m.ReqContext) {
  161. settings, err := hs.getFrontendSettingsMap(c)
  162. if err != nil {
  163. c.JsonApiErr(400, "Failed to get frontend settings", err)
  164. return
  165. }
  166. c.JSON(200, settings)
  167. }