Skip to content

Commit

Permalink
[Maps] Use id-values from client-manifest to suggest layers (#102788)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck authored and kibanamachine committed Jun 23, 2021
1 parent 3d69df2 commit 420618b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ import { DocumentStatsTable } from './document_stats';
import { ExpandedRowContent } from './expanded_row_content';
import { ChoroplethMap } from './choropleth_map';

const COMMON_EMS_LAYER_IDS = [
'world_countries',
'administrative_regions_lvl2',
'usa_zip_codes',
'usa_states',
];

export const KeywordContent: FC<FieldDataRowProps> = ({ config }) => {
const [EMSSuggestion, setEMSSuggestion] = useState<EMSTermJoinConfig | null | undefined>();
const { stats, fieldName } = config;
Expand All @@ -32,7 +25,6 @@ export const KeywordContent: FC<FieldDataRowProps> = ({ config }) => {
const loadEMSTermSuggestions = useCallback(async () => {
if (!mapsPlugin) return;
const suggestion: EMSTermJoinConfig | null = await mapsPlugin.suggestEMSTermJoinConfig({
emsLayerIds: COMMON_EMS_LAYER_IDS,
sampleValues: Array.isArray(stats?.topValues)
? stats?.topValues.map((value) => value.key)
: [],
Expand Down
119 changes: 42 additions & 77 deletions x-pack/plugins/maps/public/ems_autosuggest/ems_autosuggest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,22 @@
*/

import { suggestEMSTermJoinConfig } from './ems_autosuggest';
import { FeatureCollection } from 'geojson';

class MockFileLayer {
private readonly _url: string;
private readonly _id: string;
private readonly _fields: Array<{ id: string }>;

constructor(url: string, fields: Array<{ id: string }>) {
this._url = url;
this._id = url;
constructor(id: string, fields: Array<{ id: string; alias?: string[]; values?: string[] }>) {
this._id = id;
this._fields = fields;
}

getFields() {
return this._fields;
getId() {
return this._id;
}

getGeoJson() {
if (this._url === 'world_countries') {
return ({
type: 'FeatureCollection',
features: [
{ properties: { iso2: 'CA', iso3: 'CAN' } },
{ properties: { iso2: 'US', iso3: 'USA' } },
],
} as unknown) as FeatureCollection;
} else if (this._url === 'zips') {
return ({
type: 'FeatureCollection',
features: [{ properties: { zip: '40204' } }, { properties: { zip: '40205' } }],
} as unknown) as FeatureCollection;
} else {
throw new Error(`unrecognized mock url ${this._url}`);
}
getFields() {
return this._fields;
}

hasId(id: string) {
Expand All @@ -51,31 +33,31 @@ jest.mock('../util', () => {
return {
async getEmsFileLayers() {
return [
new MockFileLayer('world_countries', [{ id: 'iso2' }, { id: 'iso3' }]),
new MockFileLayer('zips', [{ id: 'zip' }]),
new MockFileLayer('world_countries', [
{
id: 'iso2',
alias: ['(geo\\.){0,}country_iso_code$', '(country|countries)'],
values: ['CA', 'US'],
},
{ id: 'iso3', values: ['CAN', 'USA'] },
{ id: 'name', alias: ['(country|countries)'] },
]),
new MockFileLayer('usa_zip_codes', [
{ id: 'zip', alias: ['zip'], values: ['40204', '40205'] },
]),
];
},
};
});

describe('suggestEMSTermJoinConfig', () => {
test('no info provided', async () => {
test('Should not validate when no info provided', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({});
expect(termJoinConfig).toBe(null);
});

describe('validate common column names', () => {
test('ecs region', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValuesColumnName: 'destination.geo.region_iso_code',
});
expect(termJoinConfig).toEqual({
layerId: 'administrative_regions_lvl2',
field: 'region_iso_code',
});
});

test('ecs country', async () => {
describe('With common column names', () => {
test('should match first match', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValuesColumnName: 'country_iso_code',
});
Expand All @@ -85,78 +67,61 @@ describe('suggestEMSTermJoinConfig', () => {
});
});

test('country', async () => {
test('When sampleValues are provided, should reject match if no sampleValues for a layer, even though the name matches', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValuesColumnName: 'Country_name',
});
expect(termJoinConfig).toEqual({
layerId: 'world_countries',
field: 'name',
sampleValuesColumnName: 'country_iso_code',
sampleValues: ['FO', 'US', 'CA'],
});
expect(termJoinConfig).toEqual(null);
});

test('unknown name', async () => {
test('should reject match if sampleValues not in id-list', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValuesColumnName: 'cntry',
sampleValuesColumnName: 'zip',
sampleValues: ['90201', '40205'],
});
expect(termJoinConfig).toEqual(null);
});
});

describe('validate well known formats', () => {
test('5-digit zip code', async () => {
test('should return first match (regex matches both iso2 and name)', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['90201', 40204],
sampleValuesColumnName: 'Country_name',
});
expect(termJoinConfig).toEqual({
layerId: 'usa_zip_codes',
field: 'zip',
layerId: 'world_countries',
field: 'iso2',
});
});

test('mismatch', async () => {
test('unknown name', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['90201', 'foobar'],
sampleValuesColumnName: 'cntry',
});
expect(termJoinConfig).toEqual(null);
});
});

describe('validate based on EMS data', () => {
test('Should validate with zip codes layer', async () => {
describe('validate well known formats (using id-values in manifest)', () => {
test('Should validate known zipcodes', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['40204', 40205],
emsLayerIds: ['world_countries', 'zips'],
sampleValues: ['40205', 40204],
});
expect(termJoinConfig).toEqual({
layerId: 'zips',
layerId: 'usa_zip_codes',
field: 'zip',
});
});

test('Should not validate with faulty zip codes', async () => {
test('Should not validate unknown zipcode (in this case, 90201)', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['40204', '00000'],
emsLayerIds: ['world_countries', 'zips'],
sampleValues: ['90201', 40204],
});
expect(termJoinConfig).toEqual(null);
});

test('Should validate against countries', async () => {
test('Should not validate mismatches', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['USA', 'USA', 'CAN'],
emsLayerIds: ['world_countries', 'zips'],
});
expect(termJoinConfig).toEqual({
layerId: 'world_countries',
field: 'iso3',
});
});

test('Should not validate against missing countries', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['USA', 'BEL', 'CAN'],
emsLayerIds: ['world_countries', 'zips'],
sampleValues: ['90201', 'foobar'],
});
expect(termJoinConfig).toEqual(null);
});
Expand Down
Loading

0 comments on commit 420618b

Please sign in to comment.