frontendsettings.go 5.2 KB

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