AMBARI-5421. Some unit test fail depending on timezone. (onechiporenko)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f71d6b92 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f71d6b92 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f71d6b92 Branch: refs/heads/trunk Commit: f71d6b92366854fca0b90d6fdbd2c6bbfeeb4092 Parents: 526a16b Author: Oleg Nechiporenko <[email protected]> Authored: Thu Apr 10 16:51:07 2014 +0300 Committer: Oleg Nechiporenko <[email protected]> Committed: Thu Apr 10 16:57:11 2014 +0300 ---------------------------------------------------------------------- ambari-web/app/assets/licenses/NOTICE.txt | 4 +- ambari-web/app/utils/date.js | 132 +- .../dashboard/widgets/uptime_text_widget.js | 31 +- ambari-web/config.coffee | 1 + ambari-web/test/utils/date_test.js | 42 +- ambari-web/vendor/scripts/moment.js | 7768 ++++++++++++++++++ 6 files changed, 7894 insertions(+), 84 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/f71d6b92/ambari-web/app/assets/licenses/NOTICE.txt ---------------------------------------------------------------------- diff --git a/ambari-web/app/assets/licenses/NOTICE.txt b/ambari-web/app/assets/licenses/NOTICE.txt index d2aa8f8..9be0e09 100644 --- a/ambari-web/app/assets/licenses/NOTICE.txt +++ b/ambari-web/app/assets/licenses/NOTICE.txt @@ -40,4 +40,6 @@ This product includes Timeago (http://timeago.yarp.com/ - MIT License) Copyright (c) 2008-2012, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org) This product includes Spin.js (http://fgnass.github.com/spin.js/ - MIT license) -Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de] \ No newline at end of file +Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de] + +This product includes Moment.js (https://github.com/moment/moment/ - MIT license) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/f71d6b92/ambari-web/app/utils/date.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/date.js b/ambari-web/app/utils/date.js index 5434588..37d8959 100644 --- a/ambari-web/app/utils/date.js +++ b/ambari-web/app/utils/date.js @@ -20,53 +20,85 @@ var validator = require('utils/validator'); var App = require('app'); module.exports = { - dateMonths:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - dateDays:['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dateFormatZeroFirst:function (time) { + + /** + * List of monthes short names + * @type {string[]} + */ + dateMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + + /** + * List of days short names + * @type {string[]} + */ + dateDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + + /** + * Add leading zero + * + * @param {string} time + * @returns {string} + * @method dateFormatZeroFirst + */ + dateFormatZeroFirst: function (time) { if (time < 10) return '0' + time; return time; }, + /** * Convert timestamp to date-string 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES' - * @param timestamp - * @return string date + * + * @param {number} timestamp + * @param {bool} showSeconds should seconds be added to result string + * @param {bool} showMilliseconds should miliseconds be added to result string (if <code>showSeconds</code> is false, milliseconds wouldn't be added) + * @return {*} date + * @method dateFormat */ - dateFormat:function (timestamp, showSeconds, showMilliseconds) { - if (!validator.isValidInt(timestamp)) return timestamp; - var date = new Date(timestamp); - var months = this.dateMonths; - var days = this.dateDays; - var formattedDate = days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + this.dateFormatZeroFirst(date.getDate()) + ', ' + date.getFullYear() + ' ' + this.dateFormatZeroFirst(date.getHours()) + ':' + this.dateFormatZeroFirst(date.getMinutes()); + dateFormat: function (timestamp, showSeconds, showMilliseconds) { + if (!validator.isValidInt(timestamp)) { + return timestamp; + } + var format = 'ddd, MMM DD, YYYY HH:mm'; if (showSeconds) { - formattedDate += ':' + this.dateFormatZeroFirst(date.getSeconds()); + format += ':ss'; if (showMilliseconds) { - formattedDate += '.' + this.dateFormatZeroFirst(date.getMilliseconds()); - }; - }; - return formattedDate; + format += ':SSS'; + } + } + return moment((new Date(timestamp)).toISOString().replace('Z', '')).format(format); }, + /** * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR' - * @param timestamp - * @return {*} + * + * @param {string} timestamp + * @return {string} + * @method dateFormatShort */ - dateFormatShort: function(timestamp) { - if (!validator.isValidInt(timestamp)) return timestamp; - - var date = new Date(timestamp); - var today = new Date(); - if (date.toDateString() === today.toDateString()) { - return 'Today ' + date.toLocaleTimeString(); + dateFormatShort: function (timestamp) { + if (!validator.isValidInt(timestamp)) { + return timestamp; + } + var format = 'ddd MMM DD YYYY'; + var date = moment((new Date(timestamp)).toISOString().replace('Z', '')).format(format); + var today = moment((new Date()).toISOString().replace('Z', '')).format(format); + if (date === today) { + return 'Today ' + (new Date(timestamp)).toLocaleTimeString(); } - return date.toDateString(); + return date; }, + /** * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES', except for the case: year equals 1969 - * @param startTimestamp - * @return string startTimeSummary + * + * @param {string} startTimestamp + * @return {string} startTimeSummary + * @method startTime */ startTime: function (startTimestamp) { - if (!validator.isValidInt(startTimestamp)) return ''; + if (!validator.isValidInt(startTimestamp)) { + return ''; + } var startDate = new Date(startTimestamp); var months = this.dateMonths; var days = this.dateDays; @@ -75,21 +107,25 @@ module.exports = { return 'Not started'; } var startTimeSummary = ''; - if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0) ) { //today + if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0)) { //today startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes()); } else { - startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' + this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' ' + startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' + + this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes()); } return startTimeSummary; }, + /** * Provides the duration between the given start and end timestamp. If start time * not valid, duration will be ''. If end time is not valid, duration will * be till now, showing 'Lasted for xxx secs'. - * @param startTimestamp - * @param endTimestamp - * @return string durationSummary + * + * @param {string} startTimestamp + * @param {string} endTimestamp + * @return {string} durationSummary + * @method durationSummary */ durationSummary: function (startTimestamp, endTimestamp) { // generate duration @@ -105,8 +141,8 @@ module.exports = { return '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs } else { // still running, duration till now - var time = (App.dateTime() - startTimestamp) < 0? 0 : (App.dateTime() - startTimestamp) ; - durationSummary = '' + this.timingFormat( time , 1); + var time = (App.dateTime() - startTimestamp) < 0 ? 0 : (App.dateTime() - startTimestamp); + durationSummary = '' + this.timingFormat(time, 1); } return durationSummary; }, @@ -123,14 +159,20 @@ module.exports = { * 999999 ms = 999.99 secs * 1000000 ms = 16.66 mins * 3500000 secs = 58.33 mins - * @param time - * @param zeroValid for the case to show 0 when time is 0, not null - * @return string formatted date + * + * @param {number} time + * @param {bool} zeroValid for the case to show 0 when time is 0, not null + * @return {string|null} formatted date + * @method timingFormat */ - timingFormat:function (time, /* optional */ zeroValid) { - var intTime = parseInt(time); - if (zeroValid && intTime == 0) return 0 + ' secs'; - if (!intTime) return null; + timingFormat: function (time, /* optional */ zeroValid) { + var intTime = parseInt(time); + if (zeroValid && intTime == 0) { + return 0 + ' secs'; + } + if (!intTime) { + return null; + } var timeStr = intTime.toString(); var lengthOfNumber = timeStr.length; var oneMinMs = 60000; @@ -153,6 +195,7 @@ module.exports = { return time + ' days'; } }, + /** * Provides the duration between the given start and end time. If start time * is not given, duration will be 0. If end time is not given, duration will @@ -161,8 +204,9 @@ module.exports = { * @param {Number} startTime Start time from epoch * @param {Number} endTime End time from epoch * @return {Number} duration + * @method duration */ - duration : function(startTime, endTime) { + duration: function (startTime, endTime) { var duration = 0; if (startTime && startTime > 0) { if (!endTime || endTime < 1) { http://git-wip-us.apache.org/repos/asf/ambari/blob/f71d6b92/ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js b/ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js index e583522..3d9ed34 100644 --- a/ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js +++ b/ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js @@ -54,25 +54,8 @@ App.UptimeTextDashboardWidgetView = App.TextDashboardWidgetView.extend({ }.property('data'), timeConverter: function (timestamp) { - var origin = new Date(timestamp); - origin = origin.toString(); - var result = []; - var start = origin.indexOf('GMT'); - if (start == -1) { // ie - var arr = origin.split(" "); - result.pushObject(arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3]); - var second = ''; - for (var i = 4; i < arr.length; i++) { - second = second + " " + arr[i]; - } - result.pushObject(second); - } - else { // other browsers - var end = origin.indexOf(" ", start); - result.pushObject(origin.slice(0, start-10)); - result.pushObject(origin.slice(start-9)); - } - return result; + var m = moment((new Date(timestamp)).toISOString().replace('Z', '')); + return [m.format('ddd MMM DD YYYY'), m.format('HH:mm:ss')]; }, /** @@ -84,17 +67,17 @@ App.UptimeTextDashboardWidgetView = App.TextDashboardWidgetView.extend({ * } * </code> */ - didInsertElement: function() { + didInsertElement: function () { this._super(); this.addObserver('model.' + this.get('modelField'), this, this.calc); }, - calc: function() { + calc: function () { this.set('data', this.calcData()); this.set('content', this.calcContent()); }, - uptimeProcessing: function(uptime) { + uptimeProcessing: function (uptime) { var uptimeString = this.timeConverter(uptime); var diff = App.dateTime() - uptime; if (diff < 0) { @@ -137,14 +120,14 @@ App.UptimeTextDashboardWidgetView = App.TextDashboardWidgetView.extend({ return parseFloat(formatted.split(" ")[0]); } } - this.set('hiddenInfo', [this.get('component'),'Not Running']); + this.set('hiddenInfo', [this.get('component'), 'Not Running']); return null; }, calcContent: function () { var data = this.get('data'); if (data) { - return data.toFixed(1) + ' '+ this.get('timeUnit'); + return data.toFixed(1) + ' ' + this.get('timeUnit'); } else { return Em.I18n.t('services.service.summary.notAvailable'); http://git-wip-us.apache.org/repos/asf/ambari/blob/f71d6b92/ambari-web/config.coffee ---------------------------------------------------------------------- diff --git a/ambari-web/config.coffee b/ambari-web/config.coffee index f03cc33..5ba91d3 100644 --- a/ambari-web/config.coffee +++ b/ambari-web/config.coffee @@ -52,6 +52,7 @@ exports.config = 'vendor/scripts/jquery.ui.custom-effects.js', 'vendor/scripts/jquery.timeago.js', 'vendor/scripts/jquery.ajax-retry.js', + 'vendor/scripts/moment.js', 'vendor/scripts/workflow_visualization.js', 'vendor/scripts/rickshaw.js', 'vendor/scripts/spin.js', http://git-wip-us.apache.org/repos/asf/ambari/blob/f71d6b92/ambari-web/test/utils/date_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/date_test.js b/ambari-web/test/utils/date_test.js index d9b7d61..fad8ccf 100644 --- a/ambari-web/test/utils/date_test.js +++ b/ambari-web/test/utils/date_test.js @@ -25,9 +25,9 @@ var date = require('utils/date'); describe('date', function () { var correct_tests = Em.A([ - {t: 1349752195000, e: 'Tue, Oct 09, 2012 06:09', e2: 'Tue Oct 09 2012'}, - {t: 1367752195000, e: 'Sun, May 05, 2013 14:09', e2: 'Sun May 05 2013'}, - {t: 1369952195000, e: 'Fri, May 31, 2013 01:16', e2: 'Fri May 31 2013'} + {t: 1349752195000, e: 'Tue, Oct 09, 2012 03:09', e2: 'Tue Oct 09 2012'}, + {t: 1367752195000, e: 'Sun, May 05, 2013 11:09', e2: 'Sun May 05 2013'}, + {t: 1369952195000, e: 'Thu, May 30, 2013 22:16', e2: 'Thu May 30 2013'} ]); var incorrect_tests = Em.A([ @@ -41,22 +41,28 @@ describe('date', function () { ]); describe('#dateFormat', function() { - it('Correct timestamps', function(){ + describe('Correct timestamps', function(){ correct_tests.forEach(function(test) { - expect(date.dateFormat(test.t)).to.equal(test.e); + it(test.t, function() { + expect(date.dateFormat(test.t)).to.equal(test.e); + }); }); }); - it('Incorrect timestamps', function() { + describe('Incorrect timestamps', function() { incorrect_tests.forEach(function(test) { - expect(date.dateFormat(test.t)).to.equal(test.t); + it(test.t, function() { + expect(date.dateFormat(test.t)).to.equal(test.t); + }); }); }); }); describe('#dateFormatShort', function() { - it('Correct timestamps', function(){ + describe('Correct timestamps', function() { correct_tests.forEach(function(test) { - expect(date.dateFormatShort(test.t)).to.equal(test.e2); + it(test.t, function() { + expect(date.dateFormatShort(test.t)).to.equal(test.e2); + }); }); }); it('Today timestamp', function() { @@ -64,9 +70,11 @@ describe('date', function () { var then = new Date(now.getFullYear(),now.getMonth(),now.getDate(),0,0,0); expect(date.dateFormatShort(then.getTime() + 10*3600*1000)).to.equal('Today 10:00:00'); }); - it('Incorrect timestamps', function() { + describe('Incorrect timestamps', function() { incorrect_tests.forEach(function(test) { - expect(date.dateFormatShort(test.t)).to.equal(test.t); + it(test.t, function() { + expect(date.dateFormatShort(test.t)).to.equal(test.t); + }); }); }); }); @@ -89,15 +97,19 @@ describe('date', function () { {i: '35000000000', e:'405.09 days'} ]); - it('Correct data', function(){ + describe('Correct data', function(){ tests.forEach(function(test) { - expect(date.timingFormat(test.i)).to.equal(test.e); + it(test.t, function() { + expect(date.timingFormat(test.i)).to.equal(test.e); + }); }); }); - it('Incorrect data', function(){ + describe('Incorrect data', function(){ incorrect_tests.forEach(function(test) { - expect(date.timingFormat(test.t)).to.equal(null); + it(test.t, function() { + expect(date.timingFormat(test.t)).to.equal(null); + }); }); });
