Skip to content

Commit b900c58

Browse files
committed
feat: add network monitor
This is from kotelnik/plasma-applet-resources-monitor#13
1 parent 5238bc2 commit b900c58

File tree

9 files changed

+203
-37
lines changed

9 files changed

+203
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# plasma-applet-resources-monitor (fork)
22

3-
Plasmoid for monitoring CPU and RAM.
3+
Plasmoid for monitoring CPU, memory and network traffic
44

55
## Why make an fork ?
66

package/contents/config/config.qml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import QtQuick 2.2
22
import org.kde.plasma.configuration 2.0
33

44
ConfigModel {
5-
ConfigCategory {
6-
name: i18n('General')
7-
icon: 'preferences-system-windows'
8-
source: 'config/ConfigGeneral.qml'
9-
}
5+
ConfigCategory {
6+
name: i18n('General')
7+
icon: 'preferences-system-windows'
8+
source: 'config/ConfigGeneral.qml'
9+
}
10+
ConfigCategory {
11+
name: i18n('Data')
12+
icon: 'preferences-system-settings'
13+
source: 'config/ConfigData.qml'
14+
}
1015
}

package/contents/config/main.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<entry name="showClock" type="Bool">
2323
<default>true</default>
2424
</entry>
25+
<entry name="showNetMonitor" type="Bool">
26+
<default>false</default>
27+
</entry>
2528

2629
<entry name="verticalLayout" type="Bool">
2730
<default>false</default>
@@ -35,6 +38,18 @@
3538
<entry name="fontScale" type="int">
3639
<default>26</default>
3740
</entry>
41+
42+
<group name="Data">
43+
<entry name="networkSensorInterface" type="String">
44+
<default>enp3s0</default>
45+
</entry>
46+
<entry name="downloadMaxKBs" type="Double">
47+
<default></default>
48+
</entry>
49+
<entry name="uploadMaxKBs" type="Double">
50+
<default></default>
51+
</entry>
52+
</group>
3853
</group>
3954

4055
</kcfg>

package/contents/ui/components/GraphItem.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Item {
102102
anchors.verticalCenter: parent.verticalCenter
103103
anchors.right: parent.right
104104
font.pointSize: -1
105-
visible: secondLineValue != ''
105+
visible: secondLineInfoText != ''
106106
}
107107
}
108108

package/contents/ui/components/functions.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ function getHumanReadableClock(clockMhz) {
2929
return Math.round(clockNumber * floatingPointCount) / floatingPointCount + 'GHz'
3030
}
3131

