-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
fetch_indices.ts
157 lines (139 loc) · 5.2 KB
/
fetch_indices.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { ByteSizeValue } from '@kbn/config-schema';
import { IScopedClusterClient } from '@kbn/core/server';
import { IndexDataEnricher } from '../services';
import { Index } from '..';
import { RouteDependencies } from '../types';
import type { MeteringStats } from './types';
interface MeteringStatsResponse {
indices: MeteringStats[];
}
async function fetchIndicesCall(
client: IScopedClusterClient,
config: RouteDependencies['config'],
indexNames?: string[]
): Promise<Index[]> {
const indexNamesString = indexNames && indexNames.length ? indexNames.join(',') : '*';
// This call retrieves alias and settings (incl. hidden status) information about indices
const indices = await client.asCurrentUser.indices.get({
index: indexNamesString,
expand_wildcards: ['hidden', 'all'],
// only get specified index properties from ES to keep the response under 536MB
// node.js string length limit: https://github.com/nodejs/node/issues/33960
filter_path: [
'*.aliases',
'*.settings.index.number_of_shards',
'*.settings.index.number_of_replicas',
'*.settings.index.frozen',
'*.settings.index.hidden',
'*.data_stream',
],
// for better performance only compute aliases and settings of indices but not mappings
features: ['aliases', 'settings'],
});
if (!Object.keys(indices).length) {
return [];
}
const indicesNames = Object.keys(indices);
if (config.isIndexStatsEnabled) {
const { indices: indicesStats } = await client.asCurrentUser.indices.stats({
index: indexNamesString,
expand_wildcards: ['hidden', 'all'],
forbid_closed_indices: false,
metric: ['docs', 'store'],
});
return indicesNames.map((indexName: string) => {
const indexData = indices[indexName];
const aliases = Object.keys(indexData.aliases!);
const baseResponse = {
name: indexName,
primary: indexData.settings?.index?.number_of_shards,
replica: indexData.settings?.index?.number_of_replicas,
isFrozen: indexData.settings?.index?.frozen === 'true',
aliases: aliases.length ? aliases : 'none',
hidden: indexData.settings?.index?.hidden === 'true',
data_stream: indexData.data_stream,
};
if (indicesStats) {
const indexStats = indicesStats[indexName];
return {
...baseResponse,
health: indexStats?.health,
status: indexStats?.status,
uuid: indexStats?.uuid,
documents: indexStats?.primaries?.docs?.count ?? 0,
documents_deleted: indexStats?.primaries?.docs?.deleted ?? 0,
size: new ByteSizeValue(indexStats?.total?.store?.size_in_bytes ?? 0).toString(),
primary_size: new ByteSizeValue(
indexStats?.primaries?.store?.size_in_bytes ?? 0
).toString(),
};
}
return baseResponse;
});
}
// uses the _metering/stats API to get the number of documents and size of the index
// this API is only available in ES3
if (config.isSizeAndDocCountEnabled) {
const { indices: indicesStats } =
await client.asSecondaryAuthUser.transport.request<MeteringStatsResponse>({
method: 'GET',
path: `/_metering/stats/` + indexNamesString,
});
return indicesNames.map((indexName: string) => {
const indexData = indices[indexName];
const aliases = Object.keys(indexData.aliases!);
const baseResponse = {
name: indexName,
isFrozen: false,
aliases: aliases.length ? aliases : 'none',
hidden: indexData.settings?.index?.hidden === 'true',
data_stream: indexData.data_stream,
};
if (indicesStats) {
const indexStats = indicesStats.find((index) => index.name === indexName);
return {
...baseResponse,
documents: indexStats?.num_docs ?? 0,
size: new ByteSizeValue(indexStats?.size_in_bytes ?? 0).toString(),
};
}
return baseResponse;
});
}
// if neither index stats (Stateful only API)
// nor size and doc count are enabled (ES3 only API)
// return the base response
return indicesNames.map((indexName: string) => {
const indexData = indices[indexName];
const aliases = Object.keys(indexData.aliases!);
return {
name: indexName,
primary: indexData.settings?.index?.number_of_shards,
replica: indexData.settings?.index?.number_of_replicas,
isFrozen: indexData.settings?.index?.frozen === 'true',
aliases: aliases.length ? aliases : 'none',
hidden: indexData.settings?.index?.hidden === 'true',
data_stream: indexData.data_stream,
};
});
}
export const fetchIndices = async ({
client,
indexDataEnricher,
config,
indexNames,
}: {
client: IScopedClusterClient;
indexDataEnricher: IndexDataEnricher;
config: RouteDependencies['config'];
indexNames?: string[];
}) => {
const indices = await fetchIndicesCall(client, config, indexNames);
return await indexDataEnricher.enrichIndices(indices, client);
};