admin.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/codegangsta/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. var sysStatus struct {
  16. NumGoroutine int
  17. // General statistics.
  18. MemAllocated string // bytes allocated and still in use
  19. MemTotal string // bytes allocated (even if freed)
  20. MemSys string // bytes obtained from system (sum of XxxSys below)
  21. Lookups uint64 // number of pointer lookups
  22. MemMallocs uint64 // number of mallocs
  23. MemFrees uint64 // number of frees
  24. // Main allocation heap statistics.
  25. HeapAlloc string // bytes allocated and still in use
  26. HeapSys string // bytes obtained from system
  27. HeapIdle string // bytes in idle spans
  28. HeapInuse string // bytes in non-idle span
  29. HeapReleased string // bytes released to the OS
  30. HeapObjects uint64 // total number of allocated objects
  31. // Low-level fixed-size structure allocator statistics.
  32. // Inuse is bytes used now.
  33. // Sys is bytes obtained from system.
  34. StackInuse string // bootstrap stacks
  35. StackSys string
  36. MSpanInuse string // mspan structures
  37. MSpanSys string
  38. MCacheInuse string // mcache structures
  39. MCacheSys string
  40. BuckHashSys string // profiling bucket hash table
  41. GCSys string // GC metadata
  42. OtherSys string // other system allocations
  43. // Garbage collector statistics.
  44. NextGC string // next run in HeapAlloc time (bytes)
  45. LastGC string // last run in absolute time (ns)
  46. PauseTotalNs string
  47. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  48. NumGC uint32
  49. }
  50. func updateSystemStatus() {
  51. m := new(runtime.MemStats)
  52. runtime.ReadMemStats(m)
  53. sysStatus.NumGoroutine = runtime.NumGoroutine()
  54. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  55. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  56. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  57. sysStatus.Lookups = m.Lookups
  58. sysStatus.MemMallocs = m.Mallocs
  59. sysStatus.MemFrees = m.Frees
  60. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  61. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  62. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  63. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  64. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  65. sysStatus.HeapObjects = m.HeapObjects
  66. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  67. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  68. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  69. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  70. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  71. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  72. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  73. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  74. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  75. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  76. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  77. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs/1000/1000/1000))
  78. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256]/1000/1000/1000))
  79. sysStatus.NumGC = m.NumGC
  80. }
  81. func Dashboard(ctx *middleware.Context) {
  82. ctx.Data["Title"] = "Admin Dashboard"
  83. ctx.Data["PageIsDashboard"] = true
  84. ctx.Data["Stats"] = models.GetStatistic()
  85. updateSystemStatus()
  86. ctx.Data["SysStatus"] = sysStatus
  87. ctx.HTML(200, "admin/dashboard")
  88. }
  89. func Users(ctx *middleware.Context) {
  90. ctx.Data["Title"] = "User Management"
  91. ctx.Data["PageIsUsers"] = true
  92. var err error
  93. ctx.Data["Users"], err = models.GetUsers(100, 0)
  94. if err != nil {
  95. ctx.Handle(200, "admin.Users", err)
  96. return
  97. }
  98. ctx.HTML(200, "admin/users")
  99. }
  100. func Repositories(ctx *middleware.Context) {
  101. ctx.Data["Title"] = "Repository Management"
  102. ctx.Data["PageIsRepos"] = true
  103. var err error
  104. ctx.Data["Repos"], err = models.GetRepos(100, 0)
  105. if err != nil {
  106. ctx.Handle(200, "admin.Repositories", err)
  107. return
  108. }
  109. ctx.HTML(200, "admin/repos")
  110. }
  111. func Config(ctx *middleware.Context) {
  112. ctx.Data["Title"] = "Server Configuration"
  113. ctx.Data["PageIsConfig"] = true
  114. ctx.Data["AppUrl"] = base.AppUrl
  115. ctx.Data["Domain"] = base.Domain
  116. ctx.Data["RunUser"] = base.RunUser
  117. ctx.Data["RunMode"] = strings.Title(martini.Env)
  118. ctx.Data["RepoRootPath"] = base.RepoRootPath
  119. ctx.Data["Service"] = base.Service
  120. ctx.Data["DbCfg"] = models.DbCfg
  121. ctx.Data["MailerEnabled"] = false
  122. if base.MailService != nil {
  123. ctx.Data["MailerEnabled"] = true
  124. ctx.Data["Mailer"] = base.MailService
  125. }
  126. ctx.Data["CacheAdapter"] = base.CacheAdapter
  127. ctx.Data["CacheConfig"] = base.CacheConfig
  128. ctx.Data["PictureService"] = base.PictureService
  129. ctx.Data["PictureRootPath"] = base.PictureRootPath
  130. ctx.Data["LogMode"] = base.LogMode
  131. ctx.Data["LogConfig"] = base.LogConfig
  132. ctx.HTML(200, "admin/config")
  133. }