-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathRollupIndices.tsx
284 lines (267 loc) · 9.74 KB
/
RollupIndices.tsx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { Component, Fragment } from "react";
import {
EuiSpacer,
EuiCompressedFormRow,
EuiCallOut,
EuiText,
EuiLink,
EuiFlexGroup,
EuiHorizontalRule,
EuiPanel,
EuiTitle,
} from "@elastic/eui";
import { EuiComboBoxOptionOption } from "@elastic/eui/src/components/combo_box/types";
import _ from "lodash";
import EuiCompressedComboBox from "../../../../components/ComboBoxWithoutWarning";
import { IndexItem } from "../../../../../models/interfaces";
import IndexService from "../../../../services/IndexService";
import { CoreServicesContext } from "../../../../components/core_services";
import { wildcardOption } from "../../../../utils/helpers";
import AdvancedSettings from "../../../../components/AdvancedSettings";
import flat from "flat";
import { INDEX_SETTINGS_URL } from "../../../../utils/constants";
interface RollupIndicesProps {
indexService: IndexService;
sourceIndex: { label: string; value?: IndexItem }[];
sourceIndexError: string;
targetIndex: { label: string; value?: IndexItem }[];
targetIndexError: string;
targetIndexSettings: Pick<IndexItem, "settings"> | null;
targetIndexSettingsError: string;
onChangeSourceIndex: (options: EuiComboBoxOptionOption<IndexItem>[]) => void;
onChangeTargetIndex: (options: EuiComboBoxOptionOption<IndexItem>[]) => void;
onChangeTargetIndexSettings: (settings: Pick<IndexItem, "settings"> | null) => void;
hasAggregation: boolean;
}
interface RollupIndicesState {
isLoading: boolean;
indexOptions: { label: string; value?: IndexItem }[];
targetIndexOptions: { label: string; value?: IndexItem }[];
}
export const ROLLUP_RESULTS_HELP_TEXT_LINK = "https://opensearch.org/docs/latest/im-plugin/index-rollups/index/#step-1-set-up-indices";
export default class RollupIndices extends Component<RollupIndicesProps, RollupIndicesState> {
static contextType = CoreServicesContext;
_isMount: boolean;
constructor(props: RollupIndicesProps) {
super(props);
this.state = {
isLoading: true,
indexOptions: [],
targetIndexOptions: [],
};
this._isMount = true;
this.onIndexSearchChange = _.debounce(this.onIndexSearchChange, 500, { leading: true });
}
async componentDidMount(): Promise<void> {
await this.onIndexSearchChange("");
}
componentWillUnmount(): void {
this._isMount = false;
}
onIndexSearchChange = async (searchValue: string): Promise<void> => {
if (!this._isMount) {
return;
}
const { indexService } = this.props;
this.setState({ isLoading: true, indexOptions: [] });
try {
const dataStreamsAndIndicesNamesResponse = await indexService.getDataStreamsAndIndicesNames(searchValue);
if (dataStreamsAndIndicesNamesResponse.ok) {
// Adding wildcard to search value
const options = searchValue.trim() ? [{ label: wildcardOption(searchValue) }] : [];
const dataStreams = dataStreamsAndIndicesNamesResponse.response.dataStreams.map((label) => ({ label }));
const indices = dataStreamsAndIndicesNamesResponse.response.indices.map((label) => ({ label }));
if (this._isMount) {
this.setState({ indexOptions: options.concat(dataStreams, indices), targetIndexOptions: indices });
}
} else {
if (dataStreamsAndIndicesNamesResponse.error.startsWith("[index_not_found_exception]")) {
this.context.notifications.toasts.addDanger("No index available");
} else {
this.context.notifications.toasts.addDanger(dataStreamsAndIndicesNamesResponse.error);
}
}
} catch (err) {
this.context.notifications.toasts.addDanger(err.message);
}
if (this._isMount) {
this.setState({ isLoading: false });
}
};
onCreateOption = (searchValue: string, flattenedOptions: { label: string; value?: IndexItem }[]): void => {
const { targetIndexOptions } = this.state;
const { onChangeTargetIndex } = this.props;
const normalizedSearchValue = searchValue.trim();
if (!normalizedSearchValue) {
return;
}
const newOption = {
label: searchValue,
};
// Create the option if it doesn't exist.
if (flattenedOptions.findIndex((option) => option.label.trim() === normalizedSearchValue) === -1) {
targetIndexOptions.concat(newOption);
this.setState({ targetIndexOptions: targetIndexOptions });
}
onChangeTargetIndex([newOption]);
};
render() {
const {
sourceIndex,
sourceIndexError,
targetIndex,
targetIndexError,
targetIndexSettings,
targetIndexSettingsError,
onChangeSourceIndex,
onChangeTargetIndex,
onChangeTargetIndexSettings,
hasAggregation,
} = this.props;
const { isLoading, indexOptions, targetIndexOptions } = this.state;
return (
<EuiPanel>
<EuiFlexGroup gutterSize="xs" alignItems="center">
<EuiTitle size="s">
<h2>Indices</h2>
</EuiTitle>
</EuiFlexGroup>
<EuiHorizontalRule margin={"xs"} />
<EuiSpacer size="s" />
<EuiCallOut color="warning">
<EuiText size="s">
<p>You can't change indices after creating a job. Double-check the source and target index names before proceeding.</p>
</EuiText>
</EuiCallOut>
{hasAggregation && (
<Fragment>
<EuiSpacer />
<EuiCallOut color="warning">
<p>Note: changing source index will erase all existing definitions about aggregations and metrics.</p>
</EuiCallOut>
</Fragment>
)}
<EuiSpacer size="m" />
<EuiCompressedFormRow
label={
<EuiText size="s">
<h3>Source index</h3>
</EuiText>
}
error={sourceIndexError}
isInvalid={sourceIndexError != ""}
helpText="The index pattern on which to performed the rollup job. You can use * as a wildcard."
>
<EuiCompressedComboBox
placeholder="Select source index"
options={indexOptions}
selectedOptions={sourceIndex}
onChange={onChangeSourceIndex}
singleSelection={{ asPlainText: true }}
onSearchChange={this.onIndexSearchChange}
isLoading={isLoading}
isInvalid={sourceIndexError != ""}
data-test-subj="sourceIndexCombobox"
/>
</EuiCompressedFormRow>
<EuiCompressedFormRow
label={
<EuiText size="s">
<h3>Target index</h3>
</EuiText>
}
error={targetIndexError}
isInvalid={targetIndexError != ""}
helpText={
<EuiText size={"xs"}>
{
"The target index stores rollup results. You can select an existing index or type in a new index name with embedded variables "
}
{
<EuiLink external href={ROLLUP_RESULTS_HELP_TEXT_LINK} target={"_blank"} rel="noopener noreferrer">
Learn more
</EuiLink>
}
</EuiText>
}
>
<EuiCompressedComboBox
placeholder="Select or create target index"
options={targetIndexOptions}
selectedOptions={targetIndex}
onChange={onChangeTargetIndex}
onCreateOption={this.onCreateOption}
singleSelection={{ asPlainText: true }}
onSearchChange={this.onIndexSearchChange}
isLoading={isLoading}
isInvalid={targetIndexError != ""}
data-test-subj="targetIndexCombobox"
/>
</EuiCompressedFormRow>
<EuiCompressedFormRow
error={targetIndexSettingsError}
isInvalid={targetIndexSettingsError != ""}
helpText={
<EuiText size={"xs"}>
{"Optional. The target index settings will be apply only if target index will be created during the rollup."}
{
<EuiLink external href={ROLLUP_RESULTS_HELP_TEXT_LINK} target={"_blank"} rel="noopener noreferrer">
Learn more
</EuiLink>
}
</EuiText>
}
>
<AdvancedSettings
value={targetIndexSettings || {}}
onChange={(val) => {
if (Object.keys(val).length === 0) {
onChangeTargetIndexSettings(null);
} else {
onChangeTargetIndexSettings(val);
}
}}
accordionProps={{
initialIsOpen: false,
id: "accordionForCreateRollupTargetIndexSettings",
buttonContent: <h3>Target index settings</h3>,
}}
editorProps={{
disabled: false,
width: "100%",
formatValue: flat,
}}
rowProps={{
fullWidth: true,
label: "Specify advanced index settings",
helpText: (
<>
<p>
Specify a comma-delimited list of settings.{" "}
<EuiLink href={INDEX_SETTINGS_URL} target="_blank" external>
View index settings
</EuiLink>
</p>
<p>
All the settings will be handled in flat structure.{" "}
<EuiLink
href="https://opensearch.org/docs/latest/api-reference/index-apis/get-index/#query-parameters"
external
target="_blank"
>
Learn more
</EuiLink>
</p>
</>
),
}}
/>
</EuiCompressedFormRow>
</EuiPanel>
);
}
}