Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lens/SQL text data source #118680

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
273fa23
make it work somehow
flash1293 Nov 16, 2021
265b94a
use data views instead of index string
flash1293 Nov 16, 2021
5fc034f
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Nov 17, 2021
79fd576
add essql support to Lens
flash1293 Nov 17, 2021
69fa156
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Nov 18, 2021
2a2663f
force apply
flash1293 Nov 19, 2021
853ca42
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Nov 19, 2021
670f46d
auto map
flash1293 Nov 19, 2021
7413c93
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Nov 22, 2021
edd1fd5
do drag and drop
flash1293 Nov 22, 2021
e7e8308
add expression datasource
flash1293 Nov 22, 2021
42b94d3
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Nov 22, 2021
be26d8a
switch to code editor
flash1293 Nov 22, 2021
a03b270
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Dec 9, 2021
a1659f8
hide filter bar in SQL mode by default
flash1293 Dec 9, 2021
1db9ff3
fix imports
flash1293 Dec 9, 2021
31dbd35
fix imports
flash1293 Dec 9, 2021
b824762
fix bugs
flash1293 Dec 9, 2021
0a48ed4
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Mar 2, 2022
55edf6e
hack sql support into discover
flash1293 Mar 4, 2022
3d24ba8
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Mar 4, 2022
4d707a0
make sql in discover work
flash1293 Mar 4, 2022
570b419
show all fields
flash1293 Mar 7, 2022
db772b0
Merge remote-tracking branch 'upstream/main' into lens-text-data-source
flash1293 Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/server/http_resources/get_apm_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getConfiguration, shouldInstrumentClient } from '@kbn/apm-config-loader

