]> source.dussan.org Git - rspamd.git/commitdiff
Restore `counters` command.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 27 May 2015 16:30:06 +0000 (17:30 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 27 May 2015 16:30:06 +0000 (17:30 +0100)
src/controller.c
src/libserver/symbols_cache.c
src/libserver/symbols_cache.h

index 3a5fb26fe011c1992293e81b75a6bc28e4e8e28a..8c3b4909177662425cb2007d983fc3f91f85e655 100644 (file)
@@ -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;
 }
index 52c5ea4bf98ab164f9c787d09617976185348228..25bccc6e951f25b88065da54ffceaa5055d3f6d4 100644 (file)
@@ -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;
 }
index 5a476f414c7cc0a17cde43b0de4c0604dfbbb21d..8eee4e690c3a35191e80d05313c6a09ba6255e99 100644 (file)
@@ -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