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,11 +2,25 @@ local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
return function(update_freq)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local __string_gmatch = string.gmatch
local __math_floor = math.floor
local _PLOT_SEC_BREAK_ = 20
local _PLOT_HEIGHT_ = 56
-----------------------------------------------------------------------------
-- header
local header = Common.Header(
Geometry.CENTER_RIGHT_X,
Geometry.TOP_Y,
Geometry.SECTION_WIDTH,
'NETWORK'
)
-----------------------------------------------------------------------------
-- download plot
local network_label_function = function(bits)
local new_unit, new_value = Util.convert_data_val(bits)
@ -18,52 +32,41 @@ local value_format_function = function(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')
local interface_counters_tbl = {}
-----------------------------------------------------------------------------
-- upload plot
local upload = build_plot(
header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
'Upload'
)
-----------------------------------------------------------------------------
-- update function
local get_bits = function(path)
return Util.read_file(path, nil, '*n') * 8
end
-- _PLOT_SEC_BREAK_ = nil
-- _PLOT_HEIGHT_ = nil
local interface_counters_tbl = {}
return function(update_freq)
local header = Common.Header(
Geometry.CENTER_RIGHT_X,
Geometry.TOP_Y,
Geometry.SECTION_WIDTH,
'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
)
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
)
local _update = function(cr)
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,23 +2,26 @@ local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
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
local _PLOT_SEC_BREAK_ = 20
local _PLOT_HEIGHT_ = 56
-----------------------------------------------------------------------------
-- header
local BLOCK_SIZE_BYTES = 512
local STAT_FILE = '/sys/block/sda/stat'
local header = Common.Header(
Geometry.CENTER_LEFT_X,
Geometry.TOP_Y,
Geometry.SECTION_WIDTH,
'INPUT / OUTPUT'
)
-- 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
local io_label_function = function(bytes)
local new_unit, new_value = Util.convert_data_val(bytes)
@ -30,40 +33,46 @@ local format_value_function = function(bps)
return Util.precision_round_to_string(value, 3)..' '..unit..'B/s'
end
return function(update_freq)
local header = Common.Header(
local build_plot = function(y, label)
return Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
Geometry.TOP_Y,
y,
Geometry.SECTION_WIDTH,
'INPUT / OUTPUT'
)
local reads = Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
header.bottom_y,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
PLOT_HEIGHT,
format_value_function,
io_label_function,
_PLOT_SEC_BREAK_,
'Reads',
PLOT_SEC_BREAK,
label,
2,
update_freq
)
end
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
local reads = build_plot(header.bottom_y, 'Reads')
-----------------------------------------------------------------------------
-- writes
local writes = build_plot(
header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
'Writes'
)
-----------------------------------------------------------------------------
-- 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
reads.prev_byte_cnt, writes.prev_byte_cnt = read_stat_file()
@ -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)