Skip to content

Commit 35c542b

Browse files
authored
feat: hide system components when switch enabled (#197)
feat: hide system components when switch enabled Refs: #182 Signed-off-by: seven <[email protected]>
1 parent 810b3ad commit 35c542b

File tree

3 files changed

+78
-66
lines changed

3 files changed

+78
-66
lines changed

src/datasources/esApi.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ export type ClusterIndex = {
190190
};
191191
};
192192

193+
export enum TemplateType {
194+
INDEX_TEMPLATE = 'INDEX_TEMPLATE',
195+
COMPONENT_TEMPLATE = 'COMPONENT_TEMPLATE',
196+
}
197+
198+
type ComponentTemplate = {
199+
name: string;
200+
type: TemplateType;
201+
version: number | null;
202+
alias_count: number | null;
203+
mapping_count: number | null;
204+
settings_count: number | null;
205+
metadata_count: number | null;
206+
included_in: Array<string>;
207+
};
208+
type IndexTemplate = {
209+
name: string;
210+
type: TemplateType;
211+
index_patterns: Array<string>;
212+
order: number | null;
213+
version: number | null;
214+
composed_of: Array<string>;
215+
};
216+
217+
export type ClusterTemplate = ComponentTemplate | IndexTemplate;
218+
193219
interface ESApi {
194220
createIndex(
195221
connection: ElasticsearchConnection,
@@ -260,6 +286,8 @@ interface ESApi {
260286
catShards(
261287
connection: ElasticsearchConnection,
262288
): Promise<Array<{ index: string; shards: Array<ClusterShard> }>>;
289+
290+
catTemplates(connection: ElasticsearchConnection): Promise<Array<ClusterTemplate>>;
263291
}
264292

265293
const esApi: ESApi = {
@@ -706,6 +734,48 @@ const esApi: ESApi = {
706734
);
707735
}
708736
},
737+
738+
catTemplates: async connection => {
739+
const client = loadHttpClient(connection);
740+
const fetchIndexTemplates = async () => {
741+
const data = (await client.get('/_cat/templates', 'format=json')) as Array<{
742+
[key: string]: string;
743+
}>;
744+
return data.map((template: { [key: string]: string }) => ({
745+
name: template.name,
746+
type: TemplateType.INDEX_TEMPLATE,
747+
order: optionalToNullableInt(template.order),
748+
version: optionalToNullableInt(template.version),
749+
index_patterns: template.index_patterns.slice(1, -1).split(',').filter(Boolean),
750+
composed_of: template.composed_of.slice(1, -1).split(',').filter(Boolean),
751+
}));
752+
};
753+
const fetchComponentTemplates = async () => {
754+
const data = (await client.get('/_component_template', 'format=json')) as {
755+
component_templates: Array<{
756+
[key: string]: string;
757+
}>;
758+
};
759+
760+
return data?.component_templates.map((template: { [key: string]: string }) => ({
761+
name: template.name,
762+
type: TemplateType.COMPONENT_TEMPLATE,
763+
version: optionalToNullableInt(template.version),
764+
alias_count: optionalToNullableInt(template.alias_count),
765+
mapping_count: optionalToNullableInt(template.mapping_count),
766+
settings_count: optionalToNullableInt(template.settings_count),
767+
metadata_count: optionalToNullableInt(template.metadata_count),
768+
included_in: template.included_in?.slice(1, -1).split(',').filter(Boolean),
769+
}));
770+
};
771+
772+
const [indexTemplates, componentTemplates] = await Promise.all([
773+
fetchIndexTemplates(),
774+
fetchComponentTemplates(),
775+
]);
776+
777+
return [...indexTemplates, ...componentTemplates];
778+
},
709779
};
710780

711781
export { esApi };

