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 Common = require 'Common'
local Geometry = require 'Geometry' 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) 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( local header = Common.Header(
Geometry.CENTER_RIGHT_X, Geometry.CENTER_RIGHT_X,
Geometry.TOP_Y, Geometry.TOP_Y,
@ -37,33 +19,54 @@ return function(update_freq)
'NETWORK' 'NETWORK'
) )
local dnload = Common.initLabeledScalePlot( -----------------------------------------------------------------------------
-- 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, Geometry.CENTER_RIGHT_X,
header.bottom_y, y,
Geometry.SECTION_WIDTH, Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_, PLOT_HEIGHT,
value_format_function, value_format_function,
network_label_function, network_label_function,
_PLOT_SEC_BREAK_, PLOT_SEC_BREAK,
'Download', label,
2, 2,
update_freq update_freq
) )
end
local upload = Common.initLabeledScalePlot( local dnload = build_plot(header.bottom_y, 'Download')
Geometry.CENTER_RIGHT_X,
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2, -----------------------------------------------------------------------------
Geometry.SECTION_WIDTH, -- upload plot
_PLOT_HEIGHT_,
value_format_function, local upload = build_plot(
network_label_function, header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
_PLOT_SEC_BREAK_, 'Upload'
'Upload',
2,
update_freq
) )
local _update = function(cr) -----------------------------------------------------------------------------
-- update function
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 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,35 +2,17 @@ local Util = require 'Util'
local Common = require 'Common' local Common = require 'Common'
local Geometry = require 'Geometry' 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) 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( local header = Common.Header(
Geometry.CENTER_LEFT_X, Geometry.CENTER_LEFT_X,
Geometry.TOP_Y, Geometry.TOP_Y,
@ -38,31 +20,58 @@ return function(update_freq)
'INPUT / OUTPUT' 'INPUT / OUTPUT'
) )
local reads = Common.initLabeledScalePlot( -----------------------------------------------------------------------------
-- 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, Geometry.CENTER_LEFT_X,
header.bottom_y, y,
Geometry.SECTION_WIDTH, Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_, 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 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, -- update function
header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2,
Geometry.SECTION_WIDTH, -- TODO add more devices to this
_PLOT_HEIGHT_, local BLOCK_SIZE_BYTES = 512
format_value_function, local STAT_FILE = '/sys/block/sda/stat'
io_label_function,
_PLOT_SEC_BREAK_, -- fields 3 and 7 (sectors read and written)
'Writes', local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
2,
update_freq 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
@ -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)