-
Notifications
You must be signed in to change notification settings - Fork 8k
/
filter_bar.ts
377 lines (325 loc) · 12.1 KB
/
filter_bar.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { $Values } from '@kbn/utility-types';
import { FtrService } from '../ftr_provider_context';
export const Operation = {
IS: 'is',
IS_NOT: 'is not',
IS_ONE_OF: 'is one of',
IS_NOT_ONE_OF: 'is not one of',
IS_BETWEEN: 'is between',
IS_NOT_BETWEEN: 'is not between',
EXISTS: 'exists',
DOES_NOT_EXIST: 'does not exist',
} as const;
export const BooleanRelation = {
AND: 'AND',
OR: 'OR',
} as const;
type BooleanRelation = $Values<typeof BooleanRelation>;
interface BasicFilter {
field: string;
}
interface FilterWithMultipleValues extends BasicFilter {
operation: typeof Operation.IS_ONE_OF | typeof Operation.IS_NOT_ONE_OF;
value: string[] | string;
}
interface FilterWithRange extends BasicFilter {
operation: typeof Operation.IS_BETWEEN | typeof Operation.IS_NOT_BETWEEN;
value: { from: string | undefined; to: string | undefined };
}
interface FilterWithSingleValue extends BasicFilter {
operation: typeof Operation.IS | typeof Operation.IS_NOT;
value: string;
}
interface FilterWithoutValue extends BasicFilter {
operation: typeof Operation.EXISTS | typeof Operation.DOES_NOT_EXIST;
}
type FilterLeaf =
| FilterWithoutValue
| FilterWithSingleValue
| FilterWithMultipleValues
| FilterWithRange;
interface FilterNode {
condition: BooleanRelation;
filters: Array<FilterLeaf | FilterNode>;
}
type Filter = FilterLeaf | FilterNode;
export class FilterBarService extends FtrService {
private readonly comboBox = this.ctx.getService('comboBox');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly common = this.ctx.getPageObject('common');
private readonly header = this.ctx.getPageObject('header');
private readonly retry = this.ctx.getService('retry');
private readonly config = this.ctx.getService('config');
private readonly defaultTryTimeout = this.config.get('timeouts.try');
private readonly browser = this.ctx.getService('browser');
/**
* Checks if specified filter exists
*
* @param key field name
* @param value filter value
* @param enabled filter status
* @param pinned filter pinned status
* @param negated filter including or excluding value
*/
public async hasFilter(
key: string,
value: string,
enabled: boolean = true,
pinned: boolean = false,
negated: boolean = false
): Promise<boolean> {
const filterActivationState = enabled ? 'enabled' : 'disabled';
const filterPinnedState = pinned ? 'pinned' : 'unpinned';
const filterNegatedState = negated ? '~filter-negated' : '';
const dataSubj = [
'~filter',
`~filter-${filterActivationState}`,
key !== '' && `~filter-key-${key}`,
value !== '' && `~filter-value-${value}`,
`~filter-${filterPinnedState}`,
filterNegatedState,
]
.filter(Boolean)
.join(' & ');
return this.testSubjects.exists(dataSubj, { allowHidden: true });
}
public async hasFilterWithId(
id: string,
enabled: boolean = true,
pinned: boolean = false,
negated: boolean = false
): Promise<boolean> {
const filterActivationState = enabled ? 'enabled' : 'disabled';
const filterPinnedState = pinned ? 'pinned' : 'unpinned';
const filterNegatedState = negated ? '~filter-negated' : '';
const dataSubj = [
'~filter',
`~filter-${filterActivationState}`,
`~filter-${filterPinnedState}`,
filterNegatedState,
`~filter-id-${id}`,
]
.filter(Boolean)
.join(' & ');
return this.testSubjects.exists(dataSubj, { allowHidden: true });
}
/**
* Removes specified filter
*
* @param key field name
*/
public async removeFilter(key: string): Promise<void> {
await this.retry.try(async () => {
await this.testSubjects.click(`~filter & ~filter-key-${key}`);
await this.testSubjects.click(`deleteFilter`);
});
await this.header.awaitGlobalLoadingIndicatorHidden();
}
/**
* Removes all filters
*/
public async removeAllFilters(): Promise<void> {
await this.testSubjects.click('showQueryBarMenu');
await this.testSubjects.click('filter-sets-removeAllFilters');
await this.header.waitUntilLoadingHasFinished();
await this.common.waitUntilUrlIncludes('filters:!()');
}
/**
* Changes filter active status
*
* @param key field name
*/
public async toggleFilterEnabled(key: string): Promise<void> {
await this.testSubjects.click(`~filter & ~filter-key-${key}`);
await this.testSubjects.click(`disableFilter`);
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async toggleFilterPinned(key: string): Promise<void> {
await this.testSubjects.click(`~filter & ~filter-key-${key}`);
await this.testSubjects.click(`pinFilter`);
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async toggleFilterNegated(key: string): Promise<void> {
await this.testSubjects.click(`~filter & ~filter-key-${key}`);
await this.testSubjects.click(`negateFilter`);
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async isFilterPinned(key: string): Promise<boolean> {
const filter = await this.testSubjects.find(`~filter & ~filter-key-${key}`);
return (await filter.getAttribute('data-test-subj')).includes('filter-pinned');
}
public async getFilterCount(): Promise<number> {
const filters = await this.testSubjects.findAll('~filter');
return filters.length;
}
public async getFiltersLabel(): Promise<string[]> {
const filters = await this.testSubjects.findAll('~filter');
return Promise.all(filters.map((filter) => filter.getVisibleText()));
}
public async openFilterBuilder() {
await this.retry.try(async () => {
await this.testSubjects.click('addFilter');
await this.testSubjects.existOrFail('addFilterPopover');
});
}
public async pressConfirmOnFilterEditorConfirmModal() {
await this.retry.try(async () => {
const confirmModal = await this.testSubjects.find(`close-filter-editor-confirm-modal`);
const cancelButton = await confirmModal.findByTestSubject('confirmModalConfirmButton');
await cancelButton.click();
});
}
private async addOrFilter(path: string) {
const filterForm = await this.testSubjects.find(`filter-${path}`);
const addOrBtn = await filterForm.findByTestSubject('add-or-filter');
await addOrBtn.click();
}
public async addAndFilter(path: string) {
const filterForm = await this.testSubjects.find(`filter-${path}`);
const addAndBtn = await filterForm.findByTestSubject('add-and-filter');
await addAndBtn.click();
}
private async addConditionalFilter(filter: FilterNode, path: string) {
if (filter.condition === BooleanRelation.OR) {
return await this.addOrFilter(path);
}
await this.addAndFilter(path);
}
private isFilterLeafWithoutValue(filter: FilterLeaf): filter is FilterWithoutValue {
return !('value' in filter);
}
private isFilterWithRange(filter: FilterLeaf): filter is FilterWithRange {
return (
'value' in filter &&
!Array.isArray(filter.value) &&
typeof filter.value === 'object' &&
'from' in filter.value &&
'to' in filter.value
);
}
private async pasteFilterData(filter: FilterLeaf, path: string) {
const filterForm = await this.testSubjects.find(`filter-${path}`);
const fieldInput = await filterForm.findByTestSubject('filterFieldSuggestionList');
await this.comboBox.setElement(fieldInput, filter.field);
const operatorInput = await filterForm.findByTestSubject('filterOperatorList');
await this.comboBox.setElement(operatorInput, filter.operation);
if (this.isFilterLeafWithoutValue(filter)) {
return;
}
if (this.isFilterWithRange(filter)) {
const startInput = await filterForm.findByTestSubject('range-start');
const endInput = await filterForm.findByTestSubject('range-end');
await startInput.type(`${filter.value.from ?? ''}`);
await endInput.type(`${filter.value.to ?? ''}`);
return;
}
const fieldParams = await filterForm.findByTestSubject('filterParams');
const filterValueInput = await fieldParams.findByTagName('input');
if (Array.isArray(filter.value)) {
for (const value of filter.value) {
await filterValueInput.type(value);
await filterValueInput.type(this.browser.keys.ENTER);
}
return;
}
return await filterValueInput.type(filter.value);
}
private isFilterNode(filter: Filter): filter is FilterNode {
return 'filters' in filter && 'condition' in filter;
}
public async createFilter(filter: Filter, path: string = '0'): Promise<unknown> {
if (this.isFilterNode(filter)) {
let startedAdding = false;
for (const [index, f] of filter.filters.entries()) {
if (index < filter.filters.length - 1) {
await this.addConditionalFilter(filter, startedAdding ? `${path}.${index}` : path);
}
await this.createFilter(f, `${path}.${index}`);
startedAdding = true;
}
return;
}
return await this.pasteFilterData(filter, path);
}
public async addFilterAndSelectDataView(
dataViewTitle: string | null,
filter: Filter
): Promise<void> {
await this.openFilterBuilder();
await this.retry.tryForTime(this.defaultTryTimeout * 2, async () => {
if (dataViewTitle) {
await this.comboBox.set('filterIndexPatternsSelect', dataViewTitle);
}
await this.createFilter(filter);
await this.testSubjects.scrollIntoView('saveFilter');
await this.testSubjects.clickWhenNotDisabled('saveFilter');
});
await this.retry.try(async () => {
await this.testSubjects.waitForDeleted('saveFilter');
});
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async addFilter(filter: Filter): Promise<void> {
await this.addFilterAndSelectDataView(null, filter);
}
/**
* Activates filter editing
* @param key field name
* @param value field value
*/
public async clickEditFilter(key: string, value: string): Promise<void> {
await this.testSubjects.click(`~filter & ~filter-key-${key} & ~filter-value-${value}`);
await this.testSubjects.click(`editFilter`);
await this.header.awaitGlobalLoadingIndicatorHidden();
}
public async clickEditFilterById(id: string): Promise<void> {
await this.testSubjects.click(`~filter & ~filter-id-${id}`);
await this.testSubjects.click(`editFilter`);
await this.header.awaitGlobalLoadingIndicatorHidden();
}
/**
* Returns available phrases in the filter
*/
public async getFilterEditorSelectedPhrases(): Promise<string[]> {
return await this.comboBox.getComboBoxSelectedOptions('~filterParamsComboBox');
}
/**
* Returns available fields in the filter
*/
public async getFilterEditorFields(): Promise<string[]> {
const optionsString = await this.comboBox.getOptionsList('filterFieldSuggestionList');
return optionsString.split('\n');
}
public async getFilterEditorPreview(): Promise<string> {
const filterPreview = await this.testSubjects.find('filter-preview');
return await filterPreview.getVisibleText();
}
/**
* Closes field editor modal window
*/
public async ensureFieldEditorModalIsClosed(): Promise<void> {
const cancelSaveFilterModalButtonExists = await this.testSubjects.exists('cancelSaveFilter', {
timeout: 1000,
});
if (cancelSaveFilterModalButtonExists) {
await this.testSubjects.click('cancelSaveFilter');
}
await this.testSubjects.waitForDeleted('cancelSaveFilter');
}
/**
* Returns comma-separated list of index patterns
*/
public async getIndexPatterns(): Promise<string> {
await this.testSubjects.click('addFilter');
const indexPatterns = await this.comboBox.getOptionsList('filterIndexPatternsSelect');
await this.ensureFieldEditorModalIsClosed();
return indexPatterns.trim().split('\n').join(',');
}
}