]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add ability to get symbols dynamic stats from Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 24 Jun 2017 12:40:14 +0000 (13:40 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 24 Jun 2017 12:40:14 +0000 (13:40 +0100)
src/lua/lua_config.c

index cba81c2a617b1bad3c0c84e55b8ee084251ae5d0..08e28fa709623a50ed1ba5d423266082cf48a917 100644 (file)
@@ -525,6 +525,17 @@ LUA_FUNCTION_DEF (config, get_symbols_cksum);
  */
 LUA_FUNCTION_DEF (config, get_symbol_callback);
 
+/***
+ * @method rspamd_config:get_symbol_stat(name)
+ * Returns table with statistics for a specific symbol:
+ * - `frequency`: frequency for symbol's hits
+ * - `stddev`: standard deviation of `frequency`
+ * - `time`: average time in seconds (floating point)
+ * - `count`: total number of hits
+ * @return {table} symbol stats
+ */
+LUA_FUNCTION_DEF (config, get_symbol_stat);
+
 /***
  * @method rspamd_config:set_symbol_callback(name, callback)
  * Sets callback for the specified symbol
@@ -656,6 +667,7 @@ static const struct luaL_reg configlib_m[] = {
        LUA_INTERFACE_DEF (config, get_symbols_cksum),
        LUA_INTERFACE_DEF (config, get_symbol_callback),
        LUA_INTERFACE_DEF (config, set_symbol_callback),
+       LUA_INTERFACE_DEF (config, get_symbol_stat),
        LUA_INTERFACE_DEF (config, register_finish_script),
        LUA_INTERFACE_DEF (config, register_monitored),
        LUA_INTERFACE_DEF (config, add_doc),
@@ -2586,6 +2598,43 @@ lua_config_set_symbol_callback (lua_State *L)
        return 1;
 }
 
+static gint
+lua_config_get_symbol_stat (lua_State *L)
+{
+       struct rspamd_config *cfg = lua_check_config (L, 1);
+       const gchar *sym = luaL_checkstring (L, 2);
+       gdouble freq, stddev, tm;
+       guint hits;
+
+       if (cfg != NULL && sym != NULL) {
+               if (!rspamd_symbols_cache_stat_symbol (cfg->cache, sym, &freq,
+                               &stddev, &tm, &hits)) {
+                       lua_pushnil (L);
+               }
+               else {
+                       lua_createtable (L, 0, 4);
+                       lua_pushstring (L, "frequency");
+                       lua_pushnumber (L, freq);
+                       lua_settable (L, -3);
+                       lua_pushstring (L, "sttdev");
+                       lua_pushnumber (L, stddev);
+                       lua_settable (L, -3);
+                       lua_pushstring (L, "time");
+                       lua_pushnumber (L, tm);
+                       lua_settable (L, -3);
+                       lua_pushstring (L, "hits");
+                       lua_pushnumber (L, hits);
+                       lua_settable (L, -3);
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
+
 static gint
 lua_config_register_finish_script (lua_State *L)
 {