Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

history.js 18KB

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