From: Vsevolod Stakhov Date: Thu, 5 Jan 2023 16:18:08 +0000 (+0000) Subject: [Minor] Fuzzystat: Allow to sort keys X-Git-Tag: 3.5~104 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aff665421fa575c7e11f84a4b3d0b97f80487a5a;p=rspamd.git [Minor] Fuzzystat: Allow to sort keys --- diff --git a/lualib/rspamadm/fuzzy_stat.lua b/lualib/rspamadm/fuzzy_stat.lua index a83457510..76a75956d 100644 --- a/lualib/rspamadm/fuzzy_stat.lua +++ b/lualib/rspamadm/fuzzy_stat.lua @@ -4,7 +4,7 @@ local opts = {} local argparse = require "argparse" local parser = argparse() - :name "rspamadm confighelp" + :name "rspamadm control fuzzystat" :description "Shows help for the specified configuration options" :help_description_margin(32) parser:flag "--no-ips" @@ -15,12 +15,13 @@ parser:flag "--short" :description "Short output mode" parser:flag "-n --number" :description "Disable numbers humanization" -parser:option "-s --sort" +parser:option "--sort" :description "Sort order" :convert { + checked = "checked", matched = "matched", errors = "errors", - ip = "ip" + name = "name" } local function add_data(target, src) @@ -75,31 +76,31 @@ local function print_stat(st, tabs) end -- Sort by checked -local function sort_ips(tbl, sort_opts) +local function sort_hash_table(tbl, sort_opts, key_key) local res = {} for k,v in pairs(tbl) do - table.insert(res, {ip = k, data = v}) + table.insert(res, {[key_key] = k, data = v}) end local function sort_order(elt) local key = 'checked' - local _res = 0 + local sort_res = 0 if sort_opts['sort'] then if sort_opts['sort'] == 'matched' then key = 'matched' elseif sort_opts['sort'] == 'errors' then key = 'errors' - elseif sort_opts['sort'] == 'ip' then - return elt['ip'] + elseif sort_opts['sort'] == 'name' then + return elt[key_key] end end - if elt['data'][key] then - _res = elt['data'][key] + if elt.data[key] then + sort_res = elt.data[key] end - return _res + return sort_res end table.sort(res, function(a, b) @@ -255,11 +256,15 @@ return function(args, res) local res_keys = st['keys'] if res_keys and not opts['no-keys'] and not opts['short'] then print('Keys statistics:') - for k, key_stat in pairs(res_keys) do + -- Convert into an array to allow sorting + local sorted_keys = sort_hash_table(res_keys, opts, 'key') + + for _, key in ipairs(sorted_keys) do + local key_stat = key.data if key_stat.name then - print(string.format('Key id: %s, name: %s', k, key_stat.name)) + print(string.format('Key id: %s, name: %s', key.key, key_stat.name)) else - print(string.format('Key id: %s', k)) + print(string.format('Key id: %s', key.key)) end print_stat(key_stat, '\t') @@ -267,7 +272,7 @@ return function(args, res) if key_stat['ips'] and not opts['no-ips'] then print('') print('\tIPs stat:') - local sorted_ips = sort_ips(key_stat['ips'], opts) + local sorted_ips = sort_hash_table(key_stat['ips'], opts, 'ip') for _,v in ipairs(sorted_ips) do print(string.format('\t%s', v['ip'])) @@ -299,7 +304,7 @@ return function(args, res) print('') print('IPs statistics:') - local sorted_ips = sort_ips(res_ips, opts) + local sorted_ips = sort_hash_table(res_ips, opts) for _, v in ipairs(sorted_ips) do print(string.format('%s', v['ip'])) print_stat(v['data'], '\t')