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

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