Skip to content

Commit

Permalink
[Bug] Added better check for bins in bottom widget (#1361)
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Macri <macri.giuseppe@gmail.com>
Signed-off-by: Shan He <heshan0131@gmail.com>
  • Loading branch information
heshan0131 committed Dec 8, 2020
1 parent ef8bdba commit 2aad97f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/components/bottom-widget.js
Expand Up @@ -25,6 +25,7 @@ import TimeWidgetFactory from './filters/time-widget';
import AnimationControlFactory from './common/animation-control/animation-control';
import AnimationControllerFactory from './common/animation-control/animation-controller';
import {ANIMATION_WINDOW, FILTER_TYPES} from 'constants';
import {getIntervalBins} from 'utils/filter-utils';

const propTypes = {
filters: PropTypes.arrayOf(PropTypes.object),
Expand Down Expand Up @@ -61,11 +62,8 @@ export function FilterAnimationControllerFactory(AnimationController) {
setFilterAnimationTime,
children
}) => {
const intervalBins = useMemo(() => {
const bins = filter && filter.bins;
const interval = filter.plotType && filter.plotType.interval;
return bins && Object.keys(bins).length && Object.values(bins)[0][interval];
}, [filter]);
const intervalBins = useMemo(() => getIntervalBins(filter), [filter]);

const steps = useMemo(() => (intervalBins ? intervalBins.map(x => x.x0) : null), [
intervalBins
]);
Expand Down Expand Up @@ -197,7 +195,7 @@ export default function BottomWidgetFactory(
}
</LayerAnimationController>
<FilterAnimationController
/* pass if filter is not animating, pass in
/* pass if filter is not animating, pass in
enlarged filter here because animation controller needs to call reset on it
we can */
filter={animatedFilter || filters[enlargedFilterIdx]}
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/vis-state-updaters.d.ts
Expand Up @@ -108,6 +108,10 @@ export type TimeRangeFilter = FilterBase &
fieldType: 'timestamp';
fixedDomain: true;
value: [number, number];
bins?: Object,
plotType: {
[key: string]: any
}
};

export type PolygonFilter = FilterBase & {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/filter-utils.d.ts
Expand Up @@ -11,8 +11,8 @@ import {
Feature,
FeatureValue,
VisState,
Field,
LineChart
LineChart,
TimeRangeFilter
} from '../reducers/vis-state-updaters';
import {Layer} from 'layers';
import {Field} from 'reducers/types';
Expand Down Expand Up @@ -166,6 +166,8 @@ export function validateFiltersUpdateDatasets(state: VisState, filtersToValidate
failed: Filter[],
updatedDatasets: Datasets
}
export function isInPolygon(point: number[], polygon: object): boolean;
export function getIntervalBins(filter: TimeRangeFilter)

export const FILTER_UPDATER_PROPS: {
dataId: string;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/filter-utils.js
Expand Up @@ -1221,3 +1221,17 @@ export function validateFiltersUpdateDatasets(state, filtersToValidate = []) {

return {validated, failed, updatedDatasets};
}

/**
* Retrieve interval bins for time filter
* @type {typeof import('./filter-utils').getIntervalBins}
*/
export function getIntervalBins(filter) {
const {bins} = filter;
const interval = filter.plotType && filter.plotType.interval;
if (!interval || !bins || Object.keys(bins).length === 0) {
return null;
}
const values = Object.values(bins);
return values[0] ? values[0][interval] : null;
}
4 changes: 2 additions & 2 deletions src/utils/index.js
Expand Up @@ -35,9 +35,9 @@ export {
applyFilterFieldName,
applyFiltersToDatasets,
validateFilterWithData,
validateFiltersUpdateDatasets
validateFiltersUpdateDatasets,
getIntervalBins
} from 'utils/filter-utils';

export {resetFilterGpuMode, assignGpuChannels} from 'utils/gpu-filter-utils';

// REDUCER UTILS
Expand Down
41 changes: 40 additions & 1 deletion test/node/utils/filter-utils-test.js
Expand Up @@ -33,7 +33,8 @@ import {
generatePolygonFilter,
isValidFilterValue,
isInPolygon,
diffFilters
diffFilters,
getIntervalBins
} from 'utils/filter-utils';

import {preciseRound} from 'utils/data-utils';
Expand Down Expand Up @@ -758,3 +759,41 @@ test('filterUtils -> diffFilters', t => {

t.end();
});

test('filterUtils => getIntervalBins', t => {
const interval = '15 minutes';
const dataId = '123test';
const filter = {
plotType: {
interval
},
bins: {
[dataId]: {
[interval]: [0, 1, 2]
}
}
};

t.deepEqual(getIntervalBins(filter), [0, 1, 2]);

const notValidPlotFilter = {
plotType: {},
bins: {
[dataId]: {
[interval]: [0, 1, 2]
}
}
};

t.equal(getIntervalBins(notValidPlotFilter), null);

const notValidBinFilter = {
plotType: {
interval
},
bins: {}
};

t.deepEqual(getIntervalBins(notValidBinFilter), null);
t.end();
});

0 comments on commit 2aad97f

Please sign in to comment.