export const getApmConfig = (requestPath: string) => {
const baseConfig = getConfiguration('kibana-frontend');
if (!shouldInstrumentClient(baseConfig)) {
if (true || !shouldInstrumentClient(baseConfig)) {
return null;
}

Expand Down
30 changes: 29 additions & 1 deletion src/plugins/data/common/search/expressions/esdsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { i18n } from '@kbn/i18n';
import { buildEsQuery } from '@kbn/es-query';
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { get } from 'lodash';

import { EsRawResponse } from './es_raw_response';
import { RequestStatistics, RequestAdapter } from '../../../../inspector/common';
Expand All @@ -25,6 +26,7 @@ interface Arguments {
dsl: string;
index: string;
size: number;
timeField?: string;
}

export type EsdslExpressionFunctionDefinition = ExpressionFunctionDefinition<
Expand Down Expand Up @@ -67,6 +69,10 @@ export const getEsdslFn = ({
}),
required: true,
},
timeField: {
types: ['string'],
help: '',
},
size: {
types: ['number'],
help: i18n.translate('data.search.esdsl.size.help', {
Expand All @@ -76,11 +82,29 @@ export const getEsdslFn = ({
},
},
async fn(input, args, { inspectorAdapters, abortSignal, getKibanaRequest }) {
const { search, uiSettingsClient } = await getStartDependencies(getKibanaRequest);
const {
search,
uiSettingsClient,
query: queryS,
} = await getStartDependencies(getKibanaRequest);

const dsl = JSON.parse(args.dsl);

if (input) {
const timeRange: any = get(input, 'timeRange', undefined);
const timeField = get(args, 'timeField', undefined);
const timeBounds =
timeRange && timeField && queryS.timefilter.timefilter.calculateBounds(timeRange);
const timeFilter = timeBounds && {
range: {
[timeField]: {
format: 'strict_date_optional_time',
gte: timeBounds.min!.toISOString(),
lte: timeBounds.max!.toISOString(),
},
},
};

const esQueryConfigs = getEsQueryConfig(uiSettingsClient as any);
const query = buildEsQuery(
undefined, // args.index,
Expand All @@ -93,6 +117,10 @@ export const getEsdslFn = ({
query.bool.must.push(dsl.query);
}

if (timeFilter) {
query.bool.must.push(timeFilter);
}

dsl.query = query;
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/search/expressions/esdsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export function getEsdsl({
}) {
return getEsdslFn({
getStartDependencies: async () => {
const [core, , { search }] = await getStartServices();
const [core, , { search, query }] = await getStartServices();
return {
uiSettingsClient: core.uiSettings as any as UiSettingsCommon,
search: search.search,
query,
};
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EuiText,
EuiLoadingSpinner,
EuiScreenReaderOnly,
EuiCallOut,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { useDiscoverServices } from '../../../../utils/use_discover_services';
Expand Down Expand Up @@ -67,7 +68,11 @@ function DiscoverDocumentsComponent({
const documentState: DataDocumentsMsg = useDataState(documents$);
const isLoading = documentState.fetchStatus === FetchStatus.LOADING;

const rows = useMemo(() => documentState.result || [], [documentState.result]);
const rows = useMemo(
() =>
state.sqlMode && documentState.sql ? documentState.sql.rows : documentState.result || [],
[documentState.result, documentState.sql, state.sqlMode]
);

const { columns, onAddColumn, onRemoveColumn, onMoveColumn, onSetColumns } = useColumns({
capabilities,
Expand Down Expand Up @@ -111,6 +116,18 @@ function DiscoverDocumentsComponent({
[uiSettings, indexPattern.timeFieldName]
);

if (state.sqlMode && documentState.sqlError) {
return (
<div className="dscDocuments__loading">
<EuiCallOut title="SQL ERROR" color="danger" iconType="alert">
<p>
<pre>{JSON.stringify(documentState.sqlError, null, 2)}</pre>
</p>
</EuiCallOut>
</div>
);
}

if (
(!documentState.result || documentState.result.length === 0) &&
documentState.fetchStatus === FetchStatus.LOADING
Expand Down Expand Up @@ -157,6 +174,8 @@ function DiscoverDocumentsComponent({
{!isLegacy && (
<div className="dscDiscoverGrid">
<DataGridMemoized
sqlMode={state.sqlMode}
sql={documentState.sql}
ariaLabelledBy="documentsAriaLabel"
columns={columns}
expandedDoc={expandedDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import {
EuiPageBody,
EuiPageContent,
EuiSpacer,
EuiButton,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { METRIC_TYPE } from '@kbn/analytics';
import classNames from 'classnames';
import { CodeEditor } from '../../../../../../kibana_react/public';
import { useDiscoverServices } from '../../../../utils/use_discover_services';
import { DiscoverNoResults } from '../no_results';
import { LoadingSpinner } from '../loading_spinner/loading_spinner';
Expand Down Expand Up @@ -49,6 +51,7 @@ import { FieldStatisticsTable } from '../field_stats_table';
import { VIEW_MODE } from '../../../../components/view_mode_toggle';
import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants';
import { DataViewType, DataView } from '../../../../../../data_views/common';
import { triggerVisualizeActions } from '../sidebar/lib/visualize_trigger_utils';

/**
* Local storage key for sidebar persistence state
Expand All @@ -75,6 +78,7 @@ export function DiscoverLayout({
savedSearch,
searchSource,
state,
setState,
stateContainer,
}: DiscoverLayoutProps) {
const {
Expand Down Expand Up @@ -226,8 +230,40 @@ export function DiscoverLayout({
stateContainer={stateContainer}
updateQuery={onUpdateQuery}
resetSavedSearch={resetSavedSearch}
hideQuery={state.sqlMode}
/>
<EuiPageBody className="dscPageBody" aria-describedby="savedSearchTitle">
{state.sqlMode && (
<EuiFlexGroup direction="column">
<EuiFlexItem>
<div style={{ height: 250, width: '100%' }}>
<CodeEditor
mode="sql"
theme="github"
value={state.sqlQuery}
width="100%"
height="250px"
onChange={(val) => {
setState({ ...state, sqlQuery: val });
}}
/>
</div>
</EuiFlexItem>
<EuiFlexItem>
<EuiFlexGroup direction="row" alignItems="flexEnd">
<EuiFlexItem grow={false}>
<EuiButton
onClick={() => {
savedSearchRefetch$.next();
}}
>
Apply
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
</EuiFlexGroup>
)}
<SavedSearchURLConflictCallout
savedSearch={savedSearch}
spaces={spaces}
Expand All @@ -248,6 +284,7 @@ export function DiscoverLayout({
onChangeIndexPattern={onChangeIndexPattern}
selectedIndexPattern={indexPattern}
state={state}
setState={setState}
isClosed={isSidebarClosed}
trackUiMetric={trackUiMetric}
useNewFieldsApi={useNewFieldsApi}
Expand Down Expand Up @@ -311,20 +348,33 @@ export function DiscoverLayout({
gutterSize="none"
responsive={false}
>
<EuiFlexItem grow={false}>
<DiscoverChartMemoized
resetSavedSearch={resetSavedSearch}
savedSearch={savedSearch}
savedSearchDataChart$={charts$}
savedSearchDataTotalHits$={totalHits$}
stateContainer={stateContainer}
indexPattern={indexPattern}
viewMode={viewMode}
setDiscoverViewMode={setDiscoverViewMode}
hideChart={state.hideChart}
interval={state.interval}
/>
</EuiFlexItem>
{!state.sqlMode && (
<EuiFlexItem grow={false}>
<DiscoverChartMemoized
resetSavedSearch={resetSavedSearch}
savedSearch={savedSearch}
savedSearchDataChart$={charts$}
savedSearchDataTotalHits$={totalHits$}
stateContainer={stateContainer}
indexPattern={indexPattern}
viewMode={viewMode}
setDiscoverViewMode={setDiscoverViewMode}
hideChart={state.hideChart}
interval={state.interval}
/>
</EuiFlexItem>
)}
{state.sqlMode && (
<EuiFlexItem grow={false}>
<EuiButton
onClick={() => {
triggerVisualizeActions(undefined, undefined, [], state.sqlQuery);
}}
>
Visualize
</EuiButton>
</EuiFlexItem>
)}
<EuiHorizontalRule margin="none" />
{viewMode === VIEW_MODE.DOCUMENT_LEVEL ? (
<DiscoverDocuments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManage
defaultMessage: 'Create new data view',
})}
</EuiContextMenuItem>,
<EuiHorizontalRule style={{ margin: '0px' }} />,
<EuiContextMenuItem
key="sql"
data-test-subj="dataview-create-new"
onClick={() => {
props.setState({ ...props.state, sqlMode: true, columns: ['_source'] });
}}
>
SQL
</EuiContextMenuItem>,
]}
/>
</EuiPopover>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export function DiscoverSidebarComponent({
editField,
viewMode,
createNewDataView,
state,
setState,
table,
}: DiscoverSidebarProps) {
const { uiSettings, dataViewFieldEditor } = useDiscoverServices();
const [fields, setFields] = useState<DataViewField[] | null>(null);
Expand All @@ -120,10 +123,15 @@ export function DiscoverSidebarComponent({

useEffect(() => {
if (documents) {
const newFields = getIndexPatternFieldList(selectedIndexPattern, fieldCounts);
const newFields = getIndexPatternFieldList(
selectedIndexPattern,
fieldCounts,
state.sqlMode,
table
);
setFields(newFields);
}
}, [selectedIndexPattern, fieldCounts, documents]);
}, [selectedIndexPattern, fieldCounts, documents, state.sqlMode, table]);

const scrollDimensions = useResizeObserver(scrollContainer);

Expand All @@ -148,7 +156,16 @@ export function DiscoverSidebarComponent({
popular: popularFields,
unpopular: unpopularFields,
} = useMemo(
() => groupFields(fields, columns, popularLimit, fieldCounts, fieldFilter, useNewFieldsApi),
() =>
groupFields(
fields,
columns,
popularLimit,
fieldCounts,
fieldFilter,
useNewFieldsApi,
state.sqlMode
),
[fields, columns, popularLimit, fieldCounts, fieldFilter, useNewFieldsApi]
);

Expand Down Expand Up @@ -303,6 +320,8 @@ export function DiscoverSidebarComponent({
editField={editField}
useNewFieldsApi={useNewFieldsApi}
createNewDataView={createNewDataView}
state={state}
setState={setState}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down Expand Up @@ -341,6 +360,8 @@ export function DiscoverSidebarComponent({
useNewFieldsApi={useNewFieldsApi}
editField={editField}
createNewDataView={createNewDataView}
state={state}
setState={setState}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
<DiscoverSidebar
{...props}
documents={documentState.result}
table={documentState.sql}
fieldFilter={fieldFilter}
fieldCounts={fieldCounts.current}
setFieldFilter={setFieldFilter}
Expand Down Expand Up @@ -309,6 +310,8 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
</EuiFlexItem>
<EuiFlexItem grow={false}>
<DiscoverIndexPatternManagement
state={props.state}
setState={props.setState}
selectedIndexPattern={selectedIndexPattern}
editField={editField}
useNewFieldsApi={useNewFieldsApi}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ import { isNestedFieldParent } from '../../../utils/nested_fields';

export function getIndexPatternFieldList(
indexPattern?: DataView,
fieldCounts?: Record<string, number>
fieldCounts?: Record<string, number>,
sqlMode: boolean,
table: any
) {
if (!indexPattern || !fieldCounts) return [];

const fieldNamesInDocs = Object.keys(fieldCounts);
const fieldNamesInIndexPattern = indexPattern.fields.getAll().map((fld) => fld.name);
const unknownFields: DataViewField[] = [];

if (sqlMode && table) {
return table.columns.map((c) => ({
displayName: c.name,
name: c.name,
type: c.meta.type,
}));
}

difference(fieldNamesInDocs, fieldNamesInIndexPattern).forEach((unknownFieldName) => {
if (isNestedFieldParent(unknownFieldName, indexPattern)) {
unknownFields.push({
Expand Down
Loading