From: Grégoire Aubert Date: Wed, 4 Oct 2017 15:42:36 +0000 (+0200) Subject: SONAR-9526 Fix cumulative facets calculation with NO_DATA X-Git-Tag: 6.6-RC1~27 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fdc4b4f13f2b5f650185a7c7f094d60341bd0a63;p=sonarqube.git SONAR-9526 Fix cumulative facets calculation with NO_DATA --- diff --git a/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts b/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts index d48601e76ba..e40971e373b 100644 --- a/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts +++ b/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts @@ -38,3 +38,57 @@ describe('parseSorting', () => { expect(utils.parseSorting('-size')).toEqual({ sortDesc: true, sortValue: 'size' }); }); }); + +describe('cumulativeMapFacetValues', () => { + it('should correctly cumulate facets', () => { + expect( + utils.cumulativeMapFacetValues([ + { val: '1', count: 50 }, + { val: '2', count: 1 }, + { val: '3', count: 6 }, + { val: '4', count: 2 }, + { val: '5', count: 0 } + ]) + ).toEqual({ '1': 50, '2': 9, '3': 8, '4': 2, '5': 0 }); + }); + it('should correctly cumulate facets with NO_DATA items', () => { + const expectedResult = { + '80.0-*': 59, + '70.0-80.0': 26, + '50.0-70.0': 15, + '30.0-50.0': 11, + '*-30.0': 7, + NO_DATA: 5 + }; + expect( + utils.cumulativeMapFacetValues([ + { val: '80.0-*', count: 59 }, + { val: '70.0-80.0', count: 11 }, + { val: '50.0-70.0', count: 4 }, + { val: '30.0-50.0', count: 4 }, + { val: '*-30.0', count: 7 }, + { val: 'NO_DATA', count: 5 } + ]) + ).toEqual(expectedResult); + expect( + utils.cumulativeMapFacetValues([ + { val: 'NO_DATA', count: 5 }, + { val: '80.0-*', count: 59 }, + { val: '70.0-80.0', count: 11 }, + { val: '50.0-70.0', count: 4 }, + { val: '30.0-50.0', count: 4 }, + { val: '*-30.0', count: 7 } + ]) + ).toEqual(expectedResult); + expect( + utils.cumulativeMapFacetValues([ + { val: '80.0-*', count: 59 }, + { val: '70.0-80.0', count: 11 }, + { val: '50.0-70.0', count: 4 }, + { val: 'NO_DATA', count: 5 }, + { val: '30.0-50.0', count: 4 }, + { val: '*-30.0', count: 7 } + ]) + ).toEqual(expectedResult); + }); +}); diff --git a/server/sonar-web/src/main/js/apps/projects/utils.ts b/server/sonar-web/src/main/js/apps/projects/utils.ts index ab50c3fded6..c8f99c27b56 100644 --- a/server/sonar-web/src/main/js/apps/projects/utils.ts +++ b/server/sonar-web/src/main/js/apps/projects/utils.ts @@ -281,13 +281,20 @@ function mapFacetValues(values: Array<{ val: string; count: number }>) { return map; } -function cumulativeMapFacetValues(values: Array<{ val: string; count: number }>) { +export function cumulativeMapFacetValues(values: Array<{ val: string; count: number }>) { + const noDataVal = values.find(value => value.val === 'NO_DATA'); + const filteredValues = noDataVal ? values.filter(value => value.val !== 'NO_DATA') : values; + + let sum = sumBy(filteredValues, value => value.count); const map: { [value: string]: number } = {}; - let sum = sumBy(values, value => value.count); - values.forEach((value, index) => { + filteredValues.forEach((value, index) => { map[value.val] = index > 0 && index < values.length - 1 ? sum : value.count; sum -= value.count; }); + + if (noDataVal) { + map[noDataVal.val] = noDataVal.count; + } return map; }