123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var App = require('app');
- require('views/common/chart/linear_time');
- describe('App.ChartLinearTimeView', function () {
- var chartLinearTimeView = App.ChartLinearTimeView.create({});
- describe('#transformData', function () {
- var result;
- var data = [[1, 1200000000], [2, 1200000000], [3, 1200000000]];
- var name = 'abc';
- beforeEach(function () {
- sinon.stub(App.router, 'get').withArgs('userSettingsController.userSettings.timezone').returns('(UTC+00:00) Greenwich');
- sinon.stub(App, 'dateTimeWithTimeZone').returns(1);
- });
- afterEach(function () {
- App.router.get.restore();
- App.dateTimeWithTimeZone.restore();
- });
- it('"name" should be "abc" ', function () {
- result = chartLinearTimeView.transformData(data, name);
- expect(result.name).to.equal('abc');
- });
- it('data size should be 3 ', function () {
- result = chartLinearTimeView.transformData(data, name);
- expect(result.data.length).to.equal(3);
- });
- it('data[0].y should be 1 ', function () {
- result = chartLinearTimeView.transformData(data, name);
- expect(result.data[0].y).to.equal(1);
- })
- });
- describe('#yAxisFormatter', function() {
- var tests = [
- {m:'undefined to 0',i:undefined,e:0},
- {m:'NaN to 0',i:NaN,e:0},
- {m:'0 to 0',i:'0',e:'0'},
- {m:'1000 to 1K',i:'1000',e:'1K'},
- {m:'1000000 to 1M',i:'1000000',e:'1M'},
- {m:'1000000000 to 1B',i:'1000000000',e:'1B'},
- {m:'1000000000000 to 1T',i:'1000000000000',e:'1T'},
- {m:'1048576 to 1.049M',i:'1048576',e:'1.049M'},
- {m:'1073741824 to 1.074B',i:'1073741824',e:'1.074B'}
- ];
- tests.forEach(function(test) {
- it(test.m + ' ', function () {
- expect(chartLinearTimeView.yAxisFormatter(test.i)).to.equal(test.e);
- });
- });
- });
- describe('#checkSeries', function() {
- var tests = [
- {m:'undefined - false',i:undefined,e:false},
- {m:'NaN - false',i:NaN,e:false},
- {m:'object without data property - false',i:[{}],e:false},
- {m:'object with empty data property - false',i:[{data:[]}],e:false},
- {m:'object with invalid data property - false',i:[{data:[1]}],e:false},
- {m:'object with valid data property - true',i:[{data:[{x:1,y:1},{x:2,y:2}]}],e:true}
- ];
- tests.forEach(function(test) {
- it(test.m + ' ', function () {
- expect(chartLinearTimeView.checkSeries(test.i)).to.equal(test.e);
- });
- });
- });
- describe('#BytesFormatter', function() {
- var tests = [
- {m:'undefined to "0 B"',i:undefined,e:'0 B'},
- {m:'NaN to "0 B"',i:NaN,e:'0 B'},
- {m:'0 to "0 B"',i:0,e:'0 B'},
- {m:'124 to "124 B"',i:124,e:'124 B'},
- {m:'1024 to "1 KB"',i:1024,e:'1 KB'},
- {m:'1536 to "1 KB"',i:1536,e:'1.5 KB'},
- {m:'1048576 to "1 MB"',i:1048576,e:'1 MB'},
- {m:'1073741824 to "1 GB"',i:1073741824,e:'1 GB'},
- {m:'1610612736 to "1.5 GB"',i:1610612736,e:'1.5 GB'}
- ];
- tests.forEach(function(test) {
- it(test.m + ' ', function () {
- expect(App.ChartLinearTimeView.BytesFormatter(test.i)).to.equal(test.e);
- });
- });
- });
- describe('#PercentageFormatter', function() {
- var tests = [
- {m:'undefined to "0 %"',i:undefined,e:'0 %'},
- {m:'NaN to "0 %"',i:NaN,e:'0 %'},
- {m:'0 to "0 %"',i:0,e:'0 %'},
- {m:'1 to "1%"',i:1,e:'1%'},
- {m:'1.12341234 to "1.123%"',i:1.12341234,e:'1.123%'},
- {m:'-11 to "-11%"',i:-11,e:'-11%'},
- {m:'-11.12341234 to "-11.123%"',i:-11.12341234,e:'-11.123%'}
- ];
- tests.forEach(function(test) {
- it(test.m + ' ', function () {
- expect(App.ChartLinearTimeView.PercentageFormatter(test.i)).to.equal(test.e);
- });
- });
- });
- describe('#TimeElapsedFormatter', function() {
- var tests = [
- {m:'undefined to "0 ms"',i:undefined,e:'0 ms'},
- {m:'NaN to "0 ms"',i:NaN,e:'0 ms'},
- {m:'0 to "0 ms"',i:0,e:'0 ms'},
- {m:'1000 to "1000 ms"',i:1000,e:'1000 ms'},
- {m:'120000 to "2 m"',i:120000,e:'2 m'},
- {m:'3600000 to "60 m"',i:3600000,e:'60 m'},
- {m:'5000000 to "1 hr"',i:5000000,e:'1 hr'},
- {m:'7200000 to "2 hr"',i:7200000,e:'2 hr'},
- {m:'90000000 to "1 d"',i:90000000,e:'1 d'}
- ];
- tests.forEach(function(test) {
- it(test.m + ' ', function () {
- expect(App.ChartLinearTimeView.TimeElapsedFormatter(test.i)).to.equal(test.e);
- });
- });
- });
- describe("#getDataForAjaxRequest()", function() {
- var services = {
- yarnService: [],
- hdfsService: []
- };
- var rangeCases = [
- {
- currentTimeIndex: 0,
- customStartTime: 100000,
- customEndTime: 200000,
- fromSeconds: -3599,
- toSeconds: 1,
- title: 'preset time range'
- },
- {
- currentTimeIndex: 8,
- customStartTime: 100000,
- customEndTime: 200000,
- fromSeconds: 100,
- toSeconds: 200,
- title: 'custom time range'
- },
- {
- currentTimeIndex: 8,
- customStartTime: null,
- customEndTime: null,
- fromSeconds: -3599,
- toSeconds: 1,
- title: 'custom time range, no boundaries set'
- }
- ];
- beforeEach(function(){
- sinon.stub(App.HDFSService, 'find', function(){return services.hdfsService});
- sinon.stub(App.YARNService, 'find', function(){return services.yarnService});
- sinon.stub(App, 'dateTime').returns(1000);
- chartLinearTimeView.set('content', null);
- });
- afterEach(function(){
- App.HDFSService.find.restore();
- App.YARNService.find.restore();
- App.dateTime.restore();
- });
- it("content has hostName", function() {
- chartLinearTimeView.set('content', Em.Object.create({
- hostName: 'host1'
- }));
- expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
- toSeconds: 1,
- fromSeconds: -3599,
- stepSeconds: 15,
- hostName: 'host1',
- nameNodeName: '',
- resourceManager: ''
- });
- });
- it("get Namenode host", function() {
- services.hdfsService = [
- Em.Object.create({
- nameNode: {hostName: 'host1'}
- })
- ];
- expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
- toSeconds: 1,
- fromSeconds: -3599,
- stepSeconds: 15,
- hostName: '',
- nameNodeName: 'host1',
- resourceManager: ''
- });
- services.hdfsService = [];
- });
- it("get Namenode host HA", function() {
- services.hdfsService = [
- Em.Object.create({
- activeNameNode: {hostName: 'host1'}
- })
- ];
- expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
- toSeconds: 1,
- fromSeconds: -3599,
- stepSeconds: 15,
- hostName: '',
- nameNodeName: 'host1',
- resourceManager: ''
- });
- services.hdfsService = [];
- });
- it("get resourceManager host", function() {
- services.yarnService = [
- Em.Object.create({
- resourceManager: {hostName: 'host1'}
- })
- ];
- expect(chartLinearTimeView.getDataForAjaxRequest()).to.be.eql({
- toSeconds: 1,
- fromSeconds: -3599,
- stepSeconds: 15,
- hostName: '',
- nameNodeName: '',
- resourceManager: 'host1'
- });
- services.yarnService = [];
- });
- rangeCases.forEach(function (item) {
- it(item.title, function () {
- chartLinearTimeView.setProperties({
- currentTimeIndex: item.currentTimeIndex,
- customStartTime: item.customStartTime,
- customEndTime: item.customEndTime
- });
- var requestData = Em.Object.create(chartLinearTimeView.getDataForAjaxRequest());
- expect(requestData.getProperties(['fromSeconds', 'toSeconds'])).to.eql({
- fromSeconds: item.fromSeconds,
- toSeconds: item.toSeconds
- });
- });
- });
- });
- describe('#setCurrentTimeIndexFromParent', function () {
- var view,
- cases = [
- {
- parent: 1,
- child: 2,
- result: 2,
- title: 'child and parent have currentTimeRangeIndex'
- },
- {
- parent: undefined,
- child: 2,
- result: 2,
- title: 'only child has currentTimeRangeIndex'
- },
- {
- parent: 1,
- child: undefined,
- result: 1,
- title: 'only parent has currentTimeRangeIndex'
- }
- ];
- beforeEach(function () {
- view = App.ChartLinearTimeView.create({
- controller: {},
- parentView: Em.Object.create({
- currentTimeRangeIndex: 1,
- parentView: Em.Object.create({
- currentTimeRangeIndex: 2
- })
- })
- });
- });
- cases.forEach(function (item) {
- it(item.title, function () {
- view.set('parentView.currentTimeRangeIndex', item.child);
- view.set('parentView.parentView.currentTimeRangeIndex', item.parent);
- view.propertyDidChange('parentView.currentTimeRangeIndex');
- expect(view.get('currentTimeIndex')).to.equal(item.result);
- });
- });
- });
- describe('#loadDataSuccessCallback', function () {
- beforeEach(function () {
- sinon.stub(chartLinearTimeView, '_refreshGraph', Em.K);
- });
- afterEach(function () {
- chartLinearTimeView._refreshGraph.restore();
- });
- it('should refresh graph', function () {
- var response = {
- key: 'value'
- };
- chartLinearTimeView.loadDataSuccessCallback(response);
- expect(chartLinearTimeView._refreshGraph.calledOnce).to.be.true;
- expect(chartLinearTimeView._refreshGraph.calledWith(response)).to.be.true;
- });
- });
- describe('#setYAxisFormatter', function () {
- var view,
- cases = [
- {
- displayUnit: '%',
- formatter: 'PercentageFormatter'
- },
- {
- displayUnit: 'B',
- formatter: 'BytesFormatter'
- },
- {
- displayUnit: 'ms',
- formatter: 'TimeElapsedFormatter'
- },
- {
- displayUnit: 'kg',
- formatter: 'DefaultFormatter',
- title: 'other display unit'
- },
- {
- formatter: 'DefaultFormatter',
- title: 'no display unit'
- }
- ],
- methodNames = ['PercentageFormatter', 'CreateRateFormatter', 'BytesFormatter', 'TimeElapsedFormatter', 'DefaultFormatter'];
- beforeEach(function () {
- view = App.ChartLinearTimeView.create();
- methodNames.forEach(function (name) {
- sinon.stub(App.ChartLinearTimeView, name, Em.K);
- });
- });
- afterEach(function () {
- methodNames.forEach(function (name) {
- App.ChartLinearTimeView[name].restore();
- });
- });
- cases.forEach(function (item) {
- it(item.title || item.displayUnit, function () {
- view.set('displayUnit', item.displayUnit);
- view.setYAxisFormatter();
- view.yAxisFormatter();
- methodNames.forEach(function (name) {
- expect(App.ChartLinearTimeView[name].callCount).to.equal(Number(name === item.formatter));
- });
- });
- });
- });
- });
- describe('App.ChartLinearTimeView.LoadAggregator', function () {
- var aggregator = App.ChartLinearTimeView.LoadAggregator;
- describe("#add()", function () {
- beforeEach(function () {
- sinon.stub(window, 'setTimeout').returns('timeId');
- });
- afterEach(function () {
- window.setTimeout.restore();
- });
- it("timeout started", function () {
- aggregator.set('timeoutId', 'timeId');
- aggregator.get('requests').clear();
- aggregator.add({}, {});
- expect(aggregator.get('requests')).to.not.be.empty;
- expect(window.setTimeout.called).to.be.false;
- });
- it("timeout started (2)", function () {
- aggregator.set('timeoutId', null);
- aggregator.get('requests').clear();
- aggregator.add({}, {});
- expect(aggregator.get('requests')).to.not.be.empty;
- expect(window.setTimeout.calledOnce).to.be.true;
- expect(aggregator.get('timeoutId')).to.equal('timeId');
- });
- });
- describe("#groupRequests()", function () {
- var result;
- beforeEach(function () {
- var requests = [
- {
- name: 'r1',
- context: 'c1',
- fields: ['f1']
- },
- {
- name: 'r2',
- context: 'c2',
- fields: ['f2']
- },
- {
- name: 'r2',
- context: 'c3',
- fields: ['f3', 'f4']
- }
- ];
- result = aggregator.groupRequests(requests);
- });
- it("result['r1'].subRequests.length", function () {
- expect(result.r1.subRequests.length).to.equal(1);
- });
- it("result['r1'].fields.length", function () {
- expect(result.r1.fields.length).to.equal(1);
- });
- it("result['r2'].subRequests.length", function () {
- expect(result.r2.subRequests.length).to.equal(2);
- });
- it("result['r2'].fields.length", function () {
- expect(result.r2.fields.length).to.equal(3);
- });
- });
- describe("#runRequests()", function () {
- beforeEach(function () {
- sinon.stub(aggregator, 'groupRequests', function (requests) {
- return requests;
- });
- sinon.stub(aggregator, 'formatRequestData', function(_request){
- return _request.fields;
- });
- sinon.stub(App.ajax, 'send', function(){
- return {
- done: Em.K,
- fail: Em.K
- }
- });
- });
- afterEach(function () {
- aggregator.groupRequests.restore();
- App.ajax.send.restore();
- aggregator.formatRequestData.restore();
- });
- it("valid request is sent", function () {
- var context = Em.Object.create({content: {hostName: 'host1'}});
- var requests = {
- 'r1': {
- name: 'r1',
- context: context,
- fields: ['f3', 'f4']
- }
- };
- aggregator.runRequests(requests);
- expect(App.ajax.send.getCall(0).args[0]).to.eql({
- name: 'r1',
- sender: context,
- data: {
- fields: ['f3', 'f4'],
- hostName: 'host1'
- }
- });
- });
- });
- describe("#formatRequestData()", function () {
- var cases = [
- {
- currentTimeIndex: 0,
- customStartTime: 100000,
- customEndTime: 200000,
- result: 'f3[400,4000,15],f4[400,4000,15]',
- title: 'preset time range'
- },
- {
- currentTimeIndex: 8,
- customStartTime: 100000,
- customEndTime: 200000,
- result: 'f3[100,200,15],f4[100,200,15]',
- title: 'custom time range'
- },
- {
- currentTimeIndex: 8,
- customStartTime: null,
- customEndTime: null,
- result: 'f3[400,4000,15],f4[400,4000,15]',
- title: 'custom time range, no boundaries set'
- }
- ];
- beforeEach(function () {
- sinon.stub(App, 'dateTime').returns(4000000);
- });
- afterEach(function () {
- App.dateTime.restore();
- });
- cases.forEach(function (item) {
- it(item.title, function () {
- var context = Em.Object.create({
- timeUnitSeconds: 3600,
- currentTimeIndex: item.currentTimeIndex,
- customStartTime: item.customStartTime,
- customEndTime: item.customEndTime
- });
- var request = {
- name: 'r1',
- context: context,
- fields: ['f3', 'f4']
- };
- expect(aggregator.formatRequestData(request)).to.equal(item.result);
- });
- });
- });
- });
|