You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.go 6.6 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "fmt"
  7. "runtime"
  8. "strings"
  9. "time"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/middleware"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. var startTime = time.Now()
  17. var sysStatus struct {
  18. Uptime string
  19. NumGoroutine int
  20. // General statistics.
  21. MemAllocated string // bytes allocated and still in use
  22. MemTotal string // bytes allocated (even if freed)
  23. MemSys string // bytes obtained from system (sum of XxxSys below)
  24. Lookups uint64 // number of pointer lookups
  25. MemMallocs uint64 // number of mallocs
  26. MemFrees uint64 // number of frees
  27. // Main allocation heap statistics.
  28. HeapAlloc string // bytes allocated and still in use
  29. HeapSys string // bytes obtained from system
  30. HeapIdle string // bytes in idle spans
  31. HeapInuse string // bytes in non-idle span
  32. HeapReleased string // bytes released to the OS
  33. HeapObjects uint64 // total number of allocated objects
  34. // Low-level fixed-size structure allocator statistics.
  35. // Inuse is bytes used now.
  36. // Sys is bytes obtained from system.
  37. StackInuse string // bootstrap stacks
  38. StackSys string
  39. MSpanInuse string // mspan structures
  40. MSpanSys string
  41. MCacheInuse string // mcache structures
  42. MCacheSys string
  43. BuckHashSys string // profiling bucket hash table
  44. GCSys string // GC metadata
  45. OtherSys string // other system allocations
  46. // Garbage collector statistics.
  47. NextGC string // next run in HeapAlloc time (bytes)
  48. LastGC string // last run in absolute time (ns)
  49. PauseTotalNs string
  50. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  51. NumGC uint32
  52. }
  53. func updateSystemStatus() {
  54. sysStatus.Uptime = base.TimeSincePro(startTime)
  55. m := new(runtime.MemStats)
  56. runtime.ReadMemStats(m)
  57. sysStatus.NumGoroutine = runtime.NumGoroutine()
  58. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  59. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  60. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  61. sysStatus.Lookups = m.Lookups
  62. sysStatus.MemMallocs = m.Mallocs
  63. sysStatus.MemFrees = m.Frees
  64. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  65. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  66. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  67. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  68. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  69. sysStatus.HeapObjects = m.HeapObjects
  70. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  71. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  72. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  73. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  74. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  75. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  76. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  77. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  78. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  79. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  80. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  81. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  82. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  83. sysStatus.NumGC = m.NumGC
  84. }
  85. // Operation types.
  86. const (
  87. OT_CLEAN_OAUTH = iota + 1
  88. )
  89. func Dashboard(ctx *middleware.Context) {
  90. ctx.Data["Title"] = "Admin Dashboard"
  91. ctx.Data["PageIsDashboard"] = true
  92. // Run operation.
  93. op, _ := base.StrTo(ctx.Query("op")).Int()
  94. if op > 0 {
  95. var err error
  96. var success string
  97. switch op {
  98. case OT_CLEAN_OAUTH:
  99. success = "All unbind OAuthes have been deleted."
  100. err = models.CleanUnbindOauth()
  101. }
  102. if err != nil {
  103. ctx.Flash.Error(err.Error())
  104. } else {
  105. ctx.Flash.Success(success)
  106. }
  107. ctx.Redirect("/admin")
  108. return
  109. }
  110. ctx.Data["Stats"] = models.GetStatistic()
  111. updateSystemStatus()
  112. ctx.Data["SysStatus"] = sysStatus
  113. ctx.HTML(200, "admin/dashboard")
  114. }
  115. func Users(ctx *middleware.Context) {
  116. ctx.Data["Title"] = "User Management"
  117. ctx.Data["PageIsUsers"] = true
  118. var err error
  119. ctx.Data["Users"], err = models.GetUsers(200, 0)
  120. if err != nil {
  121. ctx.Handle(500, "admin.Users", err)
  122. return
  123. }
  124. ctx.HTML(200, "admin/users")
  125. }
  126. func Repositories(ctx *middleware.Context) {
  127. ctx.Data["Title"] = "Repository Management"
  128. ctx.Data["PageIsRepos"] = true
  129. var err error
  130. ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0)
  131. if err != nil {
  132. ctx.Handle(500, "admin.Repositories", err)
  133. return
  134. }
  135. ctx.HTML(200, "admin/repos")
  136. }
  137. func Auths(ctx *middleware.Context) {
  138. ctx.Data["Title"] = "Auth Sources"
  139. ctx.Data["PageIsAuths"] = true
  140. var err error
  141. ctx.Data["Sources"], err = models.GetAuths()
  142. if err != nil {
  143. ctx.Handle(500, "admin.Auths", err)
  144. return
  145. }
  146. ctx.HTML(200, "admin/auths")
  147. }
  148. func Config(ctx *middleware.Context) {
  149. ctx.Data["Title"] = "Server Configuration"
  150. ctx.Data["PageIsConfig"] = true
  151. ctx.Data["AppUrl"] = setting.AppUrl
  152. ctx.Data["Domain"] = setting.Domain
  153. ctx.Data["OfflineMode"] = setting.OfflineMode
  154. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  155. ctx.Data["RunUser"] = setting.RunUser
  156. ctx.Data["RunMode"] = strings.Title(martini.Env)
  157. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  158. ctx.Data["ScriptType"] = setting.ScriptType
  159. ctx.Data["Service"] = setting.Service
  160. ctx.Data["DbCfg"] = models.DbCfg
  161. ctx.Data["MailerEnabled"] = false
  162. if setting.MailService != nil {
  163. ctx.Data["MailerEnabled"] = true
  164. ctx.Data["Mailer"] = setting.MailService
  165. }
  166. ctx.Data["OauthEnabled"] = false
  167. if setting.OauthService != nil {
  168. ctx.Data["OauthEnabled"] = true
  169. ctx.Data["Oauther"] = setting.OauthService
  170. }
  171. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  172. ctx.Data["CacheConfig"] = setting.CacheConfig
  173. ctx.Data["SessionProvider"] = setting.SessionProvider
  174. ctx.Data["SessionConfig"] = setting.SessionConfig
  175. ctx.Data["PictureService"] = setting.PictureService
  176. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  177. type logger struct {
  178. Mode, Config string
  179. }
  180. loggers := make([]*logger, len(setting.LogModes))
  181. for i := range setting.LogModes {
  182. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  183. }
  184. ctx.Data["Loggers"] = loggers
  185. ctx.HTML(200, "admin/config")
  186. }