[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
This commit is contained in:
parent
a579f32879
commit
cb04cf6de6
1 changed files with 7 additions and 1 deletions
|
@ -8,6 +8,7 @@ Parameters:
|
||||||
* traffic.showname: If set to False, hide network interface name (defaults to True)
|
* traffic.showname: If set to False, hide network interface name (defaults to True)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
import psutil
|
import psutil
|
||||||
import netifaces
|
import netifaces
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ class Module(bumblebee.engine.Module):
|
||||||
self._showname = bumblebee.util.asbool(self.parameter("showname", True))
|
self._showname = bumblebee.util.asbool(self.parameter("showname", True))
|
||||||
self._prev = {}
|
self._prev = {}
|
||||||
self._states = {}
|
self._states = {}
|
||||||
|
self._lastcheck = 0
|
||||||
self._states["include"] = []
|
self._states["include"] = []
|
||||||
self._states["exclude"] = []
|
self._states["exclude"] = []
|
||||||
for state in tuple(filter(len, self.parameter("states", "").split(","))):
|
for state in tuple(filter(len, self.parameter("states", "").split(","))):
|
||||||
|
@ -71,6 +73,10 @@ class Module(bumblebee.engine.Module):
|
||||||
del widgets[:]
|
del widgets[:]
|
||||||
|
|
||||||
counters = psutil.net_io_counters(pernic=True)
|
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:
|
for interface in interfaces:
|
||||||
if not interface: interface = "lo"
|
if not interface: interface = "lo"
|
||||||
state = "down"
|
state = "down"
|
||||||
|
@ -96,7 +102,7 @@ class Module(bumblebee.engine.Module):
|
||||||
name = "traffic.{}-{}".format(direction, interface)
|
name = "traffic.{}-{}".format(direction, interface)
|
||||||
widget = self.create_widget(widgets, name, attributes={"theme.minwidth": "1000.00MB"})
|
widget = self.create_widget(widgets, name, attributes={"theme.minwidth": "1000.00MB"})
|
||||||
prev = self._prev.get(name, 0)
|
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)
|
widget.full_text(speed)
|
||||||
self._prev[name] = data[direction]
|
self._prev[name] = data[direction]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue