-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisk.go
106 lines (92 loc) · 3.39 KB
/
disk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"github.com/shirou/gopsutil/disk"
)
func DrawDiskGraph(r_Data []float64, w_Data []float64, diskUsage float64) *fyne.Container {
graph := container.NewWithoutLayout()
background := canvas.NewRectangle(color.RGBA{0, 0, 0, 255}) // Black background
background.SetMinSize(fyne.NewSize(300, 180))
graph.Add(background)
r_color := color.RGBA{20, 245, 230, 255} //read speed graph color
w_color := color.RGBA{120, 229, 40, 255} //write speed graph color
drawGraph := func() {
graph.Objects = []fyne.CanvasObject{background} // Clear previous lines
titleLabel := canvas.NewText("Disk", color.RGBA{255, 255, 255, 255})
titleLabel.Move(fyne.NewPos(0, 0))
graph.Add(titleLabel)
w_speedLabel := canvas.NewText(fmt.Sprintf("Write %.2fMB/s", w_Data[len(w_Data)-1]), w_color)
w_speedLabel.Move(fyne.NewPos(40, 0))
graph.Add(w_speedLabel)
r_speedLabel := canvas.NewText(fmt.Sprintf("Read %.2fMB/s", r_Data[len(r_Data)-1]), r_color)
r_speedLabel.Move(fyne.NewPos(150, 0))
graph.Add(r_speedLabel)
partitions, err := disk.Partitions(false)
if err == nil {
h := 0
for _, partition := range partitions {
// Get usage statistics for each partition
usage, err := disk.Usage(partition.Mountpoint)
if err == nil {
if usage.Total > 1024 {
// Print partition info
partNameLabel := canvas.NewText(partition.Device, color.White)
partNameLabel.TextSize = 11
partNameLabel.Move(fyne.NewPos(320, float32(40)+float32(h)))
graph.Add(partNameLabel)
partTotalLabel := canvas.NewText(fmt.Sprintf("Total: %v GB", usage.Total/(1024*1024*1024)), color.RGBA{20, 220, 20, 255})
partTotalLabel.TextSize = 11
point_x := 320 + partNameLabel.MinSize().Width + 10
partTotalLabel.Move(fyne.NewPos(point_x, float32(40)+float32(h)))
graph.Add(partTotalLabel)
partUsedLabel := canvas.NewText(fmt.Sprintf("Used: %v GB", usage.Used/(1024*1024*1024)), color.RGBA{220, 20, 20, 255})
partUsedLabel.TextSize = 11
point_x = point_x + partTotalLabel.MinSize().Width + 10
partUsedLabel.Move(fyne.NewPos(point_x, float32(40)+float32(h)))
graph.Add(partUsedLabel)
h = h + 20
}
}
}
}
axisY_0 := canvas.NewText("0", color.RGBA{255, 255, 255, 200})
axisY_0.Move(fyne.NewPos(0, 140))
axisY_100 := canvas.NewText("100", color.RGBA{255, 255, 255, 200})
axisY_100.Move(fyne.NewPos(0, 20))
border := canvas.NewRectangle(color.RGBA{255, 255, 255, 0})
border.StrokeWidth = 2
border.StrokeColor = color.RGBA{255, 255, 255, 100}
border.Resize(fyne.NewSize(300, 100))
border.Move(fyne.NewPos(0, 40))
graph.Add(axisY_0)
graph.Add(axisY_100)
graph.Add(border)
for i := 1; i < len(r_Data); i++ {
x1 := float32((i - 1) * 30)
y1 := float32(100-r_Data[i-1]) + 40
x2 := float32(i * 30)
y2 := float32(100-r_Data[i]) + 40
line := canvas.NewLine(r_color)
line.Position1 = fyne.NewPos(x1, y1)
line.Position2 = fyne.NewPos(x2, y2)
graph.Add(line)
}
for i := 1; i < len(w_Data); i++ {
x1 := float32((i - 1) * 30)
y1 := float32(100-w_Data[i-1]) + 40
x2 := float32(i * 30)
y2 := float32(100-w_Data[i]) + 40
line := canvas.NewLine(w_color)
line.Position1 = fyne.NewPos(x1, y1)
line.Position2 = fyne.NewPos(x2, y2)
graph.Add(line)
}
canvas.Refresh(graph)
}
drawGraph()
return graph
}