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