Skip to content

Commit

Permalink
remove ems-layer id config
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jun 22, 2021
1 parent 140a939 commit 8161ca5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 146 deletions.
83 changes: 12 additions & 71 deletions x-pack/plugins/maps/public/ems_autosuggest/ems_autosuggest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class MockFileLayer {
return this._fields;
}

getGeoJson() {
return this._geoJson;
}

hasId(id: string) {
return id === this._id;
}
Expand All @@ -44,33 +40,18 @@ jest.mock('../util', () => {
return {
async getEmsFileLayers() {
return [
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)'] },
],
({
type: 'FeatureCollection',
features: [
{ properties: { iso2: 'CA', iso3: 'CAN', name: 'Canada' } },
{ properties: { iso2: 'US', iso3: 'USA', name: 'United States of America' } },
],
} as unknown) as FeatureCollection
),
new MockFileLayer(
'usa_zip_codes',
[{ id: 'zip', alias: ['zip'], values: ['40204', '40205'] }],
({
type: 'FeatureCollection',
features: [{ properties: { zip: '40204' } }, { properties: { zip: '40205' } }],
} as unknown) as FeatureCollection
),
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'] },
]),
];
},
};
Expand Down Expand Up @@ -152,44 +133,4 @@ describe('suggestEMSTermJoinConfig', () => {
expect(termJoinConfig).toEqual(null);
});
});

describe('validate based on EMS data', () => {
test('Should validate with zip codes layer', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['40204', 40205],
emsLayerIds: ['world_countries', 'usa_zip_codes'],
});
expect(termJoinConfig).toEqual({
layerId: 'usa_zip_codes',
field: 'zip',
});
});

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

test('Should validate against countries', async () => {
const termJoinConfig = await suggestEMSTermJoinConfig({
sampleValues: ['USA', 'USA', 'CAN'],
emsLayerIds: ['world_countries', 'usa_zip_codes'],
});
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', 'usa_zip_codes'],
});
expect(termJoinConfig).toEqual(null);
});
});
});
75 changes: 0 additions & 75 deletions x-pack/plugins/maps/public/ems_autosuggest/ems_autosuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { FileLayer } from '@elastic/ems-client';
import { getEmsFileLayers } from '../util';

export interface SampleValuesConfig {
emsLayerIds?: string[];
sampleValues?: Array<string | number>;
sampleValuesColumnName?: string;
}
Expand Down Expand Up @@ -98,16 +97,6 @@ export async function suggestEMSTermJoinConfig(
if (sampleValuesConfig.sampleValues && sampleValuesConfig.sampleValues.length) {
// Only looks at id-values in main manifest
matches.push(...suggestByIdValues(sampleValuesConfig.sampleValues));

// Looks at _all_ columns for EMS-layers.
if (sampleValuesConfig.emsLayerIds && sampleValuesConfig.emsLayerIds.length) {
matches.push(
...(await suggestsByAllEMSColumns(
sampleValuesConfig.emsLayerIds,
sampleValuesConfig.sampleValues
))
);
}
}

const uniqMatches: UniqueMatch[] = matches.reduce((accum: UniqueMatch[], match) => {
Expand Down Expand Up @@ -188,67 +177,3 @@ function suggestByIdValues(sampleValues: SampleValues): EMSTermJoinConfig[] {
});
return matches;
}

function existsInEMS(emsJson: any, emsFieldId: string, sampleValue: string): boolean {
for (let i = 0; i < emsJson.features.length; i++) {
const emsFieldValue = emsJson.features[i].properties[emsFieldId].toString();
if (emsFieldValue.toString() === sampleValue) {
return true;
}
}
return false;
}

function matchesEmsField(emsJson: any, emsFieldId: string, sampleValues: SampleValues) {
for (let j = 0; j < sampleValues.length; j++) {
const sampleValue = sampleValues[j].toString();
if (!existsInEMS(emsJson, emsFieldId, sampleValue)) {
return false;
}
}
return true;
}

async function getMatchesForEMSLayer(
emsLayerId: string,
sampleValues: SampleValues
): Promise<EMSTermJoinConfig[]> {
const fileLayers: FileLayer[] = await getEmsFileLayers();
const emsFileLayer: FileLayer | undefined = fileLayers.find((fl: FileLayer) =>
fl.hasId(emsLayerId)
);

if (!emsFileLayer) {
return [];
}

const emsFields = emsFileLayer.getFields();

try {
const emsJson = await emsFileLayer.getGeoJson();
const matches: EMSTermJoinConfig[] = [];
for (let f = 0; f < emsFields.length; f++) {
if (matchesEmsField(emsJson, emsFields[f].id, sampleValues)) {
matches.push({
layerId: emsLayerId,
field: emsFields[f].id,
});
}
}
return matches;
} catch (e) {
return [];
}
}

async function suggestsByAllEMSColumns(
emsLayerIds: string[],
values: SampleValues
): Promise<EMSTermJoinConfig[]> {
const matches = [];
for (const emsLayerId of emsLayerIds) {
const layerIdMathes = await getMatchesForEMSLayer(emsLayerId, values);
matches.push(...layerIdMathes);
}
return matches;
}

0 comments on commit 8161ca5

Please sign in to comment.