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.

history.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* global d3:false FooTable:false */
  21. define(["jquery", "footable"],
  22. function ($) {
  23. "use strict";
  24. var ui = {};
  25. var prevVersion = null;
  26. function process_history_legacy(rspamd, data) {
  27. var items = [];
  28. var compare = function (e1, e2) {
  29. return e1.name.localeCompare(e2.name);
  30. };
  31. $("#selSymOrder_history, label[for='selSymOrder_history']").hide();
  32. $.each(data, function (i, item) {
  33. item.time = rspamd.unix_time_format(item.unix_time);
  34. rspamd.preprocess_item(rspamd, item);
  35. item.symbols = Object.keys(item.symbols)
  36. .map(function (key) {
  37. return item.symbols[key];
  38. })
  39. .sort(compare)
  40. .map(function (e) { return e.name; })
  41. .join(", ");
  42. item.time = {
  43. value: rspamd.unix_time_format(item.unix_time),
  44. options: {
  45. sortValue: item.unix_time
  46. }
  47. };
  48. items.push(item);
  49. });
  50. return {items:items};
  51. }
  52. function columns_v2() {
  53. return [{
  54. name: "id",
  55. title: "ID",
  56. style: {
  57. "font-size": "11px",
  58. "minWidth": 130,
  59. "overflow": "hidden",
  60. "textOverflow": "ellipsis",
  61. "wordBreak": "break-all",
  62. "whiteSpace": "normal"
  63. }
  64. }, {
  65. name: "ip",
  66. title: "IP address",
  67. breakpoints: "xs sm md",
  68. style: {
  69. "font-size": "11px",
  70. "minWidth": 88
  71. }
  72. }, {
  73. name: "sender_mime",
  74. title: "[Envelope From] From",
  75. breakpoints: "xs sm md",
  76. style: {
  77. "font-size": "11px",
  78. "minWidth": 100,
  79. "maxWidth": 200,
  80. "word-wrap": "break-word"
  81. }
  82. }, {
  83. name: "rcpt_mime_short",
  84. title: "[Envelope To] To/Cc/Bcc",
  85. breakpoints: "xs sm md",
  86. style: {
  87. "font-size": "11px",
  88. "minWidth": 100,
  89. "maxWidth": 200,
  90. "word-wrap": "break-word"
  91. }
  92. }, {
  93. name: "rcpt_mime",
  94. title: "[Envelope To] To/Cc/Bcc",
  95. breakpoints: "all",
  96. style: {
  97. "font-size": "11px",
  98. "word-wrap": "break-word"
  99. }
  100. }, {
  101. name: "subject",
  102. title: "Subject",
  103. breakpoints: "xs sm md",
  104. style: {
  105. "font-size": "11px",
  106. "word-break": "break-all",
  107. "minWidth": 150
  108. }
  109. }, {
  110. name: "action",
  111. title: "Action",
  112. style: {
  113. "font-size": "11px",
  114. "minwidth": 82
  115. }
  116. }, {
  117. name: "score",
  118. title: "Score",
  119. style: {
  120. "font-size": "11px",
  121. "maxWidth": 110
  122. },
  123. sortValue: function (val) { return Number(val.options.sortValue); }
  124. }, {
  125. name: "symbols",
  126. title: "Symbols<br /><br />" +
  127. '<span style="font-weight:normal;">Sort by:</span><br />' +
  128. '<div class="btn-group btn-group-toggle btn-group-xs btn-sym-order-history" data-toggle="buttons">' +
  129. '<label type="button" class="btn btn-outline-secondary btn-sym-history-magnitude">' +
  130. '<input type="radio" value="magnitude">Magnitude</label>' +
  131. '<label type="button" class="btn btn-outline-secondary btn-sym-history-score">' +
  132. '<input type="radio" value="score">Value</label>' +
  133. '<label type="button" class="btn btn-outline-secondary btn-sym-history-name">' +
  134. '<input type="radio" value="name">Name</label>' +
  135. "</div>",
  136. breakpoints: "all",
  137. style: {
  138. "font-size": "11px",
  139. "width": 550,
  140. "maxWidth": 550
  141. }
  142. }, {
  143. name: "size",
  144. title: "Msg size",
  145. breakpoints: "xs sm md",
  146. style: {
  147. "font-size": "11px",
  148. "minwidth": 50,
  149. },
  150. formatter: d3.format(".3~s")
  151. }, {
  152. name: "time_real",
  153. title: "Scan time",
  154. breakpoints: "xs sm md",
  155. style: {
  156. "font-size": "11px",
  157. "maxWidth": 72
  158. },
  159. sortValue: function (val) { return Number(val); }
  160. }, {
  161. sorted: true,
  162. direction: "DESC",
  163. name: "time",
  164. title: "Time",
  165. style: {
  166. "font-size": "11px"
  167. },
  168. sortValue: function (val) { return Number(val.options.sortValue); }
  169. }, {
  170. name: "user",
  171. title: "Authenticated user",
  172. breakpoints: "xs sm md",
  173. style: {
  174. "font-size": "11px",
  175. "minWidth": 100,
  176. "maxWidth": 130,
  177. "word-wrap": "break-word"
  178. }
  179. }];
  180. }
  181. function columns_legacy() {
  182. return [{
  183. name: "id",
  184. title: "ID",
  185. style: {
  186. "font-size": "11px",
  187. "width": 300,
  188. "maxWidth": 300,
  189. "overflow": "hidden",
  190. "textOverflow": "ellipsis",
  191. "wordBreak": "keep-all",
  192. "whiteSpace": "nowrap"
  193. }
  194. }, {
  195. name: "ip",
  196. title: "IP address",
  197. breakpoints: "xs sm",
  198. style: {
  199. "font-size": "11px",
  200. "width": 150,
  201. "maxWidth": 150
  202. }
  203. }, {
  204. name: "action",
  205. title: "Action",
  206. style: {
  207. "font-size": "11px",
  208. "width": 110,
  209. "maxWidth": 110
  210. }
  211. }, {
  212. name: "score",
  213. title: "Score",
  214. style: {
  215. "font-size": "11px",
  216. "maxWidth": 110
  217. },
  218. sortValue: function (val) { return Number(val.options.sortValue); }
  219. }, {
  220. name: "symbols",
  221. title: "Symbols",
  222. breakpoints: "all",
  223. style: {
  224. "font-size": "11px",
  225. "width": 550,
  226. "maxWidth": 550
  227. }
  228. }, {
  229. name: "size",
  230. title: "Message size",
  231. breakpoints: "xs sm",
  232. style: {
  233. "font-size": "11px",
  234. "width": 120,
  235. "maxWidth": 120
  236. },
  237. formatter: d3.format(".3~s")
  238. }, {
  239. name: "scan_time",
  240. title: "Scan time",
  241. breakpoints: "xs sm",
  242. style: {
  243. "font-size": "11px",
  244. "maxWidth": 80
  245. },
  246. sortValue: function (val) { return Number(val); }
  247. }, {
  248. sorted: true,
  249. direction: "DESC",
  250. name: "time",
  251. title: "Time",
  252. style: {
  253. "font-size": "11px"
  254. },
  255. sortValue: function (val) { return Number(val.options.sortValue); }
  256. }, {
  257. name: "user",
  258. title: "Authenticated user",
  259. breakpoints: "xs sm",
  260. style: {
  261. "font-size": "11px",
  262. "width": 200,
  263. "maxWidth": 200
  264. }
  265. }];
  266. }
  267. var columns = {
  268. 2: columns_v2,
  269. legacy: columns_legacy
  270. };
  271. function process_history_data(rspamd, data) {
  272. var process_functions = {
  273. 2: rspamd.process_history_v2,
  274. legacy: process_history_legacy
  275. };
  276. var pf = process_functions.legacy;
  277. if (data.version) {
  278. var strkey = data.version.toString();
  279. if (process_functions[strkey]) {
  280. pf = process_functions[strkey];
  281. }
  282. }
  283. return pf(rspamd, data, "history");
  284. }
  285. function get_history_columns(data) {
  286. var func = columns.legacy;
  287. if (data.version) {
  288. var strkey = data.version.toString();
  289. if (columns[strkey]) {
  290. func = columns[strkey];
  291. }
  292. }
  293. return func();
  294. }
  295. ui.getHistory = function (rspamd, tables) {
  296. rspamd.query("history", {
  297. success: function (req_data) {
  298. function differentVersions(neighbours_data) {
  299. var dv = neighbours_data.some(function (e) {
  300. return e.version !== neighbours_data[0].version;
  301. });
  302. if (dv) {
  303. rspamd.alertMessage("alert-error",
  304. "Neighbours history backend versions do not match. Cannot display history.");
  305. return true;
  306. }
  307. return false;
  308. }
  309. var neighbours_data = req_data
  310. .filter(function (d) { return d.status; }) // filter out unavailable neighbours
  311. .map(function (d) { return d.data; });
  312. if (neighbours_data.length && !differentVersions(neighbours_data)) {
  313. var data = {};
  314. var version = neighbours_data[0].version;
  315. if (version) {
  316. data.rows = [].concat.apply([], neighbours_data
  317. .map(function (e) {
  318. return e.rows;
  319. }));
  320. data.version = version;
  321. } else {
  322. // Legacy version
  323. data = [].concat.apply([], neighbours_data);
  324. }
  325. var o = process_history_data(rspamd, data);
  326. var items = o.items;
  327. rspamd.symbols.history = o.symbols;
  328. if (Object.prototype.hasOwnProperty.call(tables, "history") &&
  329. version === prevVersion) {
  330. tables.history.rows.load(items);
  331. if (version) { // Non-legacy
  332. // Is there a way to get an event when all rows are loaded?
  333. rspamd.waitForRowsDisplayed("history", items.length, function () {
  334. rspamd.drawTooltips();
  335. });
  336. }
  337. } else {
  338. rspamd.destroyTable("history");
  339. // Is there a way to get an event when the table is destroyed?
  340. setTimeout(function () {
  341. rspamd.initHistoryTable(rspamd, data, items, "history", get_history_columns(data), false);
  342. }, 200);
  343. }
  344. prevVersion = version;
  345. } else {
  346. rspamd.destroyTable("history");
  347. }
  348. },
  349. errorMessage: "Cannot receive history",
  350. });
  351. };
  352. ui.setup = function (rspamd, tables) {
  353. rspamd.set_page_size("history", $("#history_page_size").val());
  354. rspamd.bindHistoryTableEventHandlers("history", 8);
  355. $("#updateHistory").off("click");
  356. $("#updateHistory").on("click", function (e) {
  357. e.preventDefault();
  358. ui.getHistory(rspamd, tables);
  359. });
  360. // @reset history log
  361. $("#resetHistory").off("click");
  362. $("#resetHistory").on("click", function (e) {
  363. e.preventDefault();
  364. if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
  365. return;
  366. }
  367. rspamd.destroyTable("history");
  368. rspamd.destroyTable("errors");
  369. rspamd.query("historyreset", {
  370. success: function () {
  371. ui.getHistory(rspamd, tables);
  372. ui.getErrors(rspamd, tables);
  373. },
  374. errorMessage: "Cannot reset history log"
  375. });
  376. });
  377. };
  378. function initErrorsTable(rspamd, tables, rows) {
  379. tables.errors = FooTable.init("#errorsLog", {
  380. columns: [
  381. {sorted:true, direction:"DESC", name:"ts", title:"Time", style:{"font-size":"11px", "width":300, "maxWidth":300}},
  382. {name:"type", title:"Worker type", breakpoints:"xs sm", style:{"font-size":"11px", "width":150, "maxWidth":150}},
  383. {name:"pid", title:"PID", breakpoints:"xs sm", style:{"font-size":"11px", "width":110, "maxWidth":110}},
  384. {name:"module", title:"Module", style:{"font-size":"11px"}},
  385. {name:"id", title:"Internal ID", style:{"font-size":"11px"}},
  386. {name:"message", title:"Message", breakpoints:"xs sm", style:{"font-size":"11px"}},
  387. ],
  388. rows: rows,
  389. paging: {
  390. enabled: true,
  391. limit: 5,
  392. size: rspamd.page_size.errors
  393. },
  394. filtering: {
  395. enabled: true,
  396. position: "left",
  397. connectors: false
  398. },
  399. sorting: {
  400. enabled: true
  401. }
  402. });
  403. }
  404. ui.getErrors = function (rspamd, tables) {
  405. if (rspamd.read_only) return;
  406. rspamd.query("errors", {
  407. success: function (data) {
  408. var neighbours_data = data
  409. .filter(function (d) {
  410. return d.status;
  411. }) // filter out unavailable neighbours
  412. .map(function (d) {
  413. return d.data;
  414. });
  415. var rows = [].concat.apply([], neighbours_data);
  416. $.each(rows, function (i, item) {
  417. item.ts = {
  418. value: rspamd.unix_time_format(item.ts),
  419. options: {
  420. sortValue: item.ts
  421. }
  422. };
  423. });
  424. if (Object.prototype.hasOwnProperty.call(tables, "errors")) {
  425. tables.errors.rows.load(rows);
  426. } else {
  427. initErrorsTable(rspamd, tables, rows);
  428. }
  429. }
  430. });
  431. $("#updateErrors").off("click");
  432. $("#updateErrors").on("click", function (e) {
  433. e.preventDefault();
  434. ui.getErrors(rspamd, tables);
  435. });
  436. };
  437. return ui;
  438. });