From 729d76da74339dd267cfad768e7b72ec805e6c51 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) --- .../js/helpers/__tests__/measures-test.js | 39 ++++++---------- .../sonar-web/src/main/js/helpers/measures.js | 46 +++++++++---------- 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/server/sonar-web/src/main/js/helpers/__tests__/measures-test.js b/server/sonar-web/src/main/js/helpers/__tests__/measures-test.js index e6c377bce01..596ae960bb3 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/measures-test.js +++ b/server/sonar-web/src/main/js/helpers/__tests__/measures-test.js @@ -1,32 +1,14 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ import { expect } from 'chai'; import { resetBundle } from '../l10n'; import { formatMeasure, formatMeasureVariation } from '../measures'; + describe('Measures', function () { - const HOURS_IN_DAY = 8; - const ONE_MINUTE = 1; - const ONE_HOUR = ONE_MINUTE * 60; - const ONE_DAY = HOURS_IN_DAY * ONE_HOUR; + var HOURS_IN_DAY = 8, + ONE_MINUTE = 1, + ONE_HOUR = ONE_MINUTE * 60, + ONE_DAY = HOURS_IN_DAY * ONE_HOUR; before(function () { resetBundle({ @@ -111,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'); @@ -221,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'); diff --git a/server/sonar-web/src/main/js/helpers/measures.js b/server/sonar-web/src/main/js/helpers/measures.js index 064c7733cf8..9771331973b 100644 --- a/server/sonar-web/src/main/js/helpers/measures.js +++ b/server/sonar-web/src/main/js/helpers/measures.js @@ -266,22 +266,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; } @@ -305,22 +305,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) { @@ -345,10 +343,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); } -- 2.39.5