added Dashboard

This commit is contained in:
Jacky 2021-03-16 20:36:27 +08:00
parent e2629b6117
commit 3bf1655db7
25 changed files with 1535 additions and 36 deletions

View file

@ -45,10 +45,12 @@ func Analytic(c *gin.Context) {
return
}
response["memory_total"] = humanize.Bytes(memoryStat.Total)
response["memory_used"] = humanize.Bytes(memoryStat.Total)
response["memory_used"] = humanize.Bytes(memoryStat.Used)
response["memory_cached"] = humanize.Bytes(memoryStat.Cached)
response["memory_free"] = humanize.Bytes(memoryStat.Free)
response["memory_pressure"] = memoryStat.Used * 100 / memoryStat.Total
before, err := cpu.Get()
if err != nil {
fmt.Println(err)
@ -61,19 +63,18 @@ func Analytic(c *gin.Context) {
total := float64(after.Total - before.Total)
response["cpu_user"] = strconv.FormatFloat(
float64(after.User-before.User)/total*100,
'f', 2, 64)
response["cpu_user"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
float64(after.User-before.User)/total*100), 64)
response["cpu_system"] = strconv.FormatFloat(float64(after.System-before.System)/total*100,
'f', 2, 64)
response["cpu_system"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
float64(after.System-before.System)/total*100), 64)
response["cpu_idle"] = strconv.FormatFloat(float64(after.Idle-before.Idle)/total*100,
'f', 2, 64)
response["cpu_idle"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
float64(after.Idle-before.Idle)/total*100), 64)
used, _total, percentage, err := tool.DiskUsage(".")
response["disk_userd"] = used
response["disk_used"] = used
response["disk_total"] = _total
response["disk_percentage"] = percentage

View file

@ -1,10 +1,9 @@
package router
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/gin-gonic/gin"
"net/http"
"github.com/gin-contrib/cors"
"github.com/0xJacky/Nginx-UI/api"
"github.com/gin-gonic/gin"
"net/http"
)
func InitRouter() *gin.Engine {
@ -13,8 +12,6 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery())
r.Use(cors.Default())
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello World",

View file

@ -7,10 +7,10 @@ import (
"strconv"
)
func DiskUsage(path string) (string, string, string, error) {
func DiskUsage(path string) (string, string, float64, error) {
di, err := disk.GetInfo(path)
if err != nil {
return "", "", "", err
return "", "", 0, err
}
percentage := (float64(di.Total-di.Free) / float64(di.Total)) * 100
fmt.Printf("%s of %s disk space used (%0.2f%%)\n",
@ -18,6 +18,8 @@ func DiskUsage(path string) (string, string, string, error) {
humanize.Bytes(di.Total),
percentage,
)
percentage, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", percentage), 64)
return humanize.Bytes(di.Total-di.Free), humanize.Bytes(di.Total),
strconv.FormatFloat(percentage, 'f', 2, 64), nil
percentage, nil
}