Skip to content

Commit

Permalink
[Composable template] Demo and PR review fixes (#71065)
Browse files Browse the repository at this point in the history
# Conflicts:
#	x-pack/plugins/index_management/public/application/components/mappings_editor/mappings_state.tsx
  • Loading branch information
sebelga authored and alisonelizabeth committed Jul 9, 2020
1 parent c1fd354 commit 8434daf
Show file tree
Hide file tree
Showing 38 changed files with 768 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ export const FormRow = ({
titleWrapped = title;
}

if (!children && !field) {
throw new Error('You need to provide either children or a field to the FormRow');
}

return (
<EuiDescribedFormGroup title={titleWrapped} description={description} fullWidth>
{children ? children : <Field field={field!} {...rest} />}
{children ? children : field ? <Field field={field!} {...rest} /> : null}
</EuiDescribedFormGroup>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
find('closeDetailsButton').simulate('click');
};

const toggleViewItem = (view: 'composable' | 'system') => {
const toggleViewItem = (view: 'managed' | 'cloudManaged' | 'system') => {
const { find, component } = testBed;
const views = ['composable', 'system'];
const views = ['managed', 'cloudManaged', 'system'];

// First open the pop over
act(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe('Index Templates tab', () => {
},
},
});
(template1 as any).hasSettings = true;

const template2 = fixtures.getTemplate({
name: `b${getRandomString()}`,
Expand All @@ -73,6 +72,7 @@ describe('Index Templates tab', () => {
const template3 = fixtures.getTemplate({
name: `.c${getRandomString()}`, // mock system template
indexPatterns: ['template3Pattern1*', 'template3Pattern2', 'template3Pattern3'],
type: 'system',
});

const template4 = fixtures.getTemplate({
Expand Down Expand Up @@ -101,6 +101,7 @@ describe('Index Templates tab', () => {
name: `.c${getRandomString()}`, // mock system template
indexPatterns: ['template6Pattern1*', 'template6Pattern2', 'template6Pattern3'],
isLegacy: true,
type: 'system',
});

const templates = [template1, template2, template3];
Expand All @@ -124,44 +125,49 @@ describe('Index Templates tab', () => {
// Test composable table content
tableCellsValues.forEach((row, i) => {
const indexTemplate = templates[i];
const { name, indexPatterns, priority, ilmPolicy, composedOf, template } = indexTemplate;
const { name, indexPatterns, ilmPolicy, composedOf, template } = indexTemplate;

const hasContent = !!template.settings || !!template.mappings || !!template.aliases;
const ilmPolicyName = ilmPolicy && ilmPolicy.name ? ilmPolicy.name : '';
const composedOfString = composedOf ? composedOf.join(',') : '';
const priorityFormatted = priority ? priority.toString() : '';

expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'', // Checkbox to select row
name,
indexPatterns.join(', '),
ilmPolicyName,
composedOfString,
priorityFormatted,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);

try {
expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'', // Checkbox to select row
name,
indexPatterns.join(', '),
ilmPolicyName,
composedOfString,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);
} catch (e) {
console.error(`Error in index template at row ${i}`); // eslint-disable-line no-console
throw e;
}
});

// Test legacy table content
legacyTableCellsValues.forEach((row, i) => {
const template = legacyTemplates[i];
const { name, indexPatterns, order, ilmPolicy } = template;
const legacyIndexTemplate = legacyTemplates[i];
const { name, indexPatterns, ilmPolicy, template } = legacyIndexTemplate;

const hasContent = !!template.settings || !!template.mappings || !!template.aliases;
const ilmPolicyName = ilmPolicy && ilmPolicy.name ? ilmPolicy.name : '';
const orderFormatted = order ? order.toString() : order;

expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'',
name,
indexPatterns.join(', '),
ilmPolicyName,
orderFormatted,
'',
'',
'',
'',
]);

try {
expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'',
name,
indexPatterns.join(', '),
ilmPolicyName,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);
} catch (e) {
console.error(`Error in legacy template at row ${i}`); // eslint-disable-line no-console
throw e;
}
});
});

Expand Down Expand Up @@ -211,7 +217,7 @@ describe('Index Templates tab', () => {
await actions.clickTemplateAt(0);
expect(exists('templateList')).toBe(true);
expect(exists('templateDetails')).toBe(true);
expect(find('templateDetails.title').text()).toBe(templates[0].name);
expect(find('templateDetails.title').text().trim()).toBe(templates[0].name);

// Close flyout
await act(async () => {
Expand All @@ -223,7 +229,7 @@ describe('Index Templates tab', () => {

expect(exists('templateList')).toBe(true);
expect(exists('templateDetails')).toBe(true);
expect(find('templateDetails.title').text()).toBe(legacyTemplates[0].name);
expect(find('templateDetails.title').text().trim()).toBe(legacyTemplates[0].name);
});

describe('table row actions', () => {
Expand Down Expand Up @@ -460,7 +466,7 @@ describe('Index Templates tab', () => {
const { find } = testBed;
const [{ name }] = templates;

expect(find('templateDetails.title').text()).toEqual(name);
expect(find('templateDetails.title').text().trim()).toEqual(name);
});

it('should have a close button and be able to close flyout', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ describe.skip('<TemplateCreate />', () => {
aliases: ALIASES,
},
_kbnMeta: {
type: 'default',
isLegacy: false,
isManaged: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe.skip('<TemplateEdit />', () => {
aliases: ALIASES,
},
_kbnMeta: {
isManaged: false,
type: 'default',
isLegacy: templateToEdit._kbnMeta.isLegacy,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ import {
LegacyTemplateSerialized,
TemplateSerialized,
TemplateListItem,
TemplateType,
} from '../types';

const hasEntries = (data: object = {}) => Object.entries(data).length > 0;

export function serializeTemplate(templateDeserialized: TemplateDeserialized): TemplateSerialized {
const { version, priority, indexPatterns, template, composedOf, _meta } = templateDeserialized;
const {
version,
priority,
indexPatterns,
template,
composedOf,
dataStream,
_meta,
} = templateDeserialized;

return {
version,
priority,
template,
index_patterns: indexPatterns,
data_stream: dataStream,
composed_of: composedOf,
_meta,
};
Expand All @@ -41,6 +51,15 @@ export function deserializeTemplate(
} = templateEs;
const { settings } = template;

let type: TemplateType = 'default';
if (Boolean(cloudManagedTemplatePrefix && name.startsWith(cloudManagedTemplatePrefix))) {
type = 'cloudManaged';
} else if (name.startsWith('.')) {
type = 'system';
} else if (Boolean(_meta?.managed === true)) {
type = 'managed';
}

const deserializedTemplate: TemplateDeserialized = {
name,
version,
Expand All @@ -52,10 +71,7 @@ export function deserializeTemplate(
dataStream,
_meta,
_kbnMeta: {
isManaged: Boolean(_meta?.managed === true),
isCloudManaged: Boolean(
cloudManagedTemplatePrefix && name.startsWith(cloudManagedTemplatePrefix)
),
type,
hasDatastream: Boolean(dataStream),
},
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/index_management/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export const getTemplateParameter = (
) => {
return isLegacyTemplate(template)
? (template as LegacyTemplateSerialized)[setting]
: (template as TemplateSerialized).template[setting];
: (template as TemplateSerialized).template?.[setting];
};
18 changes: 9 additions & 9 deletions x-pack/plugins/index_management/common/types/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,24 @@ export interface TemplateDeserialized {
aliases?: Aliases;
mappings?: Mappings;
};
composedOf?: string[]; // Used on composable index template
composedOf?: string[]; // Composable template only
version?: number;
priority?: number;
order?: number; // Used on legacy index template
priority?: number; // Composable template only
order?: number; // Legacy template only
ilmPolicy?: {
name: string;
};
_meta?: { [key: string]: any };
dataStream?: { timestamp_field: string };
_meta?: { [key: string]: any }; // Composable template only
dataStream?: { timestamp_field: string }; // Composable template only
_kbnMeta: {
isManaged: boolean;
isCloudManaged: boolean;
type: TemplateType;
hasDatastream: boolean;
isLegacy?: boolean;
};
}

export type TemplateType = 'default' | 'managed' | 'cloudManaged' | 'system';

export interface TemplateFromEs {
name: string;
index_template: TemplateSerialized;
Expand All @@ -78,8 +79,7 @@ export interface TemplateListItem {
name: string;
};
_kbnMeta: {
isManaged: boolean;
isCloudManaged: boolean;
type: TemplateType;
hasDatastream: boolean;
isLegacy?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ describe('<ComponentTemplateCreate />', () => {
template: {
settings: SETTINGS,
mappings: {
_source: {},
_meta: {},
properties: {
[BOOLEAN_MAPPING_FIELD.name]: {
type: BOOLEAN_MAPPING_FIELD.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ describe('<ComponentTemplateEdit />', () => {
...COMPONENT_TEMPLATE_TO_EDIT,
template: {
...COMPONENT_TEMPLATE_TO_EDIT.template,
mappings: {
_meta: {},
_source: {},
properties: {},
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ interface Filters {
[key: string]: { name: string; checked: 'on' | 'off' };
}

/**
* Copied from https://stackoverflow.com/a/9310752
*/
function escapeRegExp(text: string) {
return text.replace(/[-\[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

function fuzzyMatch(searchValue: string, text: string) {
const pattern = `.*${searchValue.split('').join('.*')}.*`;
const pattern = `.*${searchValue.split('').map(escapeRegExp).join('.*')}.*`;
const regex = new RegExp(pattern);
return regex.test(text);
}
Expand All @@ -48,7 +55,7 @@ const i18nTexts = {
searchBoxPlaceholder: i18n.translate(
'xpack.idxMgmt.componentTemplatesSelector.searchBox.placeholder',
{
defaultMessage: 'Search components',
defaultMessage: 'Search component templates',
}
),
};
Expand Down Expand Up @@ -78,24 +85,33 @@ export const ComponentTemplates = ({ isLoading, components, listItemProps }: Pro
return [];
}

return components.filter((component) => {
if (filters.settings.checked === 'on' && !component.hasSettings) {
return false;
}
if (filters.mappings.checked === 'on' && !component.hasMappings) {
return false;
}
if (filters.aliases.checked === 'on' && !component.hasAliases) {
return false;
}

if (searchValue.trim() === '') {
return true;
}

const match = fuzzyMatch(searchValue, component.name);
return match;
});
return components
.filter((component) => {
if (filters.settings.checked === 'on' && !component.hasSettings) {
return false;
}
if (filters.mappings.checked === 'on' && !component.hasMappings) {
return false;
}
if (filters.aliases.checked === 'on' && !component.hasAliases) {
return false;
}

if (searchValue.trim() === '') {
return true;
}

const match = fuzzyMatch(searchValue, component.name);
return match;
})
.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
return 0;
});
}, [isLoading, components, searchValue, filters]);

const isSearchResultEmpty = filteredComponents.length === 0 && components.length > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@
font-weight: 600;
}
}

&__content {
mask-image: none;
}
}
}
Loading

0 comments on commit 8434daf

Please sign in to comment.