conky-config/drawing/network.lua

82 lines
2.3 KiB
Lua
Raw Normal View History

2021-08-08 15:58:53 -04:00
local format = require 'format'
2022-07-14 22:25:47 -04:00
local pure = require 'pure'
2021-08-08 15:58:53 -04:00
local i_o = require 'i_o'
local common = require 'common'
local geometry = require 'geometry'
2021-08-08 19:12:31 -04:00
local sys = require 'sys'
2017-07-19 00:36:15 -04:00
return function(update_freq, point)
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
2021-08-08 19:12:31 -04:00
local INTERFACE_PATHS = sys.get_net_interface_paths()
2021-07-19 23:58:58 -04:00
local get_bits = function(path)
2021-08-08 15:58:53 -04:00
return i_o.read_file(path, nil, '*n') * 8
2021-07-19 23:58:58 -04:00
end
2022-07-14 22:25:47 -04:00
local state = {rx_bits = 0, tx_bits = 0}
2021-07-19 23:58:58 -04:00
local read_interfaces = function()
2022-07-14 22:25:47 -04:00
state.rx_bits = 0
state.tx_bits = 0
2021-07-19 23:58:58 -04:00
for i = 1, #INTERFACE_PATHS do
local p = INTERFACE_PATHS[i]
2022-07-14 22:25:47 -04:00
state.rx_bits = state.rx_bits + get_bits(p.rx)
state.tx_bits = state.tx_bits + get_bits(p.tx)
2021-07-19 23:58:58 -04:00
end
2022-07-14 22:25:47 -04:00
return state
2021-07-19 23:58:58 -04:00
end
2022-07-14 22:25:47 -04:00
-- prime initial state
read_interfaces()
local value_format_function = function(bits)
2021-08-08 15:58:53 -04:00
local unit, value = format.convert_data_val(bits)
return format.precision_round_to_string(value, 3)..' '..unit..'b/s'
end
2022-07-14 22:49:08 -04:00
-----------------------------------------------------------------------------
-- down/up plots
2022-07-14 22:25:47 -04:00
local mk_plot = function(label, key, y)
local obj = common.make_rate_timeseries(
point.x,
y,
geometry.SECTION_WIDTH,
PLOT_HEIGHT,
value_format_function,
common.converted_y_label_format_generator('b'),
PLOT_SEC_BREAK,
label,
2,
2021-07-19 23:58:58 -04:00
update_freq,
2022-07-14 22:25:47 -04:00
state[key]
)
return common.mk_acc(
2022-07-16 00:00:06 -04:00
geometry.SECTION_WIDTH,
2022-07-14 22:25:47 -04:00
PLOT_HEIGHT + PLOT_SEC_BREAK,
function(s) common.update_rate_timeseries(obj, s[key]) end,
pure.partial(common.tagged_scaled_timeseries_draw_static, obj),
pure.partial(common.tagged_scaled_timeseries_draw_dynamic, obj)
)
end
2022-07-14 22:25:47 -04:00
local mk_rx = pure.partial(mk_plot, 'Download', 'rx_bits')
local mk_tx = pure.partial(mk_plot, 'Upload', 'tx_bits')
2021-07-19 23:58:58 -04:00
-----------------------------------------------------------------------------
-- main drawing functions
2022-07-14 22:25:47 -04:00
local rbs = common.reduce_blocks_(
2022-07-14 22:49:08 -04:00
'NETWORK',
point,
geometry.SECTION_WIDTH,
2022-07-14 22:25:47 -04:00
{
{mk_rx, true, PLOT_SEC_BREAK},
{mk_tx, true, 0},
2022-07-14 22:25:47 -04:00
}
)
2022-07-16 00:00:06 -04:00
return pure.map_at("update", function(f) return function(_) f(read_interfaces()) end end, rbs)
end