Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import test from 'tape';
import moment from 'moment';
import {getDatasetFieldIndexForFilter, KeplerTable} from '@kepler.gl/table';
import {
isValidFilterValue,
adjustValueToFilterDomain,
getFilterFunction,
getDefaultFilter,
validatePolygonFilter,
generatePolygonFilter,
isInPolygon,
getPolygonFilterFunctor,
diffFilters,
getTimestampFieldDomain,
scaleSourceDomainToDestination,
mergeFilterWithTimeline,
createDataContainer,
shouldApplyFilter,
isTimeIntervalFilter,
timeWindowOverlapsInterval,
applyTimeFilterEndFieldName,
validateFilterWithData,
disableGpuIfInverted,
getPlotBaseIndexByDataId,
areFilterFieldTypesCompatible,
mergeFilterDomainStep
} from '@kepler.gl/utils';
import {ALL_FIELD_TYPES, FILTER_TYPES} from '@kepler.gl/constants';
import {mockPolygonFeature, mockPolygonData} from '../../fixtures/polygon';
/* eslint-disable max-statements */
test('filterUtils -> shouldApplyFilter skips an unselected synced field', t => {
const filter = {
...getDefaultFilter({dataId: ['first', 'second']}),
type: FILTER_TYPES.range,
name: ['value', ''],
value: [0, 1]
};
t.equal(shouldApplyFilter(filter, 'first'), true, 'applies the selected dataset field');
t.equal(shouldApplyFilter(filter, 'second'), false, 'skips the empty dataset field');
t.equal(shouldApplyFilter(filter, 'missing'), false, 'skips an unrelated dataset');
t.end();
});
test('filterUtils -> adjustValueToFilterDomain', t => {
// TODO: needs id
const rangeFilter = getDefaultFilter();
rangeFilter.type = FILTER_TYPES.range;
rangeFilter.domain = [0, 1];
t.deepEqual(
adjustValueToFilterDomain([0, 0.5], rangeFilter),
[0, 0.5],
'should return value matched to range filter'
);
t.deepEqual(
adjustValueToFilterDomain([-1, 0.5], rangeFilter),
[0, 0.5],
'should return value adjust to range filter'
);
t.deepEqual(
adjustValueToFilterDomain([0.1, 1.5], rangeFilter),
[0.1, 1],
'should return value adjust to range filter'
);
t.deepEqual(
adjustValueToFilterDomain([1.1, 2], rangeFilter),
[0, 1],
'should return value adjust to range filter'
);
t.deepEqual(
adjustValueToFilterDomain(null, rangeFilter),
[0, 1],
'should return value adjust to range filter'
);
t.deepEqual(
adjustValueToFilterDomain([undefined, 0.5], rangeFilter),
[0, 0.5],
'should return value adjust to range filter'
);
// TODO needs id
const multiSelectFilter = getDefaultFilter();
multiSelectFilter.type = FILTER_TYPES.multiSelect;
multiSelectFilter.domain = ['a', 'b', 'c'];
t.deepEqual(
adjustValueToFilterDomain(['a', 'b'], multiSelectFilter),
['a', 'b'],
'should return value matched to multiSelect filter'
);
t.deepEqual(
adjustValueToFilterDomain(['a', 'b', 'd'], multiSelectFilter),
['a', 'b'],
'should return value matched to multiSelect filter'
);
t.deepEqual(
adjustValueToFilterDomain(['a', 'b', null], multiSelectFilter),
['a', 'b'],
'should return value matched to multiSelect filter'
);
t.deepEqual(
adjustValueToFilterDomain(null, multiSelectFilter),
[],
'should return [] if nothing matched to multiSelect filter'
);
t.deepEqual(
adjustValueToFilterDomain([1, 2], multiSelectFilter),
[],
'should return [] if nothing matched to multiSelect filter'
);
// TODO needs id
const selectFilter = getDefaultFilter();
selectFilter.type = FILTER_TYPES.select;
selectFilter.domain = ['a', 'b', 'c'];
t.equal(
adjustValueToFilterDomain('a', selectFilter),
'a',
'should return value matched to select filter'
);
t.equal(
adjustValueToFilterDomain(['a', 'b'], selectFilter),
true,
'should return true if nothing matched to select filter'
);
t.equal(
adjustValueToFilterDomain(null, selectFilter),
true,
'should return true if nothing matched to select filter'
);
t.end();
});
test('filterUtils -> compatible numeric filter field types', t => {
t.ok(
areFilterFieldTypesCompatible(ALL_FIELD_TYPES.integer, ALL_FIELD_TYPES.real),
'integer filters accept real fields'
);
t.ok(
areFilterFieldTypesCompatible(ALL_FIELD_TYPES.real, ALL_FIELD_TYPES.integer),
'real filters accept integer fields'
);
t.notOk(
areFilterFieldTypesCompatible(ALL_FIELD_TYPES.integer, ALL_FIELD_TYPES.string),
'numeric filters reject string fields'
);
const merged = mergeFilterDomainStep(
{
fieldType: ALL_FIELD_TYPES.real,
domain: [0.25, 1.5],
step: 0.25
},
{
fieldType: ALL_FIELD_TYPES.integer,
domain: [-1, 3],
step: 1
}
);
t.deepEqual(merged.domain, [-1, 3], 'merges integer and real domains');
t.equal(merged.fieldType, ALL_FIELD_TYPES.real, 'keeps the first field type');
t.equal(merged.step, 0.25, 'keeps the finest numeric step');
t.end();
});
test('filterUtils -> getDatasetFieldIndexForFilter', t => {
const dataId = 'test-this-id';
let fieldIndex = getDatasetFieldIndexForFilter(dataId, {
dataId: [dataId],
fieldIdx: [3]
});
t.equal(fieldIndex, 3, 'FieldIndex should be 3');
fieldIndex = getDatasetFieldIndexForFilter(dataId, {
dataId: ['different-id', dataId],
fieldIdx: [3, 5]
});
t.equal(fieldIndex, 5, 'FieldIndex should be 5');
fieldIndex = getDatasetFieldIndexForFilter(dataId, {dataId: ['different-id']});
t.equal(fieldIndex, -1, 'FieldIndex should be -1');
t.end();
});
test('filterUtils -> isValidFilterValue', t => {
t.equal(isValidFilterValue(null, true), false, 'Should return false because type is null');
t.equal(
isValidFilterValue(FILTER_TYPES.select, true),
true,
'Should return true because type is select and value is true'
);
t.equal(
isValidFilterValue(FILTER_TYPES.select, false),
true,
'Should return true because type is select and value is true'
);
t.equal(
isValidFilterValue(FILTER_TYPES.timeRange, false),
false,
'Should return false because type is timeRange and value is not an array'
);
t.equal(
isValidFilterValue(FILTER_TYPES.timeRange, []),
true,
'Should return true because type is timeRange and value is an empty array'
);
t.equal(
isValidFilterValue(FILTER_TYPES.timeRange, [1]),
true,
'Should return false because type is timeRange and value is an array'
);
t.equal(
isValidFilterValue(FILTER_TYPES.multiSelect, true),
false,
'Should return false because type is multiSelect and value is not an array'
);
t.equal(
isValidFilterValue(FILTER_TYPES.multiSelect, []),
false,
'Should return false because type is multiSelect and value is an empty array'
);
t.equal(
isValidFilterValue(FILTER_TYPES.multiSelect, [1]),
true,
'Should return false because type is multiSelect and value is an array'
);
t.end();
});
test('filterUtils -> isInPolygon', t => {
t.equal(
isInPolygon([120.47448, 23.667604], {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[120.21949418752885, 23.755486652156186],
[120.21949418752885, 23.221461105318184],
[121.05994828909135, 23.221461105318184],
[121.05994828909135, 23.755486652156186],
[120.21949418752885, 23.755486652156186]
]
]
},
properties: {
renderType: 'Rectangle',
isClosed: true,
bbox: {
xmin: 120.21949418752885,
xmax: null,
ymin: 23.755486652156186,
ymax: null
},
isVisible: true,
filterId: 'z1ilfjv6'
},
id: '036d9e21-af6b-4350-aab9-f1ce37c35cce'
}),
true,
'Should return true because the point is within the polygon'
);
t.equal(
isInPolygon([119.47448, 23.667604], {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[120.21949418752885, 23.755486652156186],
[120.21949418752885, 23.221461105318184],
[121.05994828909135, 23.221461105318184],
[121.05994828909135, 23.755486652156186],
[120.21949418752885, 23.755486652156186]
]
]
},
properties: {
renderType: 'Rectangle',
isClosed: true,
bbox: {
xmin: 120.21949418752885,
xmax: null,
ymin: 23.755486652156186,
ymax: null
},
isVisible: true,
filterId: 'z1ilfjv6'
},
id: '036d9e21-af6b-4350-aab9-f1ce37c35cce'
}),
false,
'Should return false because the point is not within the polygon'
);
t.end();
});
test('filterUtils -> validatePolygonFilter', t => {
const filter = {
layerId: ['layer1'],
dataId: ['puppy'],
value: {
id: 'feature_1',
geometry: {
coordinates: []
}
},
type: 'polygon'
};
const dataset = {
id: 'puppy'
};
const layers = [
{
id: 'layer1'
}
];
t.deepEqual(
validatePolygonFilter(dataset, filter, layers).filter,
{
...filter,
fieldIdx: []
},
'Should positively validate filter'
);
t.equal(
validatePolygonFilter(dataset, filter, [{id: 'layer2'}]).filter,
null,
'Should not validate the filter since layers are not matched'
);
t.equal(
validatePolygonFilter(dataset, {}, layers).filter,
null,
'Should not validate empty filter'
);
t.deepEqual(
validatePolygonFilter(
dataset,
{
...filter,
dataId: ['non_valid']
},
layers
).filter,
null,
'Should not validate filter with non existing dataId'
);
t.deepEqual(
validatePolygonFilter(
dataset,
{
...filter,
value: {
id: 'wrong-value-for-polygon-type'
}
},
layers
).filter,
null,
'Should not validate filter given type and value without corresponding layer'
);
t.end();
});
test('filterUtils -> Polygon getFilterFunction ', t => {
const dataset = {
id: 'puppy',
data: mockPolygonData.data,
fields: mockPolygonData.fields
};
const dataContainer = createDataContainer(dataset.data);
const {layers, data} = mockPolygonData;
const polygonFilter = generatePolygonFilter(layers, mockPolygonFeature);
let filterFunction = getFilterFunction(null, dataset.id, polygonFilter, [], dataContainer);
t.equal(filterFunction(data[0], 0), true, `Should return true because layer list is empty`);
filterFunction = getFilterFunction(null, 'puppy-2', polygonFilter, layers, dataContainer);
t.equal(
filterFunction(data[0], 0),
true,
`${data[0][0]} - ${data[0][1]} should be inside the range`
);
t.end();
});
/* eslint-enable max-statements */
test('filterUtils -> diffFilters', t => {
const testCases = [
{
filterRecord: {
dynamicDomain: [],
fixedDomain: [],
cpu: [],
gpu: []
},
oldFilterRecord: undefined,
result: {
dynamicDomain: null,
fixedDomain: null,
cpu: null,
gpu: null
}
},
{
filterRecord: {
dynamicDomain: [],
fixedDomain: [],
cpu: [],
gpu: []
},
oldFilterRecord: {
dynamicDomain: [],
fixedDomain: [],
cpu: [],
gpu: []
},
result: {
dynamicDomain: null,
fixedDomain: null,
cpu: null,
gpu: null
}
},
{
filterRecord: {
dynamicDomain: [{id: 'aa', name: 'hello', value: 'bb'}],
fixedDomain: [{id: 'bb', name: 'ab', value: 'ab'}],
cpu: [
{id: 'dd', name: 'hey', value: 'ee'},
{id: 'ee', name: 'ee', value: 'ff'}
],
gpu: []
},
oldFilterRecord: {
dynamicDomain: [{id: 'aa', name: 'hello', value: 'bb'}],
fixedDomain: [
{id: 'bb', name: 'cd', value: 'ab'},
{id: 'cc', name: 'world', value: 'dd'}
],
cpu: [{id: 'ee', name: 'ee', value: 'gg'}],
gpu: []
},
result: {
dynamicDomain: null,
fixedDomain: {bb: 'name_changed', cc: 'deleted'},
cpu: {dd: 'added', ee: 'value_changed'},
gpu: null
}
}
];
testCases.forEach(({filterRecord, oldFilterRecord, result}) => {
t.deepEqual(
diffFilters(filterRecord, oldFilterRecord),
result,
'diff filters should be correct'
);
});
t.end();
});
test('filterUtils -> getTimestampFieldDomain', t => {
const timeData = {
zero: {
input: ['2016-10-01 09:45:39', '2016-10-01 09:45:39'],
expect: {
domain: [1475315139000, 1475315140000],
mappedValue: [1475315139000, 1475315139000],
step: 0.05,
defaultTimeFormat: 'L LTS'
}
},
tiny: {
input: ['2016-10-01 09:45:39.001', '2016-10-01 09:45:39.002', '2016-10-01 09:45:39.003'],
expect: {
domain: [1475315139001, 1475315139003],
mappedValue: [1475315139001, 1475315139002, 1475315139003],
step: 0.1,
defaultTimeFormat: 'L LTS'
}
},
small: {
input: ['2016-10-01 09:45:39.010', '2016-10-01 09:45:39.020', '2016-10-01 09:45:39.030'],
expect: {
domain: [1475315139010, 1475315139030],
mappedValue: [1475315139010, 1475315139020, 1475315139030],
step: 1,
defaultTimeFormat: 'L LTS'
}
},
medium: {
input: ['2016-10-01 09:45:39.100', '2016-10-01 09:45:39.200', '2016-10-01 09:45:39.300'],
expect: {
domain: [1475315139100, 1475315139300],
mappedValue: [1475315139100, 1475315139200, 1475315139300],
step: 5,
defaultTimeFormat: 'L LTS'
}
},
large: {
input: ['2016-10-01 09:45:39', '2016-10-01 09:45:45'],
expect: {
domain: [1475315139000, 1475315145000],
mappedValue: [1475315139000, 1475315145000],
step: 1000,
defaultTimeFormat: 'L LTS'
}
}
};
Object.keys(timeData).forEach(key => {
const dataContainer = createDataContainer(timeData[key].input.map(d => [d]));
const valueAccessor = dc => d => moment.utc(dc.valueAt(d.index, 0)).valueOf();
const tsFieldDomain = getTimestampFieldDomain(dataContainer, valueAccessor(dataContainer));
t.deepEqual(
Object.keys(tsFieldDomain).sort(),
Object.keys(timeData[key].expect).sort(),
'domain should have same keys'
);
Object.keys(timeData[key].expect).forEach(k => {
// histogram is created by d3, only need to test they exist
t.deepEqual(tsFieldDomain[k], timeData[key].expect[k], `time domain ${k} should be the same`);
});
});
t.end();
});
test('filterUtils -> scaleSourceDomainToDestination', t => {
const sourceDomain = [1564174363000, 1564179109000];
const destinationDomain = [1564174363000, 1564184336370];
t.deepEqual(
scaleSourceDomainToDestination(sourceDomain, destinationDomain),
[0, 47.586723444532794]
);
t.end();
});
test('filterUtils -> mergeFilterWithTimeline', t => {
const animationConfig = {
domain: [1564174363000, 1564179109000],
currentTime: 1564178316089.6846,
speed: 1,
isAnimating: false,
timeSteps: null,
timeFormat: null,
timezone: null,
defaultTimeFormat: 'L LTS',
duration: null
};
const filter = {
dataId: ['fe422b77-b0fd-4c0e-848c-190e7bf94a72'],
id: 'daqu9ulv',
fixedDomain: true,
enlarged: true,
isAnimating: false,
animationWindow: 'free',
speed: 1,
name: ['DateTime'],
type: 'timeRange',
fieldIdx: [0],
domain: [1564176748230, 1564184336370],
value: [1564176936089.6848, 1564178316089.6846],
plotType: {
interval: '1-minute',
defaultTimeFormat: 'L LT',
type: 'histogram',
aggregation: 'average'
},
yAxis: null,
gpu: true,
syncedWithLayerTimeline: true,
syncTimelineMode: 1,
step: 1000,
mappedValue: [
1564176748230, 1564177260220, 1564178662720, 1564178735140, 1564182284550, 1564183811180,
1564184245340, 1564184331290, 1564184336370
],
defaultTimeFormat: 'L LTS',
fieldType: 'timestamp',
timeBins: {
'fe422b77-b0fd-4c0e-848c-190e7bf94a72': {
'1-minute': [
{
count: 1,
indexes: [0],
x0: 1564176720000,
x1: 1564176780000
},
{
count: 1,
indexes: [1],
x0: 1564177260000,
x1: 1564177320000
},
{
count: 1,
indexes: [2],
x0: 1564178640000,
x1: 1564178700000
},
{
count: 1,
indexes: [3],
x0: 1564178700000,
x1: 1564178760000
},
{
count: 1,
indexes: [4],
x0: 1564182240000,
x1: 1564182300000
},
{
count: 1,
indexes: [5],
x0: 1564183800000,
x1: 1564183860000
},
{
count: 1,
indexes: [6],
x0: 1564184220000,
x1: 1564184280000
},
{
count: 2,
indexes: [7, 8],
x0: 1564184280000,
x1: 1564184340000
}
]
}
},
gpuChannel: [0]
};
const {filter: newFilter, animationConfig: newAnimationConfig} = mergeFilterWithTimeline(
filter,
animationConfig
);
t.deepEqual(
newFilter.domain,
[1564174363000, 1564184336370],
'Merged filter should have the same domain'
);
t.deepEqual(
newAnimationConfig.domain,
[1564174363000, 1564184336370],
'Merged animationConfig should have the same domain'
);
t.deepEqual(
newFilter.domain,
newAnimationConfig.domain,
'New filter and animationConfig should have the same domain'
);
t.end();
});
test('filterUtils -> getPolygonFilterFunctor -> point layer with dataToFeature (GeoJSON column mode)', t => {
const squarePolygon = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
[-1, -1]
]
]
}
};
const filter = {value: squarePolygon};
// Point layer with dataToFeature (GeoJSON column mode)
const layerWithDataToFeature = {
type: 'point',
config: {columnMode: 'geojson'},
getPositionAccessor: () => () => null,
dataToFeature: {
length: 6,
0: [0.5, 0.5], // inside
1: [10, 10], // outside
2: [
[10, 10],
[0.2, 0.2]
], // MultiPoint: one inside
3: [
[10, 10],
[20, 20]
], // MultiPoint: all outside
4: [], // empty coordinates (e.g. empty GeometryCollection)
5: [[], [0.5, 0.5]] // MultiPoint with one empty coord pair
}
};
const fn = getPolygonFilterFunctor(layerWithDataToFeature, filter, null);
t.equal(fn({index: 0}), true, 'Single point inside polygon should return true');
t.equal(fn({index: 1}), false, 'Single point outside polygon should return false');
t.equal(fn({index: 2}), true, 'MultiPoint with at least one point inside should return true');
t.equal(fn({index: 3}), false, 'MultiPoint with all points outside should return false');
t.equal(fn({index: 4}), false, 'Empty coordinates should return false');
t.equal(
fn({index: 5}),
true,
'MultiPoint with one empty and one valid inside should return true'
);
// Point layer without dataToFeature (standard column mode)
const layerWithoutDataToFeature = {
type: 'point',
config: {columnMode: 'points'},
getPositionAccessor: () => d => d.position,
dataToFeature: []
};
const fn2 = getPolygonFilterFunctor(layerWithoutDataToFeature, filter, null);
t.equal(fn2({position: [0.5, 0.5]}), true, 'Standard mode: point inside should return true');
t.equal(fn2({position: [10, 10]}), false, 'Standard mode: point outside should return false');
t.end();
});
test('filterUtils -> getPolygonFilterFunctor -> aggregation layers (grid/hexagon/cluster)', t => {
const squarePolygon = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
[-1, -1]
]
]
}
};
const filter = {value: squarePolygon};
['grid', 'hexagon', 'cluster'].forEach(type => {
// GeoJSON column mode: aggregation layers store parsed Features in
// dataToFeature but precompute per-row centroids used for filtering.
const geojsonLayer = {
type,
config: {columnMode: 'geojson'},
getPositionAccessor: () => () => null,
// dataToFeature holds Feature objects (must NOT be used for the point test)
dataToFeature: [
{type: 'Feature', geometry: {type: 'Point', coordinates: [0.5, 0.5]}},
{type: 'Feature', geometry: {type: 'Point', coordinates: [10, 10]}},
null
],
centroids: [
[0.5, 0.5], // inside
[10, 10], // outside
null // no geometry
]
};
const fn = getPolygonFilterFunctor(geojsonLayer, filter, null);
t.equal(fn({index: 0}), true, `${type} geojson: centroid inside should return true`);
t.equal(fn({index: 1}), false, `${type} geojson: centroid outside should return false`);
t.equal(fn({index: 2}), null, `${type} geojson: missing centroid should be falsy`);
// Point column mode: no centroids, falls back to the position accessor.
const pointLayer = {
type,
config: {columnMode: 'points'},
getPositionAccessor: () => d => d.position,
dataToFeature: [],
centroids: []
};
const fn2 = getPolygonFilterFunctor(pointLayer, filter, null);
t.equal(fn2({position: [0.5, 0.5]}), true, `${type} points: point inside should return true`);
t.equal(fn2({position: [10, 10]}), false, `${type} points: point outside should return false`);
});
t.end();
});
test('filterUtils -> getPolygonFilterFunctor -> point layer ignores non-finite altitude', t => {
const squarePolygon = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
[-1, -1]
]
]
}
};
const filter = {value: squarePolygon};
const layer = {
type: 'point',
config: {columnMode: 'points'},
getPositionAccessor: () => d => d.position,
dataToFeature: []
};
const fn = getPolygonFilterFunctor(layer, filter, null);
t.equal(fn({position: [0.5, 0.5, 0]}), true, 'finite altitude should keep a point inside');
t.equal(
fn({position: [0.5, 0.5, null]}),
true,
'null altitude should not exclude a valid lng/lat'
);
t.equal(
fn({position: [0.5, 0.5, undefined]}),
true,
'undefined altitude should not exclude a valid lng/lat'
);
t.equal(
fn({position: [10, 10, null]}),
false,
'null altitude should not include a point outside'
);
t.equal(
fn({position: new Float64Array([0.5, 0.5, 0])}),
true,
'typed-array lng/lat should be treated as a valid position'
);
t.equal(
fn({position: [null, 0.5, 0]}),
false,
'null longitude should not coerce to 0 and pass the polygon filter'
);
t.equal(
fn({position: ['', 0.5, 0]}),
false,
'empty-string longitude should not coerce to 0 and pass the polygon filter'
);
t.end();
});
test('filterUtils -> getPolygonFilterFunctor -> arc layer validates both endpoints', t => {
const squarePolygon = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
[-1, -1]
]
]
}
};
const fn = getPolygonFilterFunctor(
{
type: 'arc',
getPositionAccessor: () => d => d.position
},
{value: squarePolygon},
null
);
t.equal(
fn({position: [0.5, 0.5, 0, 0.2, 0.2, 0]}),
true,
'arc with both endpoints inside should pass'
);
t.equal(
fn({position: [0.5, 0.5, null, 0.2, 0.2, undefined]}),
true,
'null altitude should not exclude a valid arc'
);
t.equal(
fn({position: [0.5, 0.5, 0, null, 0.2, 0]}),
false,
'missing destination longitude should not reach turfPoint'
);
t.equal(
fn({position: [0.5, 0.5, 0, 10, 10, 0]}),
false,
'arc with destination outside should fail'
);
t.end();
});
test('filterUtils -> timeWindowOverlapsInterval', t => {
t.equal(timeWindowOverlapsInterval(0, 10, [2, 8]), true, 'window inside feature interval');
t.equal(timeWindowOverlapsInterval(0, 10, [-5, 0]), true, 'window touches start');
t.equal(timeWindowOverlapsInterval(0, 10, [10, 15]), true, 'window touches end');
t.equal(timeWindowOverlapsInterval(0, 10, [11, 20]), false, 'window after feature');
t.equal(timeWindowOverlapsInterval(20, 30, [0, 10]), false, 'window before feature');
t.equal(
timeWindowOverlapsInterval(5, null, [0, 10]),
true,
'null end is still active once started'
);
t.equal(timeWindowOverlapsInterval(20, null, [0, 10]), false, 'null end is hidden before start');
t.equal(timeWindowOverlapsInterval(null, 10, [0, 10]), false, 'null start is hidden');
t.equal(
timeWindowOverlapsInterval(10, 0, [0, 20]),
false,
'inverted interval (end < start) is hidden'
);
t.equal(
timeWindowOverlapsInterval(5, 5, [0, 10]),
true,
'zero-length interval at a point overlaps'
);
t.end();
});
test('filterUtils -> getFilterFunction time interval overlap', t => {
const startMapped = [0, 10, 20, 30];
const endMapped = [15, 15, 40, null];
const field = {
filterProps: {mappedValue: startMapped},
valueAccessor: () => null,
format: ''
};
const filter = {
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
value: [12, 18],
endName: ['end'],
endMappedValue: [endMapped]
};
t.ok(isTimeIntervalFilter(filter, 0), 'should detect interval mode from endName');
const filterFunction = getFilterFunction(field, 'ds', filter, [], null);
t.equal(filterFunction({index: 0}), true, '[0, 15] overlaps window [12, 18]');
t.equal(filterFunction({index: 1}), true, '[10, 15] overlaps window [12, 18]');
t.equal(filterFunction({index: 2}), false, '[20, 40] starts after the window');
t.equal(filterFunction({index: 3}), false, '[30, inf] has not started yet');
const instantFn = getFilterFunction(
field,
'ds',
{
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
value: [12, 18]
},
[],
null
);
t.equal(instantFn({index: 0}), false, 'instant mode still requires the timestamp in range');
t.equal(instantFn({index: 1}), false, 'timestamp 10 is outside [12, 18]');
t.end();
});
test('filterUtils -> applyTimeFilterEndFieldName', t => {
const dataset = {
id: 'ds',
fields: [
{
name: 'start',
type: 'timestamp',
filterProps: {
fieldType: 'timestamp',
mappedValue: [0, 10],
domain: [0, 10],
step: 1
}
},
{
name: 'end',
type: 'timestamp',
filterProps: {
fieldType: 'timestamp',
mappedValue: [20, 50],
domain: [20, 50],
step: 1
}
},
{
name: 'other',
type: 'real',
filterProps: {fieldType: 'real', domain: [0, 1], step: 0.1}
}
],
getColumnFieldIdx(name) {
return this.fields.findIndex(f => f.name === name);
},
getColumnFilterProps(name) {
return this.fields.find(f => f.name === name)?.filterProps || null;
}
};
const filter = {
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
name: ['start'],
fieldIdx: [0],
domain: [0, 10],
value: [0, 10],
step: 1
};
const {filter: withEnd} = applyTimeFilterEndFieldName(filter, {ds: dataset}, 'ds', 'end', 0);
t.ok(withEnd, 'should apply a timestamp end field');
t.deepEqual(withEnd.endName, ['end'], 'should store endName');
t.deepEqual(withEnd.endFieldIdx, [1], 'should store endFieldIdx');
t.deepEqual(withEnd.domain, [0, 50], 'domain should span start min to end max');
t.deepEqual(withEnd.value, [0, 50], 'full-range window should expand with domain');
const {filter: sameAsStart} = applyTimeFilterEndFieldName(
filter,
{ds: dataset},
'ds',
'start',
0
);
t.equal(sameAsStart, null, 'should reject using the start field as end');
const {filter: nonTimestamp} = applyTimeFilterEndFieldName(
filter,
{ds: dataset},
'ds',
'other',
0
);
t.equal(nonTimestamp, null, 'should reject a non-timestamp end field');
const {filter: cleared} = applyTimeFilterEndFieldName(withEnd, {ds: dataset}, 'ds', null, 0);
t.notOk(cleared.endName, 'clearing end field should remove endName');
t.deepEqual(cleared.domain, [0, 10], 'domain should shrink back to the start field');
t.end();
});
test('filterUtils -> getFilterFunction inverted', t => {
const field = {valueAccessor: d => d.value};
const range = {type: FILTER_TYPES.range, value: [2, 4], enabled: true};
const keep = getFilterFunction(field, 'ds', range, [], null);
const drop = getFilterFunction(field, 'ds', {...range, inverted: true}, [], null);
t.equal(keep({value: 3}), true, 'keeps values inside the range');
t.equal(keep({value: 5}), false, 'drops values outside the range');
t.equal(drop({value: 3}), false, 'filter out hides values inside the range');
t.equal(drop({value: 5}), true, 'filter out keeps values outside the range');
t.equal(keep({value: null}), false, 'a null is not coerced to zero by a range filter');
t.equal(drop({value: null}), true, 'filter out keeps a null that does not match the range');
t.equal(keep({value: ''}), false, 'an empty value is not coerced to zero by a range filter');
t.equal(drop({value: ''}), true, 'filter out keeps an empty value that does not match the range');
const disabled = getFilterFunction(
field,
'ds',
{...range, inverted: true, enabled: false},
[],
null
);
t.equal(disabled({value: 5}), true, 'a disabled filter still keeps every row');
const multi = {type: FILTER_TYPES.multiSelect, value: ['a'], enabled: true};
const exclude = getFilterFunction(field, 'ds', {...multi, inverted: true}, [], null);
t.equal(exclude({value: 'a'}), false, 'filter out hides a selected category');
t.equal(exclude({value: 'b'}), true, 'filter out keeps an unselected category');
const timeField = {
valueAccessor: () => null,
format: '',
filterProps: {mappedValue: [0, 10, 20]}
};
const time = {
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
value: [12, 18],
enabled: true,
inverted: true,
endName: ['end'],
endMappedValue: [[15, 15, 40]]
};
const timeOut = getFilterFunction(timeField, 'ds', time, [], null);
t.equal(timeOut({index: 1}), false, 'filter out hides an interval that overlaps the window');
t.equal(timeOut({index: 2}), true, 'filter out keeps an interval outside the window');
t.end();
});
test('filterUtils -> disableGpuIfInverted', t => {
const inverted = disableGpuIfInverted({
inverted: true,
gpu: true,
gpuChannel: [0],
gpuEndChannel: [1],
type: FILTER_TYPES.range
});
t.equal(inverted.gpu, false, 'inverted range filters leave the GPU');
t.equal(inverted.gpuChannel, undefined, 'should release the GPU channel');
t.equal(inverted.gpuEndChannel, undefined, 'should release the end channel');
const kept = disableGpuIfInverted({inverted: false, gpu: true, gpuChannel: [0]});
t.equal(kept.gpu, true, 'filter in still uses the GPU');
t.deepEqual(kept.gpuChannel, [0], 'filter in keeps its channel');
t.end();
});
test('filterUtils -> validateFilterWithData rejects unsupported inversion', t => {
const dataset = new KeplerTable({
info: {id: 'remote'},
color: [0, 0, 0],
disableDataOperation: true
});
dataset.updateSchema({
fields: [{name: 'value', type: 'real', analyzerType: 'FLOAT'}],
rows: []
});
const {filter} = validateFilterWithData(
{remote: dataset},
'remote',
{
...getDefaultFilter({dataId: ['remote']}),
name: ['value'],
type: FILTER_TYPES.range,
value: [0, 10],
inverted: true
},
[]
);
t.equal(filter.inverted, false, 'saved filter out is cleared for a GPU-only dataset');
t.equal(filter.gpu, true, 'the validated range filter remains on the GPU');
t.end();
});
test('filterUtils -> getPlotBaseIndexByDataId excludes the plotted filter', t => {
const values = [1, 5, 9];
const dataContainer = createDataContainer(values.map(v => [v]));
const field = {valueAccessor: d => dataContainer.valueAt(d.index, 0)};
const inverted = {
id: 'self',
type: FILTER_TYPES.range,
dataId: ['ds'],
fieldIdx: [0],
value: [4, 6],
inverted: true,
enabled: true
};
const other = {
id: 'other',
type: FILTER_TYPES.range,
dataId: ['ds'],
fieldIdx: [0],
value: [0, 8],
enabled: true
};
const dataset = {
id: 'ds',
fields: [field],
dataContainer,
allIndexes: [0, 1, 2],
filterRecord: {
cpu: [inverted, other],
gpu: [],
dynamicDomain: [],
fixedDomain: []
}
};
t.deepEqual(
getPlotBaseIndexByDataId({ds: dataset}, inverted).ds,
[0, 1],
'histogram index keeps the other filter and ignores the inverted filter itself'
);
t.equal(
getPlotBaseIndexByDataId({ds: dataset}, {id: 'gpu-only', dataId: ['ds']}),
undefined,
'GPU filters are already absent from the CPU index'
);
t.end();
});
test('filterUtils -> getPlotBaseIndexByDataId retains polygon filters', t => {
const dataset = new KeplerTable({info: {id: 'ds'}, color: [0, 0, 0]});
dataset.updateSchema({
fields: [
{name: 'value', type: 'real', analyzerType: 'FLOAT'},
{name: 'lat', type: 'real', analyzerType: 'FLOAT'},
{name: 'lng', type: 'real', analyzerType: 'FLOAT'}
],
rows: [
[1, 12.25, 30.5],
[5, 12.25, 35.5],
[9, 14.25, 30.5]
]
});
const layer = {
id: 'layer',
type: 'point',
config: {dataId: 'ds'},
getPositionAccessor:
dataContainer =>
({index}) =>
[dataContainer.valueAt(index, 2), dataContainer.valueAt(index, 1)]
};
const inverted = {
id: 'self',
type: FILTER_TYPES.range,
dataId: ['ds'],
fieldIdx: [0],
name: ['value'],
value: [4, 6],
inverted: true,
enabled: true,
gpu: false
};
const other = {
id: 'other',
type: FILTER_TYPES.range,
dataId: ['ds'],
fieldIdx: [0],
name: ['value'],
value: [0, 8],
enabled: true,
gpu: false
};
const polygon = {
id: 'polygon',
type: FILTER_TYPES.polygon,
dataId: ['ds'],
fieldIdx: [],
layerId: ['layer'],
value: mockPolygonFeature,
enabled: true,
fixedDomain: true,
gpu: false
};
dataset.filterTable([inverted, other, polygon], [layer]);
const customTable = {
id: dataset.id,
fields: dataset.fields,
dataContainer: dataset.dataContainer,
allIndexes: dataset.allIndexes,
filteredIndex: dataset.filteredIndex,
filterRecord: dataset.filterRecord
};
t.deepEqual(
getPlotBaseIndexByDataId({ds: customTable}, inverted, [layer]).ds,
[0],
'custom-table histogram base omits itself while keeping CPU and polygon filters'
);
t.end();
});
test('filterUtils -> diffFilters notices inverted', t => {
const filterRecord = {
dynamicDomain: [],
fixedDomain: [],
cpu: [{id: 'aa', name: 'col', value: ['a'], inverted: true}],
gpu: []
};
const oldFilterRecord = {
dynamicDomain: [],
fixedDomain: [],
cpu: [{id: 'aa', name: 'col', value: ['a'], inverted: false}],
gpu: []
};
t.deepEqual(
diffFilters(filterRecord, oldFilterRecord).cpu,
{aa: 'inverted_changed'},
'toggling filter out should recompute CPU filters'
);
t.end();
});