conky-config/drawing/ReadWrite.lua

143 lines
3.5 KiB
Lua
Raw Normal View History

local M = {}
2017-07-19 00:36:15 -04:00
local Text = require 'Text'
local Line = require 'Line'
local ScalePlot = require 'ScalePlot'
local Util = require 'Util'
2017-07-19 00:36:15 -04:00
local __tonumber = tonumber
local __string_match = string.match
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)
2017-07-19 00:36:15 -04:00
return __tonumber(bytes_r) * BLOCK_SIZE_BYTES, __tonumber(bytes_w) * BLOCK_SIZE_BYTES
end
local update_stat = function(cr, stat, byte_cnt, update_frequency)
local delta_bytes = byte_cnt - stat.prev_byte_cnt
stat.prev_byte_cnt = byte_cnt
2019-09-01 15:34:10 -04:00
2017-07-19 00:36:15 -04:00
if delta_bytes > 0 then
local bps = delta_bytes * update_frequency
2021-06-17 18:18:09 -04:00
local unit, value = Util.convert_data_val(bps)
stat.rate.append_end = ' '..unit..'B/s'
Text.set(stat.rate, cr, Util.precision_round_to_string(value, 3))
2019-09-01 15:34:10 -04:00
ScalePlot.update(stat.plot, cr, bps)
2017-07-19 00:36:15 -04:00
else
stat.rate.append_end = ' B/s'
Text.set(stat.rate, cr, '0.00')
2019-09-01 15:34:10 -04:00
ScalePlot.update(stat.plot, cr, 0)
2017-07-19 00:36:15 -04:00
end
end
local io_label_function = function(bytes)
2021-06-17 18:18:09 -04:00
local new_unit, new_value = Util.convert_data_val(bytes)
2019-09-01 15:34:10 -04:00
2017-07-19 00:36:15 -04:00
local precision = 0
2021-06-17 18:18:09 -04:00
if new_value < 10 then precision = 1 end
2019-09-01 15:34:10 -04:00
2021-06-17 18:18:09 -04:00
return Util.round_to_string(new_value, precision)..' '..new_unit..'B/s'
2017-07-19 00:36:15 -04:00
end
local header = _G_Widget_.Header{
2017-07-19 00:36:15 -04:00
x = _G_INIT_DATA_.CENTER_LEFT_X,
y = _G_INIT_DATA_.TOP_Y,
width = _G_INIT_DATA_.SECTION_WIDTH,
header = 'INPUT / OUTPUT'
}
local _RIGHT_X_ = _G_INIT_DATA_.CENTER_LEFT_X + _G_INIT_DATA_.SECTION_WIDTH
local reads = {
label = _G_Widget_.Text{
2017-07-19 00:36:15 -04:00
x = _G_INIT_DATA_.CENTER_LEFT_X,
y = header.bottom_y,
text = 'Reads',
},
rate = _G_Widget_.Text{
2017-07-19 00:36:15 -04:00
x = _RIGHT_X_,
y = header.bottom_y,
x_align = 'right',
append_end=' B/s',
text_color = _G_Patterns_.BLUE
2017-07-19 00:36:15 -04:00
},
plot = _G_Widget_.ScalePlot{
2017-07-19 00:36:15 -04:00
x = _G_INIT_DATA_.CENTER_LEFT_X,
y = header.bottom_y + _PLOT_SEC_BREAK_,
width = _G_INIT_DATA_.SECTION_WIDTH,
height = _PLOT_HEIGHT_,
y_label_func = io_label_function,
}
}
local _WRITE_Y_ = header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2
local writes = {
label = _G_Widget_.Text{
2017-07-19 00:36:15 -04:00
x = _G_INIT_DATA_.CENTER_LEFT_X,
y = _WRITE_Y_,
text = 'Writes',
},
rate = _G_Widget_.Text{
2017-07-19 00:36:15 -04:00
x = _RIGHT_X_,
y = _WRITE_Y_,
x_align = 'right',
append_end =' B/s',
text_color = _G_Patterns_.BLUE
2017-07-19 00:36:15 -04:00
},
plot = _G_Widget_.ScalePlot{
2017-07-19 00:36:15 -04:00
x = _G_INIT_DATA_.CENTER_LEFT_X,
y = _WRITE_Y_ + _PLOT_SEC_BREAK_,
width = _G_INIT_DATA_.SECTION_WIDTH,
height = _PLOT_HEIGHT_,
y_label_func = io_label_function,
}
}
_PLOT_SEC_BREAK_ = nil
_PLOT_HEIGHT_ = nil
_RIGHT_X_ = nil
_WRITE_Y_ = nil
reads.byte_cnt = 0
writes.byte_cnt = 0
reads.prev_byte_cnt, writes.prev_byte_cnt = read_stat_file()
local update = function(cr, update_frequency)
local read_byte_cnt, write_byte_cnt = read_stat_file()
update_stat(cr, reads, read_byte_cnt, update_frequency)
update_stat(cr, writes, write_byte_cnt, update_frequency)
end
local draw_static = function(cr)
Text.draw(header.text, cr)
Line.draw(header.underline, cr)
Text.draw(reads.label, cr)
Text.draw(writes.label, cr)
end
local draw_dynamic = function(cr, update_frequency)
2018-08-05 11:08:37 -04:00
update(cr, update_frequency)
2017-07-19 00:36:15 -04:00
2018-08-05 11:08:37 -04:00
Text.draw(reads.rate, cr)
2018-08-05 14:29:52 -04:00
ScalePlot.draw_dynamic(reads.plot, cr)
2019-09-01 15:34:10 -04:00
2018-08-05 11:08:37 -04:00
Text.draw(writes.rate, cr)
2018-08-05 14:29:52 -04:00
ScalePlot.draw_dynamic(writes.plot, cr)
2017-07-19 00:36:15 -04:00
end
M.draw_static = draw_static
M.draw_dynamic = draw_dynamic
return M