linear_time_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. var App = require('app');
  19. require('views/common/chart/linear_time');
  20. describe('App.ChartLinearTimeView', function () {
  21. var chartLinearTimeView = App.ChartLinearTimeView.create({});
  22. describe('#transformData', function () {
  23. var result;
  24. var data = [[1, 1200000000], [2, 1200000000], [3, 1200000000]];
  25. var name = 'abc';
  26. beforeEach(function () {
  27. sinon.stub(App.router, 'get').withArgs('userSettingsController.userSettings.timezone').returns('(UTC+00:00) Greenwich');
  28. sinon.stub(App, 'dateTimeWithTimeZone').returns(1);
  29. });
  30. afterEach(function () {
  31. App.router.get.restore();
  32. App.dateTimeWithTimeZone.restore();
  33. });
  34. it('"name" should be "abc" ', function () {
  35. result = chartLinearTimeView.transformData(data, name);
  36. expect(result.name).to.equal('abc');
  37. });
  38. it('data size should be 3 ', function () {
  39. result = chartLinearTimeView.transformData(data, name);
  40. expect(result.data.length).to.equal(3);
  41. });
  42. it('data[0].y should be 1 ', function () {
  43. result = chartLinearTimeView.transformData(data, name);
  44. expect(result.data[0].y).to.equal(1);
  45. })
  46. });
  47. describe('#yAxisFormatter', function() {
  48. var tests = [
  49. {m:'undefined to 0',i:undefined,e:0},
  50. {m:'NaN to 0',i:NaN,e:0},
  51. {m:'0 to 0',i:'0',e:'0'},
  52. {m:'1000 to 1K',i:'1000',e:'1K'},
  53. {m:'1000000 to 1M',i:'1000000',e:'1M'},
  54. {m:'1000000000 to 1B',i:'1000000000',e:'1B'},
  55. {m:'1000000000000 to 1T',i:'1000000000000',e:'1T'},
  56. {m:'1048576 to 1.049M',i:'1048576',e:'1.049M'},
  57. {m:'1073741824 to 1.074B',i:'1073741824',e:'1.074B'}
  58. ];
  59. tests.forEach(function(test) {
  60. it(test.m + ' ', function () {
  61. expect(chartLinearTimeView.yAxisFormatter(test.i)).to.equal(test.e);
  62. });
  63. });
  64. });
  65. describe('#checkSeries', function() {
  66. var tests = [
  67. {m:'undefined - false',i:undefined,e:false},
  68. {m:'NaN - false',i:NaN,e:false},
  69. {m:'object without data property - false',i:[{}],e:false},
  70. {m:'object with empty data property - false',i:[{data:[]}],e:false},
  71. {m:'object with invalid data property - false',i:[{data:[1]}],e:false},
  72. {m:'object with valid data property - true',i:[{data:[{x:1,y:1},{x:2,y:2}]}],e:true}
  73. ];
  74. tests.forEach(function(test) {
  75. it(test.m + ' ', function () {
  76. expect(chartLinearTimeView.checkSeries(test.i)).to.equal(test.e);
  77. });
  78. });
  79. });
  80. describe('#BytesFormatter', function() {
  81. var tests = [
  82. {m:'undefined to "0 B"',i:undefined,e:'0 B'},
  83. {m:'NaN to "0 B"',i:NaN,e:'0 B'},
  84. {m:'0 to "0 B"',i:0,e:'0 B'},
  85. {m:'124 to "124 B"',i:124,e:'124 B'},
  86. {m:'1024 to "1 KB"',i:1024,e:'1 KB'},
  87. {m:'1536 to "1 KB"',i:1536,e:'1.5 KB'},
  88. {m:'1048576 to "1 MB"',i:1048576,e:'1 MB'},
  89. {m:'1073741824 to "1 GB"',i:1073741824,e:'1 GB'},
  90. {m:'1610612736 to "1.5 GB"',i:1610612736,e:'1.5 GB'}
  91. ];
  92. tests.forEach(function(test) {
  93. it(test.m + ' ', function () {
  94. expect(App.ChartLinearTimeView.BytesFormatter(test.i)).to.equal(test.e);
  95. });
  96. });
  97. });
  98. describe('#PercentageFormatter', function() {
  99. var tests = [
  100. {m:'undefined to "0 %"',i:undefined,e:'0 %'},
  101. {m:'NaN to "0 %"',i:NaN,e:'0 %'},
  102. {m:'0 to "0 %"',i:0,e:'0 %'},
  103. {m:'1 to "1%"',i:1,e:'1%'},
  104. {m:'1.12341234 to "1.123%"',i:1.12341234,e:'1.123%'},
  105. {m:'-11 to "-11%"',i:-11,e:'-11%'},
  106. {m:'-11.12341234 to "-11.123%"',i:-11.12341234,e:'-11.123%'}
  107. ];
  108. tests.forEach(function(test) {
  109. it(test.m + ' ', function () {
  110. expect(App.ChartLinearTimeView.PercentageFormatter(test.i)).to.equal(test.e);
  111. });
  112. });
  113. });
  114. describe('#TimeElapsedFormatter', function() {
  115. var tests = [
  116. {m:'undefined to "0 ms"',i:undefined,e:'0 ms'},
  117. {m:'NaN to "0 ms"',i:NaN,e:'0 ms'},
  118. {m:'0 to "0 ms"',i:0,e:'0 ms'},
  119. {m:'1000 to "1000 ms"',i:1000,e:'1000 ms'},
  120. {m:'120000 to "2 m"',i:120000,e:'2 m'},
  121. {m:'3600000 to "60 m"',i:3600000,e:'60 m'},
  122. {m:'5000000 to "1 hr"',i:5000000,e:'1 hr'},
  123. {m:'7200000 to "2 hr"',i:7200000,e:'2 hr'},
  124. {m:'90000000 to "1 d"',i:90000000,e:'1 d'}
  125. ];
  126. tests.forEach(function(test) {
  127. it(test.m + ' ', function () {
  128. expect(App.ChartLinearTimeView.TimeElapsedFormatter(test.i)).to.equal(test.e);
  129. });
  130. });
  131. });
  132. describe("#getDataForAjaxRequest()", function() {
  133. var services = {
  134. yarnService: [],
  135. hdfsService: []
  136. };
  137. var rangeCases = [
  138. {
  139. currentTimeIndex: 0,
  140. customStartTime: 100000,
  141. customEndTime: 200000,
  142. fromSeconds: -3599,
  143. toSeconds: 1,
  144. title: 'preset time range'
  145. },
  146. {
  147. currentTimeIndex: 8,
  148. customStartTime: 100000,
  149. customEndTime: 200000,
  150. fromSeconds: 100,
  151. toSeconds: 200,
  152. title: 'custom time range'
  153. },
  154. {
  155. currentTimeIndex: 8,
  156. customStartTime: null,
  157. customEndTime: null,
  158. fromSeconds: -3599,
  159. toSeconds: 1,
  160. title: 'custom time range, no boundaries set'
  161. }
  162. ];
  163. beforeEach(function(){
  164. sinon.stub(App.HDFSService, 'find', function(){return services.hdfsService});
  165. sinon.stub(App.YARNService, 'find', function(){return services.yarnService});
  166. sinon.stub(App, 'dateTime').returns(1000);
  167. chartLinearTimeView.set('content', null);
  168. });
  169. afterEach(function(){
  170. App.HDFSService.find.restore();
  171. App.YARNService.find.restore();
  172. App.dateTime.restore();
  173. });
  174. it("content has hostName", function() {
  175. chartLinearTimeView.set('content', Em.Object.create({
  176. hostName: 'host1'
  177. }));
  178. expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
  179. toSeconds: 1,
  180. fromSeconds: -3599,
  181. stepSeconds: 15,
  182. hostName: 'host1',
  183. nameNodeName: '',
  184. resourceManager: ''
  185. });
  186. });
  187. it("get Namenode host", function() {
  188. services.hdfsService = [
  189. Em.Object.create({
  190. nameNode: {hostName: 'host1'}
  191. })
  192. ];
  193. expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
  194. toSeconds: 1,
  195. fromSeconds: -3599,
  196. stepSeconds: 15,
  197. hostName: '',
  198. nameNodeName: 'host1',
  199. resourceManager: ''
  200. });
  201. services.hdfsService = [];
  202. });
  203. it("get Namenode host HA", function() {
  204. services.hdfsService = [
  205. Em.Object.create({
  206. activeNameNode: {hostName: 'host1'}
  207. })
  208. ];
  209. expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
  210. toSeconds: 1,
  211. fromSeconds: -3599,
  212. stepSeconds: 15,
  213. hostName: '',
  214. nameNodeName: 'host1',
  215. resourceManager: ''
  216. });
  217. services.hdfsService = [];
  218. });
  219. it("get resourceManager host", function() {
  220. services.yarnService = [
  221. Em.Object.create({
  222. resourceManager: {hostName: 'host1'}
  223. })
  224. ];
  225. expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
  226. toSeconds: 1,
  227. fromSeconds: -3599,
  228. stepSeconds: 15,
  229. hostName: '',
  230. nameNodeName: '',
  231. resourceManager: 'host1'
  232. });
  233. services.yarnService = [];
  234. });
  235. rangeCases.forEach(function (item) {
  236. it(item.title, function () {
  237. chartLinearTimeView.setProperties({
  238. currentTimeIndex: item.currentTimeIndex,
  239. customStartTime: item.customStartTime,
  240. customEndTime: item.customEndTime
  241. });
  242. var requestData = Em.Object.create(chartLinearTimeView.getDataForAjaxRequest());
  243. expect(requestData.getProperties(['fromSeconds', 'toSeconds'])).to.eql({
  244. fromSeconds: item.fromSeconds,
  245. toSeconds: item.toSeconds
  246. });
  247. });
  248. });
  249. });
  250. describe('#setCurrentTimeIndexFromParent', function () {
  251. var view,
  252. cases = [
  253. {
  254. parent: 1,
  255. child: 2,
  256. result: 2,
  257. title: 'child and parent have currentTimeRangeIndex'
  258. },
  259. {
  260. parent: undefined,
  261. child: 2,
  262. result: 2,
  263. title: 'only child has currentTimeRangeIndex'
  264. },
  265. {
  266. parent: 1,
  267. child: undefined,
  268. result: 1,
  269. title: 'only parent has currentTimeRangeIndex'
  270. }
  271. ];
  272. beforeEach(function () {
  273. view = App.ChartLinearTimeView.create({
  274. controller: {},
  275. parentView: Em.Object.create({
  276. currentTimeRangeIndex: 1,
  277. parentView: Em.Object.create({
  278. currentTimeRangeIndex: 2
  279. })
  280. })
  281. });
  282. });
  283. cases.forEach(function (item) {
  284. it(item.title, function () {
  285. view.set('parentView.currentTimeRangeIndex', item.child);
  286. view.set('parentView.parentView.currentTimeRangeIndex', item.parent);
  287. view.propertyDidChange('parentView.currentTimeRangeIndex');
  288. expect(view.get('currentTimeIndex')).to.equal(item.result);
  289. });
  290. });
  291. });
  292. describe('#loadDataSuccessCallback', function () {
  293. beforeEach(function () {
  294. sinon.stub(chartLinearTimeView, '_refreshGraph', Em.K);
  295. });
  296. afterEach(function () {
  297. chartLinearTimeView._refreshGraph.restore();
  298. });
  299. it('should refresh graph', function () {
  300. var response = {
  301. key: 'value'
  302. };
  303. chartLinearTimeView.loadDataSuccessCallback(response);
  304. expect(chartLinearTimeView._refreshGraph.calledOnce).to.be.true;
  305. expect(chartLinearTimeView._refreshGraph.calledWith(response)).to.be.true;
  306. });
  307. });
  308. describe('#setYAxisFormatter', function () {
  309. var view,
  310. cases = [
  311. {
  312. displayUnit: '%',
  313. formatter: 'PercentageFormatter'
  314. },
  315. {
  316. displayUnit: 'B',
  317. formatter: 'BytesFormatter'
  318. },
  319. {
  320. displayUnit: 'ms',
  321. formatter: 'TimeElapsedFormatter'
  322. },
  323. {
  324. displayUnit: 'kg',
  325. formatter: 'DefaultFormatter',
  326. title: 'other display unit'
  327. },
  328. {
  329. formatter: 'DefaultFormatter',
  330. title: 'no display unit'
  331. }
  332. ],
  333. methodNames = ['PercentageFormatter', 'CreateRateFormatter', 'BytesFormatter', 'TimeElapsedFormatter', 'DefaultFormatter'];
  334. beforeEach(function () {
  335. view = App.ChartLinearTimeView.create();
  336. methodNames.forEach(function (name) {
  337. sinon.stub(App.ChartLinearTimeView, name, Em.K);
  338. });
  339. });
  340. afterEach(function () {
  341. methodNames.forEach(function (name) {
  342. App.ChartLinearTimeView[name].restore();
  343. });
  344. });
  345. cases.forEach(function (item) {
  346. it(item.title || item.displayUnit, function () {
  347. view.set('displayUnit', item.displayUnit);
  348. view.setYAxisFormatter();
  349. view.yAxisFormatter();
  350. methodNames.forEach(function (name) {
  351. expect(App.ChartLinearTimeView[name].callCount).to.equal(Number(name === item.formatter));
  352. });
  353. });
  354. });
  355. });
  356. });
  357. describe('App.ChartLinearTimeView.LoadAggregator', function () {
  358. var aggregator = App.ChartLinearTimeView.LoadAggregator;
  359. describe("#add()", function () {
  360. beforeEach(function () {
  361. sinon.stub(window, 'setTimeout').returns('timeId');
  362. });
  363. afterEach(function () {
  364. window.setTimeout.restore();
  365. });
  366. it("timeout started", function () {
  367. aggregator.set('timeoutId', 'timeId');
  368. aggregator.get('requests').clear();
  369. aggregator.add({}, {});
  370. expect(aggregator.get('requests')).to.not.be.empty;
  371. expect(window.setTimeout.called).to.be.false;
  372. });
  373. it("timeout started (2)", function () {
  374. aggregator.set('timeoutId', null);
  375. aggregator.get('requests').clear();
  376. aggregator.add({}, {});
  377. expect(aggregator.get('requests')).to.not.be.empty;
  378. expect(window.setTimeout.calledOnce).to.be.true;
  379. expect(aggregator.get('timeoutId')).to.equal('timeId');
  380. });
  381. });
  382. describe("#groupRequests()", function () {
  383. var result;
  384. beforeEach(function () {
  385. var requests = [
  386. {
  387. name: 'r1',
  388. context: 'c1',
  389. fields: ['f1']
  390. },
  391. {
  392. name: 'r2',
  393. context: 'c2',
  394. fields: ['f2']
  395. },
  396. {
  397. name: 'r2',
  398. context: 'c3',
  399. fields: ['f3', 'f4']
  400. }
  401. ];
  402. result = aggregator.groupRequests(requests);
  403. });
  404. it("result['r1'].subRequests.length", function () {
  405. expect(result.r1.subRequests.length).to.equal(1);
  406. });
  407. it("result['r1'].fields.length", function () {
  408. expect(result.r1.fields.length).to.equal(1);
  409. });
  410. it("result['r2'].subRequests.length", function () {
  411. expect(result.r2.subRequests.length).to.equal(2);
  412. });
  413. it("result['r2'].fields.length", function () {
  414. expect(result.r2.fields.length).to.equal(3);
  415. });
  416. });
  417. describe("#runRequests()", function () {
  418. beforeEach(function () {
  419. sinon.stub(aggregator, 'groupRequests', function (requests) {
  420. return requests;
  421. });
  422. sinon.stub(aggregator, 'formatRequestData', function(_request){
  423. return _request.fields;
  424. });
  425. sinon.stub(App.ajax, 'send', function(){
  426. return {
  427. done: Em.K,
  428. fail: Em.K
  429. }
  430. });
  431. });
  432. afterEach(function () {
  433. aggregator.groupRequests.restore();
  434. App.ajax.send.restore();
  435. aggregator.formatRequestData.restore();
  436. });
  437. it("valid request is sent", function () {
  438. var context = Em.Object.create({content: {hostName: 'host1'}});
  439. var requests = {
  440. 'r1': {
  441. name: 'r1',
  442. context: context,
  443. fields: ['f3', 'f4']
  444. }
  445. };
  446. aggregator.runRequests(requests);
  447. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  448. name: 'r1',
  449. sender: context,
  450. data: {
  451. fields: ['f3', 'f4'],
  452. hostName: 'host1'
  453. }
  454. });
  455. });
  456. });
  457. describe("#formatRequestData()", function () {
  458. var cases = [
  459. {
  460. currentTimeIndex: 0,
  461. customStartTime: 100000,
  462. customEndTime: 200000,
  463. result: 'f3[400,4000,15],f4[400,4000,15]',
  464. title: 'preset time range'
  465. },
  466. {
  467. currentTimeIndex: 8,
  468. customStartTime: 100000,
  469. customEndTime: 200000,
  470. result: 'f3[100,200,15],f4[100,200,15]',
  471. title: 'custom time range'
  472. },
  473. {
  474. currentTimeIndex: 8,
  475. customStartTime: null,
  476. customEndTime: null,
  477. result: 'f3[400,4000,15],f4[400,4000,15]',
  478. title: 'custom time range, no boundaries set'
  479. }
  480. ];
  481. beforeEach(function () {
  482. sinon.stub(App, 'dateTime').returns(4000000);
  483. });
  484. afterEach(function () {
  485. App.dateTime.restore();
  486. });
  487. cases.forEach(function (item) {
  488. it(item.title, function () {
  489. var context = Em.Object.create({
  490. timeUnitSeconds: 3600,
  491. currentTimeIndex: item.currentTimeIndex,
  492. customStartTime: item.customStartTime,
  493. customEndTime: item.customEndTime
  494. });
  495. var request = {
  496. name: 'r1',
  497. context: context,
  498. fields: ['f3', 'f4']
  499. };
  500. expect(aggregator.formatRequestData(request)).to.equal(item.result);
  501. });
  502. });
  503. });
  504. });