diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-05 17:02:39 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-05 17:02:39 +0100 |
commit | 5c040a0e5edeedc9237e8cd0b9a464fc7ecd1946 (patch) | |
tree | 84619d643d24245678f3d148f7d1c915d36fc1d1 /src/libserver/task.c | |
parent | 647d5d2a8f00aa97800a4b40fcab0f1bf02c3f9b (diff) | |
download | rspamd-5c040a0e5edeedc9237e8cd0b9a464fc7ecd1946.tar.gz rspamd-5c040a0e5edeedc9237e8cd0b9a464fc7ecd1946.zip |
[Feature] Sort symbols when displaying them in log
Diffstat (limited to 'src/libserver/task.c')
-rw-r--r-- | src/libserver/task.c | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/libserver/task.c b/src/libserver/task.c index 3bedb7cf1..eca443c62 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -23,7 +23,8 @@ #include "composites.h" #include "stat_api.h" #include "unix-std.h" -#include <utlist.h> +#include "utlist.h" +#include <math.h> static GQuark rspamd_task_quark (void) @@ -711,6 +712,27 @@ rspamd_task_log_check_condition (struct rspamd_task *task, return ret; } +/* + * Sort by symbol's score -> name + */ +static gint +rspamd_task_compare_log_sym (gconstpointer a, gconstpointer b) +{ + const struct symbol *s1 = *(const struct symbol **)a, + *s2 = *(const struct symbol **)b; + gdouble w1, w2; + + + w1 = fabs (s1->score); + w2 = fabs (s2->score); + + if (w1 == w2 && s1->name && s2->name) { + return strcmp (s1->name, s2->name); + } + + return (w2 - w1) * 1000.0; +} + static rspamd_ftok_t rspamd_task_log_metric_res (struct rspamd_task *task, struct rspamd_log_format *lf) @@ -723,6 +745,8 @@ rspamd_task_log_metric_res (struct rspamd_task *task, gpointer k, v; rspamd_fstring_t *symbuf; struct symbol *sym; + GPtrArray *sorted_symbols; + guint i; mres = g_hash_table_lookup (task->results, DEFAULT_METRIC); @@ -753,9 +777,16 @@ rspamd_task_log_metric_res (struct rspamd_task *task, case RSPAMD_LOG_SYMBOLS: symbuf = rspamd_fstring_sized_new (128); g_hash_table_iter_init (&it, mres->symbols); + sorted_symbols = g_ptr_array_sized_new (g_hash_table_size (mres->symbols)); while (g_hash_table_iter_next (&it, &k, &v)) { - sym = (struct symbol *) v; + g_ptr_array_add (sorted_symbols, v); + } + + g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_sym); + + for (i = 0; i < sorted_symbols->len; i ++) { + sym = g_ptr_array_index (sorted_symbols, i); if (first) { rspamd_printf_fstring (&symbuf, "%s", sym->name); @@ -783,6 +814,8 @@ rspamd_task_log_metric_res (struct rspamd_task *task, first = FALSE; } + g_ptr_array_free (sorted_symbols, TRUE); + rspamd_mempool_add_destructor (task->task_pool, (rspamd_mempool_destruct_t)rspamd_fstring_free, symbuf); |