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

224 lines
6.3 KiB

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