diff options
Diffstat (limited to 'widgets/network.lua')
| -rw-r--r-- | widgets/network.lua | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/widgets/network.lua b/widgets/network.lua index 3a11521..20b0573 100644 --- a/widgets/network.lua +++ b/widgets/network.lua @@ -37,6 +37,11 @@ local rx_hist, tx_hist = {}, {} local hist_len = 0 local lan_addr = nil +-- Sampled every ~2s whatever the draw rate, so the chart keeps one point per +-- sample rather than per draw. 1.9 not 2: draws jitter by a few ms. +local gate = data.new_gate(1.9) +local down, up = nil, nil + -- Append to a fixed-length history, dropping the oldest. A plain array with -- table.remove(1) rather than a circular buffer with an index: the lengths -- here are a few hundred at most and the draw is every two seconds, so the @@ -73,8 +78,11 @@ function M.draw(cr, rect, colors) rx_counter = rx_counter or data.new_rate_counter(interval) tx_counter = tx_counter or data.new_rate_counter(interval) - local down = rx_counter:sample(read_counter('rx_bytes')) - local up = tx_counter:sample(read_counter('tx_bytes')) + local sampled, now = gate() + if sampled then + down = rx_counter:sample(read_counter('rx_bytes'), now) + up = tx_counter:sample(read_counter('tx_bytes'), now) + end -- Fluid type, as every other card does: one base size S drives everything, -- taken as the smaller of a height budget and a measured width fit. @@ -140,8 +148,8 @@ function M.draw(cr, rect, colors) while #rx_hist > hist_len do table.remove(rx_hist, 1) end while #tx_hist > hist_len do table.remove(tx_hist, 1) end end - if down then push(rx_hist, down, hist_len) end - if up then push(tx_hist, up, hist_len) end + if sampled and down then push(rx_hist, down, hist_len) end + if sampled and up then push(tx_hist, up, hist_len) end -- One ceiling for both series. Scaled independently they would lie about -- their relative size, which is the entire reason to draw them together. |
