frontendsettings.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "exploreEnabled": setting.ExploreEnabled,
  127. "googleAnalyticsId": setting.GoogleAnalyticsId,
  128. "disableLoginForm": setting.DisableLoginForm,
  129. "externalUserMngInfo": setting.ExternalUserMngInfo,
  130. "externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
  131. "externalUserMngLinkName": setting.ExternalUserMngLinkName,
  132. "buildInfo": map[string]interface{}{
  133. "version": setting.BuildVersion,
  134. "commit": setting.BuildCommit,
  135. "buildstamp": setting.BuildStamp,
  136. "latestVersion": plugins.GrafanaLatestVersion,
  137. "hasUpdate": plugins.GrafanaHasUpdate,
  138. "env": setting.Env,
  139. },
  140. }
  141. return jsonObj, nil
  142. }
  143. func getPanelSort(id string) int {
  144. sort := 100
  145. switch id {
  146. case "graph":
  147. sort = 1
  148. case "singlestat":
  149. sort = 2
  150. case "table":
  151. sort = 3
  152. case "text":
  153. sort = 4
  154. case "heatmap":
  155. sort = 5
  156. case "alertlist":
  157. sort = 6
  158. case "dashlist":
  159. sort = 7
  160. }
  161. return sort
  162. }
  163. func GetFrontendSettings(c *m.ReqContext) {
  164. settings, err := getFrontendSettingsMap(c)
  165. if err != nil {
  166. c.JsonApiErr(400, "Failed to get frontend settings", err)
  167. return
  168. }
  169. c.JSON(200, settings)
  170. }