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.

168 lines
5.1 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
  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 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. func Dashboard(ctx *middleware.Context) {
  85. ctx.Data["Title"] = "Admin Dashboard"
  86. ctx.Data["PageIsDashboard"] = true
  87. ctx.Data["Stats"] = models.GetStatistic()
  88. updateSystemStatus()
  89. ctx.Data["SysStatus"] = sysStatus
  90. ctx.HTML(200, "admin/dashboard")
  91. }
  92. func Users(ctx *middleware.Context) {
  93. ctx.Data["Title"] = "User Management"
  94. ctx.Data["PageIsUsers"] = true
  95. var err error
  96. ctx.Data["Users"], err = models.GetUsers(100, 0)
  97. if err != nil {
  98. ctx.Handle(200, "admin.Users", err)
  99. return
  100. }
  101. ctx.HTML(200, "admin/users")
  102. }
  103. func Repositories(ctx *middleware.Context) {
  104. ctx.Data["Title"] = "Repository Management"
  105. ctx.Data["PageIsRepos"] = true
  106. var err error
  107. ctx.Data["Repos"], err = models.GetRepos(100, 0)
  108. if err != nil {
  109. ctx.Handle(200, "admin.Repositories", err)
  110. return
  111. }
  112. ctx.HTML(200, "admin/repos")
  113. }
  114. func Config(ctx *middleware.Context) {
  115. ctx.Data["Title"] = "Server Configuration"
  116. ctx.Data["PageIsConfig"] = true
  117. ctx.Data["AppUrl"] = base.AppUrl
  118. ctx.Data["Domain"] = base.Domain
  119. ctx.Data["RunUser"] = base.RunUser
  120. ctx.Data["RunMode"] = strings.Title(martini.Env)
  121. ctx.Data["RepoRootPath"] = base.RepoRootPath
  122. ctx.Data["Service"] = base.Service
  123. ctx.Data["DbCfg"] = models.DbCfg
  124. ctx.Data["MailerEnabled"] = false
  125. if base.MailService != nil {
  126. ctx.Data["MailerEnabled"] = true
  127. ctx.Data["Mailer"] = base.MailService
  128. }
  129. ctx.Data["CacheAdapter"] = base.CacheAdapter
  130. ctx.Data["CacheConfig"] = base.CacheConfig
  131. ctx.Data["SessionProvider"] = base.SessionProvider
  132. ctx.Data["SessionConfig"] = base.SessionConfig
  133. ctx.Data["PictureService"] = base.PictureService
  134. ctx.Data["LogMode"] = base.LogMode
  135. ctx.Data["LogConfig"] = base.LogConfig
  136. ctx.HTML(200, "admin/config")
  137. }