dashboard.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dashboard
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/xinliangnote/go-gin-api/configs"
  10. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  11. "github.com/xinliangnote/go-gin-api/internal/repository/mysql"
  12. "github.com/xinliangnote/go-gin-api/internal/repository/redis"
  13. "github.com/xinliangnote/go-gin-api/pkg/env"
  14. "github.com/shirou/gopsutil/cpu"
  15. "github.com/shirou/gopsutil/disk"
  16. "github.com/shirou/gopsutil/host"
  17. "github.com/shirou/gopsutil/mem"
  18. "go.uber.org/zap"
  19. )
  20. type handler struct {
  21. logger *zap.Logger
  22. cache redis.Repo
  23. db mysql.Repo
  24. }
  25. func New(logger *zap.Logger, db mysql.Repo, cache redis.Repo) *handler {
  26. return &handler{
  27. logger: logger,
  28. cache: cache,
  29. db: db,
  30. }
  31. }
  32. const (
  33. B = 1
  34. KB = 1024 * B
  35. MB = 1024 * KB
  36. GB = 1024 * MB
  37. )
  38. func (h *handler) View() core.HandlerFunc {
  39. type mysqlVersion struct {
  40. Ver string
  41. }
  42. mysqlVer := new(mysqlVersion)
  43. if h.db != nil {
  44. h.db.GetDbR().Raw("SELECT version() as ver").Scan(mysqlVer)
  45. }
  46. redisVer := ""
  47. if h.cache != nil {
  48. redisVer = h.cache.Version()
  49. }
  50. type viewResponse struct {
  51. MemTotal string
  52. MemUsed string
  53. MemUsedPercent float64
  54. DiskTotal string
  55. DiskUsed string
  56. DiskUsedPercent float64
  57. HostOS string
  58. HostName string
  59. CpuName string
  60. CpuCores int32
  61. CpuUsedPercent float64
  62. GoPath string
  63. GoVersion string
  64. Goroutine int
  65. ProjectPath string
  66. Env string
  67. Host string
  68. GoOS string
  69. GoArch string
  70. ProjectVersion string
  71. MySQLVersion string
  72. RedisVersion string
  73. }
  74. return func(ctx core.Context) {
  75. memInfo, _ := mem.VirtualMemory()
  76. diskInfo, _ := disk.Usage("/")
  77. hostInfo, _ := host.Info()
  78. cpuInfo, _ := cpu.Info()
  79. cpuPercent, _ := cpu.Percent(time.Second, false)
  80. obj := new(viewResponse)
  81. obj.MemTotal = fmt.Sprintf("%d GB", memInfo.Total/GB)
  82. obj.MemUsed = fmt.Sprintf("%d GB", memInfo.Used/GB)
  83. obj.MemUsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", memInfo.UsedPercent), 64)
  84. obj.DiskTotal = fmt.Sprintf("%d GB", diskInfo.Total/GB)
  85. obj.DiskUsed = fmt.Sprintf("%d GB", diskInfo.Used/GB)
  86. obj.DiskUsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", diskInfo.UsedPercent), 64)
  87. obj.HostOS = fmt.Sprintf("%s(%s) %s", hostInfo.Platform, hostInfo.PlatformFamily, hostInfo.PlatformVersion)
  88. obj.HostName = hostInfo.Hostname
  89. if len(cpuInfo) > 0 {
  90. obj.CpuName = cpuInfo[0].ModelName
  91. obj.CpuCores = cpuInfo[0].Cores
  92. }
  93. if len(cpuPercent) > 0 {
  94. obj.CpuUsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cpuPercent[0]), 64)
  95. }
  96. obj.GoPath = runtime.GOROOT()
  97. obj.GoVersion = runtime.Version()
  98. obj.Goroutine = runtime.NumGoroutine()
  99. dir, _ := os.Getwd()
  100. obj.ProjectPath = strings.Replace(dir, "\\", "/", -1)
  101. obj.Host = ctx.Host()
  102. obj.Env = env.Active().Value()
  103. obj.GoOS = runtime.GOOS
  104. obj.GoArch = runtime.GOARCH
  105. obj.ProjectVersion = configs.ProjectVersion
  106. obj.MySQLVersion = mysqlVer.Ver
  107. obj.RedisVersion = redisVer
  108. ctx.HTML("dashboard", obj)
  109. }
  110. }