linear_time_test.js 19 KB

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