From c4d2fb4ea214171acea38eb832bb15d7d2ec5015 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sun, 18 Jul 2021 01:31:51 -0400 Subject: [PATCH] REF simplify readwrite and network code --- drawing/Network.lua | 31 +++++++++++++++++++------------ drawing/ReadWrite.lua | 35 ++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/drawing/Network.lua b/drawing/Network.lua index 5c13b98..d343e95 100644 --- a/drawing/Network.lua +++ b/drawing/Network.lua @@ -84,24 +84,31 @@ return function(update_freq) return rx, tx end + local compute_speed = function(x0, x1) + -- mask overflow + if x1 > x0 then + return (x1 - x0) * update_freq + else + return 0 + end + end + local prev_rx_bits, prev_tx_bits = read_interfaces() local update = function(cr) - local dspeed, uspeed = 0, 0 local rx_bits, tx_bits = read_interfaces() - - -- mask overflow - if rx_bits > prev_rx_bits then - dspeed = (rx_bits - prev_rx_bits) * update_freq - end - if tx_bits > prev_tx_bits then - uspeed = (tx_bits - prev_tx_bits) * update_freq - end + Common.annotated_scale_plot_set( + dnload, + cr, + compute_speed(prev_rx_bits, rx_bits) + ) + Common.annotated_scale_plot_set( + upload, + cr, + compute_speed(prev_tx_bits, tx_bits) + ) prev_rx_bits = rx_bits prev_tx_bits = tx_bits - - Common.annotated_scale_plot_set(dnload, cr, dspeed) - Common.annotated_scale_plot_set(upload, cr, uspeed) end ----------------------------------------------------------------------------- diff --git a/drawing/ReadWrite.lua b/drawing/ReadWrite.lua index 7814096..6496f55 100644 --- a/drawing/ReadWrite.lua +++ b/drawing/ReadWrite.lua @@ -85,26 +85,31 @@ return function(update_freq) return read_bytes * BLOCK_SIZE_BYTES, write_bytes * BLOCK_SIZE_BYTES end - reads.byte_cnt = 0 - writes.byte_cnt = 0 - reads.prev_byte_cnt, writes.prev_byte_cnt = read_devices() + local prev_read_bytes, prev_write_bytes = read_devices() - local update_stat = function(cr, stat, byte_cnt) - local delta_bytes = byte_cnt - stat.prev_byte_cnt - stat.prev_byte_cnt = byte_cnt - - local plot_value = 0 - if delta_bytes > 0 then - local bps = delta_bytes * update_freq - plot_value = bps + local compute_rate = function(x0, x1) + -- mask overflow + if x1 > x0 then + return (x1 - x0) * update_freq + else + return 0 end - Common.annotated_scale_plot_set(stat, cr, plot_value) end local update = function(cr) - local read_byte_cnt, write_byte_cnt = read_devices() - update_stat(cr, reads, read_byte_cnt) - update_stat(cr, writes, write_byte_cnt) + local read_bytes, write_bytes = read_devices() + Common.annotated_scale_plot_set( + reads, + cr, + compute_rate(prev_read_bytes, read_bytes) + ) + Common.annotated_scale_plot_set( + writes, + cr, + compute_rate(prev_write_bytes, write_bytes) + ) + prev_read_bytes = read_bytes + prev_write_bytes = write_bytes end -----------------------------------------------------------------------------