diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-02-23 14:13:58 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-02-23 14:13:58 +0000 |
commit | 4fda9686da8300e8f187ae784819f8eb040846e5 (patch) | |
tree | b8539fc73d2fe96523b865d7b0320d17486dddaa | |
parent | 13885fec3a2a84a49687f2d50ce7cbe8723b2af2 (diff) | |
download | rspamd-4fda9686da8300e8f187ae784819f8eb040846e5.tar.gz rspamd-4fda9686da8300e8f187ae784819f8eb040846e5.zip |
[Minor] Move parse_time_interval to lua_util
-rw-r--r-- | lualib/lua_stat.lua | 7 | ||||
-rw-r--r-- | lualib/lua_util.lua | 41 | ||||
-rw-r--r-- | lualib/rspamadm/configwizard.lua | 43 | ||||
-rw-r--r-- | lualib/rspamadm/stat_convert.lua | 7 |
4 files changed, 54 insertions, 44 deletions
diff --git a/lualib/lua_stat.lua b/lualib/lua_stat.lua index a5987f9da..d3a435554 100644 --- a/lualib/lua_stat.lua +++ b/lualib/lua_stat.lua @@ -479,8 +479,9 @@ exports.load_sqlite_config = load_sqlite_config -- A helper method that suggests a user how to configure Redis based -- classifier based on the existing sqlite classifier -local function redis_classifier_from_sqlite(sqlite_classifier) +local function redis_classifier_from_sqlite(sqlite_classifier, expire) local result = { + new_schema = true, backend = 'redis', cache = { backend = 'redis' @@ -495,6 +496,10 @@ local function redis_classifier_from_sqlite(sqlite_classifier) } } + if expire then + result.expire = expire + end + return {classifier = {bayes = result}} end diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index f7e92937d..729e73a5f 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -215,4 +215,45 @@ end exports.disable_module = disable_module +local function parse_time_interval(str) + local function parse_time_suffix(s) + if s == 's' then + return 1 + elseif s == 'm' then + return 60 + elseif s == 'h' then + return 3600 + elseif s == 'd' then + return 86400 + elseif s == 'y' then + return 365 * 86400; + end + end + + local lpeg = require "lpeg" + + local digit = lpeg.R("09") + local parser = {} + parser.integer = + (lpeg.S("+-") ^ -1) * + (digit ^ 1) + parser.fractional = + (lpeg.P(".") ) * + (digit ^ 1) + parser.number = + (parser.integer * + (parser.fractional ^ -1)) + + (lpeg.S("+-") * parser.fractional) + parser.time = lpeg.Cf(lpeg.Cc(1) * + (parser.number / tonumber) * + ((lpeg.S("smhdy") / parse_time_suffix) ^ -1), + function (acc, val) return acc * val end) + + local t = lpeg.match(parser.time, str) + + return t +end + +exports.parse_time_interval = parse_time_interval + return exports diff --git a/lualib/rspamadm/configwizard.lua b/lualib/rspamadm/configwizard.lua index 14e066939..8607cdc1e 100644 --- a/lualib/rspamadm/configwizard.lua +++ b/lualib/rspamadm/configwizard.lua @@ -291,45 +291,6 @@ local function setup_dkim_signing(cfg, changes) changes.l.dkim_signing = {domain = domains} end -local function parse_time_interval(str) - local function parse_time_suffix(s) - if s == 's' then - return 1 - elseif s == 'm' then - return 60 - elseif s == 'h' then - return 3600 - elseif s == 'd' then - return 86400 - elseif s == 'y' then - return 365 * 86400; - end - end - - local lpeg = require "lpeg" - - local digit = lpeg.R("09") - local parser = {} - parser.integer = - (lpeg.S("+-") ^ -1) * - (digit ^ 1) - parser.fractional = - (lpeg.P(".") ) * - (digit ^ 1) - parser.number = - (parser.integer * - (parser.fractional ^ -1)) + - (lpeg.S("+-") * parser.fractional) - parser.time = lpeg.Cf(lpeg.Cc(1) * - (parser.number / tonumber) * - ((lpeg.S("smhdy") / parse_time_suffix) ^ -1), - function (acc, val) return acc * val end) - - local t = lpeg.match(parser.time, str) - - return t -end - local function check_redis_classifier(cls, changes) local symbol_spam, symbol_ham -- Load symbols from statfiles @@ -373,7 +334,7 @@ local function check_redis_classifier(cls, changes) if ask_yes_no("Do you wish to convert data to the new schema?", true) then local expire = readline_default("Expire time for new tokens [default: 100d]: ", '100d') - expire = parse_time_interval(expire) + expire = lua_util.parse_time_interval(expire) if not lua_stat_tools.convert_bayes_schema(parsed_redis, symbol_spam, symbol_ham) then printf("Conversion failed") @@ -432,7 +393,7 @@ local function setup_statistic(cfg, changes) printf('You have %d sqlite classifiers', #sqlite_configs) local expire = readline_default("Expire time for new tokens [default: 100d]: ", '100d') - expire = parse_time_interval(expire) + expire = lua_util.parse_time_interval(expire) local reset_previous = ask_yes_no("Reset previuous data?") if ask_yes_no('Do you wish to convert them to Redis?', true) then diff --git a/lualib/rspamadm/stat_convert.lua b/lualib/rspamadm/stat_convert.lua index 6ad3b0332..230dc3f3f 100644 --- a/lualib/rspamadm/stat_convert.lua +++ b/lualib/rspamadm/stat_convert.lua @@ -2,10 +2,13 @@ local lua_redis = require "lua_redis" local stat_tools = require "lua_stat" local ucl = require "ucl" local logger = require "rspamd_logger" - +local lua_util = require "lua_util" return function (_, res) local redis_params = {} + if res.expire then + res.expire = lua_util.parse_time_interval(res.expire) + end if not lua_redis.try_load_redis_servers(res.redis, nil, redis_params) then logger.errx('cannot load redis server definition') @@ -29,7 +32,7 @@ return function (_, res) end logger.messagex('Converted classifier to the from sqlite to redis') logger.messagex('Suggested configuration:') - logger.messagex(ucl.to_format(stat_tools.redis_classifier_from_sqlite(cls), + logger.messagex(ucl.to_format(stat_tools.redis_classifier_from_sqlite(cls, res.expire), 'config')) end end |