timeline-view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. import Ember from 'ember';
  19. import Converter from 'yarn-ui/utils/converter';
  20. import ColumnDef from 'em-table/utils/column-definition';
  21. export default Ember.Component.extend({
  22. canvas: {
  23. svg: undefined,
  24. h: 0,
  25. w: 0,
  26. tooltip: undefined
  27. },
  28. clusterMetrics: undefined,
  29. modelArr: [],
  30. containerIdArr: [],
  31. colors: d3.scale.category10().range(),
  32. _selected: undefined,
  33. gridColumns: [],
  34. gridRows: [],
  35. serviceName: undefined,
  36. selected: function() {
  37. return this._selected;
  38. }.property(),
  39. tableComponentName: function() {
  40. return "app-attempt-table";
  41. }.property(),
  42. setSelected: function(d) {
  43. var dom;
  44. if (this._selected === d) {
  45. return;
  46. }
  47. // restore color
  48. if (this._selected) {
  49. dom = d3.select("#timeline-bar-" + this._selected.get("id"));
  50. dom.attr("fill", this.colors[0]);
  51. }
  52. this._selected = d;
  53. this.set("selected", d);
  54. dom = d3.select("#timeline-bar-" + d.get("id"));
  55. dom.attr("fill", this.colors[1]);
  56. },
  57. getPerItemHeight: function() {
  58. var arrSize = this.modelArr.length;
  59. if (arrSize < 20) {
  60. return 30;
  61. } else if (arrSize < 100) {
  62. return 10;
  63. } else {
  64. return 2;
  65. }
  66. },
  67. getPerItemGap: function() {
  68. var arrSize = this.modelArr.length;
  69. if (arrSize < 20) {
  70. return 5;
  71. } else if (arrSize < 100) {
  72. return 1;
  73. } else {
  74. return 1;
  75. }
  76. },
  77. getCanvasHeight: function() {
  78. return (this.getPerItemHeight() + this.getPerItemGap()) * this.modelArr.length + 200;
  79. },
  80. draw: function(start, end) {
  81. // get w/h of the svg
  82. var bbox = d3.select("#" + this.get("parent-id"))
  83. .node()
  84. .getBoundingClientRect();
  85. this.canvas.w = bbox.width;
  86. this.canvas.h = this.getCanvasHeight();
  87. this.canvas.svg = d3.select("#" + this.get("parent-id"))
  88. .append("svg")
  89. .attr("width", this.canvas.w)
  90. .attr("height", this.canvas.h)
  91. .attr("id", this.get("my-id"));
  92. this.renderTimeline(start, end);
  93. },
  94. renderTimeline: function(start, end) {
  95. var border = 30;
  96. var singleBarHeight = this.getPerItemHeight();
  97. var gap = this.getPerItemGap();
  98. var textWidth = 200;
  99. /*
  100. start-time end-time
  101. |--------------------------------------|
  102. ==============
  103. ==============
  104. ==============
  105. ===============
  106. */
  107. var xScaler = d3.scale.linear()
  108. .domain([start, end])
  109. .range([0, this.canvas.w - 2 * border - textWidth]);
  110. /*
  111. * Render frame of timeline view
  112. */
  113. this.canvas.svg.append("line")
  114. .attr("x1", border + textWidth)
  115. .attr("y1", border - 5)
  116. .attr("x2", this.canvas.w - border)
  117. .attr("y2", border - 5)
  118. .attr("class", "chart");
  119. this.canvas.svg.append("line")
  120. .attr("x1", border + textWidth)
  121. .attr("y1", border - 10)
  122. .attr("x2", border + textWidth)
  123. .attr("y2", border - 5)
  124. .attr("class", "chart");
  125. this.canvas.svg.append("line")
  126. .attr("x1", this.canvas.w - border)
  127. .attr("y1", border - 10)
  128. .attr("x2", this.canvas.w - border)
  129. .attr("y2", border - 5)
  130. .attr("class", "chart");
  131. this.canvas.svg.append("text")
  132. .text(Converter.timeStampToDate(start))
  133. .attr("y", border - 15)
  134. .attr("x", border + textWidth)
  135. .attr("class", "bar-chart-text")
  136. .attr("text-anchor", "left");
  137. this.canvas.svg.append("text")
  138. .text(Converter.timeStampToDate(end))
  139. .attr("y", border - 15)
  140. .attr("x", this.canvas.w - border)
  141. .attr("class", "bar-chart-text")
  142. .attr("text-anchor", "end");
  143. // show bar
  144. var bar = this.canvas.svg.selectAll("bars")
  145. .data(this.modelArr)
  146. .enter()
  147. .append("rect")
  148. .attr("y", function(d, i) {
  149. return border + (gap + singleBarHeight) * i;
  150. })
  151. .attr("x", function(d) {
  152. return border + textWidth + xScaler(d.get("startTs"));
  153. })
  154. .attr("height", singleBarHeight)
  155. .attr("fill", function() {
  156. return this.colors[0];
  157. }.bind(this))
  158. .attr("width", function(d) {
  159. var finishedTs = xScaler(d.get("finishedTs"));
  160. finishedTs = finishedTs > 0 ? finishedTs : xScaler(end);
  161. return finishedTs - xScaler(d.get("startTs"));
  162. })
  163. .attr("id", function(d) {
  164. return "timeline-bar-" + d.get("id");
  165. });
  166. bar.on("click", function(d) {
  167. this.setSelected(d);
  168. }.bind(this));
  169. this.bindTooltip(bar);
  170. if (this.modelArr.length <= 20) {
  171. // show bar texts
  172. for (var i = 0; i < this.modelArr.length; i++) {
  173. this.canvas.svg.append("text")
  174. .text(this.modelArr[i].get(this.get("label")))
  175. .attr("y", border + (gap + singleBarHeight) * i + singleBarHeight / 2)
  176. .attr("x", border)
  177. .attr("class", "bar-chart-text");
  178. }
  179. }
  180. },
  181. bindTooltip: function(d) {
  182. d.on("mouseover", function() {
  183. this.tooltip
  184. .style("left", (d3.event.pageX) + "px")
  185. .style("top", (d3.event.pageY - 28) + "px");
  186. }.bind(this))
  187. .on("mousemove", function(d) {
  188. this.tooltip.style("opacity", 0.9);
  189. this.tooltip.html(d.get("tooltipLabel"))
  190. .style("left", (d3.event.pageX) + "px")
  191. .style("top", (d3.event.pageY - 28) + "px");
  192. }.bind(this))
  193. .on("mouseout", function() {
  194. this.tooltip.style("opacity", 0);
  195. }.bind(this));
  196. },
  197. initTooltip: function() {
  198. this.tooltip = d3.select("body")
  199. .append("div")
  200. .attr("class", "tooltip")
  201. .attr("id", "chart-tooltip")
  202. .style("opacity", 0);
  203. },
  204. didInsertElement: function() {
  205. // init tooltip
  206. this.initTooltip();
  207. this.modelArr = [];
  208. this.containerIdArr = [];
  209. // init model
  210. if (this.get("rmModel")) {
  211. this.get("rmModel").forEach(function(o) {
  212. if(!this.modelArr.contains(o)) {
  213. this.modelArr.push(o);
  214. this.containerIdArr.push(o.id);
  215. }
  216. }.bind(this));
  217. }
  218. if (this.get("tsModel")) {
  219. this.get("tsModel").forEach(function(o) {
  220. if(!this.containerIdArr.contains(o.id)) {
  221. this.modelArr.push(o);
  222. }
  223. }.bind(this));
  224. }
  225. if(this.modelArr.length === 0) {
  226. return;
  227. }
  228. this.modelArr.sort(function(a, b) {
  229. var tsA = a.get("startTs");
  230. var tsB = b.get("startTs");
  231. return tsA - tsB;
  232. });
  233. var begin = 0;
  234. if (this.modelArr.length > 0) {
  235. begin = this.modelArr[0].get("startTs");
  236. }
  237. var end = 0;
  238. for (var i = 0; i < this.modelArr.length; i++) {
  239. var ts = this.modelArr[i].get("finishedTs");
  240. if (ts > end) {
  241. end = ts;
  242. }
  243. }
  244. if (end < begin) {
  245. end = Date.now();
  246. }
  247. this.draw(begin, end);
  248. if (this.modelArr.length > 0) {
  249. this.setSelected(this.modelArr[0]);
  250. }
  251. if (this.get('attemptModel')) {
  252. this.setAttemptsGridColumnsAndRows();
  253. } else {
  254. this.setContainersGridColumnsAndRows();
  255. }
  256. },
  257. setAttemptsGridColumnsAndRows: function() {
  258. var self = this;
  259. var columns = [];
  260. var serviceName = this.get('serviceName');
  261. columns.push({
  262. id: 'id',
  263. headerTitle: 'Attempt ID',
  264. contentPath: 'id',
  265. cellComponentName: 'em-table-linked-cell',
  266. minWidth: '300px',
  267. getCellContent: function(row) {
  268. var attemptId = row.get('id');
  269. var query = serviceName? '?service='+serviceName : '';
  270. return {
  271. displayText: attemptId,
  272. href: `#/yarn-app-attempt/${attemptId}${query}`
  273. };
  274. }
  275. }, {
  276. id: 'attemptStartedTime',
  277. headerTitle: 'Started Time',
  278. contentPath: 'attemptStartedTime'
  279. }, {
  280. id: 'finishedTime',
  281. headerTitle: 'Finished Time',
  282. contentPath: 'finishedTime',
  283. getCellContent: function(row) {
  284. if (row.get('finishedTs')) {
  285. return row.get('finishedTime');
  286. }
  287. return 'N/A';
  288. }
  289. }, {
  290. id: 'elapsedTime',
  291. headerTitle: 'Elapsed Time',
  292. contentPath: 'elapsedTime'
  293. }, {
  294. id: 'appMasterContainerId',
  295. headerTitle: 'AM Container ID',
  296. contentPath: 'appMasterContainerId',
  297. minWidth: '350px'
  298. }, {
  299. id: 'amNodeId',
  300. headerTitle: 'AM Node ID',
  301. contentPath: 'amNodeId'
  302. }, {
  303. id: 'attemptState',
  304. headerTitle: 'State',
  305. contentPath: 'attemptState',
  306. getCellContent: function(row) {
  307. var state = row.get('attemptState');
  308. if (state) {
  309. return state;
  310. } else {
  311. return 'N/A';
  312. }
  313. }
  314. }, {
  315. id: 'nodeHttpAddress',
  316. headerTitle: 'NodeManager Web UI',
  317. contentPath: 'nodeHttpAddress',
  318. cellComponentName: 'em-table-html-cell',
  319. getCellContent: function(row) {
  320. var address = self.checkHttpProtocol(row.get('nodeHttpAddress'));
  321. if (address) {
  322. return `<a href="${address}" target="_blank">${address}</a>`;
  323. } else {
  324. return 'N/A';
  325. }
  326. }
  327. }, {
  328. id: 'logsLink',
  329. headerTitle: 'Logs',
  330. contentPath: 'logsLink',
  331. cellComponentName: 'em-table-html-cell',
  332. getCellContent: function(row) {
  333. var logUrl = self.checkHttpProtocol(row.get('logsLink'));
  334. if (logUrl) {
  335. return `<a href="${logUrl}" target="_blank">Link</a>`;
  336. } else {
  337. return 'N/A';
  338. }
  339. }
  340. });
  341. var gridCols = ColumnDef.make(columns);
  342. this.set('gridColumns', gridCols);
  343. this.set('gridRows', this.modelArr);
  344. },
  345. setContainersGridColumnsAndRows: function() {
  346. var self = this;
  347. var columns = [];
  348. columns.push({
  349. id: 'id',
  350. headerTitle: 'Container ID',
  351. contentPath: 'id',
  352. minWidth: '350px'
  353. }, {
  354. id: 'startedTime',
  355. headerTitle: 'Started Time',
  356. contentPath: 'startedTime'
  357. }, {
  358. id: 'finishedTime',
  359. headerTitle: 'Finished Time',
  360. contentPath: 'finishedTime',
  361. getCellContent: function(row) {
  362. if (row.get('finishedTs')) {
  363. return row.get('finishedTime');
  364. }
  365. return 'N/A';
  366. }
  367. }, {
  368. id: 'elapsedTime',
  369. headerTitle: 'Elapsed Time',
  370. contentPath: 'elapsedTime'
  371. }, {
  372. id: 'priority',
  373. headerTitle: 'Priority',
  374. contentPath: 'priority'
  375. }, {
  376. id: 'containerExitStatus',
  377. headerTitle: 'Exit Status',
  378. contentPath: 'containerExitStatus',
  379. getCellContent: function(row) {
  380. var status = row.get('containerExitStatus');
  381. if (status) {
  382. return status;
  383. } else {
  384. return 'N/A';
  385. }
  386. }
  387. }, {
  388. id: 'containerState',
  389. headerTitle: 'State',
  390. contentPath: 'containerState',
  391. getCellContent: function(row) {
  392. var state = row.get('containerState');
  393. if (state) {
  394. return state;
  395. } else {
  396. return 'N/A';
  397. }
  398. }
  399. }, {
  400. id: 'logUrl',
  401. headerTitle: 'Logs',
  402. contentPath: 'logUrl',
  403. cellComponentName: 'em-table-html-cell',
  404. getCellContent: function(row) {
  405. var url = self.checkHttpProtocol(row.get('logUrl'));
  406. if (url) {
  407. return `<a href="${url}" target="_blank">${url}</a>`;
  408. } else {
  409. return 'N/A';
  410. }
  411. }
  412. }, {
  413. id: 'nodeHttpAddress',
  414. headerTitle: 'Node Manager UI',
  415. contentPath: 'nodeHttpAddress',
  416. cellComponentName: 'em-table-html-cell',
  417. getCellContent: function(row) {
  418. var address = self.checkHttpProtocol(row.get('nodeHttpAddress'));
  419. if (address) {
  420. return `<a href="${address}" target="_blank">${address}</a>`;
  421. } else {
  422. return 'N/A';
  423. }
  424. }
  425. });
  426. var gridCols = ColumnDef.make(columns);
  427. this.set('gridColumns', gridCols);
  428. this.set('gridRows', this.modelArr);
  429. },
  430. checkHttpProtocol: function(prop) {
  431. if (prop && prop.indexOf('://') < 0) {
  432. prop = 'http://' + prop;
  433. }
  434. return prop;
  435. }
  436. });