From 2dbda626b9646206dc343b01295e7440a0aed50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20K=C3=B6nig?= Date: Sun, 14 Jul 2024 18:11:16 +0200 Subject: [PATCH] improve index templates, fixes #242 --- CHANGELOG.md | 1 + .../indextemplates/IndexTemplateRow.vue | 17 +++-- .../indextemplates/IndexTemplates.vue | 8 +- .../indextemplates/IndexTemplatesTable.vue | 57 +++++++++++---- src/components/indices/IndicesTable.vue | 2 +- .../indextemplates/IndexTemplates.ts | 73 +++++++++++++++++++ .../indextemplates/IndexTemplatesTable.ts | 35 ++++----- src/services/ElasticsearchAdapter.ts | 10 ++- src/store/index_templates.ts | 31 ++++++++ src/store/resize.ts | 2 + 10 files changed, 188 insertions(+), 48 deletions(-) create mode 100644 src/composables/components/indextemplates/IndexTemplates.ts create mode 100644 src/store/index_templates.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 251e3360..06d32179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 1.0.9 +* improve index templates, fixes [#242](https://github.com/cars10/elasticvue/issues/242) * dependency updates ## 1.0.8 diff --git a/src/components/indextemplates/IndexTemplateRow.vue b/src/components/indextemplates/IndexTemplateRow.vue index 09d122c0..f057274b 100644 --- a/src/components/indextemplates/IndexTemplateRow.vue +++ b/src/components/indextemplates/IndexTemplateRow.vue @@ -4,16 +4,14 @@ {{ row.name }} - {{ row.index_patterns }} - {{ row.order }} - {{ row.version }} + {{ row.index_patterns || row.template || row.index_template?.index_patterns }}
- +
@@ -21,12 +19,17 @@ \ No newline at end of file diff --git a/src/components/indextemplates/IndexTemplates.vue b/src/components/indextemplates/IndexTemplates.vue index 9151f800..c2439be6 100644 --- a/src/components/indextemplates/IndexTemplates.vue +++ b/src/components/indextemplates/IndexTemplates.vue @@ -10,7 +10,7 @@ - + @@ -20,12 +20,12 @@ import ReloadButton from '../shared/ReloadButton.vue' import LoaderStatus from '../shared/LoaderStatus.vue' import IndexTemplatesTable from './IndexTemplatesTable.vue' - import { useElasticsearchRequest } from '../../composables/CallElasticsearch' import { useTranslation } from '../../composables/i18n.ts' - import { EsIndexTemplates } from '../../composables/components/indextemplates/IndexTemplatesTable.ts' + import { useIndexTemplates } from '../../composables/components/indextemplates/IndexTemplates.ts' const t = useTranslation() - const { requestState, data, load } = useElasticsearchRequest('catIndexTemplates') + const { data, requestState, load } = useIndexTemplates() + onMounted(load) diff --git a/src/components/indextemplates/IndexTemplatesTable.vue b/src/components/indextemplates/IndexTemplatesTable.vue index 3b954fd6..82938265 100644 --- a/src/components/indextemplates/IndexTemplatesTable.vue +++ b/src/components/indextemplates/IndexTemplatesTable.vue @@ -1,21 +1,43 @@ diff --git a/src/components/indices/IndicesTable.vue b/src/components/indices/IndicesTable.vue index ca004b5e..4e042d50 100644 --- a/src/components/indices/IndicesTable.vue +++ b/src/components/indices/IndicesTable.vue @@ -2,7 +2,7 @@
- {{ t('index_templates.heading') }} diff --git a/src/composables/components/indextemplates/IndexTemplates.ts b/src/composables/components/indextemplates/IndexTemplates.ts new file mode 100644 index 00000000..16efbeee --- /dev/null +++ b/src/composables/components/indextemplates/IndexTemplates.ts @@ -0,0 +1,73 @@ +import { useElasticsearchAdapter } from '../../CallElasticsearch.ts' +import { ref, Ref } from 'vue' +import { useConnectionStore } from '../../../store/connection.ts' + +export type GenericIndexTemplate = { + name: string, + order?: string, + version?: string, + priority?: string, + template?: string, + index_patterns?: string[], + indexPatterns?: string, + index_template?: { + index_patterns?: string[], + } +} + +type IndexTemplates = { + index_templates?: Record, + component_templates?: Record +} | Record + +export const useIndexTemplates = () => { + const { requestState, callElasticsearch } = useElasticsearchAdapter() + const data: Ref = ref(null) + const connectionStore = useConnectionStore() + + const load = async () => { + if (!connectionStore.activeCluster) return + const majorVersion = parseInt(connectionStore.activeCluster.majorVersion) + const method = templateEndpoint(majorVersion) + if (!method) return + + return callElasticsearch(method) + .then(body => (data.value = enrich(body))) + .catch(() => (data.value = null)) + } + + return { + data, + requestState, + load + } +} + +const enrich = (data: IndexTemplates) => { + const templates = data.index_templates || data.component_templates || data + const results: GenericIndexTemplate[] = [] + Object.entries(templates).map(([name, template]) => { + const indexPatterns = template.index_patterns?.join('') || template.index_template?.index_patterns?.join('') || template.component_template?.index_patterns?.join('') || template.template + results.push({ + name, + indexPatterns, + ...template, + }) + }) + return results +} + +const templateEndpoint = (majorVersion: number) => { + switch (majorVersion) { + case 5: + return 'templates' + case 6: + return 'templates' + case 7: + return 'templates' + case 8: + return 'indexTemplates' + default: + return null + } +} \ No newline at end of file diff --git a/src/composables/components/indextemplates/IndexTemplatesTable.ts b/src/composables/components/indextemplates/IndexTemplatesTable.ts index c1cdb369..25219764 100644 --- a/src/composables/components/indextemplates/IndexTemplatesTable.ts +++ b/src/composables/components/indextemplates/IndexTemplatesTable.ts @@ -1,45 +1,36 @@ import { useTranslation } from '../../i18n' -import { computed, ref } from 'vue' +import { computed } from 'vue' import { genColumns } from '../../../helpers/tableColumns' import { filterItems } from '../../../helpers/filters.ts' - -export type EsIndexTemplate = { - name: string, - index_patterns: string[], - order: string, - version: string, -} - -export type EsIndexTemplates = Record +import { GenericIndexTemplate } from './IndexTemplates.ts' +import { useIndexTemplatesStore } from '../../../store/index_templates.ts' +import { DEFAULT_HIDE_INDICES_REGEX } from '../../../consts.ts' export type IndexTemplatesTableProps = { - indexTemplates: EsIndexTemplates + indexTemplates: GenericIndexTemplate[] } export const useIndexTemplatesTable = (props: IndexTemplatesTableProps) => { const t = useTranslation() - - const filter = ref('') - const enrichedIndexTemplates = computed(() => { - return Object.entries(props.indexTemplates).map(([name, template]) => { - return Object.assign({}, { name }, { indexPatterns: template.index_patterns?.join('') }, template) - }) - }) + const indexTemplatesStore = useIndexTemplatesStore() const filteredItems = computed(() => { - return filterItems(enrichedIndexTemplates.value, filter.value, ['name', 'indexPatterns']) + let results = props.indexTemplates + if (results.length === 0) return [] + + if (!indexTemplatesStore.showHiddenIndices) { + results = results.filter((item: any) => !item.name.match(new RegExp(DEFAULT_HIDE_INDICES_REGEX))) + } + return filterItems(results, indexTemplatesStore.filter, ['name', 'indexPatterns']) }) const columns = genColumns([ { label: '' }, { label: t('index_templates.index_templates_table.table.headers.name'), field: 'name', }, { label: t('index_templates.index_templates_table.table.headers.index_patterns') }, - { label: t('index_templates.index_templates_table.table.headers.priority') }, - { label: t('index_templates.index_templates_table.table.headers.version') }, ]) return { - filter, filteredItems, columns } diff --git a/src/services/ElasticsearchAdapter.ts b/src/services/ElasticsearchAdapter.ts index 7ebfa1de..526ed21c 100644 --- a/src/services/ElasticsearchAdapter.ts +++ b/src/services/ElasticsearchAdapter.ts @@ -72,10 +72,18 @@ export default class ElasticsearchAdapter { return this.request(`_cat/indices/${query}`, 'GET', params) } - catIndexTemplates () { + templates () { return this.request('_template', 'GET') } + indexTemplates () { + return this.request('_index_template', 'GET') + } + + componentTemplates () { + return this.request('_component_template', 'GET') + } + catShards (params: object, filter?: string) { const query = filter ? `${filter}*` : '' return this.request(`_cat/shards/${query}`, 'GET', params) diff --git a/src/store/index_templates.ts b/src/store/index_templates.ts new file mode 100644 index 00000000..e682e8ba --- /dev/null +++ b/src/store/index_templates.ts @@ -0,0 +1,31 @@ +import { defineStore } from 'pinia' + +type IndexTemplatesState = { + filter: string, + showHiddenIndices: boolean, + stickyTableHeader: boolean, + pagination: any, +} + +export const useIndexTemplatesStore = defineStore('indexTemplates', { + state: (): IndexTemplatesState => ({ + filter: '', + showHiddenIndices: false, + stickyTableHeader: false, + pagination: { + sortBy: 'index', + descending: false, + rowsPerPage: 10 + }, + }), + persist: { + paths: [ + 'filter', + 'showHiddenIndices', + 'stickyTableHeader', + 'pagination.sortBy', + 'pagination.descending', + 'pagination.rowsPerPage', + ] + } +}) diff --git a/src/store/resize.ts b/src/store/resize.ts index e780b3e1..795e6715 100644 --- a/src/store/resize.ts +++ b/src/store/resize.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' type ResizeState = { indicesTable: number, + indexTemplatesTable: number, modalLoaderCodeViewer: number, restForm: number, searchQuery: number, @@ -12,6 +13,7 @@ type ResizeState = { export const useResizeStore = defineStore('resize', { state: (): ResizeState => ({ indicesTable: 500, + indexTemplatesTable: 500, modalLoaderCodeViewer: 600, restForm: 400, searchQuery: 400,