-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graph): handle dynamic interfaces and right convert
thanks to @dfaust work at https://github.com/dfaust/plasma-applet-netspeed-widget fix #23
- Loading branch information
Showing
6 changed files
with
176 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// .pragma library | ||
|
||
/** | ||
* Command retrieve all interface speed with following format (one line per interface and rx/tx in bytes) : | ||
* - `<ifname>,<rx>,<tx>` | ||
*/ | ||
const NET_DATA_SOURCE = | ||
"awk -v OFS=, 'NR > 2 { print substr($1, 1, length($1)-1), $2, $10 }' /proc/net/dev"; | ||
|
||
/** | ||
* @typedef {Object.<string, [number, number]>} TransferData The transfer data with format `{"<ifname>": [rx, tx]}` | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} data The raw transfer data output by {@link NET_DATA_SOURCE} | ||
* @returns {TransferData} The parsed transfer data | ||
*/ | ||
function parseTransferData(data) { | ||
const transferData = {}; | ||
for (const line of data.trim("\n").split("\n")) { | ||
const [name, rx, tx] = line.split(","); | ||
// Skip loopback interface | ||
if (name === "lo") { | ||
continue; | ||
} | ||
transferData[name] = [rx, tx]; | ||
} | ||
return transferData; | ||
} | ||
|
||
/** | ||
* Calculate speed data in kilobytes for {@link duration} | ||
* @param {TransferData} prevTransferData The transfer data at X moment (in bytes) | ||
* @param {TransferData} nextTransferData The transfer data at X+{@link duration} moment (in bytes) | ||
* @param {number} duration The duration elapsed between {@link prevTransferData} and {@link nextTransferData} | ||
* @returns {TransferData} The speed data for {@link duration}, returned in kilobytes | ||
*/ | ||
function calcSpeedData(prevTransferData, nextTransferData, duration) { | ||
const speedData = {}; | ||
for (const key in nextTransferData) { | ||
if (prevTransferData && key in prevTransferData) { | ||
const prev = prevTransferData[key]; | ||
const next = nextTransferData[key]; | ||
speedData[key] = [ | ||
((next[0] - prev[0]) * 1000) / duration, | ||
((next[1] - prev[1]) * 1000) / duration, | ||
]; | ||
} | ||
} | ||
return speedData; | ||
} |
41 changes: 0 additions & 41 deletions
41
package/contents/ui/components/NetworkInterfaceDetector.qml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import QtQuick | ||
import org.kde.plasma.plasma5support as Plasma5Support | ||
import "../../code/network.js" as NetworkUtils | ||
|
||
/** | ||
* SRCs: | ||
* - https://invent.kde.org/plasma/plasma5support/-/blob/master/src/declarativeimports/datasource.h | ||
* - https://invent.kde.org/plasma/plasma-workspace/-/blob/master/dataengines/executable/executable.h | ||
* - https://github.com/dfaust/plasma-applet-netspeed-widget | ||
*/ | ||
Plasma5Support.DataSource { | ||
id: root | ||
engine: 'executable' | ||
|
||
// Format: {"interface":[tx,rx]} (in kilobytes) | ||
property var value: { | ||
} | ||
|
||
// Cache for calculate | ||
property real _previousTs: 0 | ||
property var _transferData: { | ||
} // Format: {"interface":[tx,rx]} (in bytes) | ||
|
||
// Retrieve data | ||
onNewData: (sourceName, data) => { | ||
// run just once (reconnected when update) | ||
connectedSources.length = 0; | ||
if (data['exit code'] > 0) { | ||
print(data.stderr); | ||
} else { | ||
const now = Date.now(); | ||
const nextTransferData = NetworkUtils.parseTransferData(data.stdout); | ||
// Skip calculate if is first run | ||
if (root._previousTs !== 0) { | ||
const duration = now - root._previousTs; | ||
value = NetworkUtils.calcSpeedData(root._transferData, nextTransferData, duration); | ||
// root.valueChanged(); | ||
} | ||
root._transferData = nextTransferData; | ||
root._previousTs = now; | ||
} | ||
} | ||
|
||
function execute() { | ||
root.connectSource(NetworkUtils.NET_DATA_SOURCE); | ||
} | ||
} |
Oops, something went wrong.
ea00f7a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