Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/semver-5.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenrikoverland authored Sep 11, 2023
2 parents 9617f70 + 6d1bcc9 commit c76ee53
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 29 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## [26.0.17](https://github.com/dhis2/analytics/compare/v26.0.16...v26.0.17) (2023-08-28)


### Bug Fixes

* use correct aggregation type if numberType undefined DHIS2-15698 ([#1564](https://github.com/dhis2/analytics/issues/1564)) ([c740e32](https://github.com/dhis2/analytics/commit/c740e320be568cce4430afaab688dd52ced83fa3))

## [26.0.16](https://github.com/dhis2/analytics/compare/v26.0.15...v26.0.16) (2023-08-18)


### Bug Fixes

* avoid undefined in totals DHIS2-14511 ([#1552](https://github.com/dhis2/analytics/issues/1552)) ([d93bc3e](https://github.com/dhis2/analytics/commit/d93bc3e35c560aac7e64b3db8196032201686d3a))

## [26.0.15](https://github.com/dhis2/analytics/compare/v26.0.14...v26.0.15) (2023-07-27)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dhis2/analytics",
"version": "26.0.15",
"version": "26.0.17",
"main": "./build/cjs/index.js",
"module": "./build/es/index.js",
"exports": {
Expand Down
61 changes: 33 additions & 28 deletions src/modules/pivotTable/PivotTableEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DIMENSION_ID_ORGUNIT } from '../predefinedDimensions.js'
import { renderValue } from '../renderValue.js'
import { VALUE_TYPE_NUMBER, VALUE_TYPE_TEXT } from '../valueTypes.js'
import { AdaptiveClippingController } from './AdaptiveClippingController.js'
import { addToTotalIfNumber } from './addToTotalIfNumber.js'
import { parseValue } from './parseValue.js'
import {
AGGREGATE_TYPE_NA,
Expand Down Expand Up @@ -705,9 +706,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
totalCell[field] = (totalCell[field] || 0) + value
}
totalCell[field] = addToTotalIfNumber(
value,
totalCell[field]
)
})
}
totalCell.count += 1
Expand All @@ -724,10 +726,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})

if (totals.columnSubtotal) {
Expand All @@ -742,10 +744,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}

Expand All @@ -761,10 +763,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}
}
Expand All @@ -780,10 +782,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})

if (totals.rowSubtotal) {
Expand All @@ -798,10 +800,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}

Expand All @@ -817,10 +819,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}
}
Expand All @@ -833,7 +835,10 @@ export class PivotTableEngine {
if (totalCell && totalCell.count) {
totalCell.value = applyTotalAggregationType(
totalCell,
this.visualization.numberType !== NUMBER_TYPE_VALUE &&
// DHIS2-15698: do not override total aggregation type when numberType option is not present
// (numberType option default is VALUE)
this.visualization.numberType &&
this.visualization.numberType !== NUMBER_TYPE_VALUE &&
AGGREGATE_TYPE_SUM
)
this.adaptiveClippingController.add(
Expand Down
84 changes: 84 additions & 0 deletions src/modules/pivotTable/__tests__/addToTotalIfNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { addToTotalIfNumber } from '../addToTotalIfNumber.js'

const tests = [
{
testName: 'negative value',
value: -1,
total: undefined,
expected: -1,
},
{
testName: 'zero value',
value: 0,
total: undefined,
expected: 0,
},
{
testName: 'positive value',
value: 1,
total: undefined,
expected: 1,
},
{
testName: 'null value',
value: null,
total: undefined,
expected: undefined,
},
{
testName: 'undefined value',
value: undefined,
total: undefined,
expected: undefined,
},
{
testName: 'string value',
value: 'string',
total: undefined,
expected: undefined,
},
{
testName: 'negative value with existing total',
value: -1,
total: 100,
expected: 99,
},
{
testName: 'zero value with existing total',
value: 0,
total: 100,
expected: 100,
},
{
testName: 'positive value with existing total',
value: 1,
total: 100,
expected: 101,
},
{
testName: 'null value with existing total',
value: null,
total: 100,
expected: 100,
},
{
testName: 'undefined value with existing total',
value: undefined,
total: 100,
expected: 100,
},
{
testName: 'string value with existing total',
value: 'string',
total: 100,
expected: 100,
},
]

describe('addToTotalIfNumber', () => {
tests.forEach((t) => {
it(t.testName, () => {
expect(addToTotalIfNumber(t.value, t.total)).toEqual(t.expected)
})
})
})
4 changes: 4 additions & 0 deletions src/modules/pivotTable/addToTotalIfNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const addToTotalIfNumber = (value, total) =>
typeof value === 'number' && Number.isFinite(value)
? (total ?? 0) + value
: total

0 comments on commit c76ee53

Please sign in to comment.