From: Vsevolod Stakhov Date: Wed, 27 May 2015 16:30:06 +0000 (+0100) Subject: Restore `counters` command. X-Git-Tag: 1.0.0~598 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ce9a26d98932473c449e5d4bf39dbb93a6d7a924;p=thirdparty%2Frspamd.git Restore `counters` command. --- diff --git a/src/controller.c b/src/controller.c index 3a5fb26fe0..8c3b490917 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1580,26 +1580,6 @@ rspamd_controller_handle_statreset ( return rspamd_controller_handle_stat_common (conn_ent, msg, TRUE); } -#if 0 -/* XXX: restore counters */ -static ucl_object_t * -rspamd_controller_cache_item_to_ucl (struct cache_item *item) -{ - ucl_object_t *obj; - - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromstring (item->s->symbol), - "symbol", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (item->s->weight), - "weight", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (item->s->frequency), - "frequency", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (item->s->avg_time), - "time", 0, false); - - return obj; -} -#endif /* * Counters command handler: @@ -1623,32 +1603,15 @@ rspamd_controller_handle_counters ( } cache = session->ctx->cfg->cache; - top = ucl_object_typed_new (UCL_ARRAY); + if (cache != NULL) { -#if 0 -/* XXX: restore counters */ - cur = cache->negative_items; - while (cur) { - item = cur->data; - if (!item->is_callback) { - ucl_array_append (top, rspamd_controller_cache_item_to_ucl ( - item)); - } - cur = g_list_next (cur); - } - cur = cache->static_items; - while (cur) { - item = cur->data; - if (!item->is_callback) { - ucl_array_append (top, rspamd_controller_cache_item_to_ucl ( - item)); - } - cur = g_list_next (cur); - } -#endif + top = rspamd_symbols_cache_counters (cache); + rspamd_controller_send_ucl (conn_ent, top); + ucl_object_unref (top); + } + else { + rspamd_controller_send_error (conn_ent, 500, "Invalid cache"); } - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); return 0; } diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index 52c5ea4bf9..25bccc6e95 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -655,5 +655,38 @@ call_symbol_callback (struct rspamd_task * task, *save = GUINT_TO_POINTER (idx); return TRUE; +} + +static void +rspamd_symbols_cache_counters_cb (gpointer k, gpointer v, gpointer ud) +{ + ucl_object_t *obj, *top = ud; + struct cache_item *item = v; + + if (item->type != SYMBOL_TYPE_CALLBACK) { + obj = ucl_object_typed_new (UCL_OBJECT); + ucl_object_insert_key (obj, ucl_object_fromstring (item->symbol), + "symbol", 0, false); + ucl_object_insert_key (obj, ucl_object_fromdouble (item->weight), + "weight", 0, false); + ucl_object_insert_key (obj, ucl_object_fromint (item->frequency), + "frequency", 0, false); + ucl_object_insert_key (obj, ucl_object_fromdouble (item->avg_time), + "time", 0, false); + + ucl_array_append (top, obj); + } +} + +ucl_object_t * +rspamd_symbols_cache_counters (struct symbols_cache * cache) +{ + ucl_object_t *top; + + g_assert (cache != NULL); + top = ucl_object_typed_new (UCL_ARRAY); + g_hash_table_foreach (cache->items_by_symbol, + rspamd_symbols_cache_counters_cb, top); + return top; } diff --git a/src/libserver/symbols_cache.h b/src/libserver/symbols_cache.h index 5a476f414c..8eee4e690c 100644 --- a/src/libserver/symbols_cache.h +++ b/src/libserver/symbols_cache.h @@ -26,6 +26,7 @@ #define RSPAMD_SYMBOLS_CACHE_H #include "config.h" +#include "ucl.h" #define MAX_SYMBOL 128 @@ -152,5 +153,11 @@ gboolean validate_cache (struct symbols_cache *cache, struct rspamd_config *cfg, gboolean strict); +/** + * Return statistics about the cache as ucl object (array of objects one per item) + * @param cache + * @return + */ +ucl_object_t *rspamd_symbols_cache_counters (struct symbols_cache * cache); #endif