diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-11-04 15:42:15 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-11-04 15:42:15 +0000 |
commit | 70c5a7b68850e825b29b12c672fda71e366f58ff (patch) | |
tree | f100c3558bdf7cbcff45d7156ea5681c4ab4c60a /src/lua | |
parent | cc2301c1517da93dfe0867a5a91bafae18001e1a (diff) | |
download | rspamd-70c5a7b68850e825b29b12c672fda71e366f58ff.tar.gz rspamd-70c5a7b68850e825b29b12c672fda71e366f58ff.zip |
[Rework] Kill metrics!11
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_cfg_file.c | 37 | ||||
-rw-r--r-- | src/lua/lua_config.c | 116 | ||||
-rw-r--r-- | src/lua/lua_fann.c | 2 | ||||
-rw-r--r-- | src/lua/lua_task.c | 7 |
4 files changed, 50 insertions, 112 deletions
diff --git a/src/lua/lua_cfg_file.c b/src/lua/lua_cfg_file.c index f564f1e79..daa8223ff 100644 --- a/src/lua/lua_cfg_file.c +++ b/src/lua/lua_cfg_file.c @@ -28,31 +28,22 @@ static void lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg) { - GList *metric_list; gchar *symbol; const gchar *desc = NULL; - struct rspamd_metric *metric; gdouble *score; struct rspamd_symbol *s; - /* Get module opt structure */ - if ((metric = g_hash_table_lookup (cfg->metrics, name)) == NULL) { - metric = rspamd_config_new_metric (cfg, metric, name); - } - /* Now iterate through module table */ for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { /* key - -2, value - -1 */ - symbol = - rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2)); + symbol = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2)); if (symbol != NULL) { if (lua_istable (L, -1)) { /* We got a table, so extract individual attributes */ lua_pushstring (L, "weight"); lua_gettable (L, -2); if (lua_isnumber (L, -1)) { - score = - rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double)); + score = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double)); *score = lua_tonumber (L, -1); } else { @@ -78,37 +69,23 @@ lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg) } /* Insert symbol */ if ((s = - g_hash_table_lookup (metric->symbols, symbol)) != NULL) { + g_hash_table_lookup (cfg->symbols, symbol)) != NULL) { msg_info_config("replacing weight for symbol %s: %.2f -> %.2f", - symbol, - *s->weight_ptr, - *score); + symbol, + *s->weight_ptr, + *score); s->weight_ptr = score; } else { s = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*s)); s->name = symbol; s->weight_ptr = score; - g_hash_table_insert (metric->symbols, symbol, s); + g_hash_table_insert (cfg->symbols, symbol, s); } if (desc) { s->description = rspamd_mempool_strdup (cfg->cfg_pool, desc); } - - if ((metric_list = - g_hash_table_lookup (cfg->metrics_symbols, symbol)) == NULL) { - metric_list = g_list_prepend (NULL, metric); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_list_free, metric_list); - g_hash_table_insert (cfg->metrics_symbols, symbol, metric_list); - } - else { - /* Slow but keep start element of list in safe */ - if (!g_list_find (metric_list, metric)) { - metric_list = g_list_append (metric_list, metric); - } - } } } } diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 6407852b2..c1e31eb44 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -1525,8 +1525,8 @@ lua_config_register_symbol (lua_State * L) nshots = 1; } - rspamd_config_add_metric_symbol (cfg, name, - score, description, group, flags, (guint)priority, nshots); + rspamd_config_add_symbol (cfg, name, + score, description, group, flags, (guint) priority, nshots); } } else { @@ -1754,10 +1754,9 @@ static gint lua_config_set_metric_symbol (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *metric_name = DEFAULT_METRIC, *description = NULL, + const gchar *description = NULL, *group = NULL, *name = NULL, *flags_str = NULL; double weight; - struct rspamd_metric *metric; gboolean one_shot = FALSE, one_param = FALSE; GError *err = NULL; gdouble priority = 0.0; @@ -1769,11 +1768,11 @@ lua_config_set_metric_symbol (lua_State * L) if (lua_type (L, 2) == LUA_TTABLE) { if (!rspamd_lua_parse_table_arguments (L, 2, &err, "*name=S;score=N;description=S;" - "group=S;one_shot=B;one_param=B;metric=S;priority=N;flags=S;" + "group=S;one_shot=B;one_param=B;priority=N;flags=S;" "nshots=I", &name, &weight, &description, &group, &one_shot, &one_param, - &metric_name, &priority, &flags_str, &nshots)) { + &priority, &flags_str, &nshots)) { msg_err_config ("bad arguments: %e", err); g_error_free (err); @@ -1788,7 +1787,7 @@ lua_config_set_metric_symbol (lua_State * L) description = luaL_checkstring (L, 4); } if (lua_gettop (L) > 4 && lua_type (L, 5) == LUA_TSTRING) { - metric_name = luaL_checkstring (L, 5); + /* XXX: metrics */ } if (lua_gettop (L) > 5 && lua_type (L, 6) == LUA_TSTRING) { group = luaL_checkstring (L, 6); @@ -1798,15 +1797,10 @@ lua_config_set_metric_symbol (lua_State * L) } } - if (metric_name == NULL) { - metric_name = DEFAULT_METRIC; - } - if (nshots == 0) { nshots = cfg->default_max_shots; } - metric = g_hash_table_lookup (cfg->metrics, metric_name); if (one_shot) { nshots = 1; } @@ -1826,13 +1820,8 @@ lua_config_set_metric_symbol (lua_State * L) } } - if (metric == NULL) { - msg_err_config ("metric named %s is not defined", metric_name); - } - else if (name != NULL && weight != 0) { - rspamd_config_add_metric_symbol (cfg, name, - weight, description, group, flags, (guint)priority, nshots); - } + rspamd_config_add_symbol (cfg, name, + weight, description, group, flags, (guint) priority, nshots); } else { return luaL_error (L, "invalid arguments, rspamd_config expected"); @@ -1845,41 +1834,31 @@ static gint lua_config_get_metric_symbol (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym_name = luaL_checkstring (L, 2), - *metric_name = DEFAULT_METRIC; + const gchar *sym_name = luaL_checkstring (L, 2); struct rspamd_symbol *sym_def; - struct rspamd_metric *metric; if (cfg && sym_name) { - metric = g_hash_table_lookup (cfg->metrics, metric_name); + sym_def = g_hash_table_lookup (cfg->symbols, sym_name); - if (metric == NULL) { - msg_err_config ("metric named %s is not defined", metric_name); + if (sym_def == NULL) { lua_pushnil (L); } else { - sym_def = g_hash_table_lookup (metric->symbols, sym_name); + lua_createtable (L, 0, 3); + lua_pushstring (L, "score"); + lua_pushnumber (L, sym_def->score); + lua_settable (L, -3); - if (sym_def == NULL) { - lua_pushnil (L); - } - else { - lua_createtable (L, 0, 3); - lua_pushstring (L, "score"); - lua_pushnumber (L, sym_def->score); + if (sym_def->description) { + lua_pushstring (L, "description"); + lua_pushstring (L, sym_def->description); lua_settable (L, -3); + } - if (sym_def->description) { - lua_pushstring (L, "description"); - lua_pushstring (L, sym_def->description); - lua_settable (L, -3); - } - - if (sym_def->gr) { - lua_pushstring (L, "group"); - lua_pushstring (L, sym_def->gr->name); - lua_settable (L, -3); - } + if (sym_def->gr) { + lua_pushstring (L, "group"); + lua_pushstring (L, sym_def->gr->name); + lua_settable (L, -3); } } } @@ -1894,9 +1873,8 @@ static gint lua_config_set_metric_action (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *metric_name = DEFAULT_METRIC, *name = NULL; + const gchar *name = NULL; double weight; - struct rspamd_metric *metric; GError *err = NULL; gdouble priority = 0.0; @@ -1905,9 +1883,9 @@ lua_config_set_metric_action (lua_State * L) if (lua_type (L, 2) == LUA_TTABLE) { if (!rspamd_lua_parse_table_arguments (L, 2, &err, "*action=S;score=N;" - "metric=S;priority=N", + "priority=N", &name, &weight, - &metric_name, &priority)) { + &priority)) { msg_err_config ("bad arguments: %e", err); g_error_free (err); @@ -1918,18 +1896,8 @@ lua_config_set_metric_action (lua_State * L) return luaL_error (L, "invalid arguments, table expected"); } - if (metric_name == NULL) { - metric_name = DEFAULT_METRIC; - } - - metric = g_hash_table_lookup (cfg->metrics, metric_name); - - if (metric == NULL) { - msg_err_config ("metric named %s is not defined", metric_name); - } - else if (name != NULL && weight != 0) { - rspamd_config_set_action_score (cfg, metric_name, name, - weight, (guint)priority); + if (name != NULL && weight != 0) { + rspamd_config_set_action_score (cfg, name, weight, (guint)priority); } } else { @@ -1943,31 +1911,21 @@ static gint lua_config_get_metric_action (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *metric_name = DEFAULT_METRIC, - *act_name = luaL_checkstring (L, 2); - struct rspamd_metric *metric; + const gchar *act_name = luaL_checkstring (L, 2); gint act = 0; if (cfg && act_name) { - metric = g_hash_table_lookup (cfg->metrics, metric_name); - - if (metric == NULL) { - msg_err_config ("metric named %s is not defined", metric_name); - lua_pushnil (L); - } - else { - if (rspamd_action_from_str (act_name, &act)) { - if (!isnan (metric->actions[act].score)) { - lua_pushnumber (L, metric->actions[act].score); - } - else { - lua_pushnil (L); - } + if (rspamd_action_from_str (act_name, &act)) { + if (!isnan (cfg->actions[act].score)) { + lua_pushnumber (L, cfg->actions[act].score); } else { lua_pushnil (L); } } + else { + lua_pushnil (L); + } } else { return luaL_error (L, "invalid arguments, rspamd_config expected"); @@ -2147,7 +2105,7 @@ lua_config_newindex (lua_State *L) * Now check if a symbol has not been registered in any metric and * insert default value if applicable */ - if (g_hash_table_lookup (cfg->metrics_symbols, name) == NULL) { + if (g_hash_table_lookup (cfg->symbols, name) == NULL) { nshots = cfg->default_max_shots; lua_pushstring (L, "score"); lua_gettable (L, -2); @@ -2197,7 +2155,7 @@ lua_config_newindex (lua_State *L) * Do not override the existing symbols (using zero priority), * since we are defining default values here */ - rspamd_config_add_metric_symbol (cfg, name, score, + rspamd_config_add_symbol (cfg, name, score, description, group, flags, 0, nshots); } else { diff --git a/src/lua/lua_fann.c b/src/lua/lua_fann.c index c7fdc6c70..40353ea40 100644 --- a/src/lua/lua_fann.c +++ b/src/lua/lua_fann.c @@ -630,7 +630,7 @@ lua_fann_thread_notify (gint fd, short what, gpointer ud) lua_fann_push_train_result (cbdata, rep.errcode, rep.mse, rep.errmsg); } - write (cbdata->pair[0], "", 1); + g_assert (write (cbdata->pair[0], "", 1) == 1); g_thread_join (cbdata->t); close (cbdata->pair[0]); close (cbdata->pair[1]); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index c4f4de3fe..4172d60b7 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -1323,11 +1323,14 @@ static gint lua_task_set_pre_result (lua_State * L) { struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_config *cfg; struct rspamd_metric_result *mres; gchar *action_str; gint action = METRIC_ACTION_MAX; if (task != NULL) { + cfg = task->cfg; + if (lua_type (L, 2) == LUA_TNUMBER) { action = lua_tointeger (L, 2); } @@ -1340,7 +1343,7 @@ lua_task_set_pre_result (lua_State * L) if (!task->result) { mres = rspamd_create_metric_result (task); if (mres != NULL) { - mres->score = mres->metric->actions[action].score; + mres->score = cfg->actions[action].score; mres->action = action; } } @@ -4058,7 +4061,7 @@ lua_task_get_metric_action (lua_State *L) { struct rspamd_task *task = lua_check_task (L, 1); struct rspamd_metric_result *metric_res; - enum rspamd_metric_action action; + enum rspamd_action_type action; if (task) { if ((metric_res = task->result) != NULL) { |