You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stat_api.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef STAT_API_H_
  17. #define STAT_API_H_
  18. #include "config.h"
  19. #include "task.h"
  20. #include <lua.h>
  21. #include <event.h>
  22. /**
  23. * @file stat_api.h
  24. * High level statistics API
  25. */
  26. #define RSPAMD_STAT_TOKEN_FLAG_TEXT (1 << 0)
  27. #define RSPAMD_STAT_TOKEN_FLAG_META (1 << 1)
  28. #define RSPAMD_STAT_TOKEN_FLAG_LUA_META (1 << 2)
  29. #define RSPAMD_STAT_TOKEN_FLAG_EXCEPTION (1 << 3)
  30. #define RSPAMD_STAT_TOKEN_FLAG_SUBJECT (1 << 4)
  31. #define RSPAMD_STAT_TOKEN_FLAG_UNIGRAM (1 << 5)
  32. typedef struct rspamd_stat_token_s {
  33. const gchar *begin;
  34. gsize len;
  35. guint flags;
  36. } rspamd_stat_token_t;
  37. typedef struct token_node_s {
  38. guint64 data;
  39. guint window_idx;
  40. guint flags;
  41. rspamd_stat_token_t *t1;
  42. rspamd_stat_token_t *t2;
  43. gdouble values[];
  44. } rspamd_token_t;
  45. struct rspamd_stat_ctx;
  46. /**
  47. * The results of statistics processing:
  48. * - error
  49. * - need to do additional job for processing
  50. * - all processed
  51. */
  52. typedef enum rspamd_stat_result_e {
  53. RSPAMD_STAT_PROCESS_ERROR = 0,
  54. RSPAMD_STAT_PROCESS_DELAYED = 1,
  55. RSPAMD_STAT_PROCESS_OK
  56. } rspamd_stat_result_t;
  57. /**
  58. * Initialise statistics modules
  59. * @param cfg
  60. */
  61. void rspamd_stat_init (struct rspamd_config *cfg, struct event_base *ev_base);
  62. /**
  63. * Finalize statistics
  64. */
  65. void rspamd_stat_close (void);
  66. /**
  67. * Tokenize task
  68. * @param st_ctx
  69. * @param task
  70. */
  71. void rspamd_stat_process_tokenize (struct rspamd_stat_ctx *st_ctx,
  72. struct rspamd_task *task);
  73. /**
  74. * Classify the task specified and insert symbols if needed
  75. * @param task
  76. * @param L lua state
  77. * @param err error returned
  78. * @return TRUE if task has been classified
  79. */
  80. rspamd_stat_result_t rspamd_stat_classify (struct rspamd_task *task,
  81. lua_State *L, guint stage, GError **err);
  82. /**
  83. * Check if a task should be learned and set the appropriate flags for it
  84. * @param task
  85. * @return
  86. */
  87. gboolean rspamd_stat_check_autolearn (struct rspamd_task *task);
  88. /**
  89. * Learn task as spam or ham, task must be processed prior to this call
  90. * @param task task to learn
  91. * @param spam if TRUE learn spam, otherwise learn ham
  92. * @param L lua state
  93. * @param classifier NULL to learn all classifiers, name to learn a specific one
  94. * @param err error returned
  95. * @return TRUE if task has been learned
  96. */
  97. rspamd_stat_result_t rspamd_stat_learn (struct rspamd_task *task,
  98. gboolean spam, lua_State *L, const gchar *classifier,
  99. guint stage,
  100. GError **err);
  101. /**
  102. * Get the overall statistics for all statfile backends
  103. * @param cfg configuration
  104. * @param total_learns the total number of learns is stored here
  105. * @return array of statistical information
  106. */
  107. rspamd_stat_result_t rspamd_stat_statistics (struct rspamd_task *task,
  108. struct rspamd_config *cfg,
  109. guint64 *total_learns,
  110. ucl_object_t **res);
  111. void rspamd_stat_unload (void);
  112. #endif /* STAT_API_H_ */