Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: min/max aggregated functions #76

Merged
merged 5 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# CHANGELOG

## Not released

- Remove getUserDatasets method from api [#68](https://github.com/CartoDB/carto-react-lib/pull/68)
- Fix hover color in secondary buttons [#65](https://github.com/CartoDB/carto-react-lib/pull/65)
- Fix min/max aggregated functions [#76](https://github.com/CartoDB/carto-react-lib/pull/76)

## 1.0.0-beta12 (2021-01-22)

Expand Down
31 changes: 31 additions & 0 deletions src/tests/widgets/operations/aggregation/values.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { aggregationFunctions } from 'src/widgets/operations/aggregation/values';
import { AggregationTypes } from 'src/widgets/AggregationTypes';

const VALUES = [1, 2, 3, 4, 5];

describe('Aggregation functions', () => {
test(AggregationTypes.COUNT, () => {
const func = aggregationFunctions[AggregationTypes.COUNT];
expect(func(VALUES)).toEqual(5);
});

test(AggregationTypes.AVG, () => {
const func = aggregationFunctions[AggregationTypes.AVG];
expect(func(VALUES)).toEqual(3);
});

test(AggregationTypes.MIN, () => {
const func = aggregationFunctions[AggregationTypes.MIN];
expect(func(VALUES)).toEqual(1);
});

test(AggregationTypes.MAX, () => {
const func = aggregationFunctions[AggregationTypes.MAX];
expect(func(VALUES)).toEqual(5);
});

test(AggregationTypes.SUM, () => {
const func = aggregationFunctions[AggregationTypes.SUM];
expect(func(VALUES)).toEqual(15);
});
});
17 changes: 17 additions & 0 deletions src/utils/pickValuesFromFeatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function isFeature(value) {
return typeof value === 'object' && 'properties' in value;
}

function getValueFromFeature(feature, column) {
if (!isFeature(feature)) {
return 0;
}

const value = feature.properties[column];
const isValidValue = Number.isFinite(value);
return isValidValue ? value : 0;
}

export function pickValuesFromFeatures(features, column) {
return features.map((feat) => getValueFromFeature(feat, column));
}
5 changes: 3 additions & 2 deletions src/widgets/models/FormulaModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { executeSQL } from '../../api';
import { filtersToSQL } from '../../api/FilterQueryBuilder';
import { applyFilter } from '../../api/Filter';
import { aggregationFunctions } from '../operations/aggregation/values';
import { pickValuesFromFeatures } from '../../utils/pickValuesFromFeatures';
import { LayerTypes } from '../LayerTypes';

export const getFormula = async (props) => {
Expand Down Expand Up @@ -68,10 +69,10 @@ export const filterViewportFeaturesToGetFormula = ({
}) => {
if (viewportFeatures) {
const targetOperation = aggregationFunctions[operation];

const filteredFeatures = viewportFeatures.filter(applyFilter({ filters }));
const featureValues = pickValuesFromFeatures(filteredFeatures, column);

return [{ value: targetOperation(filteredFeatures, column) }];
return [{ value: targetOperation(featureValues) }];
}

return [{ value: null }];
Expand Down
16 changes: 6 additions & 10 deletions src/widgets/operations/aggregation/values.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { AggregationTypes } from '../../AggregationTypes';

const sum = (v, column) =>
v.reduce(
(a, b) => (typeof b === 'object' ? a + (b.properties[column] || 0) : a + b),
0
);
const sum = (values) => values.reduce((a, b) => a + b, 0);

export const aggregationFunctions = {
[AggregationTypes.COUNT]: (val) => val.length,
[AggregationTypes.MIN]: (val) => val.reduce((a, b) => Math.min(a, b), Infinity),
[AggregationTypes.MAX]: (val) => val.reduce((a, b) => Math.max(a, b), -Infinity),
[AggregationTypes.SUM]: (val, column) => sum(val, column),
[AggregationTypes.AVG]: (val, column) => sum(val, column) / val.length
[AggregationTypes.COUNT]: (values) => values.length,
[AggregationTypes.MIN]: (values) => values.reduce((a, b) => Math.min(a, b), Infinity),
[AggregationTypes.MAX]: (values) => values.reduce((a, b) => Math.max(a, b), -Infinity),
[AggregationTypes.SUM]: (values) => sum(values),
[AggregationTypes.AVG]: (values) => sum(values) / values.length
};