| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | package analyticimport (	"fmt"	"net/http"	"runtime"	"time"	"github.com/0xJacky/Nginx-UI/internal/analytic"	"github.com/0xJacky/Nginx-UI/internal/helper"	"github.com/0xJacky/Nginx-UI/internal/kernel"	"github.com/shirou/gopsutil/v4/cpu"	"github.com/shirou/gopsutil/v4/host"	"github.com/shirou/gopsutil/v4/load"	"github.com/spf13/cast"	"github.com/uozi-tech/cosy/logger"	"github.com/gin-gonic/gin"	"github.com/gorilla/websocket")func Analytic(c *gin.Context) {	var upGrader = websocket.Upgrader{		CheckOrigin: func(r *http.Request) bool {			return true		},	}	// upgrade http to websocket	ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)	if err != nil {		logger.Error(err)		return	}	defer ws.Close()	var stat Stat	for {		stat.Memory, err = analytic.GetMemoryStat()		if err != nil {			logger.Error(err)			continue		}		cpuTimesBefore, _ := cpu.Times(false)		time.Sleep(1000 * time.Millisecond)		cpuTimesAfter, _ := cpu.Times(false)		threadNum := runtime.GOMAXPROCS(0)		cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)		cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)		stat.CPU = CPUStat{			User:   cast.ToFloat64(fmt.Sprintf("%.2f", cpuUserUsage*100)),			System: cast.ToFloat64(fmt.Sprintf("%.2f", cpuSystemUsage*100)),			Idle:   cast.ToFloat64(fmt.Sprintf("%.2f", (1-cpuUserUsage-cpuSystemUsage)*100)),			Total:  cast.ToFloat64(fmt.Sprintf("%.2f", (cpuUserUsage+cpuSystemUsage)*100)),		}		stat.Uptime, err = host.Uptime()		if err != nil {			logger.Error(err)			continue		}		stat.LoadAvg, err = load.Avg()		if err != nil {			logger.Error(err)			continue		}		stat.Disk, err = analytic.GetDiskStat()		if err != nil {			logger.Error(err)			continue		}		network, err := analytic.GetNetworkStat()		if err != nil {			logger.Error(err)			continue		}		stat.Network = *network		// write		err = ws.WriteJSON(stat)		if err != nil {			if helper.IsUnexpectedWebsocketError(err) {				logger.Error(err)			}			break		}		select {		case <-kernel.Context.Done():			return		case <-time.After(1 * time.Second):		}	}}func GetAnalyticInit(c *gin.Context) {	cpuInfo, err := cpu.Info()	if err != nil {		logger.Error(err)	}	network, err := analytic.GetNetworkStat()	if err != nil {		logger.Error(err)	}	memory, err := analytic.GetMemoryStat()	if err != nil {		logger.Error(err)	}	diskStat, err := analytic.GetDiskStat()	if err != nil {		logger.Error(err)	}	hostInfo, err := host.Info()	if err != nil {		logger.Error(err)		hostInfo = &host.InfoStat{}	}	switch hostInfo.Platform {	case "ubuntu":		hostInfo.Platform = "Ubuntu"	case "centos":		hostInfo.Platform = "CentOS"	}	loadAvg, err := load.Avg()	if err != nil {		logger.Error(err)		loadAvg = &load.AvgStat{}	}	c.JSON(http.StatusOK, InitResp{		Host: hostInfo,		CPU: CPURecords{			Info:  cpuInfo,			User:  analytic.CpuUserRecord,			Total: analytic.CpuTotalRecord,		},		Network: NetworkRecords{			Init:      *network,			BytesRecv: analytic.NetRecvRecord,			BytesSent: analytic.NetSentRecord,		},		DiskIO: DiskIORecords{			Writes: analytic.DiskWriteRecord,			Reads:  analytic.DiskReadRecord,		},		Memory:  memory,		Disk:    diskStat,		LoadAvg: loadAvg,	})}
 |