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.

graph.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
  4. Copyright (C) 2017 Alexander Moisseev
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. /* global d3:false, FooTable:false */
  22. define(["jquery", "d3evolution", "d3pie", "footable"],
  23. function ($, D3Evolution, D3Pie) {
  24. "use strict";
  25. var rrd_pie_config = {
  26. cornerRadius: 2,
  27. size: {
  28. canvasWidth: 400,
  29. canvasHeight: 180,
  30. pieInnerRadius: "50%",
  31. pieOuterRadius: "80%"
  32. },
  33. labels: {
  34. outer: {
  35. format: "none"
  36. },
  37. inner: {
  38. hideWhenLessThanPercentage: 8,
  39. offset: 0
  40. },
  41. },
  42. padAngle: 0.02,
  43. pieCenterOffset: {
  44. x: -120,
  45. y: 10,
  46. },
  47. total: {
  48. enabled: true
  49. },
  50. };
  51. var ui = {};
  52. var prevUnit = "msg/s";
  53. ui.draw = function (rspamd, graphs, tables, neighbours, checked_server, type) {
  54. var graph_options = {
  55. title: "Rspamd throughput",
  56. width: 1060,
  57. height: 370,
  58. yAxisLabel: "Message rate, msg/s",
  59. legend: {
  60. space: 140,
  61. entries: rspamd.chartLegend
  62. }
  63. };
  64. function initGraph() {
  65. var graph = new D3Evolution("graph", $.extend({}, graph_options, {
  66. yScale: rspamd.getSelector("selYScale"),
  67. type: rspamd.getSelector("selType"),
  68. interpolate: rspamd.getSelector("selInterpolate"),
  69. convert: rspamd.getSelector("selConvert"),
  70. }));
  71. $("#selYScale").change(function () {
  72. graph.yScale(this.value);
  73. });
  74. $("#selConvert").change(function () {
  75. graph.convert(this.value);
  76. });
  77. $("#selType").change(function () {
  78. graph.type(this.value);
  79. });
  80. $("#selInterpolate").change(function () {
  81. graph.interpolate(this.value);
  82. });
  83. return graph;
  84. }
  85. function getRrdSummary(json, scaleFactor) {
  86. var xExtents = d3.extent(d3.merge(json), function (d) { return d.x; });
  87. var timeInterval = xExtents[1] - xExtents[0];
  88. var total = 0;
  89. var rows = json.map(function (curr, i) {
  90. // Time intervals that don't have data are excluded from average calculation as d3.mean()ignores nulls
  91. var avg = d3.mean(curr, function (d) { return d.y; });
  92. // To find an integral on the whole time interval we need to convert nulls to zeroes
  93. var value = d3.mean(curr, function (d) { return Number(d.y); }) * timeInterval / scaleFactor ^ 0; // eslint-disable-line no-bitwise
  94. var yExtents = d3.extent(curr, function (d) { return d.y; });
  95. total += value;
  96. return {
  97. label: graph_options.legend.entries[i].label,
  98. value: value,
  99. min: Number(yExtents[0].toFixed(6)),
  100. avg: Number(avg.toFixed(6)),
  101. max: Number(yExtents[1].toFixed(6)),
  102. last: Number(curr[curr.length - 1].y.toFixed(6)),
  103. color: graph_options.legend.entries[i].color,
  104. };
  105. }, []);
  106. return {
  107. rows: rows,
  108. total: total
  109. };
  110. }
  111. function initSummaryTable(rows, unit) {
  112. tables.rrd_summary = FooTable.init("#rrd-table", {
  113. sorting: {
  114. enabled: true
  115. },
  116. columns: [
  117. {name:"label", title:"Action"},
  118. {name:"value", title:"Messages", defaultContent:""},
  119. {name:"min", title:"Minimum, <span class=\"unit\">" + unit + "</span>", defaultContent:""},
  120. {name:"avg", title:"Average, <span class=\"unit\">" + unit + "</span>", defaultContent:""},
  121. {name:"max", title:"Maximum, <span class=\"unit\">" + unit + "</span>", defaultContent:""},
  122. {name:"last", title:"Last, " + unit},
  123. ],
  124. rows: rows.map(function (curr, i) {
  125. return {
  126. options: {
  127. style: {
  128. color: graph_options.legend.entries[i].color
  129. }
  130. },
  131. value: curr
  132. };
  133. }, [])
  134. });
  135. }
  136. function drawRrdTable(rows, unit) {
  137. if (Object.prototype.hasOwnProperty.call(tables, "rrd_summary")) {
  138. $.each(tables.rrd_summary.rows.all, function (i, row) {
  139. row.val(rows[i], false, true);
  140. });
  141. } else {
  142. initSummaryTable(rows, unit);
  143. }
  144. }
  145. function updateWidgets(data) {
  146. var rrd_summary = {rows:[]};
  147. var unit = "msg/s";
  148. if (data) {
  149. // Autoranging
  150. var scaleFactor = 1;
  151. var yMax = d3.max(d3.merge(data), function (d) { return d.y; });
  152. if (yMax < 1) {
  153. scaleFactor = 60;
  154. unit = "msg/min";
  155. data.forEach(function (s) {
  156. s.forEach(function (d) {
  157. if (d.y !== null) { d.y *= scaleFactor; }
  158. });
  159. });
  160. }
  161. rrd_summary = getRrdSummary(data, scaleFactor);
  162. }
  163. if (!graphs.rrd_pie) graphs.rrd_pie = new D3Pie("rrd-pie", rrd_pie_config);
  164. graphs.rrd_pie.data(rrd_summary.rows);
  165. graphs.graph.data(data);
  166. if (unit !== prevUnit) {
  167. graphs.graph.yAxisLabel("Message rate, " + unit);
  168. $(".unit").text(unit);
  169. prevUnit = unit;
  170. }
  171. drawRrdTable(rrd_summary.rows, unit);
  172. document.getElementById("rrd-total-value").innerHTML = rrd_summary.total;
  173. }
  174. if (!graphs.graph) {
  175. graphs.graph = initGraph();
  176. }
  177. rspamd.query("graph", {
  178. success: function (req_data) {
  179. var data = null;
  180. var neighbours_data = req_data
  181. .filter(function (d) { return d.status; }) // filter out unavailable neighbours
  182. .map(function (d) { return d.data; });
  183. if (neighbours_data.length === 1) {
  184. data = neighbours_data[0];
  185. } else {
  186. var time_match = true;
  187. neighbours_data.reduce(function (res, curr, _, arr) {
  188. if ((curr[0][0].x !== res[0][0].x) ||
  189. (curr[0][curr[0].length - 1].x !== res[0][res[0].length - 1].x)) {
  190. time_match = false;
  191. rspamd.alertMessage("alert-error",
  192. "Neighbours time extents do not match. Check if time is synchronized on all servers.");
  193. arr.splice(1); // Break out of .reduce() by mutating the source array
  194. }
  195. return curr;
  196. });
  197. if (time_match) {
  198. data = neighbours_data.reduce(function (res, curr) {
  199. return curr.map(function (action, j) {
  200. return action.map(function (d, i) {
  201. return {
  202. x: d.x,
  203. y: (res[j][i].y === null) ? d.y : res[j][i].y + d.y
  204. };
  205. });
  206. });
  207. });
  208. }
  209. }
  210. updateWidgets(data);
  211. },
  212. complete: function () { $("#refresh").removeAttr("disabled").removeClass("disabled"); },
  213. errorMessage: "Cannot receive throughput data",
  214. errorOnceId: "alerted_graph_",
  215. data: {type:type}
  216. });
  217. };
  218. ui.setup = function (rspamd) {
  219. // Handling mouse events on overlapping elements
  220. $("#rrd-pie").mouseover(function () {
  221. $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200");
  222. $("#rrd-table_toggle").css("z-index", "300");
  223. });
  224. $("#rrd-table_toggle").mouseover(function () {
  225. $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0");
  226. $("#rrd-table_toggle").css("z-index", "0");
  227. });
  228. return rspamd.getSelector("selData");
  229. };
  230. return ui;
  231. });