diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-10-04 17:42:36 +0200 |
---|---|---|
committer | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-10-05 10:40:08 +0200 |
commit | fdc4b4f13f2b5f650185a7c7f094d60341bd0a63 (patch) | |
tree | a83c832cb956231f1186e14969dcb4b98d0b776e /server/sonar-web/src | |
parent | 2c05aa5ac2f73714c6fd6d11d532c980ae4da26f (diff) | |
download | sonarqube-fdc4b4f13f2b5f650185a7c7f094d60341bd0a63.tar.gz sonarqube-fdc4b4f13f2b5f650185a7c7f094d60341bd0a63.zip |
SONAR-9526 Fix cumulative facets calculation with NO_DATA
Diffstat (limited to 'server/sonar-web/src')
-rw-r--r-- | server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts | 54 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/apps/projects/utils.ts | 13 |
2 files changed, 64 insertions, 3 deletions
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; } |