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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 FooTable */
  22. define(["jquery", "app/common", "d3evolution", "d3pie", "d3", "footable"],
  23. ($, common, D3Evolution, D3Pie, d3) => {
  24. "use strict";
  25. const 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. const ui = {};
  52. let prevUnit = "msg/s";
  53. ui.draw = function (graphs, neighbours, checked_server, type) {
  54. const 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: common.chartLegend
  62. }
  63. };
  64. function initGraph() {
  65. const graph = new D3Evolution("graph", $.extend({}, graph_options, {
  66. yScale: common.getSelector("selYScale"),
  67. type: common.getSelector("selType"),
  68. interpolate: common.getSelector("selInterpolate"),
  69. convert: common.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. const xExtents = d3.extent(d3.merge(json), (d) => d.x);
  87. const timeInterval = xExtents[1] - xExtents[0];
  88. let total = 0;
  89. const rows = json.map((curr, i) => {
  90. // Time intervals that don't have data are excluded from average calculation as d3.mean()ignores nulls
  91. const avg = d3.mean(curr, (d) => d.y);
  92. // To find an integral on the whole time interval we need to convert nulls to zeroes
  93. // eslint-disable-next-line no-bitwise
  94. const value = d3.mean(curr, (d) => Number(d.y)) * timeInterval / scaleFactor ^ 0;
  95. const yExtents = d3.extent(curr, (d) => d.y);
  96. total += value;
  97. return {
  98. label: graph_options.legend.entries[i].label,
  99. value: value,
  100. min: Number(yExtents[0].toFixed(6)),
  101. avg: Number(avg.toFixed(6)),
  102. max: Number(yExtents[1].toFixed(6)),
  103. last: Number(curr[curr.length - 1].y.toFixed(6)),
  104. color: graph_options.legend.entries[i].color,
  105. };
  106. }, []);
  107. return {
  108. rows: rows,
  109. total: total
  110. };
  111. }
  112. function initSummaryTable(rows, unit) {
  113. common.tables.rrd_summary = FooTable.init("#rrd-table", {
  114. breakpoints: common.breakpoints,
  115. cascade: true,
  116. sorting: {
  117. enabled: true
  118. },
  119. columns: [
  120. {name: "label", title: "Action"},
  121. {name: "value", title: "Messages", defaultContent: ""},
  122. {name: "min", title: "Minimum, <span class=\"unit\">" + unit + "</span>", defaultContent: ""},
  123. {name: "avg", title: "Average, <span class=\"unit\">" + unit + "</span>", defaultContent: ""},
  124. {name: "max", title: "Maximum, <span class=\"unit\">" + unit + "</span>", defaultContent: ""},
  125. {name: "last", title: "Last, " + unit},
  126. ],
  127. rows: rows.map((curr, i) => ({
  128. options: {
  129. style: {
  130. color: graph_options.legend.entries[i].color
  131. }
  132. },
  133. value: curr
  134. }), [])
  135. });
  136. }
  137. function drawRrdTable(rows, unit) {
  138. if (Object.prototype.hasOwnProperty.call(common.tables, "rrd_summary")) {
  139. $.each(common.tables.rrd_summary.rows.all, (i, row) => {
  140. row.val(rows[i], false, true);
  141. });
  142. } else {
  143. initSummaryTable(rows, unit);
  144. }
  145. }
  146. function updateWidgets(data) {
  147. let rrd_summary = {rows: []};
  148. let unit = "msg/s";
  149. if (data) {
  150. // Autoranging
  151. let scaleFactor = 1;
  152. const yMax = d3.max(d3.merge(data), (d) => d.y);
  153. if (yMax < 1) {
  154. scaleFactor = 60;
  155. unit = "msg/min";
  156. data.forEach((s) => {
  157. s.forEach((d) => {
  158. if (d.y !== null) { d.y *= scaleFactor; }
  159. });
  160. });
  161. }
  162. rrd_summary = getRrdSummary(data, scaleFactor);
  163. }
  164. if (!graphs.rrd_pie) graphs.rrd_pie = new D3Pie("rrd-pie", rrd_pie_config);
  165. graphs.rrd_pie.data(rrd_summary.rows);
  166. graphs.graph.data(data);
  167. if (unit !== prevUnit) {
  168. graphs.graph.yAxisLabel("Message rate, " + unit);
  169. $(".unit").text(unit);
  170. prevUnit = unit;
  171. }
  172. drawRrdTable(rrd_summary.rows, unit);
  173. document.getElementById("rrd-total-value").innerHTML = rrd_summary.total;
  174. }
  175. if (!graphs.graph) {
  176. graphs.graph = initGraph();
  177. }
  178. common.query("graph", {
  179. success: function (req_data) {
  180. let data = null;
  181. const neighbours_data = req_data
  182. .filter((d) => d.status) // filter out unavailable neighbours
  183. .map((d) => d.data);
  184. if (neighbours_data.length === 1) {
  185. [data] = neighbours_data;
  186. } else {
  187. let time_match = true;
  188. neighbours_data.reduce((res, curr, _, arr) => {
  189. if ((curr[0][0].x !== res[0][0].x) ||
  190. (curr[0][curr[0].length - 1].x !== res[0][res[0].length - 1].x)) {
  191. time_match = false;
  192. common.alertMessage("alert-error",
  193. "Neighbours time extents do not match. Check if time is synchronized on all servers.");
  194. arr.splice(1); // Break out of .reduce() by mutating the source array
  195. }
  196. return curr;
  197. });
  198. if (time_match) {
  199. data = neighbours_data.reduce((res, curr) => curr.map((action, j) => action.map((d, i) => ({
  200. x: d.x,
  201. y: (res[j][i].y === null) ? d.y : res[j][i].y + d.y
  202. }))));
  203. }
  204. }
  205. updateWidgets(data);
  206. },
  207. complete: function () { $("#refresh").removeAttr("disabled").removeClass("disabled"); },
  208. errorMessage: "Cannot receive throughput data",
  209. errorOnceId: "alerted_graph_",
  210. data: {type: type}
  211. });
  212. };
  213. // Handling mouse events on overlapping elements
  214. $("#rrd-pie").mouseover(() => {
  215. $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200");
  216. $("#rrd-table_toggle").css("z-index", "300");
  217. });
  218. $("#rrd-table_toggle").mouseover(() => {
  219. $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0");
  220. $("#rrd-table_toggle").css("z-index", "0");
  221. });
  222. return ui;
  223. });