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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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": "calc(7.6em + 8px)",
  71. "word-break": "break-all"
  72. }
  73. }, {
  74. name: "sender_mime",
  75. title: "[Envelope From] From",
  76. breakpoints: "xs sm md",
  77. style: {
  78. "font-size": "11px",
  79. "minWidth": 100,
  80. "maxWidth": 200,
  81. "word-wrap": "break-word"
  82. }
  83. }, {
  84. name: "rcpt_mime_short",
  85. title: "[Envelope To] To/Cc/Bcc",
  86. breakpoints: "xs sm md",
  87. style: {
  88. "font-size": "11px",
  89. "minWidth": 100,
  90. "maxWidth": 200,
  91. "word-wrap": "break-word"
  92. }
  93. }, {
  94. name: "rcpt_mime",
  95. title: "[Envelope To] To/Cc/Bcc",
  96. breakpoints: "all",
  97. style: {
  98. "font-size": "11px",
  99. "word-wrap": "break-word"
  100. }
  101. }, {
  102. name: "subject",
  103. title: "Subject",
  104. breakpoints: "xs sm md",
  105. style: {
  106. "font-size": "11px",
  107. "word-break": "break-all",
  108. "minWidth": 150
  109. }
  110. }, {
  111. name: "action",
  112. title: "Action",
  113. style: {
  114. "font-size": "11px",
  115. "minwidth": 82
  116. }
  117. }, {
  118. name: "score",
  119. title: "Score",
  120. style: {
  121. "font-size": "11px",
  122. "maxWidth": 110,
  123. "text-align": "right",
  124. "white-space": "nowrap"
  125. },
  126. sortValue: function (val) { return Number(val.options.sortValue); }
  127. }, {
  128. name: "symbols",
  129. title: "Symbols<br /><br />" +
  130. '<span style="font-weight:normal;">Sort by:</span><br />' +
  131. '<div class="btn-group btn-group-toggle btn-group-xs btn-sym-order-history" data-toggle="buttons">' +
  132. '<label type="button" class="btn btn-outline-secondary btn-sym-history-magnitude">' +
  133. '<input type="radio" value="magnitude">Magnitude</label>' +
  134. '<label type="button" class="btn btn-outline-secondary btn-sym-history-score">' +
  135. '<input type="radio" value="score">Value</label>' +
  136. '<label type="button" class="btn btn-outline-secondary btn-sym-history-name">' +
  137. '<input type="radio" value="name">Name</label>' +
  138. "</div>",
  139. breakpoints: "all",
  140. style: {
  141. "font-size": "11px",
  142. "width": 550,
  143. "maxWidth": 550
  144. }
  145. }, {
  146. name: "size",
  147. title: "Msg size",
  148. breakpoints: "xs sm md",
  149. style: {
  150. "font-size": "11px",
  151. "minwidth": 50,
  152. },
  153. formatter: d3.format(".3~s")
  154. }, {
  155. name: "time_real",
  156. title: "Scan time",
  157. breakpoints: "xs sm md",
  158. style: {
  159. "font-size": "11px",
  160. "maxWidth": 72
  161. },
  162. sortValue: function (val) { return Number(val); }
  163. }, {
  164. classes: "history-col-time",
  165. sorted: true,
  166. direction: "DESC",
  167. name: "time",
  168. title: "Time",
  169. style: {
  170. "font-size": "11px"
  171. },
  172. sortValue: function (val) { return Number(val.options.sortValue); }
  173. }, {
  174. name: "user",
  175. title: "Authenticated user",
  176. breakpoints: "xs sm md",
  177. style: {
  178. "font-size": "11px",
  179. "minWidth": 100,
  180. "maxWidth": 130,
  181. "word-wrap": "break-word"
  182. }
  183. }];
  184. }
  185. function columns_legacy() {
  186. return [{
  187. name: "id",
  188. title: "ID",
  189. style: {
  190. "font-size": "11px",
  191. "width": 300,
  192. "maxWidth": 300,
  193. "overflow": "hidden",
  194. "textOverflow": "ellipsis",
  195. "wordBreak": "keep-all",
  196. "whiteSpace": "nowrap"
  197. }
  198. }, {
  199. name: "ip",
  200. title: "IP address",
  201. breakpoints: "xs sm",
  202. style: {
  203. "font-size": "11px",
  204. "width": 150,
  205. "maxWidth": 150
  206. }
  207. }, {
  208. name: "action",
  209. title: "Action",
  210. style: {
  211. "font-size": "11px",
  212. "width": 110,
  213. "maxWidth": 110
  214. }
  215. }, {
  216. name: "score",
  217. title: "Score",
  218. style: {
  219. "font-size": "11px",
  220. "maxWidth": 110
  221. },
  222. sortValue: function (val) { return Number(val.options.sortValue); }
  223. }, {
  224. name: "symbols",
  225. title: "Symbols",
  226. breakpoints: "all",
  227. style: {
  228. "font-size": "11px",
  229. "width": 550,
  230. "maxWidth": 550
  231. }
  232. }, {
  233. name: "size",
  234. title: "Message size",
  235. breakpoints: "xs sm",
  236. style: {
  237. "font-size": "11px",
  238. "width": 120,
  239. "maxWidth": 120
  240. },
  241. formatter: d3.format(".3~s")
  242. }, {
  243. name: "scan_time",
  244. title: "Scan time",
  245. breakpoints: "xs sm",
  246. style: {
  247. "font-size": "11px",
  248. "maxWidth": 80
  249. },
  250. sortValue: function (val) { return Number(val); }
  251. }, {
  252. sorted: true,
  253. direction: "DESC",
  254. name: "time",
  255. title: "Time",
  256. style: {
  257. "font-size": "11px"
  258. },
  259. sortValue: function (val) { return Number(val.options.sortValue); }
  260. }, {
  261. name: "user",
  262. title: "Authenticated user",
  263. breakpoints: "xs sm",
  264. style: {
  265. "font-size": "11px",
  266. "width": 200,
  267. "maxWidth": 200
  268. }
  269. }];
  270. }
  271. var columns = {
  272. 2: columns_v2,
  273. legacy: columns_legacy
  274. };
  275. function process_history_data(rspamd, data) {
  276. var process_functions = {
  277. 2: rspamd.process_history_v2,
  278. legacy: process_history_legacy
  279. };
  280. var pf = process_functions.legacy;
  281. if (data.version) {
  282. var strkey = data.version.toString();
  283. if (process_functions[strkey]) {
  284. pf = process_functions[strkey];
  285. }
  286. }
  287. return pf(rspamd, data, "history");
  288. }
  289. function get_history_columns(data) {
  290. var func = columns.legacy;
  291. if (data.version) {
  292. var strkey = data.version.toString();
  293. if (columns[strkey]) {
  294. func = columns[strkey];
  295. }
  296. }
  297. return func();
  298. }
  299. ui.getHistory = function (rspamd, tables) {
  300. rspamd.query("history", {
  301. success: function (req_data) {
  302. function differentVersions(neighbours_data) {
  303. var dv = neighbours_data.some(function (e) {
  304. return e.version !== neighbours_data[0].version;
  305. });
  306. if (dv) {
  307. rspamd.alertMessage("alert-error",
  308. "Neighbours history backend versions do not match. Cannot display history.");
  309. return true;
  310. }
  311. return false;
  312. }
  313. var neighbours_data = req_data
  314. .filter(function (d) { return d.status; }) // filter out unavailable neighbours
  315. .map(function (d) { return d.data; });
  316. if (neighbours_data.length && !differentVersions(neighbours_data)) {
  317. var data = {};
  318. var version = neighbours_data[0].version;
  319. if (version) {
  320. data.rows = [].concat.apply([], neighbours_data
  321. .map(function (e) {
  322. return e.rows;
  323. }));
  324. data.version = version;
  325. } else {
  326. // Legacy version
  327. data = [].concat.apply([], neighbours_data);
  328. }
  329. var o = process_history_data(rspamd, data);
  330. var items = o.items;
  331. rspamd.symbols.history = o.symbols;
  332. if (Object.prototype.hasOwnProperty.call(tables, "history") &&
  333. version === prevVersion) {
  334. tables.history.rows.load(items);
  335. } else {
  336. rspamd.destroyTable("history");
  337. // Is there a way to get an event when the table is destroyed?
  338. setTimeout(function () {
  339. rspamd.initHistoryTable(rspamd, data, items, "history", get_history_columns(data), false);
  340. }, 200);
  341. }
  342. prevVersion = version;
  343. } else {
  344. rspamd.destroyTable("history");
  345. }
  346. },
  347. complete: function () { $("#refresh").removeAttr("disabled").removeClass("disabled"); },
  348. errorMessage: "Cannot receive history",
  349. });
  350. };
  351. ui.setup = function (rspamd, tables) {
  352. rspamd.set_page_size("history", $("#history_page_size").val());
  353. rspamd.bindHistoryTableEventHandlers("history", 8);
  354. $("#updateHistory").off("click");
  355. $("#updateHistory").on("click", function (e) {
  356. e.preventDefault();
  357. ui.getHistory(rspamd, tables);
  358. });
  359. // @reset history log
  360. $("#resetHistory").off("click");
  361. $("#resetHistory").on("click", function (e) {
  362. e.preventDefault();
  363. if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
  364. return;
  365. }
  366. rspamd.destroyTable("history");
  367. rspamd.destroyTable("errors");
  368. rspamd.query("historyreset", {
  369. success: function () {
  370. ui.getHistory(rspamd, tables);
  371. ui.getErrors(rspamd, tables);
  372. },
  373. errorMessage: "Cannot reset history log"
  374. });
  375. });
  376. };
  377. function initErrorsTable(rspamd, tables, rows) {
  378. tables.errors = FooTable.init("#errorsLog", {
  379. columns: [
  380. {
  381. sorted: true,
  382. direction: "DESC",
  383. name: "ts",
  384. title: "Time",
  385. style: {"font-size": "11px", "width": 300, "maxWidth": 300},
  386. sortValue: function (val) { return Number(val.options.sortValue); }
  387. },
  388. {name:"type", title:"Worker type", breakpoints:"xs sm", style:{"font-size":"11px", "width":150, "maxWidth":150}},
  389. {name:"pid", title:"PID", breakpoints:"xs sm", style:{"font-size":"11px", "width":110, "maxWidth":110}},
  390. {name:"module", title:"Module", style:{"font-size":"11px"}},
  391. {name:"id", title:"Internal ID", style:{"font-size":"11px"}},
  392. {name:"message", title:"Message", breakpoints:"xs sm", style:{"font-size":"11px"}},
  393. ],
  394. rows: rows,
  395. paging: {
  396. enabled: true,
  397. limit: 5,
  398. size: rspamd.page_size.errors
  399. },
  400. filtering: {
  401. enabled: true,
  402. position: "left",
  403. connectors: false
  404. },
  405. sorting: {
  406. enabled: true
  407. }
  408. });
  409. }
  410. ui.getErrors = function (rspamd, tables) {
  411. if (rspamd.read_only) return;
  412. rspamd.query("errors", {
  413. success: function (data) {
  414. var neighbours_data = data
  415. .filter(function (d) {
  416. return d.status;
  417. }) // filter out unavailable neighbours
  418. .map(function (d) {
  419. return d.data;
  420. });
  421. var rows = [].concat.apply([], neighbours_data);
  422. $.each(rows, function (i, item) {
  423. item.ts = {
  424. value: rspamd.unix_time_format(item.ts),
  425. options: {
  426. sortValue: item.ts
  427. }
  428. };
  429. });
  430. if (Object.prototype.hasOwnProperty.call(tables, "errors")) {
  431. tables.errors.rows.load(rows);
  432. } else {
  433. initErrorsTable(rspamd, tables, rows);
  434. }
  435. }
  436. });
  437. $("#updateErrors").off("click");
  438. $("#updateErrors").on("click", function (e) {
  439. e.preventDefault();
  440. ui.getErrors(rspamd, tables);
  441. });
  442. };
  443. return ui;
  444. });