32+
/**
33+
* Convert rate kilo bytes in human readable value
34+
* @param {number} rateKiBs the rate kilo bytes
35+
* @returns the human readable kilo bites
36+
*/
37+
function getHumanReadableNetRate(rateKiBs) {
38+
if (rateKiBs <= 1024) {
39+
return rateKiBs + 'KB/s'
40+
}
41+
else if(rateKiBs <= 1048576) {
42+
return Math.round((rateKiBs / 1024) * 100) / 100 + 'MB/s'
43+
}
44+
return Math.round((rateKiBs / 1048576) * 100) / 100 + 'GB/s'
45+
}
46+
3247
/**
3348
* Add data in graph
3449
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import QtQuick 2.2
2+
import QtQuick.Controls 1.3
3+
import QtQuick.Layouts 1.1
4+
5+
Item {
6+
7+
property alias cfg_networkSensorInterface: networkSensorInterface.text
8+
property alias cfg_downloadMaxKBs: downloadMaxKBs.value
9+
property alias cfg_uploadMaxKBs: uploadMaxKBs.value
10+
11+
GridLayout {
12+
Layout.fillWidth: true
13+
columns: 2
14+
15+
Label {
16+
text: i18n('Network sensor interface')
17+
Layout.alignment: Qt.AlignRight
18+
}
19+
TextField {
20+
id: networkSensorInterface
21+
placeholderText: 'enp3s0'
22+
Layout.preferredWidth: 500
23+
onTextChanged: cfg_networkSensorInterface = text
24+
}
25+
26+
Label {
27+
text: i18n('Max download speed:')
28+
Layout.alignment: Qt.AlignRight
29+
}
30+
SpinBox {
31+
id: downloadMaxKBs
32+
decimals: 0
33+
stepSize: 10
34+
minimumValue: 10
35+
maximumValue: 100000
36+
value: cfg_downloadMaxKBs
37+
suffix: i18nc('Abbreviation for KB/s', ' KB/s')
38+
}
39+
40+
Label {
41+
text: i18n('Max upload speed:')
42+
Layout.alignment: Qt.AlignRight
43+
}
44+
SpinBox {
45+
id: uploadMaxKBs
46+
decimals: 0
47+
stepSize: 10
48+
minimumValue: 10
49+
maximumValue: 100000
50+
value: cfg_uploadMaxKBs
51+
suffix: i18nc('Abbreviation for KB/s', ' KB/s')
52+
}
53+
}
54+
55+
}

package/contents/ui/config/ConfigGeneral.qml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Item {
1010
property alias cfg_showClock: showClock.checked
1111
property alias cfg_showRamMonitor: showRamMonitor.checked
1212
property alias cfg_memoryInPercent: memoryInPercent.checked
13+
property alias cfg_showNetMonitor: showNetMonitor.checked
1314

1415
property alias cfg_verticalLayout: verticalLayout.checked
1516
property alias cfg_enableHints: enableHints.checked
@@ -53,29 +54,35 @@ Item {
5354

5455
CheckBox {
5556
id: showCpuMonitor
56-
Layout.columnSpan: 2
57+
Layout.columnSpan: 1
5758
text: i18n('Show CPU monitor')
5859
}
5960

6061
CheckBox {
6162
id: showClock
62-
Layout.columnSpan: 2
63+
Layout.columnSpan: 1
6364
text: i18n('Show clock')
6465
enabled: showCpuMonitor.checked
6566
}
6667

6768
CheckBox {
6869
id: showRamMonitor
69-
Layout.columnSpan: 2
70+
Layout.columnSpan: 1
7071
text: i18n('Show RAM monitor')
7172
}
7273

7374
CheckBox {
7475
id: memoryInPercent
75-
Layout.columnSpan: 2
76+
Layout.columnSpan: 1
7677
text: i18n('Memory in percentage')
7778
}
7879

80+
CheckBox {
81+
id: showNetMonitor
82+
Layout.columnSpan: 1
83+
text: i18n('Show network monitor')
84+
}
85+
7986
// Layout
8087

8188
Item {

package/contents/ui/main.qml

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@ Item {
3737
property bool showRamMonitor: plasmoid.configuration.showRamMonitor
3838
property bool memoryInPercent: plasmoid.configuration.memoryInPercent
3939
property bool showMemoryInPercent: memoryInPercent
40+
property bool showNetMonitor: plasmoid.configuration.showNetMonitor
4041
property double fontScale: (plasmoid.configuration.fontScale / 100)
42+
property int downloadMaxKBs: plasmoid.configuration.downloadMaxKBs
43+
property int uploadMaxKBs: plasmoid.configuration.uploadMaxKBs
4144

4245
property color warningColor: theme.neutralTextColor
4346
property int graphGranularity: 20
4447

4548
// Component properties
49+
property int containerCount: (showCpuMonitor?1:0) + (showRamMonitor?1:0) + (showNetMonitor?1:0)
4650
property int itemMargin: 5
4751
property double parentWidth: parent === null ? 0 : parent.width
4852
property double parentHeight: parent === null ? 0 : parent.height
4953
property double itemWidth: vertical ? ( verticalLayout ? parentWidth : (parentWidth - itemMargin) / 2 ) : ( verticalLayout ? (parentHeight - itemMargin) / 2 : parentHeight )
5054
property double itemHeight: itemWidth
5155
property double fontPixelSize: itemHeight * fontScale
52-
property double widgetWidth: showCpuMonitor && showRamMonitor && !verticalLayout ? itemWidth*2 + itemMargin : itemWidth
53-
property double widgetHeight: showCpuMonitor && showRamMonitor && verticalLayout ? itemWidth*2 + itemMargin : itemWidth
56+
property double widgetWidth: !verticalLayout ? (itemWidth*containerCount + itemMargin*(containerCount)*2) : itemWidth
57+
property double widgetHeight: verticalLayout ? (itemWidth*containerCount + itemMargin*(containerCount)*2) : itemWidth
5458

5559
Layout.preferredWidth: widgetWidth
5660
Layout.maximumWidth: widgetWidth
@@ -66,15 +70,12 @@ Item {
6670
}
6771

6872
onFontPixelSizeChanged: {
69-
cpuMonitor.firstLineInfoLabel.font.pixelSize = fontPixelSize
70-
cpuMonitor.firstLineValueLabel.font.pixelSize = fontPixelSize
71-
cpuMonitor.secondLineInfoLabel.font.pixelSize = fontPixelSize
72-
cpuMonitor.secondLineValueLabel.font.pixelSize = fontPixelSize
73-
74-
ramMonitor.firstLineInfoLabel.font.pixelSize = fontPixelSize
75-
ramMonitor.firstLineValueLabel.font.pixelSize = fontPixelSize
76-
ramMonitor.secondLineInfoLabel.font.pixelSize = fontPixelSize
77-
ramMonitor.secondLineValueLabel.font.pixelSize = fontPixelSize
73+
for(var monitor of [cpuMonitor, ramMonitor, netMonitor]) {
74+
monitor.firstLineInfoLabel.font.pixelSize = fontPixelSize
75+
monitor.firstLineValueLabel.font.pixelSize = fontPixelSize
76+
monitor.secondLineInfoLabel.font.pixelSize = fontPixelSize
77+
monitor.secondLineValueLabel.font.pixelSize = fontPixelSize
78+
}
7879
}
7980

8081
// Graph data
@@ -93,15 +94,22 @@ Item {
9394
property string swap: "mem/swap/"
9495
property string swapUsed: swap + "used"
9596
property string swapFree: swap + "free"
97+
property string networkInterface: "network/interfaces/" + plasmoid.configuration.networkSensorInterface + "/"
98+
property string downloadTotal: networkInterface + "receiver/data"
99+
property string uploadTotal: networkInterface + "transmitter/data"
96100

97101
property double totalCpuLoad: .0
98102
property int averageCpuClock: 0
99103
property int ramUsedBytes: 0
100104
property double ramUsedProportion: 0
101105
property int swapUsedBytes: 0
102106
property double swapUsedProportion: 0
107+
property double downloadKBs: 0
108+
property double uploadKBs: 0
109+
property double downloadProportion: 0
110+
property double uploadProportion: 0
103111

104-
connectedSources: [memFree, memUsed, memApplication, swapUsed, swapFree, averageClock, totalLoad ]
112+
connectedSources: [memFree, memUsed, memApplication, swapUsed, swapFree, averageClock, totalLoad, downloadTotal, uploadTotal ]
105113

106114
onNewData: {
107115
if (data.value === undefined) {
@@ -122,6 +130,14 @@ Item {
122130
averageCpuClock = parseInt(data.value)
123131
allUsageProportionChanged()
124132
}
133+
else if (sourceName == downloadTotal) {
134+
downloadKBs = parseFloat(data.value)
135+
downloadProportion = fitDownloadRate(data.value)
136+
}
137+
else if (sourceName == uploadTotal) {
138+
uploadKBs = parseFloat(data.value)
139+
uploadProportion = fitUploadRate(data.value)
140+
}
125141
}
126142
interval: 1000 * plasmoid.configuration.updateInterval
127143
}
@@ -144,6 +160,20 @@ Item {
144160
return (usage / (parseFloat(usage) + parseFloat(swapFree.value)))
145161
}
146162

163+
function fitDownloadRate(rate) {
164+
if (!downloadMaxKBs) {
165+
return 0
166+
}
167+
return (rate / downloadMaxKBs)
168+
}
169+
170+
function fitUploadRate(rate) {
171+
if (!uploadMaxKBs) {
172+
return 0
173+
}
174+
return (rate / uploadMaxKBs)
175+
}
176+
147177
ListModel {
148178
id: cpuGraphModel
149179
}
@@ -156,27 +186,48 @@ Item {
156186
id: swapGraphModel
157187
}
158188

159-
function allUsageProportionChanged() {
160-
var totalCpuProportion = dataSource.totalCpuLoad
161-
var totalRamProportion = dataSource.ramUsedProportion
162-
var totalSwapProportion = dataSource.swapUsedProportion
163-
164-
cpuMonitor.firstLineValueLabel.text = Math.round(totalCpuProportion * 100) + '%'
165-
cpuMonitor.firstLineValueLabel.color = totalCpuProportion > 0.9 ? warningColor : theme.textColor
166-
cpuMonitor.secondLineValueLabel.text = Functions.getHumanReadableClock(dataSource.averageCpuClock)
189+
ListModel {
190+
id: uploadGraphModel
191+
}
167192

168-
ramMonitor.firstLineValueLabel.text = showMemoryInPercent ? Math.round(totalRamProportion * 100) + '%' : Functions.getHumanReadableMemory(dataSource.ramUsedBytes)
169-
ramMonitor.firstLineValueLabel.color = totalRamProportion > 0.9 ? warningColor : theme.textColor
170-
ramMonitor.secondLineValueLabel.text = showMemoryInPercent ? Math.round(totalSwapProportion * 100) + '%' : Functions.getHumanReadableMemory(dataSource.swapUsedBytes)
171-
ramMonitor.secondLineValueLabel.color = totalSwapProportion > 0.9 ? warningColor : theme.textColor
172-
ramMonitor.secondLineValueLabel.visible = !ramMonitor.secondLineInfoLabel.visible && totalSwapProportion > 0
193+
ListModel {
194+
id: downloadGraphModel
195+
}
173196

197+
function allUsageProportionChanged() {
174198
if (showCpuMonitor) {
199+
var totalCpuProportion = dataSource.totalCpuLoad
200+
201+
cpuMonitor.firstLineValueLabel.text = Math.round(totalCpuProportion * 100) + '%'
202+
cpuMonitor.firstLineValueLabel.color = totalCpuProportion > 0.9 ? warningColor : theme.textColor
203+
cpuMonitor.secondLineValueLabel.text = Functions.getHumanReadableClock(dataSource.averageCpuClock)
204+
175205
Functions.addGraphData(cpuGraphModel, totalCpuProportion, graphGranularity)
176206
}
207+
177208
if (showRamMonitor) {
209+
var totalRamProportion = dataSource.ramUsedProportion
210+
var totalSwapProportion = dataSource.swapUsedProportion
211+
178212
Functions.addGraphData(ramGraphModel, totalRamProportion, graphGranularity)
179213
Functions.addGraphData(swapGraphModel, totalSwapProportion, graphGranularity)
214+
215+
ramMonitor.firstLineValueLabel.text = showMemoryInPercent ? Math.round(totalRamProportion * 100) + '%' : Functions.getHumanReadableMemory(dataSource.ramUsedBytes)
216+
ramMonitor.firstLineValueLabel.color = totalRamProportion > 0.9 ? warningColor : theme.textColor
217+
ramMonitor.secondLineValueLabel.text = showMemoryInPercent ? Math.round(totalSwapProportion * 100) + '%' : Functions.getHumanReadableMemory(dataSource.swapUsedBytes)
218+
ramMonitor.secondLineValueLabel.color = totalSwapProportion > 0.9 ? warningColor : theme.textColor
219+
ramMonitor.secondLineValueLabel.visible = !ramMonitor.secondLineInfoLabel.visible && totalSwapProportion > 0
220+
}
221+
222+
if (showNetMonitor) {
223+
var totalDownloadProportion = dataSource.downloadProportion
224+
var totalUploadProportion = dataSource.uploadProportion
225+
226+
Functions.addGraphData(uploadGraphModel, totalUploadProportion, graphGranularity)
227+
Functions.addGraphData(downloadGraphModel, totalDownloadProportion, graphGranularity)
228+
229+
netMonitor.firstLineValueLabel.text = Functions.getHumanReadableNetRate(dataSource.downloadKBs)
230+
netMonitor.secondLineValueLabel.text = Functions.getHumanReadableNetRate(dataSource.uploadKBs)
180231
}
181232
}
182233

@@ -218,6 +269,24 @@ Item {
218269
secondGraphBarColor: theme.negativeTextColor
219270
}
220271

272+
GraphItem {
273+
id: netMonitor
274+
width: itemWidth
275+
height: itemHeight
276+
anchors.left: parent.left
277+
anchors.leftMargin: (showCpuMonitor && !verticalLayout ? itemWidth + itemMargin: 0) + (showRamMonitor && !verticalLayout ? itemWidth + itemMargin : 0)
278+
anchors.top: parent.top
279+
anchors.topMargin: (showCpuMonitor && verticalLayout ? itemWidth + itemMargin: 0) + (showRamMonitor && verticalLayout ? itemWidth + itemMargin : 0)
280+
281+
visible: showNetMonitor
282+
firstLineInfoText: 'Down'
283+
secondLineInfoText: 'Up'
284+
secondLineInfoTextColor: theme.positiveTextColor
285+
firstGraphModel: downloadGraphModel
286+
secondGraphModel: uploadGraphModel
287+
secondGraphBarColor: theme.positiveTextColor
288+
}
289+
221290
// Click action
222291
MouseArea {
223292
id: mouseArea

package/metadata.desktop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Encoding=UTF-8
33
Name=Resources Monitor (fork)
44
Name[fr]=Moniteur des ressources (fork)
5-
Comment=Plasmoid for monitoring CPU and memory. This is an fork from Resources Monitor by Kotelnik
5+
Comment=Plasmoid for monitoring CPU, memory and network traffic. This is an fork from Resources Monitor by Kotelnik
66
Type=Service
77
X-KDE-ParentApp=
88
X-KDE-PluginInfo-Author=Kotelnik,orblazer

0 commit comments

Comments
 (0)