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 8.6 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/cron"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/process"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. const (
  19. DASHBOARD base.TplName = "admin/dashboard"
  20. USERS base.TplName = "admin/users"
  21. REPOS base.TplName = "admin/repos"
  22. AUTHS base.TplName = "admin/auths"
  23. CONFIG base.TplName = "admin/config"
  24. MONITOR_PROCESS base.TplName = "admin/monitor/process"
  25. MONITOR_CRON base.TplName = "admin/monitor/cron"
  26. )
  27. var (
  28. startTime = time.Now()
  29. )
  30. var sysStatus struct {
  31. Uptime string
  32. NumGoroutine int
  33. // General statistics.
  34. MemAllocated string // bytes allocated and still in use
  35. MemTotal string // bytes allocated (even if freed)
  36. MemSys string // bytes obtained from system (sum of XxxSys below)
  37. Lookups uint64 // number of pointer lookups
  38. MemMallocs uint64 // number of mallocs
  39. MemFrees uint64 // number of frees
  40. // Main allocation heap statistics.
  41. HeapAlloc string // bytes allocated and still in use
  42. HeapSys string // bytes obtained from system
  43. HeapIdle string // bytes in idle spans
  44. HeapInuse string // bytes in non-idle span
  45. HeapReleased string // bytes released to the OS
  46. HeapObjects uint64 // total number of allocated objects
  47. // Low-level fixed-size structure allocator statistics.
  48. // Inuse is bytes used now.
  49. // Sys is bytes obtained from system.
  50. StackInuse string // bootstrap stacks
  51. StackSys string
  52. MSpanInuse string // mspan structures
  53. MSpanSys string
  54. MCacheInuse string // mcache structures
  55. MCacheSys string
  56. BuckHashSys string // profiling bucket hash table
  57. GCSys string // GC metadata
  58. OtherSys string // other system allocations
  59. // Garbage collector statistics.
  60. NextGC string // next run in HeapAlloc time (bytes)
  61. LastGC string // last run in absolute time (ns)
  62. PauseTotalNs string
  63. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  64. NumGC uint32
  65. }
  66. func updateSystemStatus() {
  67. sysStatus.Uptime = base.TimeSincePro(startTime)
  68. m := new(runtime.MemStats)
  69. runtime.ReadMemStats(m)
  70. sysStatus.NumGoroutine = runtime.NumGoroutine()
  71. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  72. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  73. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  74. sysStatus.Lookups = m.Lookups
  75. sysStatus.MemMallocs = m.Mallocs
  76. sysStatus.MemFrees = m.Frees
  77. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  78. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  79. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  80. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  81. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  82. sysStatus.HeapObjects = m.HeapObjects
  83. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  84. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  85. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  86. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  87. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  88. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  89. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  90. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  91. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  92. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  93. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  94. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  95. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  96. sysStatus.NumGC = m.NumGC
  97. }
  98. // Operation types.
  99. type AdminOperation int
  100. const (
  101. CLEAN_UNBIND_OAUTH AdminOperation = iota + 1
  102. CLEAN_INACTIVATE_USER
  103. )
  104. func Dashboard(ctx *middleware.Context) {
  105. ctx.Data["Title"] = "Admin Dashboard"
  106. ctx.Data["PageIsDashboard"] = true
  107. // Run operation.
  108. op, _ := base.StrTo(ctx.Query("op")).Int()
  109. if op > 0 {
  110. var err error
  111. var success string
  112. switch AdminOperation(op) {
  113. case CLEAN_UNBIND_OAUTH:
  114. success = "All unbind OAuthes have been deleted."
  115. err = models.CleanUnbindOauth()
  116. case CLEAN_INACTIVATE_USER:
  117. success = "All inactivate accounts have been deleted."
  118. err = models.DeleteInactivateUsers()
  119. }
  120. if err != nil {
  121. ctx.Flash.Error(err.Error())
  122. } else {
  123. ctx.Flash.Success(success)
  124. }
  125. ctx.Redirect("/admin")
  126. return
  127. }
  128. ctx.Data["Stats"] = models.GetStatistic()
  129. updateSystemStatus()
  130. ctx.Data["SysStatus"] = sysStatus
  131. ctx.HTML(200, DASHBOARD)
  132. }
  133. func Users(ctx *middleware.Context) {
  134. ctx.Data["Title"] = "User Management"
  135. ctx.Data["PageIsUsers"] = true
  136. p := base.StrTo(ctx.Query("p")).MustInt()
  137. if p < 1 {
  138. p = 1
  139. }
  140. pageNum := 50
  141. count := models.CountUsers()
  142. curCount := int64((p-1)*pageNum + pageNum)
  143. if curCount > count {
  144. p = int(count) / pageNum
  145. } else if count > curCount {
  146. ctx.Data["NextPageNum"] = p + 1
  147. }
  148. if p > 1 {
  149. ctx.Data["LastPageNum"] = p - 1
  150. }
  151. var err error
  152. ctx.Data["Users"], err = models.GetUsers(pageNum, (p-1)*pageNum)
  153. if err != nil {
  154. ctx.Handle(500, "admin.Users(GetUsers)", err)
  155. return
  156. }
  157. ctx.HTML(200, USERS)
  158. }
  159. func Repositories(ctx *middleware.Context) {
  160. ctx.Data["Title"] = "Repository Management"
  161. ctx.Data["PageIsRepos"] = true
  162. p := base.StrTo(ctx.Query("p")).MustInt()
  163. if p < 1 {
  164. p = 1
  165. }
  166. pageNum := 50
  167. count := models.CountRepositories()
  168. curCount := int64((p-1)*pageNum + pageNum)
  169. if curCount > count {
  170. p = int(count) / pageNum
  171. } else if count > curCount {
  172. ctx.Data["NextPageNum"] = p + 1
  173. }
  174. if p > 1 {
  175. ctx.Data["LastPageNum"] = p - 1
  176. }
  177. var err error
  178. ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(pageNum, (p-1)*pageNum)
  179. if err != nil {
  180. ctx.Handle(500, "admin.Repositories", err)
  181. return
  182. }
  183. ctx.HTML(200, REPOS)
  184. }
  185. func Auths(ctx *middleware.Context) {
  186. ctx.Data["Title"] = "Auth Sources"
  187. ctx.Data["PageIsAuths"] = true
  188. var err error
  189. ctx.Data["Sources"], err = models.GetAuths()
  190. if err != nil {
  191. ctx.Handle(500, "admin.Auths", err)
  192. return
  193. }
  194. ctx.HTML(200, AUTHS)
  195. }
  196. func Config(ctx *middleware.Context) {
  197. ctx.Data["Title"] = "Server Configuration"
  198. ctx.Data["PageIsConfig"] = true
  199. ctx.Data["AppUrl"] = setting.AppUrl
  200. ctx.Data["Domain"] = setting.Domain
  201. ctx.Data["OfflineMode"] = setting.OfflineMode
  202. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  203. ctx.Data["RunUser"] = setting.RunUser
  204. ctx.Data["RunMode"] = strings.Title(martini.Env)
  205. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  206. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  207. ctx.Data["LogRootPath"] = setting.LogRootPath
  208. ctx.Data["ScriptType"] = setting.ScriptType
  209. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  210. ctx.Data["Service"] = setting.Service
  211. ctx.Data["DbCfg"] = models.DbCfg
  212. ctx.Data["WebhookTaskInterval"] = setting.WebhookTaskInterval
  213. ctx.Data["WebhookDeliverTimeout"] = setting.WebhookDeliverTimeout
  214. ctx.Data["MailerEnabled"] = false
  215. if setting.MailService != nil {
  216. ctx.Data["MailerEnabled"] = true
  217. ctx.Data["Mailer"] = setting.MailService
  218. }
  219. ctx.Data["OauthEnabled"] = false
  220. if setting.OauthService != nil {
  221. ctx.Data["OauthEnabled"] = true
  222. ctx.Data["Oauther"] = setting.OauthService
  223. }
  224. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  225. ctx.Data["CacheConfig"] = setting.CacheConfig
  226. ctx.Data["SessionProvider"] = setting.SessionProvider
  227. ctx.Data["SessionConfig"] = setting.SessionConfig
  228. ctx.Data["PictureService"] = setting.PictureService
  229. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  230. type logger struct {
  231. Mode, Config string
  232. }
  233. loggers := make([]*logger, len(setting.LogModes))
  234. for i := range setting.LogModes {
  235. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  236. }
  237. ctx.Data["Loggers"] = loggers
  238. ctx.HTML(200, CONFIG)
  239. }
  240. func Monitor(ctx *middleware.Context) {
  241. ctx.Data["Title"] = "Monitoring Center"
  242. ctx.Data["PageIsMonitor"] = true
  243. tab := ctx.Query("tab")
  244. switch tab {
  245. case "process":
  246. ctx.Data["PageIsMonitorProcess"] = true
  247. ctx.Data["Processes"] = process.Processes
  248. ctx.HTML(200, MONITOR_PROCESS)
  249. default:
  250. ctx.Data["PageIsMonitorCron"] = true
  251. ctx.Data["Entries"] = cron.ListEntries()
  252. ctx.HTML(200, MONITOR_CRON)
  253. }
  254. }