-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Changes from 11 commits
2335451
f58b39e
808435a
78f939b
bcfef27
7010b2f
098fd8d
9bd51b8
d078443
d5a293e
ee7293d
f8dd123
7d59c8c
fcf38d4
6378956
07aeb9b
32616c4
d0fc261
87ca2e9
906789e
5ddb122
8d2222e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = ( | ||
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; | ||
} |
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,11 @@ export interface TemplateV2Serialized extends TemplateBaseSerialized { | |
composed_of?: string[]; | ||
} | ||
|
||
export interface TemplateV2Es { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also, there is no more "name" prop so it can be index_template: TemplateSerialized There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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
then it is direct access to the index templates used by a component template
There was a problem hiding this comment.
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.