1
+ from email .charset import QP
2
+ import psutil
3
+ from PySide6 .QtCharts import QChart , QLineSeries , QValueAxis
4
+ from PySide6 .QtCore import Qt , QTimer
5
+ from PySide6 .QtGui import QPen
6
+ from PySide6 .QtWidgets import QLabel
7
+
8
+ class MemMonitor (QChart ):
9
+ def __init__ (self , parent = None ):
10
+ super ().__init__ (QChart .ChartTypeCartesian , parent , Qt .WindowFlags ())
11
+
12
+ # init essential elements
13
+ self .seriesMem = QLineSeries (self )
14
+ self .seriesSwp = QLineSeries (self )
15
+ self .axisX = QValueAxis ()
16
+ self .axisYleft = QValueAxis ()
17
+ self .axisYright = QValueAxis ()
18
+
19
+ # important values
20
+ self .step = 1
21
+ self .xRange = 20
22
+ self .x = 0
23
+ # total memory space
24
+ self .memoryData = psutil .virtual_memory ()
25
+ self .swapData = psutil .swap_memory ()
26
+ self .GBCONV = 1073741824
27
+ self .memoryTotal = psutil .virtual_memory ().total / self .GBCONV
28
+ self .swapTotal = psutil .swap_memory ().total / self .GBCONV
29
+ # get the memory utilization
30
+ self .yMem = psutil .virtual_memory ()
31
+ self .ySwp = psutil .swap_memory ()
32
+
33
+ # set timer properties
34
+ self .timer = QTimer ()
35
+ self .timer .timeout .connect (self .handleTimeout )
36
+ self .timer .setInterval (1000 )
37
+
38
+ # set texts of legend and title
39
+ self .legend ().setVisible (True )
40
+ self .legend ().setAlignment (Qt .AlignBottom )
41
+ self .seriesMem .setName ("Memory: %.2f/%.2fGB (%.2f%%)"
42
+ % (self .yMem .used / self .GBCONV , self .memoryTotal , self .yMem .percent ))
43
+ self .seriesSwp .setName ("Swap: %.2f/%.2fGB (%.2f%%)"
44
+ % (self .ySwp .used / self .GBCONV , self .swapTotal , self .ySwp .percent ))
45
+ self .axisX .setLabelFormat ("%d" )
46
+ self .axisX .setTitleText ("Time(s)" )
47
+ self .axisYleft .setTitleText ("Percent(%)" )
48
+ self .axisYright .setTitleText ("Percent(%)" )
49
+
50
+ # set series properties
51
+ colorRed = QPen (Qt .red )
52
+ colorBlue = QPen (Qt .blue )
53
+ colorRed .setWidth (3 )
54
+ colorBlue .setWidth (3 )
55
+ self .seriesMem .setPen (colorRed )
56
+ self .seriesSwp .setPen (colorBlue )
57
+ self .seriesMem .append (self .x , self .yMem .percent )
58
+ self .seriesSwp .append (self .x , self .ySwp .percent )
59
+
60
+ # set axes properties
61
+ self .addAxis (self .axisX , Qt .AlignBottom )
62
+ self .addAxis (self .axisYleft , Qt .AlignLeft )
63
+ self .addAxis (self .axisYright , Qt .AlignRight )
64
+
65
+ self .axisX .setTickCount (self .xRange + 1 )
66
+ self .axisYleft .setTickCount (11 )
67
+ self .axisYright .setTickCount (11 )
68
+
69
+ self .axisX .setRange (- self .xRange , 0 )
70
+ self .axisYleft .setRange (0 , 100 )
71
+ self .axisYright .setRange (0 , 100 )
72
+
73
+ # attach axes to the graph
74
+ self .addSeries (self .seriesMem )
75
+ self .addSeries (self .seriesSwp )
76
+ self .seriesMem .attachAxis (self .axisX )
77
+ self .seriesMem .attachAxis (self .axisYleft )
78
+ self .seriesMem .attachAxis (self .axisYright )
79
+ self .seriesSwp .attachAxis (self .axisX )
80
+ self .seriesSwp .attachAxis (self .axisYleft )
81
+ self .seriesSwp .attachAxis (self .axisYright )
82
+
83
+ # start timing
84
+ self .timer .start ()
85
+
86
+ def handleTimeout (self ):
87
+ # one step forward
88
+ self .x += self .step
89
+ self .yMem = psutil .virtual_memory ()
90
+ self .ySwp = psutil .swap_memory ()
91
+ self .seriesMem .setName ("Memory: %.2f/%.2fGB (%.2f%%)"
92
+ % (self .yMem .used / self .GBCONV , self .memoryTotal , self .yMem .percent ))
93
+ self .seriesSwp .setName ("Swap: %.2f/%.2fGB (%.2f%%)"
94
+ % (self .ySwp .used / self .GBCONV , self .swapTotal , self .ySwp .percent ))
95
+ self .seriesMem .append (self .x , self .yMem .percent )
96
+ self .seriesSwp .append (self .x , self .ySwp .percent )
97
+ # move the line to the left for one step
98
+ self .scroll (self .plotArea ().width () / (self .axisX .tickCount () - 1 ), 0 )
0 commit comments