graph.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. uniformSeries: function () {
  20. var series_min_length = 100000000;
  21. for (i=0; i<arguments.length; i++) {
  22. if (arguments[i].length < series_min_length) {
  23. series_min_length = arguments[i].length;
  24. }
  25. }
  26. for (i=0; i<arguments.length; i++) {
  27. if (arguments[i].length > series_min_length) {
  28. arguments[i].length = series_min_length;
  29. }
  30. }
  31. },
  32. /**
  33. * Get min, max for X and max for Y for provided series
  34. * @param series
  35. * @return {Object}
  36. */
  37. getExtInSeries: function(series) {
  38. var maxY = 0;
  39. var maxX = 0;
  40. var minX = 2147465647; // max timestamp value
  41. if (series.length > 0) {
  42. series.forEach(function(item){
  43. if (item.y > maxY) {
  44. maxY = item.y;
  45. }
  46. if (item.x > maxX) {
  47. maxX = item.x;
  48. }
  49. if (item.x < minX) {
  50. minX = item.x;
  51. }
  52. });
  53. }
  54. return {maxX: maxX, minX: minX, maxY: maxY};
  55. },
  56. /**
  57. * Get coordinates for new circle in the graph
  58. * New circle needed to prevent cut on the borders of the graph
  59. * List of arguments - series arrays
  60. * @return {Object}
  61. */
  62. getNewCircle: function() {
  63. var maxx = [];
  64. var minx = [];
  65. var maxy = [];
  66. for (var i = 0; i < arguments.length; i++) {
  67. var localExt = this.getExtInSeries(arguments[i]);
  68. maxx.push(localExt.maxX);
  69. minx.push(localExt.minX);
  70. maxy.push(localExt.maxY);
  71. }
  72. var maxX = Math.max.apply(null, maxx);
  73. var minX = Math.min.apply(null, minx);
  74. var newX;
  75. if (minX != 2147465647) {
  76. newX = maxX + Math.round((maxX - minX) * 0.2);
  77. }
  78. else {
  79. newX = (new Date()).getTime();
  80. }
  81. var newY = Math.max.apply(null, maxy) * 1.2;
  82. return {x: newX, y: newY, r: 0, io: 0};
  83. },
  84. drawJobTimeLine:function (map, shuffle, reduce, w, h, element, legend_id, timeline_id) {
  85. map = $.parseJSON(map);
  86. shuffle = $.parseJSON(shuffle);
  87. reduce = $.parseJSON(reduce);
  88. if (!map || !shuffle || !reduce) {
  89. console.warn('drawJobTimeLine');
  90. return;
  91. }
  92. this.uniformSeries(map, reduce, shuffle);
  93. var graph = new Rickshaw.Graph({
  94. width:w,
  95. height:h,
  96. element:document.querySelector(element),
  97. renderer:'area',
  98. interpolation: 'step-after',
  99. strokeWidth: 2,
  100. stroke:true,
  101. series:[
  102. {
  103. data:map,
  104. color:'green',
  105. name:'maps'
  106. },
  107. {
  108. data:shuffle,
  109. color:'lightblue',
  110. name:'shuffles'
  111. },
  112. {
  113. data:reduce,
  114. color:'steelblue',
  115. name:'reduces'
  116. }
  117. ]
  118. }
  119. );
  120. graph.render();
  121. var legend = new Rickshaw.Graph.Legend({
  122. graph:graph,
  123. element:document.getElementById(legend_id)
  124. });
  125. var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
  126. graph:graph,
  127. legend:legend
  128. });
  129. var order = new Rickshaw.Graph.Behavior.Series.Order({
  130. graph:graph,
  131. legend:legend
  132. });
  133. var highlight = new Rickshaw.Graph.Behavior.Series.Highlight({
  134. graph:graph,
  135. legend:legend
  136. });
  137. var xAxis = new Rickshaw.Graph.Axis.Time({
  138. graph:graph,
  139. timeUnit: {
  140. name: '1 min',
  141. seconds: 60,
  142. formatter: function(d) { return d.toUTCString().match(/(\d+:\d+):/)[1] }
  143. }
  144. });
  145. xAxis.render();
  146. var yAxis = new Rickshaw.Graph.Axis.Y({
  147. orientation: 'left',
  148. element: document.querySelector('#y-axis'),
  149. graph:graph
  150. });
  151. yAxis.render();
  152. var hoverDetail = new Rickshaw.Graph.HoverDetail({
  153. graph:graph,
  154. yFormatter:function (y) {
  155. return Math.floor(y) + " tasks"
  156. }
  157. });
  158. /*var annotator = new Rickshaw.Graph.Annotate({
  159. graph:graph,
  160. //element:document.getElementById(timeline_id)
  161. });*/
  162. },
  163. drawJobTasks:function (mapNodeLocal, mapRackLocal, mapOffSwitch, reduceOffSwitch, submitTime, w, h, element, legend_id, timeline_id) {
  164. mapNodeLocal = $.parseJSON(mapNodeLocal);
  165. mapRackLocal = $.parseJSON(mapRackLocal);
  166. mapOffSwitch = $.parseJSON(mapOffSwitch);
  167. reduceOffSwitch = $.parseJSON(reduceOffSwitch);
  168. if (!mapNodeLocal || !mapRackLocal || !mapOffSwitch || !reduceOffSwitch) {
  169. console.warn('drawJobTasks');
  170. return;
  171. }
  172. this.uniformSeries(mapNodeLocal, mapRackLocal, mapOffSwitch, reduceOffSwitch);
  173. var newC = this.getNewCircle(mapNodeLocal, mapRackLocal, mapOffSwitch, reduceOffSwitch);
  174. mapNodeLocal.push(newC);
  175. mapRackLocal.push(newC);
  176. mapOffSwitch.push(newC);
  177. reduceOffSwitch.push(newC);
  178. var graph = new Rickshaw.Graph({
  179. width:w,
  180. height:h,
  181. element:document.querySelector(element),
  182. renderer:'scatterplot',
  183. stroke:true,
  184. series:[
  185. {
  186. data:mapNodeLocal,
  187. color:'green',
  188. name:'node_local_map'
  189. },
  190. {
  191. data:mapRackLocal,
  192. color:'lightblue',
  193. name:'rack_local_map'
  194. },
  195. {
  196. data:mapOffSwitch,
  197. color:'brown',
  198. name:'off_switch_map'
  199. },
  200. {
  201. data:reduceOffSwitch,
  202. color:'steelblue',
  203. name:'reduce'
  204. }
  205. ]
  206. });
  207. graph.render();
  208. var legend = new Rickshaw.Graph.Legend({
  209. graph:graph,
  210. element:document.getElementById(legend_id)
  211. });
  212. var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
  213. graph:graph,
  214. legend:legend
  215. });
  216. var order = new Rickshaw.Graph.Behavior.Series.Order({
  217. graph:graph,
  218. legend:legend
  219. });
  220. var highlight = new Rickshaw.Graph.Behavior.Series.Highlight({
  221. graph:graph,
  222. legend:legend
  223. });
  224. var ticksTreatment = 'glow';
  225. var xAxis = new Rickshaw.Graph.Axis.Time({
  226. graph:graph,
  227. timeUnit: {
  228. name: '30 seconds',
  229. seconds: 30,
  230. formatter: function(d) { return (new Date(d)).getTime() / 1000 - submitTime + 's' }
  231. },
  232. ticksTreatment:ticksTreatment
  233. });
  234. xAxis.render();
  235. var yAxis = new Rickshaw.Graph.Axis.Y({
  236. graph:graph,
  237. ticksTreatment:ticksTreatment,
  238. orientation: 'left',
  239. element: document.querySelector('#y-axis2'),
  240. tickFormat: function(y) { return y / 1000 + 's' }
  241. });
  242. yAxis.render();
  243. var hoverDetail = new Rickshaw.Graph.HoverDetail({
  244. graph:graph,
  245. xFormatter:function (x) {
  246. return (x - submitTime) + 's'
  247. },
  248. yFormatter:function (y) {
  249. return y / 1000 + 's'
  250. },
  251. formatter:function (series, x, y, formattedX, formattedY, d) {
  252. var bytesFormatter = function(y) {
  253. if (y >= 1125899906842624) { return Math.floor(10 * y / 1125899906842624)/10 + " PB" }
  254. else if (y >= 1099511627776){ return Math.floor(10 * y / 1099511627776)/10 + " TB" }
  255. else if (y >= 1073741824) { return Math.floor(10 * y / 1073741824)/10 + " GB" }
  256. else if (y >= 1048576) { return Math.floor(10 * y / 1048576)/10 + " MB" }
  257. else if (y >= 1024) { return Math.floor(10 * y / 1024)/10 + " KB" }
  258. else { return y + " B"}
  259. };
  260. var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
  261. return swatch + d.value.label +
  262. '<br>Run-time: ' + formattedY + '<br>Wait-time: ' + formattedX +
  263. '<br>I/O: ' + bytesFormatter(d.value.io) + '<br>Status: ' + d.value.status;
  264. }
  265. });
  266. }
  267. }