src/store/clusterManageStore.ts

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,13 @@ import {
44
ClusterIndex,
55
ClusterNode,
66
ClusterShard,
7+
ClusterTemplate,
78
esApi,
89
loadHttpClient,
910
} from '../datasources';
1011
import { lang } from '../lang';
1112
import { Connection, DatabaseType } from './connectionStore.ts';
12-
import { CustomError, debug, optionalToNullableInt } from '../common';
13-
14-
export enum TemplateType {
15-
INDEX_TEMPLATE = 'INDEX_TEMPLATE',
16-
COMPONENT_TEMPLATE = 'COMPONENT_TEMPLATE',
17-
}
18-
19-
type ComponentTemplate = {
20-
name: string;
21-
type: TemplateType;
22-
version: number | null;
23-
alias_count: number | null;
24-
mapping_count: number | null;
25-
settings_count: number | null;
26-
metadata_count: number | null;
27-
included_in: Array<string>;
28-
};
29-
type IndexTemplate = {
30-
name: string;
31-
type: TemplateType;
32-
index_patterns: Array<string>;
33-
order: number | null;
34-
version: number | null;
35-
composed_of: Array<string>;
36-
};
13+
import { CustomError, debug } from '../common';
3714

3815
export type RawClusterStats = {
3916
cluster_name: string;
@@ -63,8 +40,6 @@ export type RawClusterStats = {
6340
};
6441
};
6542

66-
export type ClusterTemplate = ComponentTemplate | IndexTemplate;
67-
6843
export const useClusterManageStore = defineStore('clusterManageStore', {
6944
state: (): {
7045
connection: Connection | undefined;
@@ -201,45 +176,11 @@ export const useClusterManageStore = defineStore('clusterManageStore', {
201176
async fetchTemplates() {
202177
if (!this.connection) throw new Error(lang.global.t('connection.selectConnection'));
203178
if (this.connection.type === DatabaseType.ELASTICSEARCH) {
204-
const client = loadHttpClient(this.connection);
205-
const fetchIndexTemplates = async () => {
206-
const data = (await client.get('/_cat/templates', 'format=json')) as Array<{
207-
[key: string]: string;
208-
}>;
209-
return data.map((template: { [key: string]: string }) => ({
210-
name: template.name,
211-
type: TemplateType.INDEX_TEMPLATE,
212-
order: optionalToNullableInt(template.order),
213-
version: optionalToNullableInt(template.version),
214-
index_patterns: template.index_patterns.slice(1, -1).split(',').filter(Boolean),
215-
composed_of: template.composed_of.slice(1, -1).split(',').filter(Boolean),
216-
}));
217-
};
218-
const fetchComponentTemplates = async () => {
219-
const data = (await client.get('/_component_template', 'format=json')) as {
220-
component_templates: Array<{
221-
[key: string]: string;
222-
}>;
223-
};
224-
225-
return data?.component_templates.map((template: { [key: string]: string }) => ({
226-
name: template.name,
227-
type: TemplateType.COMPONENT_TEMPLATE,
228-
version: optionalToNullableInt(template.version),
229-
alias_count: optionalToNullableInt(template.alias_count),
230-
mapping_count: optionalToNullableInt(template.mapping_count),
231-
settings_count: optionalToNullableInt(template.settings_count),
232-
metadata_count: optionalToNullableInt(template.metadata_count),
233-
included_in: template.included_in?.slice(1, -1).split(',').filter(Boolean),
234-
}));
235-
};
179+
const templates = await esApi.catTemplates(this.connection);
236180

237-
const [indexTemplates, componentTemplates] = await Promise.all([
238-
fetchIndexTemplates(),
239-
fetchComponentTemplates(),
240-
]);
241-
242-
this.templates = [...indexTemplates, ...componentTemplates];
181+
this.templates = templates.filter(template =>
182+
this.hideSystemIndices ? !template.name.startsWith('.') : true,
183+
);
243184
} else {
244185
this.templates = [];
245186
}

src/views/manage/components/template-dialog.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@
142142
import { FormRules, FormValidationError, NButton, NIcon, FormItemRule } from 'naive-ui';
143143
import { Close } from '@vicons/carbon';
144144
import { CustomError, inputProps, jsonify } from '../../../common';
145-
import { TemplateType, useClusterManageStore } from '../../../store';
145+
import { useClusterManageStore } from '../../../store';
146146
import { useLang } from '../../../lang';
147+
import { TemplateType } from '../../../datasources';
147148
148149
const clusterManageStore = useClusterManageStore();
149150
const { createTemplate } = clusterManageStore;

0 commit comments

Comments
 (0)