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 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 FooTable:false */
  21. define(["jquery", "footable", "humanize"],
  22. function ($, _, Humanize) {
  23. "use strict";
  24. var page_size = {
  25. errors: 25,
  26. history: 25
  27. };
  28. function set_page_size(n, callback) {
  29. if (n !== page_size.history && n > 0) {
  30. page_size.history = n;
  31. if (callback) {
  32. return callback(n);
  33. }
  34. }
  35. return null;
  36. }
  37. set_page_size($("#history_page_size").val());
  38. var ui = {};
  39. var prevVersion = null;
  40. var htmlEscapes = {
  41. "&": "&amp;",
  42. "<": "&lt;",
  43. ">": "&gt;",
  44. "\"": "&quot;",
  45. "'": "&#39;",
  46. "/": "&#x2F;",
  47. "`": "&#x60;",
  48. "=": "&#x3D;"
  49. };
  50. var htmlEscaper = /[&<>"'/`=]/g;
  51. var symbols = [];
  52. var symbolDescriptions = {};
  53. var escapeHTML = function (string) {
  54. return String(string).replace(htmlEscaper, function (match) {
  55. return htmlEscapes[match];
  56. });
  57. };
  58. var escape_HTML_array = function (arr) {
  59. arr.forEach(function (d, i) { arr[i] = escapeHTML(d); });
  60. };
  61. function unix_time_format(tm) {
  62. var date = new Date(tm ? tm * 1000 : 0);
  63. return date.toLocaleString();
  64. }
  65. function preprocess_item(item) {
  66. for (var prop in item) {
  67. if (!{}.hasOwnProperty.call(item, prop)) continue;
  68. switch (prop) {
  69. case "rcpt_mime":
  70. case "rcpt_smtp":
  71. escape_HTML_array(item[prop]);
  72. break;
  73. case "symbols":
  74. Object.keys(item.symbols).forEach(function (key) {
  75. var sym = item.symbols[key];
  76. if (!sym.name) {
  77. sym.name = key;
  78. }
  79. sym.name = escapeHTML(sym.name);
  80. if (sym.description) {
  81. sym.description = escapeHTML(sym.description);
  82. }
  83. if (sym.options) {
  84. escape_HTML_array(sym.options);
  85. }
  86. });
  87. break;
  88. default:
  89. if (typeof item[prop] === "string") {
  90. item[prop] = escapeHTML(item[prop]);
  91. }
  92. }
  93. }
  94. if (item.action === "clean" || item.action === "no action") {
  95. item.action = "<div style='font-size:11px' class='label label-success'>" + item.action + "</div>";
  96. } else if (item.action === "rewrite subject" || item.action === "add header" || item.action === "probable spam") {
  97. item.action = "<div style='font-size:11px' class='label label-warning'>" + item.action + "</div>";
  98. } else if (item.action === "spam" || item.action === "reject") {
  99. item.action = "<div style='font-size:11px' class='label label-danger'>" + item.action + "</div>";
  100. } else {
  101. item.action = "<div style='font-size:11px' class='label label-info'>" + item.action + "</div>";
  102. }
  103. var score_content = (item.score < item.required_score)
  104. ? "<span class='text-success'>" + item.score.toFixed(2) + " / " + item.required_score + "</span>"
  105. : "<span class='text-danger'>" + item.score.toFixed(2) + " / " + item.required_score + "</span>";
  106. item.score = {
  107. options: {
  108. sortValue: item.score
  109. },
  110. value: score_content
  111. };
  112. }
  113. function getSelector(id) {
  114. var e = document.getElementById(id);
  115. return e.options[e.selectedIndex].value;
  116. }
  117. function get_compare_function() {
  118. var compare_functions = {
  119. magnitude: function (e1, e2) {
  120. return Math.abs(e2.score) - Math.abs(e1.score);
  121. },
  122. name: function (e1, e2) {
  123. return e1.name.localeCompare(e2.name);
  124. },
  125. score: function (e1, e2) {
  126. return e2.score - e1.score;
  127. }
  128. };
  129. return compare_functions[getSelector("selSymOrder")];
  130. }
  131. function sort_symbols(o, compare_function) {
  132. return Object.keys(o)
  133. .map(function (key) {
  134. return o[key];
  135. })
  136. .sort(compare_function)
  137. .map(function (e) { return e.str; })
  138. .join("<br>\n");
  139. }
  140. function process_history_v2(data) {
  141. // Display no more than rcpt_lim recipients
  142. var rcpt_lim = 3;
  143. var items = [];
  144. var unsorted_symbols = [];
  145. var compare_function = get_compare_function();
  146. $("#selSymOrder, label[for='selSymOrder']").show();
  147. $.each(data.rows,
  148. function (i, item) {
  149. function more(p) {
  150. var l = item[p].length;
  151. return (l > rcpt_lim) ? " … (" + l + ")" : "";
  152. }
  153. function format_rcpt(smtp, mime) {
  154. var full = "";
  155. var shrt = "";
  156. if (smtp) {
  157. full = "[" + item.rcpt_smtp.join(", ") + "] ";
  158. shrt = "[" + item.rcpt_smtp.slice(0, rcpt_lim).join(",&#8203;") + more("rcpt_smtp") + "]";
  159. if (mime) {
  160. full += " ";
  161. shrt += " ";
  162. }
  163. }
  164. if (mime) {
  165. full += item.rcpt_mime.join(", ");
  166. shrt += item.rcpt_mime.slice(0, rcpt_lim).join(",&#8203;") + more("rcpt_mime");
  167. }
  168. return {full:full, shrt:shrt};
  169. }
  170. function get_symbol_class(name, score) {
  171. if (name.match(/^GREYLIST$/)) {
  172. return "symbol-special";
  173. }
  174. if (score < 0) {
  175. return "symbol-negative";
  176. } else if (score > 0) {
  177. return "symbol-positive";
  178. }
  179. return null;
  180. }
  181. preprocess_item(item);
  182. Object.keys(item.symbols).forEach(function (key) {
  183. var sym = item.symbols[key];
  184. sym.str = '<span class="symbol-default ' + get_symbol_class(sym.name, sym.score) + '"><strong>';
  185. if (sym.description) {
  186. sym.str += '<abbr data-sym-key="' + key + '">' +
  187. sym.name + "</abbr></strong> (" + sym.score + ")</span>";
  188. // Store description for tooltip
  189. symbolDescriptions[key] = sym.description;
  190. } else {
  191. sym.str += sym.name + "</strong> (" + sym.score + ")</span>";
  192. }
  193. if (sym.options) {
  194. sym.str += " [" + sym.options.join(",") + "]";
  195. }
  196. });
  197. unsorted_symbols.push(item.symbols);
  198. item.symbols = sort_symbols(item.symbols, compare_function);
  199. item.time = {
  200. value: unix_time_format(item.unix_time),
  201. options: {
  202. sortValue: item.unix_time
  203. }
  204. };
  205. var scan_time = item.time_real.toFixed(3) + " / " + item.time_virtual.toFixed(3);
  206. item.scan_time = {
  207. options: {
  208. sortValue: item.time_real
  209. },
  210. value: scan_time
  211. };
  212. item.id = item["message-id"];
  213. var rcpt = {};
  214. if (!item.rcpt_mime.length) {
  215. rcpt = format_rcpt(true, false);
  216. } else if ($(item.rcpt_mime).not(item.rcpt_smtp).length !== 0 || $(item.rcpt_smtp).not(item.rcpt_mime).length !== 0) {
  217. rcpt = format_rcpt(true, true);
  218. } else {
  219. rcpt = format_rcpt(false, true);
  220. }
  221. item.rcpt_mime_short = rcpt.shrt;
  222. item.rcpt_mime = rcpt.full;
  223. if (item.sender_mime !== item.sender_smtp) {
  224. item.sender_mime = "[" + item.sender_smtp + "] " + item.sender_mime;
  225. }
  226. items.push(item);
  227. });
  228. return {items:items, symbols:unsorted_symbols};
  229. }
  230. function process_history_legacy(data) {
  231. var items = [];
  232. var compare = function (e1, e2) {
  233. return e1.name.localeCompare(e2.name);
  234. };
  235. $("#selSymOrder, label[for='selSymOrder']").hide();
  236. $.each(data, function (i, item) {
  237. item.time = unix_time_format(item.unix_time);
  238. preprocess_item(item);
  239. item.scan_time = {
  240. options: {
  241. sortValue: item.scan_time
  242. },
  243. value: item.scan_time
  244. };
  245. item.symbols = Object.keys(item.symbols)
  246. .map(function (key) {
  247. return item.symbols[key];
  248. })
  249. .sort(compare)
  250. .map(function (e) { return e.name; })
  251. .join(", ");
  252. item.time = {
  253. value: unix_time_format(item.unix_time),
  254. options: {
  255. sortValue: item.unix_time
  256. }
  257. };
  258. items.push(item);
  259. });
  260. return {items:items};
  261. }
  262. function columns_v2() {
  263. return [{
  264. name: "id",
  265. title: "ID",
  266. style: {
  267. "font-size": "11px",
  268. "minWidth": 130,
  269. "overflow": "hidden",
  270. "textOverflow": "ellipsis",
  271. "wordBreak": "break-all",
  272. "whiteSpace": "normal"
  273. }
  274. }, {
  275. name: "ip",
  276. title: "IP address",
  277. breakpoints: "xs sm md",
  278. style: {
  279. "font-size": "11px",
  280. "minWidth": 88
  281. }
  282. }, {
  283. name: "sender_mime",
  284. title: "[Envelope From] From",
  285. breakpoints: "xs sm md",
  286. style: {
  287. "font-size": "11px",
  288. "minWidth": 100,
  289. "maxWidth": 200,
  290. "word-wrap": "break-word"
  291. }
  292. }, {
  293. name: "rcpt_mime_short",
  294. title: "[Envelope To] To/Cc/Bcc",
  295. breakpoints: "xs sm md",
  296. style: {
  297. "font-size": "11px",
  298. "minWidth": 100,
  299. "maxWidth": 200,
  300. "word-wrap": "break-word"
  301. }
  302. }, {
  303. name: "rcpt_mime",
  304. title: "[Envelope To] To/Cc/Bcc",
  305. breakpoints: "all",
  306. style: {
  307. "font-size": "11px",
  308. "word-wrap": "break-word"
  309. }
  310. }, {
  311. name: "subject",
  312. title: "Subject",
  313. breakpoints: "xs sm md",
  314. style: {
  315. "font-size": "11px",
  316. "word-break": "break-all",
  317. "minWidth": 150
  318. }
  319. }, {
  320. name: "action",
  321. title: "Action",
  322. style: {
  323. "font-size": "11px",
  324. "minwidth": 82
  325. }
  326. }, {
  327. name: "score",
  328. title: "Score",
  329. style: {
  330. "font-size": "11px",
  331. "maxWidth": 110
  332. },
  333. sortValue: function (val) { return Number(val.options.sortValue); }
  334. }, {
  335. name: "symbols",
  336. title: "Symbols<br /><br />" +
  337. '<span style="font-weight:normal;">Sort by:</span><br />' +
  338. '<div class="btn-group btn-group-xs btn-sym-order" data-toggle="buttons">' +
  339. '<button type="button" class="btn btn-default btn-sym-magnitude" value="magnitude">Magnitude</button>' +
  340. '<button type="button" class="btn btn-default btn-sym-score" value="score">Value</button>' +
  341. '<button type="button" class="btn btn-default btn-sym-name" value="name">Name</button>' +
  342. "</div>",
  343. breakpoints: "all",
  344. style: {
  345. "font-size": "11px",
  346. "width": 550,
  347. "maxWidth": 550
  348. }
  349. }, {
  350. name: "size",
  351. title: "Msg size",
  352. breakpoints: "xs sm md",
  353. style: {
  354. "font-size": "11px",
  355. "minwidth": 50,
  356. },
  357. formatter: Humanize.compactInteger
  358. }, {
  359. name: "scan_time",
  360. title: "Scan time",
  361. breakpoints: "xs sm md",
  362. style: {
  363. "font-size": "11px",
  364. "maxWidth": 72
  365. },
  366. sortValue: function (val) { return Number(val.options.sortValue); }
  367. }, {
  368. sorted: true,
  369. direction: "DESC",
  370. name: "time",
  371. title: "Time",
  372. style: {
  373. "font-size": "11px"
  374. },
  375. sortValue: function (val) { return Number(val.options.sortValue); }
  376. }, {
  377. name: "user",
  378. title: "Authenticated user",
  379. breakpoints: "xs sm md",
  380. style: {
  381. "font-size": "11px",
  382. "minWidth": 100,
  383. "maxWidth": 130,
  384. "word-wrap": "break-word"
  385. }
  386. }];
  387. }
  388. function columns_legacy() {
  389. return [{
  390. name: "id",
  391. title: "ID",
  392. style: {
  393. "font-size": "11px",
  394. "width": 300,
  395. "maxWidth": 300,
  396. "overflow": "hidden",
  397. "textOverflow": "ellipsis",
  398. "wordBreak": "keep-all",
  399. "whiteSpace": "nowrap"
  400. }
  401. }, {
  402. name: "ip",
  403. title: "IP address",
  404. breakpoints: "xs sm",
  405. style: {
  406. "font-size": "11px",
  407. "width": 150,
  408. "maxWidth": 150
  409. }
  410. }, {
  411. name: "action",
  412. title: "Action",
  413. style: {
  414. "font-size": "11px",
  415. "width": 110,
  416. "maxWidth": 110
  417. }
  418. }, {
  419. name: "score",
  420. title: "Score",
  421. style: {
  422. "font-size": "11px",
  423. "maxWidth": 110
  424. },
  425. sortValue: function (val) { return Number(val.options.sortValue); }
  426. }, {
  427. name: "symbols",
  428. title: "Symbols",
  429. breakpoints: "all",
  430. style: {
  431. "font-size": "11px",
  432. "width": 550,
  433. "maxWidth": 550
  434. }
  435. }, {
  436. name: "size",
  437. title: "Message size",
  438. breakpoints: "xs sm",
  439. style: {
  440. "font-size": "11px",
  441. "width": 120,
  442. "maxWidth": 120
  443. },
  444. formatter: Humanize.compactInteger
  445. }, {
  446. name: "scan_time",
  447. title: "Scan time",
  448. breakpoints: "xs sm",
  449. style: {
  450. "font-size": "11px",
  451. "maxWidth": 80
  452. },
  453. sortValue: function (val) { return Number(val.options.sortValue); }
  454. }, {
  455. sorted: true,
  456. direction: "DESC",
  457. name: "time",
  458. title: "Time",
  459. style: {
  460. "font-size": "11px"
  461. },
  462. sortValue: function (val) { return Number(val.options.sortValue); }
  463. }, {
  464. name: "user",
  465. title: "Authenticated user",
  466. breakpoints: "xs sm",
  467. style: {
  468. "font-size": "11px",
  469. "width": 200,
  470. "maxWidth": 200
  471. }
  472. }];
  473. }
  474. var process_functions = {
  475. 2: process_history_v2,
  476. legacy: process_history_legacy
  477. };
  478. var columns = {
  479. 2: columns_v2,
  480. legacy: columns_legacy
  481. };
  482. function process_history_data(data) {
  483. var pf = process_functions.legacy;
  484. if (data.version) {
  485. var strkey = data.version.toString();
  486. if (process_functions[strkey]) {
  487. pf = process_functions[strkey];
  488. }
  489. }
  490. return pf(data);
  491. }
  492. function get_history_columns(data) {
  493. var func = columns.legacy;
  494. if (data.version) {
  495. var strkey = data.version.toString();
  496. if (columns[strkey]) {
  497. func = columns[strkey];
  498. }
  499. }
  500. return func();
  501. }
  502. function drawTooltips() {
  503. // Update symbol description tooltips
  504. $.each(symbolDescriptions, function (key, description) {
  505. $("abbr[data-sym-key=" + key + "]").tooltip({
  506. placement: "bottom",
  507. html: true,
  508. title: description
  509. });
  510. });
  511. }
  512. function initHistoryTable(rspamd, tables, data, items) {
  513. /* eslint-disable consistent-this, no-underscore-dangle, one-var-declaration-per-line */
  514. FooTable.actionFilter = FooTable.Filtering.extend({
  515. construct: function (instance) {
  516. this._super(instance);
  517. this.actions = ["reject", "add header", "greylist",
  518. "no action", "soft reject", "rewrite subject"];
  519. this.def = "Any action";
  520. this.$action = null;
  521. },
  522. $create: function () {
  523. this._super();
  524. var self = this, $form_grp = $("<div/>", {
  525. class: "form-group"
  526. }).append($("<label/>", {
  527. class: "sr-only",
  528. text: "Action"
  529. })).prependTo(self.$form);
  530. self.$action = $("<select/>", {
  531. class: "form-control"
  532. }).on("change", {
  533. self: self
  534. }, self._onStatusDropdownChanged).append(
  535. $("<option/>", {
  536. text: self.def
  537. })).appendTo($form_grp);
  538. $.each(self.actions, function (i, action) {
  539. self.$action.append($("<option/>").text(action));
  540. });
  541. },
  542. _onStatusDropdownChanged: function (e) {
  543. var self = e.data.self, selected = $(this).val();
  544. if (selected !== self.def) {
  545. if (selected === "reject") {
  546. self.addFilter("action", "reject -soft", ["action"]);
  547. } else {
  548. self.addFilter("action", selected, ["action"]);
  549. }
  550. } else {
  551. self.removeFilter("action");
  552. }
  553. self.filter();
  554. },
  555. draw: function () {
  556. this._super();
  557. var action = this.find("action");
  558. if (action instanceof FooTable.Filter) {
  559. if (action.query.val() === "reject -soft") {
  560. this.$action.val("reject");
  561. } else {
  562. this.$action.val(action.query.val());
  563. }
  564. } else {
  565. this.$action.val(this.def);
  566. }
  567. }
  568. });
  569. /* eslint-enable consistent-this, no-underscore-dangle, one-var-declaration-per-line */
  570. tables.history = FooTable.init("#historyTable", {
  571. columns: get_history_columns(data),
  572. rows: items,
  573. paging: {
  574. enabled: true,
  575. limit: 5,
  576. size: page_size.history
  577. },
  578. filtering: {
  579. enabled: true,
  580. position: "left",
  581. connectors: false
  582. },
  583. sorting: {
  584. enabled: true
  585. },
  586. components: {
  587. filtering: FooTable.actionFilter
  588. },
  589. on: {
  590. "ready.ft.table": drawTooltips,
  591. "after.ft.sorting": drawTooltips,
  592. "after.ft.paging": drawTooltips,
  593. "after.ft.filtering": drawTooltips,
  594. "expand.ft.row": function (e, ft, row) {
  595. setTimeout(function () {
  596. var detail_row = row.$el.next();
  597. var order = getSelector("selSymOrder");
  598. detail_row.find(".btn-sym-" + order)
  599. .addClass("active").siblings().removeClass("active");
  600. }, 5);
  601. }
  602. }
  603. });
  604. }
  605. function destroyTable(tables, table) {
  606. if (tables[table]) {
  607. tables[table].destroy();
  608. delete tables[table];
  609. }
  610. }
  611. ui.getHistory = function (rspamd, tables) {
  612. function waitForRowsDisplayed(rows_total, callback, iteration) {
  613. var i = (typeof iteration === "undefined") ? 10 : iteration;
  614. var num_rows = $("#historyTable > tbody > tr").length;
  615. if (num_rows === page_size.history ||
  616. num_rows === rows_total) {
  617. return callback();
  618. } else if (--i) {
  619. setTimeout(function () {
  620. waitForRowsDisplayed(rows_total, callback, i);
  621. }, 500);
  622. }
  623. return null;
  624. }
  625. rspamd.query("history", {
  626. success: function (req_data) {
  627. function differentVersions(neighbours_data) {
  628. var dv = neighbours_data.some(function (e) {
  629. return e.version !== neighbours_data[0].version;
  630. });
  631. if (dv) {
  632. rspamd.alertMessage("alert-error",
  633. "Neighbours history backend versions do not match. Cannot display history.");
  634. return true;
  635. }
  636. return false;
  637. }
  638. var neighbours_data = req_data
  639. .filter(function (d) { return d.status; }) // filter out unavailable neighbours
  640. .map(function (d) { return d.data; });
  641. if (neighbours_data.length && !differentVersions(neighbours_data)) {
  642. var data = {};
  643. var version = neighbours_data[0].version;
  644. if (version) {
  645. data.rows = [].concat.apply([], neighbours_data
  646. .map(function (e) {
  647. return e.rows;
  648. }));
  649. data.version = version;
  650. } else {
  651. // Legacy version
  652. data = [].concat.apply([], neighbours_data);
  653. }
  654. var o = process_history_data(data);
  655. var items = o.items;
  656. symbols = o.symbols;
  657. if (Object.prototype.hasOwnProperty.call(tables, "history") &&
  658. version === prevVersion) {
  659. tables.history.rows.load(items);
  660. if (version) { // Non-legacy
  661. // Is there a way to get an event when all rows are loaded?
  662. waitForRowsDisplayed(items.length, function () {
  663. drawTooltips();
  664. });
  665. }
  666. } else {
  667. destroyTable(tables, "history");
  668. // Is there a way to get an event when the table is destroyed?
  669. setTimeout(function () {
  670. initHistoryTable(rspamd, tables, data, items);
  671. }, 200);
  672. }
  673. prevVersion = version;
  674. } else {
  675. destroyTable(tables, "history");
  676. }
  677. },
  678. errorMessage: "Cannot receive history",
  679. });
  680. };
  681. ui.setup = function (rspamd, tables) {
  682. function change_symbols_order(order) {
  683. $(".btn-sym-" + order).addClass("active").siblings().removeClass("active");
  684. var compare_function = get_compare_function();
  685. $.each(tables.history.rows.all, function (i, row) {
  686. var cell_val = sort_symbols(symbols[i], compare_function);
  687. row.cells[8].val(cell_val, false, true);
  688. });
  689. drawTooltips();
  690. }
  691. $("#updateHistory").off("click");
  692. $("#updateHistory").on("click", function (e) {
  693. e.preventDefault();
  694. ui.getHistory(rspamd, tables);
  695. });
  696. $("#selSymOrder").unbind().change(function () {
  697. var order = this.value;
  698. change_symbols_order(order);
  699. });
  700. $("#history_page_size").change(function () {
  701. set_page_size(this.value, function (n) { tables.history.pageSize(n); });
  702. });
  703. $(document).on("click", ".btn-sym-order button", function () {
  704. var order = this.value;
  705. $("#selSymOrder").val(order);
  706. change_symbols_order(order);
  707. });
  708. // @reset history log
  709. $("#resetHistory").off("click");
  710. $("#resetHistory").on("click", function (e) {
  711. e.preventDefault();
  712. if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
  713. return;
  714. }
  715. destroyTable(tables, "history");
  716. destroyTable(tables, "errors");
  717. rspamd.query("historyreset", {
  718. success: function () {
  719. ui.getHistory(rspamd, tables);
  720. ui.getErrors(rspamd, tables);
  721. },
  722. errorMessage: "Cannot reset history log"
  723. });
  724. });
  725. };
  726. function initErrorsTable(tables, rows) {
  727. tables.errors = FooTable.init("#errorsLog", {
  728. columns: [
  729. {sorted:true, direction:"DESC", name:"ts", title:"Time", style:{"font-size":"11px", "width":300, "maxWidth":300}},
  730. {name:"type", title:"Worker type", breakpoints:"xs sm", style:{"font-size":"11px", "width":150, "maxWidth":150}},
  731. {name:"pid", title:"PID", breakpoints:"xs sm", style:{"font-size":"11px", "width":110, "maxWidth":110}},
  732. {name:"module", title:"Module", style:{"font-size":"11px"}},
  733. {name:"id", title:"Internal ID", style:{"font-size":"11px"}},
  734. {name:"message", title:"Message", breakpoints:"xs sm", style:{"font-size":"11px"}},
  735. ],
  736. rows: rows,
  737. paging: {
  738. enabled: true,
  739. limit: 5,
  740. size: page_size.errors
  741. },
  742. filtering: {
  743. enabled: true,
  744. position: "left",
  745. connectors: false
  746. },
  747. sorting: {
  748. enabled: true
  749. }
  750. });
  751. }
  752. ui.getErrors = function (rspamd, tables) {
  753. if (rspamd.read_only) return;
  754. rspamd.query("errors", {
  755. success: function (data) {
  756. var neighbours_data = data
  757. .filter(function (d) {
  758. return d.status;
  759. }) // filter out unavailable neighbours
  760. .map(function (d) {
  761. return d.data;
  762. });
  763. var rows = [].concat.apply([], neighbours_data);
  764. $.each(rows, function (i, item) {
  765. item.ts = unix_time_format(item.ts);
  766. });
  767. if (Object.prototype.hasOwnProperty.call(tables, "errors")) {
  768. tables.errors.rows.load(rows);
  769. } else {
  770. initErrorsTable(tables, rows);
  771. }
  772. }
  773. });
  774. $("#updateErrors").off("click");
  775. $("#updateErrors").on("click", function (e) {
  776. e.preventDefault();
  777. ui.getErrors(rspamd, tables);
  778. });
  779. };
  780. return ui;
  781. });