tree-selector.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. // Map: <queue-name, queue>
  4. map : undefined,
  5. // Normalized data for d3
  6. treeData: undefined,
  7. // folded queues, folded[<queue-name>] == true means <queue-name> is folded
  8. foldedQueues: { },
  9. // maxDepth
  10. maxDepth: 0,
  11. // num of leaf queue, folded queue is treated as leaf queue
  12. numOfLeafQueue: 0,
  13. // mainSvg
  14. mainSvg: undefined,
  15. // Init data
  16. initData: function() {
  17. this.map = { };
  18. this.treeData = { };
  19. this.maxDepth = 0;
  20. this.numOfLeafQueue = 0;
  21. this.get("model")
  22. .forEach(function(o) {
  23. this.map[o.id] = o;
  24. }.bind(this));
  25. var selected = this.get("selected");
  26. this.initQueue("root", 1, this.treeData);
  27. },
  28. // get Children array of given queue
  29. getChildrenNamesArray: function(q) {
  30. var namesArr = [];
  31. // Folded queue's children is empty
  32. if (this.foldedQueues[q.get("name")]) {
  33. return namesArr;
  34. }
  35. var names = q.get("children");
  36. if (names) {
  37. names.forEach(function(name) {
  38. namesArr.push(name);
  39. });
  40. }
  41. return namesArr;
  42. },
  43. // Init queues
  44. initQueue: function(queueName, depth, node) {
  45. if ((!queueName) || (!this.map[queueName])) {
  46. // Queue is not existed
  47. return;
  48. }
  49. if (depth > this.maxDepth) {
  50. this.maxDepth = this.maxDepth + 1;
  51. }
  52. var queue = this.map[queueName];
  53. var names = this.getChildrenNamesArray(queue);
  54. node.name = queueName;
  55. node.parent = queue.get("parent");
  56. node.queueData = queue;
  57. if (names.length > 0) {
  58. node.children = [];
  59. names.forEach(function(name) {
  60. var childQueueData = {};
  61. node.children.push(childQueueData);
  62. this.initQueue(name, depth + 1, childQueueData);
  63. }.bind(this));
  64. } else {
  65. this.numOfLeafQueue = this.numOfLeafQueue + 1;
  66. }
  67. },
  68. update: function(source, root, tree, diagonal) {
  69. var duration = 300;
  70. var i = 0;
  71. // Compute the new tree layout.
  72. var nodes = tree.nodes(root).reverse();
  73. var links = tree.links(nodes);
  74. // Normalize for fixed-depth.
  75. nodes.forEach(function(d) { d.y = d.depth * 200; });
  76. // Update the nodes…
  77. var node = this.mainSvg.selectAll("g.node")
  78. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  79. // Enter any new nodes at the parent's previous position.
  80. var nodeEnter = node.enter().append("g")
  81. .attr("class", "node")
  82. .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
  83. .on("click", function(d,i){
  84. if (d.queueData.get("name") != this.get("selected")) {
  85. document.location.href = "yarnQueue/" + d.queueData.get("name");
  86. }
  87. }.bind(this));
  88. // .on("click", click);
  89. nodeEnter.append("circle")
  90. .attr("r", 1e-6)
  91. .style("fill", function(d) {
  92. var usedCap = d.queueData.get("usedCapacity");
  93. if (usedCap <= 60.0) {
  94. return "LimeGreen";
  95. } else if (usedCap <= 100.0) {
  96. return "DarkOrange";
  97. } else {
  98. return "LightCoral";
  99. }
  100. });
  101. // append percentage
  102. nodeEnter.append("text")
  103. .attr("x", function(d) { return 0; })
  104. .attr("dy", ".35em")
  105. .attr("text-anchor", function(d) { return "middle"; })
  106. .text(function(d) {
  107. var usedCap = d.queueData.get("usedCapacity");
  108. if (usedCap >= 100.0) {
  109. return usedCap.toFixed(0) + "%";
  110. } else {
  111. return usedCap.toFixed(1) + "%";
  112. }
  113. })
  114. .style("fill-opacity", 1e-6);
  115. // append queue name
  116. nodeEnter.append("text")
  117. .attr("x", function(d) { return 40; })
  118. .attr("dy", ".35em")
  119. .attr("text-anchor", function(d) { return "start"; })
  120. .text(function(d) { return d.name; })
  121. .style("fill-opacity", 1e-6);
  122. // Transition nodes to their new position.
  123. var nodeUpdate = node.transition()
  124. .duration(duration)
  125. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
  126. nodeUpdate.select("circle")
  127. .attr("r", 20)
  128. .attr("href",
  129. function(d) {
  130. return "yarnQueues/" + d.queueData.get("name");
  131. })
  132. .style("stroke", function(d) {
  133. if (d.queueData.get("name") == this.get("selected")) {
  134. return "red";
  135. } else {
  136. return "gray";
  137. }
  138. }.bind(this));
  139. nodeUpdate.selectAll("text")
  140. .style("fill-opacity", 1);
  141. // Transition exiting nodes to the parent's new position.
  142. var nodeExit = node.exit().transition()
  143. .duration(duration)
  144. .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
  145. .remove();
  146. nodeExit.select("circle")
  147. .attr("r", 1e-6);
  148. nodeExit.select("text")
  149. .style("fill-opacity", 1e-6);
  150. // Update the links…
  151. var link = this.mainSvg.selectAll("path.link")
  152. .data(links, function(d) { return d.target.id; });
  153. // Enter any new links at the parent's previous position.
  154. link.enter().insert("path", "g")
  155. .attr("class", "link")
  156. .attr("d", function(d) {
  157. var o = {x: source.x0, y: source.y0};
  158. return diagonal({source: o, target: o});
  159. });
  160. // Transition links to their new position.
  161. link.transition()
  162. .duration(duration)
  163. .attr("d", diagonal);
  164. // Transition exiting nodes to the parent's new position.
  165. link.exit().transition()
  166. .duration(duration)
  167. .attr("d", function(d) {
  168. var o = {x: source.x, y: source.y};
  169. return diagonal({source: o, target: o});
  170. })
  171. .remove();
  172. // Stash the old positions for transition.
  173. nodes.forEach(function(d) {
  174. d.x0 = d.x;
  175. d.y0 = d.y;
  176. });
  177. },
  178. reDraw: function() {
  179. this.initData();
  180. var margin = {top: 20, right: 120, bottom: 20, left: 120};
  181. var treeWidth = this.maxDepth * 200;
  182. var treeHeight = this.numOfLeafQueue * 80;
  183. var width = treeWidth + margin.left + margin.right;
  184. var height = treeHeight + margin.top + margin.bottom;
  185. var layout = { };
  186. if (this.mainSvg) {
  187. this.mainSvg.remove();
  188. }
  189. this.mainSvg = d3.select("#" + this.get("parentId")).append("svg")
  190. .attr("width", width)
  191. .attr("height", height)
  192. .attr("class", "tree-selector")
  193. .append("g")
  194. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  195. var tree = d3.layout.tree().size([treeHeight, treeWidth]);
  196. var diagonal = d3.svg.diagonal()
  197. .projection(function(d) { return [d.y, d.x]; });
  198. var root = this.treeData;
  199. root.x0 = height / 2;
  200. root.y0 = 0;
  201. d3.select(self.frameElement).style("height", height);
  202. this.update(root, root, tree, diagonal);
  203. },
  204. didInsertElement: function() {
  205. this.reDraw();
  206. }
  207. });