|
6 | 6 | package index |
7 | 7 |
|
8 | 8 | import ( |
9 | | - "math/big" |
10 | | - "net/http" |
11 | | - "os" |
12 | | - "runtime" |
13 | | - "strings" |
14 | | - "time" |
15 | | - |
16 | | - "github.com/duke-git/lancet/v2/mathutil" |
17 | | - humanize "github.com/dustin/go-humanize" |
18 | | - "github.com/gin-gonic/gin" |
19 | | - "github.com/shirou/gopsutil/cpu" |
20 | | - "github.com/shirou/gopsutil/disk" |
21 | | - "github.com/shirou/gopsutil/mem" |
22 | | - |
23 | | - v1 "github.com/ch3nnn/webstack-go/api/v1" |
24 | 9 | "github.com/ch3nnn/webstack-go/internal/handler" |
| 10 | + "github.com/ch3nnn/webstack-go/internal/service/dashboard" |
25 | 11 | ) |
26 | 12 |
|
27 | 13 | type Handler struct { |
28 | 14 | *handler.Handler |
| 15 | + dashboardService dashboard.Service |
29 | 16 | } |
30 | 17 |
|
31 | | -func NewHandler(handler *handler.Handler) *Handler { |
32 | | - return &Handler{Handler: handler} |
33 | | -} |
34 | | - |
35 | | -func (h *Handler) memory() (m mem.VirtualMemoryStat) { |
36 | | - info, err := mem.VirtualMemory() |
37 | | - if err != nil { |
38 | | - return m |
39 | | - } |
40 | | - |
41 | | - return *info |
42 | | -} |
43 | | - |
44 | | -func (h *Handler) disk() (d disk.UsageStat) { |
45 | | - info, err := disk.Usage("/") |
46 | | - if err != nil { |
47 | | - return d |
48 | | - } |
49 | | - |
50 | | - return *info |
51 | | -} |
52 | | - |
53 | | -func (h *Handler) cpu() (c cpu.InfoStat) { |
54 | | - info, err := cpu.Info() |
55 | | - if err != nil { |
56 | | - return c |
57 | | - } |
58 | | - |
59 | | - if len(info) > 0 { |
60 | | - return info[0] |
61 | | - } |
62 | | - |
63 | | - return c |
64 | | -} |
65 | | - |
66 | | -func (h *Handler) Dashboard(ctx *gin.Context) { |
67 | | - memoryInfo := h.memory() |
68 | | - diskInfo := h.disk() |
69 | | - cpuInfo := h.cpu() |
70 | | - |
71 | | - dir, _ := os.Getwd() |
72 | | - |
73 | | - var cpuPercent float64 |
74 | | - cpuPercents, _ := cpu.Percent(time.Second, false) |
75 | | - if len(cpuPercents) > 0 { |
76 | | - cpuPercent = mathutil.RoundToFloat(cpuPercents[0], 2) |
77 | | - } |
78 | | - |
79 | | - ctx.HTML(http.StatusOK, "dashboard.html", v1.DashboardResp{ |
80 | | - ProjectVersion: "2.0", |
81 | | - GoOS: runtime.GOOS, |
82 | | - GoArch: runtime.GOARCH, |
83 | | - GoVersion: runtime.Version(), |
84 | | - ProjectPath: strings.Replace(dir, "\\", "/", -1), |
85 | | - MemTotal: humanize.BigBytes(big.NewInt(int64(memoryInfo.Total))), |
86 | | - MemUsed: humanize.BigBytes(big.NewInt(int64(memoryInfo.Used))), |
87 | | - MemUsedPercent: mathutil.RoundToFloat(memoryInfo.UsedPercent, 2), |
88 | | - DiskTotal: humanize.BigBytes(big.NewInt(int64(diskInfo.Total))), |
89 | | - DiskUsed: humanize.BigBytes(big.NewInt(int64(diskInfo.Used))), |
90 | | - DiskUsedPercent: mathutil.RoundToFloat(diskInfo.UsedPercent, 2), |
91 | | - CpuName: cpuInfo.ModelName, |
92 | | - CpuCores: cpuInfo.Cores, |
93 | | - CpuUsedPercent: cpuPercent, |
94 | | - }) |
| 18 | +func NewHandler(handler *handler.Handler, dashboardService dashboard.Service) *Handler { |
| 19 | + return &Handler{Handler: handler, dashboardService: dashboardService} |
95 | 20 | } |
0 commit comments