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.

stats.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. define(["jquery", "d3pie", "humanize"],
  21. function ($, d3pie, Humanize) {
  22. "use strict";
  23. // @ ms to date
  24. function msToTime(seconds) {
  25. /* eslint-disable no-bitwise */
  26. var years = seconds / 31536000 >> 0; // 3600*24*365
  27. var months = seconds % 31536000 / 2628000 >> 0; // 3600*24*365/12
  28. var days = seconds % 31536000 % 2628000 / 86400 >> 0; // 24*3600
  29. var hours = seconds % 31536000 % 2628000 % 86400 / 3600 >> 0;
  30. var minutes = seconds % 31536000 % 2628000 % 86400 % 3600 / 60 >> 0;
  31. /* eslint-enable no-bitwise */
  32. var out = null;
  33. if (years > 0) {
  34. if (months > 0) {
  35. out = years + "yr " + months + "mth";
  36. } else {
  37. out = years + "yr " + days + "d";
  38. }
  39. } else if (months > 0) {
  40. out = months + "mth " + days + "d";
  41. } else if (days > 0) {
  42. out = days + "d " + hours + "hr";
  43. } else if (hours > 0) {
  44. out = hours + "hr " + minutes + "min";
  45. } else {
  46. out = minutes + "min";
  47. }
  48. return out;
  49. }
  50. function displayStatWidgets(checked_server) {
  51. var widgets = $("#statWidgets");
  52. $(widgets).empty().hide();
  53. var servers = JSON.parse(sessionStorage.getItem("Credentials"));
  54. var data = {};
  55. if (servers && servers[checked_server]) {
  56. data = servers[checked_server].data;
  57. }
  58. var stat_w = [];
  59. $.each(data, function (i, item) {
  60. var widget = "";
  61. if (i === "auth" || i === "error") return; // Skip to the next iteration
  62. if (i === "version") {
  63. widget = "<div class=\"left\"><strong>" + item + "</strong>" +
  64. i + "</div>";
  65. $(widget).appendTo(widgets);
  66. } else if (i === "uptime") {
  67. widget = "<div class=\"right\"><strong>" + msToTime(item) +
  68. "</strong>" + i + "</div>";
  69. $(widget).appendTo(widgets);
  70. } else {
  71. var titleAtt = Humanize.intComma(item) + " " + i;
  72. widget = "<li class=\"stat-box\"><div class=\"widget\" title=\"" + titleAtt + "\"><strong>" +
  73. Humanize.compactInteger(item) + "</strong>" + i + "</div></li>";
  74. if (i === "scanned") {
  75. stat_w[0] = widget;
  76. } else if (i === "clean") {
  77. stat_w[1] = widget;
  78. } else if (i === "greylist") {
  79. stat_w[2] = widget;
  80. } else if (i === "probable") {
  81. stat_w[3] = widget;
  82. } else if (i === "reject") {
  83. stat_w[4] = widget;
  84. } else if (i === "learned") {
  85. stat_w[5] = widget;
  86. }
  87. }
  88. });
  89. $.each(stat_w, function (i, item) {
  90. $(item).appendTo(widgets);
  91. });
  92. $("#statWidgets .left,#statWidgets .right").wrapAll("<li class=\"stat-box pull-right\"><div class=\"widget\"></div></li>");
  93. $("#statWidgets").find("li.pull-right").appendTo("#statWidgets");
  94. $("#clusterTable tbody").empty();
  95. $("#selSrv").empty();
  96. $.each(servers, function (key, val) {
  97. var glyph_status = "glyphicon glyphicon-remove-circle";
  98. var short_id = "???";
  99. if (!("config_id" in val.data)) {
  100. val.data.config_id = "";
  101. }
  102. if (val.status) {
  103. glyph_status = "glyphicon glyphicon-ok-circle";
  104. short_id = val.data.config_id.substring(0, 8);
  105. }
  106. $("#clusterTable tbody").append("<tr>" +
  107. "<td class=\"col1\" title=\"Radio\"><input type=\"radio\" class=\"form-control radio\" name=\"clusterName\" value=\"" + key + "\"></td>" +
  108. "<td class=\"col2\" title=\"SNAme\">" + key + "</td>" +
  109. "<td class=\"col3\" title=\"SHost\">" + val.host + "</td>" +
  110. "<td class=\"col4\" title=\"SStatus\"><span class=\"icon\"><i class=\"" + glyph_status + "\"></i></span></td>" +
  111. "<td class=\"col5\" title=\"short_id\">" + short_id + "</td></tr>");
  112. $("#selSrv").append($("<option value=\"" + key + "\">" + key + "</option>"));
  113. if (checked_server === key) {
  114. $("#clusterTable tbody [value=\"" + key + "\"]").prop("checked", true);
  115. $("#selSrv [value=\"" + key + "\"]").prop("selected", true);
  116. } else if (!val.status) {
  117. $("#clusterTable tbody [value=\"" + key + "\"]").prop("disabled", true);
  118. $("#selSrv [value=\"" + key + "\"]").prop("disabled", true);
  119. }
  120. });
  121. $(widgets).show();
  122. }
  123. function getChart(rspamd, pie, checked_server) {
  124. var creds = JSON.parse(sessionStorage.getItem("Credentials"));
  125. if (!creds || !creds[checked_server]) return null;
  126. var data = creds[checked_server].data;
  127. var new_data = [{
  128. color: "#66CC00",
  129. label: "Clean",
  130. data: data.clean,
  131. value: data.clean
  132. }, {
  133. color: "#BF8040",
  134. label: "Temporarily rejected",
  135. data: data.soft_reject,
  136. value: data.soft_reject
  137. }, {
  138. color: "#FFAD00",
  139. label: "Probable spam",
  140. data: data.probable,
  141. value: data.probable
  142. }, {
  143. color: "#436EEE",
  144. label: "Greylisted",
  145. data: data.greylist,
  146. value: data.greylist
  147. }, {
  148. color: "#FF0000",
  149. label: "Rejected",
  150. data: data.reject,
  151. value: data.reject
  152. }];
  153. return rspamd.drawPie(pie, "chart", new_data);
  154. }
  155. // Public API
  156. var ui = {
  157. statWidgets: function (rspamd, graphs, checked_server) {
  158. rspamd.query("auth", {
  159. success: function (neighbours_status) {
  160. var neighbours_sum = {
  161. version: neighbours_status[0].data.version,
  162. auth: "ok",
  163. uptime: 0,
  164. clean: 0,
  165. probable: 0,
  166. greylist: 0,
  167. reject: 0,
  168. soft_reject: 0,
  169. scanned: 0,
  170. learned: 0,
  171. config_id: ""
  172. };
  173. var status_count = 0;
  174. for (var e in neighbours_status) {
  175. if (neighbours_status[e].status === true) {
  176. // Remove alert status
  177. localStorage.removeItem(e + "_alerted");
  178. neighbours_sum.clean += neighbours_status[e].data.clean;
  179. neighbours_sum.probable += neighbours_status[e].data.probable;
  180. neighbours_sum.greylist += neighbours_status[e].data.greylist;
  181. neighbours_sum.reject += neighbours_status[e].data.reject;
  182. neighbours_sum.soft_reject += neighbours_status[e].data.soft_reject;
  183. neighbours_sum.scanned += neighbours_status[e].data.scanned;
  184. neighbours_sum.learned += neighbours_status[e].data.learned;
  185. neighbours_sum.uptime += neighbours_status[e].data.uptime;
  186. status_count++;
  187. }
  188. }
  189. neighbours_sum.uptime = Math.floor(neighbours_sum.uptime / status_count);
  190. var to_Credentials = {};
  191. to_Credentials["All SERVERS"] = {
  192. name: "All SERVERS",
  193. url: "",
  194. host: "",
  195. checked: true,
  196. data: neighbours_sum,
  197. status: true
  198. };
  199. neighbours_status.forEach(function (elmt) {
  200. to_Credentials[elmt.name] = elmt;
  201. });
  202. sessionStorage.setItem("Credentials", JSON.stringify(to_Credentials));
  203. displayStatWidgets(checked_server);
  204. graphs.chart = getChart(rspamd, graphs.chart, checked_server);
  205. },
  206. errorMessage: "Cannot receive stats data",
  207. errorOnceId: "alerted_stats_",
  208. server: "All SERVERS"
  209. });
  210. },
  211. };
  212. return ui;
  213. }
  214. );