Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Component templates] Table view #68031

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion x-pack/plugins/index_management/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { PLUGIN, API_BASE_PATH, DEFAULT_INDEX_TEMPLATE_VERSION_FORMAT } from './constants';
export {
PLUGIN,
API_BASE_PATH,
DEFAULT_INDEX_TEMPLATE_VERSION_FORMAT,
BASE_PATH,
} from './constants';

export { getTemplateParameter } from './lib';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { deserializeComponentTemplate } from './component_template_serialization';

describe('deserializeComponentTemplate', () => {
test('deserializes a component template', () => {
expect(
deserializeComponentTemplate(
{
name: 'my_component_template',
component_template: {
version: 1,
_meta: {
serialization: {
id: 10,
class: 'MyComponentTemplate',
},
description: 'set number of shards to one',
},
template: {
settings: {
number_of_shards: 1,
},
mappings: {
_source: {
enabled: false,
},
properties: {
host_name: {
type: 'keyword',
},
created_at: {
type: 'date',
format: 'EEE MMM dd HH:mm:ss Z yyyy',
},
},
},
},
},
},
[
{
name: 'my_index_template',
index_template: {
index_patterns: ['foo'],
template: {
settings: {
number_of_replicas: 2,
},
},
composed_of: ['my_component_template'],
},
},
]
)
).toEqual({
name: 'my_component_template',
version: 1,
_meta: {
serialization: {
id: 10,
class: 'MyComponentTemplate',
},
description: 'set number of shards to one',
},
template: {
settings: {
number_of_shards: 1,
},
mappings: {
_source: {
enabled: false,
},
properties: {
host_name: {
type: 'keyword',
},
created_at: {
type: 'date',
format: 'EEE MMM dd HH:mm:ss Z yyyy',
},
},
},
},
_kbnMeta: {
usedBy: ['my_index_template'],
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import {
TemplateV2Es,
ComponentTemplateEs,
ComponentTemplateDeserialized,
ComponentTemplateListItem,
} from '../types';

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

const getAssociatedIndexTemplates = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the deserializeComponenTemplateList is called inside a map, and here we have 2 loops, that's a total of 3 nested loops. Depending on the size of the arrays this could be a performance issue.

It would be better to first (once) convert the array of index template to a flat map with this function

/**
 * Normalize a list of component templates to a map where each key
 * is a component template name, and the value is an array of index templates name using it
 *
 * @example
 *
 { 
   "comp-1": [ 
     "template-1", 
     "template-2" 
   ], 
   "comp2": [ 
     "template-1", 
     "template-2" 
   ] 
 } 
 *
 * @param indexTemplatesEs List of component templates
 */
export const indexTemplatesToUsedBy = (indexTemplatesEs: TemplateV2Es[]) => {
  return indexTemplatesEs.reduce((acc, item) => {
    if (item.index_template.composed_of) {
      item.index_template.composed_of.forEach((component) => {
        acc[component] = acc[component] ? [...acc[component], item.name] : [item.name];
      });
    }
    return acc;
  }, {} as { [key: string]: string[] });
};

then it is direct access to the index templates used by a component template

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. I went ahead and addressed this.

indexTemplates: TemplateV2Es[],
componentTemplateName: string
) => {
return indexTemplates
.filter(({ index_template: indexTemplate }) => {
return indexTemplate.composed_of?.includes(componentTemplateName);
})
.map(({ name }) => name);
};

export function deserializeComponentTemplate(
componentTemplateEs: ComponentTemplateEs,
indexTemplatesEs: TemplateV2Es[]
) {
const { name, component_template: componentTemplate } = componentTemplateEs;
const { template, _meta, version } = componentTemplate;

const deserializedComponentTemplate: ComponentTemplateDeserialized = {
name,
template,
version,
_meta,
_kbnMeta: {
usedBy: getAssociatedIndexTemplates(indexTemplatesEs, name),
},
};

return deserializedComponentTemplate;
}

export function deserializeComponenTemplateList(
componentTemplateEs: ComponentTemplateEs,
indexTemplatesEs: TemplateV2Es[]
) {
const { name, component_template: componentTemplate } = componentTemplateEs;
const { template } = componentTemplate;
const associatedTemplates = getAssociatedIndexTemplates(indexTemplatesEs, name);

const componentTemplateListItem: ComponentTemplateListItem = {
name,
usedBy: associatedTemplates,
hasSettings: hasEntries(template.settings),
hasMappings: hasEntries(template.mappings),
hasAliases: hasEntries(template.aliases),
};

return componentTemplateListItem;
}
5 changes: 5 additions & 0 deletions x-pack/plugins/index_management/common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export {
} from './template_serialization';

export { getTemplateParameter } from './utils';

export {
deserializeComponentTemplate,
deserializeComponenTemplateList,
} from './component_template_serialization';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { IndexSettings } from './indices';
import { Aliases } from './aliases';
import { Mappings } from './mappings';

export interface ComponentTemplateSerialized {
template: {
settings?: IndexSettings;
aliases?: Aliases;
mappings?: Mappings;
};
version?: number;
_meta?: { [key: string]: any };
}

export interface ComponentTemplateDeserialized extends ComponentTemplateSerialized {
name: string;
_kbnMeta: {
usedBy: string[];
};
}

export interface ComponentTemplateEs {
name: string;
component_template: ComponentTemplateSerialized;
}

export interface ComponentTemplateListItem {
name: string;
usedBy: string[];
hasMappings: boolean;
hasAliases: boolean;
hasSettings: boolean;
}
2 changes: 2 additions & 0 deletions x-pack/plugins/index_management/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export * from './indices';
export * from './mappings';

export * from './templates';

export * from './component_templates';
5 changes: 5 additions & 0 deletions x-pack/plugins/index_management/common/types/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export interface TemplateV2Serialized extends TemplateBaseSerialized {
composed_of?: string[];
}

export interface TemplateV2Es {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed all references to "V1" and "V2", can we rename this interface TemplateListItemES?

Also, there is no more "name" prop so it can be

index_template: TemplateSerialized

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I forgot to update this when pulling your changes.

name: string;
index_template: Omit<TemplateV2Serialized, 'name'>;
}

/**
* Interface for the template list in our UI table
* we don't include the mappings, settings and aliases
Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/index_management/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { useEffect } from 'react';
import { HashRouter, Switch, Route, Redirect } from 'react-router-dom';
import { BASE_PATH, UIM_APP_LOAD } from '../../common/constants';
import { IndexManagementHome } from './sections/home';
import { IndexManagementHome, Section, homeSections } from './sections/home';
import { TemplateCreate } from './sections/template_create';
import { TemplateClone } from './sections/template_clone';
import { TemplateEdit } from './sections/template_edit';
Expand All @@ -31,7 +31,10 @@ export const AppWithoutRouter = () => (
<Route exact path={`${BASE_PATH}create_template`} component={TemplateCreate} />
<Route exact path={`${BASE_PATH}clone_template/:name*`} component={TemplateClone} />
<Route exact path={`${BASE_PATH}edit_template/:name*`} component={TemplateEdit} />
<Route path={`${BASE_PATH}:section(indices|templates)`} component={IndexManagementHome} />
<Redirect from={`${BASE_PATH}`} to={`${BASE_PATH}indices`} />
<Route
path={`${BASE_PATH}:section(${homeSections.join('|')})`}
component={IndexManagementHome}
/>
<Redirect from={`${BASE_PATH}`} to={`${BASE_PATH}${Section.Indices}`} />
</Switch>
);
Loading