Skip to content

Commit

Permalink
add id-value lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jun 21, 2021
1 parent 21d7eb0 commit 58bc237
Showing 1 changed file with 64 additions and 31 deletions.
95 changes: 64 additions & 31 deletions x-pack/plugins/maps/public/ems_autosuggest/ems_autosuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ export interface EMSTermJoinConfig {
field: string;
}

export interface EMSConfig {
layerId: string;
field: string;
}

export interface EMSMatch {
regex: RegExp;
emsConfig: EMSConfig;
emsConfig: EMSTermJoinConfig;
emsField: FileLayerField;
}

Expand All @@ -37,26 +32,28 @@ interface UniqueMatch {

type SampleValues = Array<string | number>;

let isMetaLoaded = false;
let wellKnownColumnNames: EMSMatch[];
let wellKnownColumnFormats: EMSMatch[];
let wellKnownIdFields: Array<{
emsConfig: EMSConfig;
emsField: FileLayerField;
let wellKnownIds: Array<{
emsConfig: EMSTermJoinConfig;
values: string[];
}>;

async function loadMeta() {
if (wellKnownColumnFormats && wellKnownColumnNames) {
if (isMetaLoaded) {
return;
}

const fileLayers: FileLayer[] = await getEmsFileLayers();

wellKnownColumnNames = [];
wellKnownColumnFormats = [];
wellKnownIds = [];

fileLayers.forEach((fileLayer) => {
const emsFields = fileLayer.getFields();
emsFields.forEach((emsField) => {
fileLayers.forEach((fileLayer: FileLayer) => {
const emsFields: FileLayerField[] = fileLayer.getFields();
emsFields.forEach((emsField: FileLayerField) => {
const emsConfig = {
layerId: fileLayer.getId(),
field: emsField.id,
Expand All @@ -71,19 +68,26 @@ async function loadMeta() {
}

if (emsField.alias && emsField.alias.length) {
emsField.alias.forEach((alias) => {
emsField.alias.forEach((alias: string) => {
wellKnownColumnNames.push({
regex: new RegExp(alias, 'i'),
emsConfig,
emsField,
});
});
}

if (emsField.values) {
wellKnownIds.push({
emsConfig,
values: emsField.values,
});
}
});
});
}

function idValuesMatch() {}
isMetaLoaded = true;
}

export async function suggestEMSTermJoinConfig(
sampleValuesConfig: SampleValuesConfig
Expand All @@ -98,15 +102,17 @@ 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
))
);
} else {
matches.push(...suggestByFormats(sampleValuesConfig.sampleValues));
}
}

Expand Down Expand Up @@ -136,30 +142,57 @@ export async function suggestEMSTermJoinConfig(

function suggestByName(columnName: string, sampleValues?: SampleValues): EMSTermJoinConfig[] {
const matches = wellKnownColumnNames.filter((wellknown) => {
return columnName.match(wellknown.regex);
const nameMatchesAlias = columnName.match(wellknown.regex);
// Check if there is matching known id-values
return sampleValues
? nameMatchesAlias && !violatesIdValues(sampleValues, wellknown.emsConfig)
: nameMatchesAlias;
});

return matches.map((m) => {
return m.emsConfig;
});
}

function suggestByIdValues(values: SampleValues): EMSTermJoinConfig[] {}
function allSamplesMatch(sampleValues: SampleValues, values: string[]) {
for (let j = 0; j < sampleValues.length; j++) {
const sampleValue = sampleValues[j].toString();
if (!existInIdValues(sampleValue, values)) {
return false;
}
}
return true;
}

function existInIdValues(sampleValue: string, values: string[]): boolean {
for (let i = 0; i < values.length; i++) {
if (values[i] === sampleValue) {
return true;
}
}
return false;
}

function suggestByFormats(values: SampleValues): EMSTermJoinConfig[] {
const matches = wellKnownColumnFormats.filter((wellknown) => {
for (let i = 0; i < values.length; i++) {
const value = values[i].toString();
if (!value.match(wellknown.regex)) {
return false;
}
function violatesIdValues(sampleValues: SampleValues, termConfig: EMSTermJoinConfig) {
for (let i = 0; i < wellKnownIds.length; i++) {
if (
wellKnownIds[i].emsConfig.field === termConfig.field &&
wellKnownIds[i].emsConfig.layerId === termConfig.layerId
) {
return !allSamplesMatch(sampleValues, wellKnownIds[i].values);
}
return true;
});
}
return false;
}

return matches.map((m) => {
return m.emsConfig;
function suggestByIdValues(sampleValues: SampleValues): EMSTermJoinConfig[] {
const matches: EMSTermJoinConfig[] = [];
wellKnownIds.forEach((wellKnownId) => {
if (allSamplesMatch(sampleValues, wellKnownId.values)) {
matches.push(wellKnownId.emsConfig);
}
});
return matches;
}

function existsInEMS(emsJson: any, emsFieldId: string, sampleValue: string): boolean {
Expand Down

0 comments on commit 58bc237

Please sign in to comment.