move tool from gogs into appstats pkg

This commit is contained in:
Rob Loranger 2019-09-13 08:22:38 -07:00
parent 66b0945b70
commit 0286dcf214
No known key found for this signature in database
GPG key ID: D6F1633A4F0903B8
2 changed files with 25 additions and 24 deletions

View file

@ -22,6 +22,7 @@ import (
"github.com/writeas/impart"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/log"
"github.com/writeas/writefreely/appstats"
"github.com/writeas/writefreely/config"
)
@ -407,37 +408,37 @@ func handleAdminUpdateConfig(apper Apper, u *User, w http.ResponseWriter, r *htt
}
func updateAppStats() {
sysStatus.Uptime = timeSincePro(appStartTime)
sysStatus.Uptime = appstats.TimeSincePro(appStartTime)
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
sysStatus.NumGoroutine = runtime.NumGoroutine()
sysStatus.MemAllocated = fileSize(int64(m.Alloc))
sysStatus.MemTotal = fileSize(int64(m.TotalAlloc))
sysStatus.MemSys = fileSize(int64(m.Sys))
sysStatus.MemAllocated = appstats.FileSize(int64(m.Alloc))
sysStatus.MemTotal = appstats.FileSize(int64(m.TotalAlloc))
sysStatus.MemSys = appstats.FileSize(int64(m.Sys))
sysStatus.Lookups = m.Lookups
sysStatus.MemMallocs = m.Mallocs
sysStatus.MemFrees = m.Frees
sysStatus.HeapAlloc = fileSize(int64(m.HeapAlloc))
sysStatus.HeapSys = fileSize(int64(m.HeapSys))
sysStatus.HeapIdle = fileSize(int64(m.HeapIdle))
sysStatus.HeapInuse = fileSize(int64(m.HeapInuse))
sysStatus.HeapReleased = fileSize(int64(m.HeapReleased))
sysStatus.HeapAlloc = appstats.FileSize(int64(m.HeapAlloc))
sysStatus.HeapSys = appstats.FileSize(int64(m.HeapSys))
sysStatus.HeapIdle = appstats.FileSize(int64(m.HeapIdle))
sysStatus.HeapInuse = appstats.FileSize(int64(m.HeapInuse))
sysStatus.HeapReleased = appstats.FileSize(int64(m.HeapReleased))
sysStatus.HeapObjects = m.HeapObjects
sysStatus.StackInuse = fileSize(int64(m.StackInuse))
sysStatus.StackSys = fileSize(int64(m.StackSys))
sysStatus.MSpanInuse = fileSize(int64(m.MSpanInuse))
sysStatus.MSpanSys = fileSize(int64(m.MSpanSys))
sysStatus.MCacheInuse = fileSize(int64(m.MCacheInuse))
sysStatus.MCacheSys = fileSize(int64(m.MCacheSys))
sysStatus.BuckHashSys = fileSize(int64(m.BuckHashSys))
sysStatus.GCSys = fileSize(int64(m.GCSys))
sysStatus.OtherSys = fileSize(int64(m.OtherSys))
sysStatus.StackInuse = appstats.FileSize(int64(m.StackInuse))
sysStatus.StackSys = appstats.FileSize(int64(m.StackSys))
sysStatus.MSpanInuse = appstats.FileSize(int64(m.MSpanInuse))
sysStatus.MSpanSys = appstats.FileSize(int64(m.MSpanSys))
sysStatus.MCacheInuse = appstats.FileSize(int64(m.MCacheInuse))
sysStatus.MCacheSys = appstats.FileSize(int64(m.MCacheSys))
sysStatus.BuckHashSys = appstats.FileSize(int64(m.BuckHashSys))
sysStatus.GCSys = appstats.FileSize(int64(m.GCSys))
sysStatus.OtherSys = appstats.FileSize(int64(m.OtherSys))
sysStatus.NextGC = fileSize(int64(m.NextGC))
sysStatus.NextGC = appstats.FileSize(int64(m.NextGC))
sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file of the Gogs project (github.com/gogs/gogs).
package writefreely
package appstats
import (
"fmt"
@ -81,8 +81,8 @@ func computeTimeDiff(diff int64) (int64, string) {
return diff, diffStr
}
// timeSincePro calculates the time interval and generate full user-friendly string.
func timeSincePro(then time.Time) string {
// TimeSincePro calculates the time interval and generate full user-friendly string.
func TimeSincePro(then time.Time) string {
now := time.Now()
diff := now.Unix() - then.Unix()
@ -121,8 +121,8 @@ func humanateBytes(s uint64, base float64, sizes []string) string {
return fmt.Sprintf(f+" %s", val, suffix)
}
// fileSize calculates the file size and generate user-friendly string.
func fileSize(s int64) string {
// FileSize calculates the file size and generate user-friendly string.
func FileSize(s int64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(uint64(s), 1024, sizes)
}