REF make readwrite and network prettier

This commit is contained in:
Nathan Dwarshuis 2021-07-18 00:03:44 -04:00
parent c9d1c7f939
commit 81b6a6b2aa
2 changed files with 121 additions and 103 deletions

View File

@ -2,34 +2,16 @@ local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
local __string_gmatch = string.gmatch
local __math_floor = math.floor
local _PLOT_SEC_BREAK_ = 20
local _PLOT_HEIGHT_ = 56
local network_label_function = function(bits)
local new_unit, new_value = Util.convert_data_val(bits)
return __math_floor(new_value)..' '..new_unit..'b/s'
end
local value_format_function = function(bits)
local unit, value = Util.convert_data_val(bits)
return Util.precision_round_to_string(value, 3)..' '..unit..'b/s'
end
local interface_counters_tbl = {}
local get_bits = function(path)
return Util.read_file(path, nil, '*n') * 8
end
-- _PLOT_SEC_BREAK_ = nil
-- _PLOT_HEIGHT_ = nil
return function(update_freq)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local __string_gmatch = string.gmatch
local __math_floor = math.floor
-----------------------------------------------------------------------------
-- header
local header = Common.Header(
Geometry.CENTER_RIGHT_X,
Geometry.TOP_Y,
@ -37,33 +19,54 @@ return function(update_freq)
'NETWORK'
)
local dnload = Common.initLabeledScalePlot(
Geometry.CENTER_RIGHT_X,
header.bottom_y,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
value_format_function,
network_label_function,
_PLOT_SEC_BREAK_,
'Download',
2,
update_freq
-----------------------------------------------------------------------------
-- download plot
local network_label_function = function(bits)
local new_unit, new_value = Util.convert_data_val(bits)
return __math_floor(new_value)..' '..new_unit..'b/s'
end
local value_format_function = function(bits)
local unit, value = Util.convert_data_val(bits)
return Util.precision_round_to_string(value, 3)..' '..unit..'b/s'
end
local build_plot = function(y, label)
return Common.initLabeledScalePlot(
Geometry.CENTER_RIGHT_X,
y,
Geometry.SECTION_WIDTH,
PLOT_HEIGHT,
value_format_function,
network_label_function,
PLOT_SEC_BREAK,
label,
2,
update_freq
)
end
local dnload = build_plot(header.bottom_y, 'Download')
-----------------------------------------------------------------------------
-- upload plot
local upload = build_plot(
header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
'Upload'
)
local upload = Common.initLabeledScalePlot(
Geometry.CENTER_RIGHT_X,
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
value_format_function,
network_label_function,
_PLOT_SEC_BREAK_,
'Upload',
2,
update_freq
)
-----------------------------------------------------------------------------
-- update function
local _update = function(cr)
local get_bits = function(path)
return Util.read_file(path, nil, '*n') * 8
end
local interface_counters_tbl = {}
local update = function(cr)
local dspeed, uspeed = 0, 0
local rx_delta, tx_delta
@ -106,6 +109,9 @@ return function(update_freq)
Common.annotated_scale_plot_set(upload, cr, uspeed)
end
-----------------------------------------------------------------------------
-- main drawing functions
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(dnload, cr)
@ -113,7 +119,7 @@ return function(update_freq)
end
local draw_dynamic = function(cr)
_update(cr)
update(cr)
Common.annotated_scale_plot_draw_dynamic(dnload, cr)
Common.annotated_scale_plot_draw_dynamic(upload, cr)
end

View File

@ -2,35 +2,17 @@ local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
local __tonumber = tonumber
local __string_match = string.match
local __math_floor = math.floor
local _PLOT_SEC_BREAK_ = 20
local _PLOT_HEIGHT_ = 56
local BLOCK_SIZE_BYTES = 512
local STAT_FILE = '/sys/block/sda/stat'
-- fields 3 and 7 (sectors read and written)
local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
local read_stat_file = function()
local bytes_r, bytes_w = __string_match(Util.read_file(STAT_FILE), RW_REGEX)
return __tonumber(bytes_r) * BLOCK_SIZE_BYTES, __tonumber(bytes_w) * BLOCK_SIZE_BYTES
end
local io_label_function = function(bytes)
local new_unit, new_value = Util.convert_data_val(bytes)
return __math_floor(new_value)..' '..new_unit..'B/s'
end
local format_value_function = function(bps)
local unit, value = Util.convert_data_val(bps)
return Util.precision_round_to_string(value, 3)..' '..unit..'B/s'
end
return function(update_freq)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local __tonumber = tonumber
local __string_match = string.match
local __math_floor = math.floor
-----------------------------------------------------------------------------
-- header
local header = Common.Header(
Geometry.CENTER_LEFT_X,
Geometry.TOP_Y,
@ -38,31 +20,58 @@ return function(update_freq)
'INPUT / OUTPUT'
)
local reads = Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
header.bottom_y,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
format_value_function,
io_label_function,
_PLOT_SEC_BREAK_,
'Reads',
2,
update_freq
-----------------------------------------------------------------------------
-- reads
local io_label_function = function(bytes)
local new_unit, new_value = Util.convert_data_val(bytes)
return __math_floor(new_value)..' '..new_unit..'B/s'
end
local format_value_function = function(bps)
local unit, value = Util.convert_data_val(bps)
return Util.precision_round_to_string(value, 3)..' '..unit..'B/s'
end
local build_plot = function(y, label)
return Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
y,
Geometry.SECTION_WIDTH,
PLOT_HEIGHT,
format_value_function,
io_label_function,
PLOT_SEC_BREAK,
label,
2,
update_freq
)
end
local reads = build_plot(header.bottom_y, 'Reads')
-----------------------------------------------------------------------------
-- writes
local writes = build_plot(
header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
'Writes'
)
local writes = Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
format_value_function,
io_label_function,
_PLOT_SEC_BREAK_,
'Writes',
2,
update_freq
)
-----------------------------------------------------------------------------
-- update function
-- TODO add more devices to this
local BLOCK_SIZE_BYTES = 512
local STAT_FILE = '/sys/block/sda/stat'
-- fields 3 and 7 (sectors read and written)
local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
local read_stat_file = function()
local bytes_r, bytes_w = __string_match(Util.read_file(STAT_FILE), RW_REGEX)
return __tonumber(bytes_r) * BLOCK_SIZE_BYTES, __tonumber(bytes_w) * BLOCK_SIZE_BYTES
end
reads.byte_cnt = 0
writes.byte_cnt = 0
@ -86,6 +95,9 @@ return function(update_freq)
update_stat(cr, writes, write_byte_cnt)
end
-----------------------------------------------------------------------------
-- main drawing functions
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(reads, cr)