package route

import (
	"github.com/gin-gonic/gin"
	"runtime"
	"time"
	"amuz.es/go/mnemonics/util"
	"fmt"
)


var (
	startTime = time.Now()
)


func Metric(c *gin.Context) {
	memory:=serializeMemoryStat()
	content := gin.H{"memory":memory}
	c.JSON(200, content)
}

func serializeMemoryStat() map[string]interface{} {
	m := new(runtime.MemStats)
	runtime.ReadMemStats(m)
	return gin.H{
		"uptime":util.TimeSincePro(startTime),
		"numGoroutine":runtime.NumGoroutine(),

		"memAllocated":util.FileSize(int64(m.Alloc)),
		"memTotal":util.FileSize(int64(m.TotalAlloc)),
		"memSys":util.FileSize(int64(m.Sys)),
		"lookups":m.Lookups,
		"memMallocs":m.Mallocs,
		"memFrees":m.Frees,

		"heapAlloc":util.FileSize(int64(m.HeapAlloc)),
		"heapSys":util.FileSize(int64(m.HeapSys)),
		"heapIdle":util.FileSize(int64(m.HeapIdle)),
		"heapInuse":util.FileSize(int64(m.HeapInuse)),
		"heapReleased":util.FileSize(int64(m.HeapReleased)),
		"heapObjects":m.HeapObjects,


		"stackInuse":util.FileSize(int64(m.StackInuse)),
		"stackSys":util.FileSize(int64(m.StackSys)),
		"mSpanInuse":util.FileSize(int64(m.MSpanInuse)),
		"mSpanSys":util.FileSize(int64(m.MSpanSys)),
		"mCacheInuse":util.FileSize(int64(m.MCacheInuse)),
		"mCacheSys":util.FileSize(int64(m.MCacheSys)),
		"buckHashSys":util.FileSize(int64(m.BuckHashSys)),
		"gCSys":util.FileSize(int64(m.GCSys)),
		"otherSys":util.FileSize(int64(m.OtherSys)),

		"nextGC":util.FileSize(int64(m.NextGC)),
		"lastGC":fmt.Sprintf("%.1fs", float64(time.Now().UnixNano() - int64(m.LastGC)) / 1000 / 1000 / 1000),
		"pauseTotalNs":fmt.Sprintf("%.1fs", float64(m.PauseTotalNs) / 1000 / 1000 / 1000),
		"pauseNs":fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC + 255) % 256]) / 1000 / 1000 / 1000),
		"numGC":m.NumGC}
}
