From: Vsevolod Stakhov Date: Thu, 26 Apr 2018 16:20:47 +0000 (+0100) Subject: [Feature] Implement map statistics function for lua API X-Git-Tag: 1.7.4~31 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=147df4300a76d4865f62cea0f3c9e33c2e8d34b0;p=thirdparty%2Frspamd.git [Feature] Implement map statistics function for lua API --- diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index 7f5195d8e7..f0520dead3 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -831,6 +831,43 @@ lua_map_get_key (lua_State * L) return 1; } +static gboolean +lua_map_traverse_cb (gconstpointer key, + gconstpointer value, gsize hits, gpointer ud) +{ + lua_State *L = (lua_State *)ud; + + lua_pushstring (L, key); + lua_pushnumber (L, hits); + lua_settable (L, -3); + + return TRUE; +} + +static gint +lua_map_get_stats (lua_State * L) +{ + struct rspamd_lua_map *map = lua_check_map (L, 1); + gboolean do_reset = FALSE; + + if (map != NULL) { + if (lua_isboolean (L, 2)) { + do_reset = lua_toboolean (L, 2); + } + + lua_createtable (L, 0, map->map->nelts); + + if (map->map->traverse_function) { + rspamd_map_traverse (map->map, lua_map_traverse_cb, L, do_reset); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + static int lua_map_is_signed (lua_State *L) {