From 54e5d34f816386987a5ccd0634e311d692a64588 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Mon, 22 Aug 2016 10:00:05 +0200 Subject: [PATCH] SONAR-7996 Rounding of technical debt is not accurate (#1154) --- .../sonar-web/src/main/js/helpers/measures.js | 46 +++++++++---------- .../sonar-web/tests/helpers/measures-test.js | 11 ++++- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/server/sonar-web/src/main/js/helpers/measures.js b/server/sonar-web/src/main/js/helpers/measures.js index bf6df21284a..430f8fb31c7 100644 --- a/server/sonar-web/src/main/js/helpers/measures.js +++ b/server/sonar-web/src/main/js/helpers/measures.js @@ -247,22 +247,22 @@ function shouldDisplayDays (days) { return days > 0; } +function shouldDisplayDaysInShortFormat(days) { + return days > 0.9; +} + function shouldDisplayHours (days, hours) { return hours > 0 && days < 10; } -function shouldDisplayHoursInShortFormat (days, hours) { - return hours > 0 && days === 0; +function shouldDisplayHoursInShortFormat (hours) { + return hours > 0.9; } function shouldDisplayMinutes (days, hours, minutes) { return minutes > 0 && hours < 10 && days === 0; } -function shouldDisplayMinutesInShortFormat (days, hours, minutes) { - return minutes > 0 && hours === 0 && days === 0; -} - function addSpaceIfNeeded (value) { return value.length > 0 ? value + ' ' : value; } @@ -286,22 +286,20 @@ function formatDuration (isNegative, days, hours, minutes) { } function formatDurationShort (isNegative, days, hours, minutes) { - let formatted = ''; - if (shouldDisplayDays(days)) { - const formattedDays = formatMeasure(isNegative ? -1 * days : days, 'SHORT_INT'); - formatted += translateWithParameters('work_duration.x_days', formattedDays); - } - if (shouldDisplayHoursInShortFormat(days, hours)) { - formatted = addSpaceIfNeeded(formatted); - formatted += translateWithParameters('work_duration.x_hours', - isNegative && formatted.length === 0 ? -1 * hours : hours); + if (shouldDisplayDaysInShortFormat(days)) { + const roundedDays = Math.round(days); + const formattedDays = formatMeasure(isNegative ? -1 * roundedDays : roundedDays, 'SHORT_INT'); + return translateWithParameters('work_duration.x_days', formattedDays); } - if (shouldDisplayMinutesInShortFormat(days, hours, minutes)) { - formatted = addSpaceIfNeeded(formatted); - formatted += translateWithParameters('work_duration.x_minutes', - isNegative && formatted.length === 0 ? -1 * minutes : minutes); + + if (shouldDisplayHoursInShortFormat(hours)) { + const roundedHours = Math.round(hours); + const formattedHours = formatMeasure(isNegative ? -1 * roundedHours : roundedHours, 'SHORT_INT'); + return translateWithParameters('work_duration.x_hours', formattedHours); } - return formatted; + + const formattedMinutes = formatMeasure(isNegative ? -1 * minutes : minutes, 'SHORT_INT'); + return translateWithParameters('work_duration.x_minutes', formattedMinutes); } function durationFormatter (value) { @@ -326,10 +324,10 @@ function shortDurationFormatter (value) { const hoursInDay = window.SS.hoursInDay; const isNegative = value < 0; const absValue = Math.abs(value); - const days = Math.floor(absValue / hoursInDay / 60); - let remainingValue = absValue - days * hoursInDay * 60; - const hours = Math.floor(remainingValue / 60); - remainingValue -= hours * 60; + const days = absValue / hoursInDay / 60; + let remainingValue = absValue - Math.floor(days) * hoursInDay * 60; + const hours = remainingValue / 60; + remainingValue -= Math.floor(hours) * 60; return formatDurationShort(isNegative, days, hours, remainingValue); } diff --git a/server/sonar-web/tests/helpers/measures-test.js b/server/sonar-web/tests/helpers/measures-test.js index 0b27578c852..d943bef3fb6 100644 --- a/server/sonar-web/tests/helpers/measures-test.js +++ b/server/sonar-web/tests/helpers/measures-test.js @@ -93,10 +93,14 @@ describe('Measures', function () { expect(formatMeasure(0, 'SHORT_WORK_DUR')).to.equal('0'); expect(formatMeasure(5 * ONE_DAY, 'SHORT_WORK_DUR')).to.equal('5d'); expect(formatMeasure(2 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('2h'); - expect(formatMeasure(40 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('40min'); expect(formatMeasure(ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('1min'); + expect(formatMeasure(40 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('40min'); + expect(formatMeasure(58 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('1h'); expect(formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('5d'); expect(formatMeasure(2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('2h'); + expect(formatMeasure(ONE_HOUR + 55 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('2h'); + expect(formatMeasure(3 * ONE_DAY + 6 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('4d'); + expect(formatMeasure(7 * ONE_HOUR + 59 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('1d'); expect(formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('5d'); expect(formatMeasure(15 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('15d'); expect(formatMeasure(7 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('7min'); @@ -203,8 +207,13 @@ describe('Measures', function () { expect(formatMeasureVariation(5 * ONE_DAY, 'SHORT_WORK_DUR')).to.equal('+5d'); expect(formatMeasureVariation(2 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('+2h'); expect(formatMeasureVariation(ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+1min'); + expect(formatMeasureVariation(30 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+30min'); + expect(formatMeasureVariation(58 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+1h'); expect(formatMeasureVariation(5 * ONE_DAY + 2 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('+5d'); expect(formatMeasureVariation(2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+2h'); + expect(formatMeasureVariation(ONE_HOUR + 55 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+2h'); + expect(formatMeasureVariation(3 * ONE_DAY + 6 * ONE_HOUR, 'SHORT_WORK_DUR')).to.equal('+4d'); + expect(formatMeasureVariation(7 * ONE_HOUR + 59 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+1d'); expect(formatMeasureVariation(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+5d'); expect(formatMeasureVariation(15 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+15d'); expect(formatMeasureVariation(7 * ONE_MINUTE, 'SHORT_WORK_DUR')).to.equal('+7min'); -- 2.39.5