From cb04cf6de6edcc0526e33bc5551b7fde6ff14a78 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 22 Oct 2018 18:34:58 +0200 Subject: [PATCH] [modules/traffic] Calculate speed per second instead of per interval Divide speed by timediff between current and previous measurement to get the rate per second. fixes #306 --- bumblebee/modules/traffic.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py index 218d64d..60255da 100644 --- a/bumblebee/modules/traffic.py +++ b/bumblebee/modules/traffic.py @@ -8,6 +8,7 @@ Parameters: * traffic.showname: If set to False, hide network interface name (defaults to True) """ +import time import psutil import netifaces @@ -26,6 +27,7 @@ class Module(bumblebee.engine.Module): self._showname = bumblebee.util.asbool(self.parameter("showname", True)) self._prev = {} self._states = {} + self._lastcheck = 0 self._states["include"] = [] self._states["exclude"] = [] for state in tuple(filter(len, self.parameter("states", "").split(","))): @@ -71,6 +73,10 @@ class Module(bumblebee.engine.Module): del widgets[:] counters = psutil.net_io_counters(pernic=True) + now = int(time.time()) + timediff = now - (self._lastcheck if self._lastcheck else now) + if timediff <= 0: timediff = 1 + self._lastcheck = now for interface in interfaces: if not interface: interface = "lo" state = "down" @@ -96,7 +102,7 @@ class Module(bumblebee.engine.Module): name = "traffic.{}-{}".format(direction, interface) widget = self.create_widget(widgets, name, attributes={"theme.minwidth": "1000.00MB"}) prev = self._prev.get(name, 0) - speed = bumblebee.util.bytefmt(int(data[direction]) - int(prev)) + speed = bumblebee.util.bytefmt((int(data[direction]) - int(prev))/timediff) widget.full_text(speed) self._prev[name] = data[direction]