frontendsettings.go 4.7 KB

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