-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
use_esql_mode.ts
138 lines (119 loc) · 4.54 KB
/
use_esql_mode.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
/*
* 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 { isEqual } from 'lodash';
import { isOfAggregateQueryType } from '@kbn/es-query';
import { hasTransformationalCommand, getIndexPatternFromESQLQuery } from '@kbn/esql-utils';
import { useCallback, useEffect, useRef } from 'react';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import { switchMap } from 'rxjs';
import { useSavedSearchInitial } from '../state_management/discover_state_provider';
import type { DiscoverStateContainer } from '../state_management/discover_state';
import { getValidViewMode } from '../utils/get_valid_view_mode';
import { FetchStatus } from '../../types';
const MAX_NUM_OF_COLUMNS = 50;
/**
* Hook to take care of ES|QL state transformations when a new result is returned
* If necessary this is setting displayed columns and selected data view
*/
export function useEsqlMode({
dataViews,
stateContainer,
}: {
stateContainer: DiscoverStateContainer;
dataViews: DataViewsContract;
}) {
const prev = useRef<{
query: string;
recentlyUpdatedToColumns: string[];
}>({
recentlyUpdatedToColumns: [],
query: '',
});
const initialFetch = useRef<boolean>(true);
const savedSearch = useSavedSearchInitial();
const cleanup = useCallback(() => {
if (prev.current.query) {
// cleanup when it's not an ES|QL query
prev.current = {
recentlyUpdatedToColumns: [],
query: '',
};
initialFetch.current = true;
}
}, []);
useEffect(() => {
const subscription = stateContainer.dataState.data$.documents$
.pipe(
switchMap(async (next) => {
const { query } = next;
if (!query || next.fetchStatus === FetchStatus.ERROR) {
return;
}
const sendComplete = () => {
stateContainer.dataState.data$.documents$.next({
...next,
fetchStatus: FetchStatus.COMPLETE,
});
};
const { viewMode } = stateContainer.appState.getState();
const isEsqlQuery = isOfAggregateQueryType(query);
if (isEsqlQuery) {
const hasResults = Boolean(next.result?.length);
if (next.fetchStatus !== FetchStatus.PARTIAL) {
return;
}
let nextColumns: string[] = prev.current.recentlyUpdatedToColumns;
if (hasResults) {
const firstRow = next.result![0];
const firstRowColumns = Object.keys(firstRow.raw);
if (hasTransformationalCommand(query.esql)) {
nextColumns = firstRowColumns.slice(0, MAX_NUM_OF_COLUMNS);
} else {
nextColumns = [];
}
}
if (initialFetch.current) {
initialFetch.current = false;
prev.current.query = query.esql;
prev.current.recentlyUpdatedToColumns = nextColumns;
}
const indexPatternChanged =
getIndexPatternFromESQLQuery(query.esql) !==
getIndexPatternFromESQLQuery(prev.current.query);
const addColumnsToState =
indexPatternChanged || !isEqual(nextColumns, prev.current.recentlyUpdatedToColumns);
const changeViewMode = viewMode !== getValidViewMode({ viewMode, isEsqlMode: true });
if (!indexPatternChanged && !addColumnsToState && !changeViewMode) {
sendComplete();
return;
}
prev.current.query = query.esql;
prev.current.recentlyUpdatedToColumns = nextColumns;
// just change URL state if necessary
if (addColumnsToState || changeViewMode) {
const nextState = {
...(addColumnsToState && { columns: nextColumns }),
...(changeViewMode && { viewMode: undefined }),
};
await stateContainer.appState.replaceUrlState(nextState);
}
sendComplete();
} else {
// cleanup for a "regular" query
cleanup();
}
})
)
.subscribe();
return () => {
// cleanup for e.g. when savedSearch is switched
cleanup();
subscription.unsubscribe();
};
}, [dataViews, stateContainer, savedSearch, cleanup]);
}