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 Common = require 'Common'
local Geometry = require 'Geometry' local Geometry = require 'Geometry'
return function(update_freq)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local __string_gmatch = string.gmatch local __string_gmatch = string.gmatch
local __math_floor = math.floor 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 network_label_function = function(bits)
local new_unit, new_value = Util.convert_data_val(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' return Util.precision_round_to_string(value, 3)..' '..unit..'b/s'
end 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) local get_bits = function(path)
return Util.read_file(path, nil, '*n') * 8 return Util.read_file(path, nil, '*n') * 8
end end
-- _PLOT_SEC_BREAK_ = nil local interface_counters_tbl = {}
-- _PLOT_HEIGHT_ = nil
return function(update_freq) local update = function(cr)
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 dspeed, uspeed = 0, 0 local dspeed, uspeed = 0, 0
local rx_delta, tx_delta local rx_delta, tx_delta
@ -106,6 +109,9 @@ return function(update_freq)
Common.annotated_scale_plot_set(upload, cr, uspeed) Common.annotated_scale_plot_set(upload, cr, uspeed)
end end
-----------------------------------------------------------------------------
-- main drawing functions
local draw_static = function(cr) local draw_static = function(cr)
Common.drawHeader(cr, header) Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(dnload, cr) Common.annotated_scale_plot_draw_static(dnload, cr)
@ -113,7 +119,7 @@ return function(update_freq)
end end
local draw_dynamic = function(cr) local draw_dynamic = function(cr)
_update(cr) update(cr)
Common.annotated_scale_plot_draw_dynamic(dnload, cr) Common.annotated_scale_plot_draw_dynamic(dnload, cr)
Common.annotated_scale_plot_draw_dynamic(upload, cr) Common.annotated_scale_plot_draw_dynamic(upload, cr)
end end

View File

@ -2,23 +2,26 @@ local Util = require 'Util'
local Common = require 'Common' local Common = require 'Common'
local Geometry = require 'Geometry' local Geometry = require 'Geometry'
return function(update_freq)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local __tonumber = tonumber local __tonumber = tonumber
local __string_match = string.match local __string_match = string.match
local __math_floor = math.floor local __math_floor = math.floor
local _PLOT_SEC_BREAK_ = 20 -----------------------------------------------------------------------------
local _PLOT_HEIGHT_ = 56 -- header
local BLOCK_SIZE_BYTES = 512 local header = Common.Header(
local STAT_FILE = '/sys/block/sda/stat' 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+)' -- reads
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 io_label_function = function(bytes)
local new_unit, new_value = Util.convert_data_val(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' return Util.precision_round_to_string(value, 3)..' '..unit..'B/s'
end end
return function(update_freq) local build_plot = function(y, label)
local header = Common.Header( return Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X, Geometry.CENTER_LEFT_X,
Geometry.TOP_Y, y,
Geometry.SECTION_WIDTH, Geometry.SECTION_WIDTH,
'INPUT / OUTPUT' PLOT_HEIGHT,
)
local reads = Common.initLabeledScalePlot(
Geometry.CENTER_LEFT_X,
header.bottom_y,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
format_value_function, format_value_function,
io_label_function, io_label_function,
_PLOT_SEC_BREAK_, PLOT_SEC_BREAK,
'Reads', label,
2, 2,
update_freq update_freq
) )
end
local writes = Common.initLabeledScalePlot( local reads = build_plot(header.bottom_y, 'Reads')
Geometry.CENTER_LEFT_X,
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2, -----------------------------------------------------------------------------
Geometry.SECTION_WIDTH, -- writes
_PLOT_HEIGHT_,
format_value_function, local writes = build_plot(
io_label_function, header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
_PLOT_SEC_BREAK_, 'Writes'
'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 reads.byte_cnt = 0
writes.byte_cnt = 0 writes.byte_cnt = 0
reads.prev_byte_cnt, writes.prev_byte_cnt = read_stat_file() 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) update_stat(cr, writes, write_byte_cnt)
end end
-----------------------------------------------------------------------------
-- main drawing functions
local draw_static = function(cr) local draw_static = function(cr)
Common.drawHeader(cr, header) Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(reads, cr) Common.annotated_scale_plot_draw_static(reads, cr)