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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/common", "app/libft", "footable"],
  22. ($, common, libft) => {
  23. "use strict";
  24. const ui = {};
  25. let prevVersion = null;
  26. function process_history_legacy(data) {
  27. const items = [];
  28. function compare(e1, e2) { return e1.name.localeCompare(e2.name); }
  29. $("#selSymOrder_history, label[for='selSymOrder_history']").hide();
  30. $.each(data, (i, item) => {
  31. item.time = libft.unix_time_format(item.unix_time);
  32. libft.preprocess_item(item);
  33. item.symbols = Object.keys(item.symbols)
  34. .map((key) => item.symbols[key])
  35. .sort(compare)
  36. .map((e) => e.name)
  37. .join(", ");
  38. item.time = {
  39. value: libft.unix_time_format(item.unix_time),
  40. options: {
  41. sortValue: item.unix_time
  42. }
  43. };
  44. items.push(item);
  45. });
  46. return {items: items};
  47. }
  48. function columns_legacy() {
  49. return [{
  50. name: "id",
  51. title: "ID",
  52. style: {
  53. width: 300,
  54. maxWidth: 300,
  55. overflow: "hidden",
  56. textOverflow: "ellipsis",
  57. wordBreak: "keep-all",
  58. whiteSpace: "nowrap"
  59. }
  60. }, {
  61. name: "ip",
  62. title: "IP address",
  63. breakpoints: "md",
  64. style: {width: 150, maxWidth: 150}
  65. }, {
  66. name: "action",
  67. title: "Action",
  68. style: {width: 110, maxWidth: 110}
  69. }, {
  70. name: "score",
  71. title: "Score",
  72. style: {maxWidth: 110},
  73. sortValue: function (val) { return Number(val.options.sortValue); }
  74. }, {
  75. name: "symbols",
  76. title: "Symbols",
  77. breakpoints: "all",
  78. style: {width: 550, maxWidth: 550}
  79. }, {
  80. name: "size",
  81. title: "Message size",
  82. breakpoints: "md",
  83. style: {width: 120, maxWidth: 120},
  84. formatter: libft.formatBytesIEC
  85. }, {
  86. name: "scan_time",
  87. title: "Scan time",
  88. breakpoints: "md",
  89. style: {maxWidth: 80},
  90. sortValue: function (val) { return Number(val); }
  91. }, {
  92. sorted: true,
  93. direction: "DESC",
  94. name: "time",
  95. title: "Time",
  96. sortValue: function (val) { return Number(val.options.sortValue); }
  97. }, {
  98. name: "user",
  99. title: "Authenticated user",
  100. breakpoints: "md",
  101. style: {width: 200, maxWidth: 200}
  102. }];
  103. }
  104. const columns = {
  105. 2: libft.columns_v2("history"),
  106. legacy: columns_legacy()
  107. };
  108. function process_history_data(data) {
  109. const process_functions = {
  110. 2: libft.process_history_v2,
  111. legacy: process_history_legacy
  112. };
  113. let pf = process_functions.legacy;
  114. if (data.version) {
  115. const strkey = data.version.toString();
  116. if (process_functions[strkey]) {
  117. pf = process_functions[strkey];
  118. }
  119. }
  120. return pf(data, "history");
  121. }
  122. function get_history_columns(data) {
  123. let func = columns.legacy;
  124. if (data.version) {
  125. const strkey = data.version.toString();
  126. if (columns[strkey]) {
  127. func = columns[strkey];
  128. }
  129. }
  130. return func;
  131. }
  132. ui.getHistory = function () {
  133. $("#refresh, #updateHistory").attr("disabled", true);
  134. common.query("history", {
  135. success: function (req_data) {
  136. function differentVersions(neighbours_data) {
  137. const dv = neighbours_data.some((e) => e.version !== neighbours_data[0].version);
  138. if (dv) {
  139. common.alertMessage("alert-error",
  140. "Neighbours history backend versions do not match. Cannot display history.");
  141. return true;
  142. }
  143. return false;
  144. }
  145. const neighbours_data = req_data
  146. .filter((d) => d.status) // filter out unavailable neighbours
  147. .map((d) => d.data);
  148. if (neighbours_data.length && !differentVersions(neighbours_data)) {
  149. let data = {};
  150. const [{version}] = neighbours_data;
  151. if (version) {
  152. data.rows = [].concat.apply([], neighbours_data
  153. .map((e) => e.rows));
  154. data.version = version;
  155. $("#legacy-history-badge").hide();
  156. } else {
  157. // Legacy version
  158. data = [].concat.apply([], neighbours_data);
  159. $("#legacy-history-badge").show();
  160. }
  161. const o = process_history_data(data);
  162. const {items} = o;
  163. common.symbols.history = o.symbols;
  164. if (Object.prototype.hasOwnProperty.call(common.tables, "history") &&
  165. version === prevVersion) {
  166. common.tables.history.rows.load(items);
  167. } else {
  168. libft.destroyTable("history");
  169. // Is there a way to get an event when the table is destroyed?
  170. setTimeout(() => {
  171. libft.initHistoryTable(data, items, "history", get_history_columns(data), false,
  172. () => $("#refresh, #updateHistory, #history .ft-columns-dropdown .btn-dropdown-apply")
  173. .removeAttr("disabled"));
  174. }, 200);
  175. }
  176. prevVersion = version;
  177. } else {
  178. libft.destroyTable("history");
  179. }
  180. },
  181. error: () => $("#refresh, #updateHistory").removeAttr("disabled"),
  182. errorMessage: "Cannot receive history",
  183. });
  184. };
  185. function initErrorsTable(rows) {
  186. common.tables.errors = FooTable.init("#errorsLog", {
  187. breakpoints: common.breakpoints,
  188. cascade: true,
  189. columns: [
  190. {sorted: true,
  191. direction: "DESC",
  192. name: "ts",
  193. title: "Time",
  194. style: {width: 300, maxWidth: 300},
  195. sortValue: function (val) { return Number(val.options.sortValue); }},
  196. {name: "type",
  197. title: "Worker type",
  198. breakpoints: "md",
  199. style: {width: 150, maxWidth: 150}},
  200. {name: "pid",
  201. title: "PID",
  202. breakpoints: "md",
  203. style: {width: 110, maxWidth: 110}},
  204. {name: "module", title: "Module"},
  205. {name: "id", title: "Internal ID"},
  206. {name: "message", title: "Message", breakpoints: "md"},
  207. ],
  208. rows: rows,
  209. paging: {
  210. enabled: true,
  211. limit: 5,
  212. size: common.page_size.errors
  213. },
  214. filtering: {
  215. enabled: true,
  216. position: "left",
  217. connectors: false
  218. },
  219. sorting: {
  220. enabled: true
  221. }
  222. });
  223. }
  224. ui.getErrors = function () {
  225. if (common.read_only) return;
  226. common.query("errors", {
  227. success: function (data) {
  228. const neighbours_data = data
  229. .filter((d) => d.status) // filter out unavailable neighbours
  230. .map((d) => d.data);
  231. const rows = [].concat.apply([], neighbours_data);
  232. $.each(rows, (i, item) => {
  233. item.ts = {
  234. value: libft.unix_time_format(item.ts),
  235. options: {
  236. sortValue: item.ts
  237. }
  238. };
  239. });
  240. if (Object.prototype.hasOwnProperty.call(common.tables, "errors")) {
  241. common.tables.errors.rows.load(rows);
  242. } else {
  243. initErrorsTable(rows);
  244. }
  245. }
  246. });
  247. $("#updateErrors").off("click");
  248. $("#updateErrors").on("click", (e) => {
  249. e.preventDefault();
  250. ui.getErrors();
  251. });
  252. };
  253. libft.set_page_size("history", $("#history_page_size").val());
  254. libft.bindHistoryTableEventHandlers("history", 8);
  255. $("#updateHistory").off("click");
  256. $("#updateHistory").on("click", (e) => {
  257. e.preventDefault();
  258. ui.getHistory();
  259. });
  260. // @reset history log
  261. $("#resetHistory").off("click");
  262. $("#resetHistory").on("click", (e) => {
  263. e.preventDefault();
  264. if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
  265. return;
  266. }
  267. libft.destroyTable("history");
  268. libft.destroyTable("errors");
  269. common.query("historyreset", {
  270. success: function () {
  271. ui.getHistory();
  272. ui.getErrors();
  273. },
  274. errorMessage: "Cannot reset history log"
  275. });
  276. });
  277. return ui;
  278. });