Skip to content

Commit

Permalink
use data views instead of index string
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 16, 2021
1 parent 273fa23 commit 265b94a
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 957 deletions.
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 @@ -68,6 +70,10 @@ export const getEsdslFn = ({
}),
required: true,
},
timeField: {
types: ['string'],
help: '',
},
size: {
types: ['number'],
help: i18n.translate('data.search.esdsl.size.help', {
Expand All @@ -77,11 +83,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 @@ -94,6 +118,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 @@ -57,9 +57,11 @@ export const renameColumnFn: RenameColumnsExpressionFunction['fn'] = (
...column,
id: mappedItem.id,
name: getColumnName(mappedItem, column),
type: overwrittenFieldTypes[column.id] || column.type,
serializedParams:
overwrittenFieldTypes[column.id] === 'date' ? { id: 'date' } : column.serializedParams,
meta: {
...column.meta,
type: overwrittenFieldTypes[column.id] || column.meta.type,
params: overwrittenFieldTypes[column.id] === 'date' ? { id: 'date' } : column.meta.params,
},
};
}),
};
Expand Down
118 changes: 118 additions & 0 deletions x-pack/plugins/lens/public/esdsl_datasource/change_indexpattern.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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 { i18n } from '@kbn/i18n';
import React, { useState } from 'react';
import { EuiPopover, EuiPopoverTitle, EuiSelectable, EuiSelectableProps } from '@elastic/eui';
import { IndexPatternRef } from './types';
import { trackUiEvent } from '../lens_ui_telemetry';
import { ToolbarButton, ToolbarButtonProps } from '../../../../../src/plugins/kibana_react/public';

export type ChangeIndexPatternTriggerProps = ToolbarButtonProps & {
label: string;
title?: string;
};

export function ChangeIndexPattern({
indexPatternRefs,
isMissingCurrent,
indexPatternId,
onChangeIndexPattern,
trigger,
selectableProps,
}: {
trigger: ChangeIndexPatternTriggerProps;
indexPatternRefs: IndexPatternRef[];
isMissingCurrent?: boolean;
onChangeIndexPattern: (newId: string) => void;
indexPatternId?: string;
selectableProps?: EuiSelectableProps;
}) {
const [isPopoverOpen, setPopoverIsOpen] = useState(false);

// be careful to only add color with a value, otherwise it will fallbacks to "primary"
const colorProp = isMissingCurrent
? {
color: 'danger' as const,
}
: {};

const createTrigger = function () {
const { label, title, ...rest } = trigger;
return (
<ToolbarButton
title={title}
onClick={() => setPopoverIsOpen(!isPopoverOpen)}
fullWidth
{...colorProp}
{...rest}
>
{label}
</ToolbarButton>
);
};

return (
<>
<EuiPopover
panelClassName="lnsChangeIndexPatternPopover"
button={createTrigger()}
panelProps={{
['data-test-subj']: 'lnsChangeIndexPatternPopover',
}}
isOpen={isPopoverOpen}
closePopover={() => setPopoverIsOpen(false)}
display="block"
panelPaddingSize="s"
ownFocus
>
<div>
<EuiPopoverTitle>
{i18n.translate('xpack.lens.indexPattern.changeDataViewTitle', {
defaultMessage: 'Data view',
})}
</EuiPopoverTitle>
<EuiSelectable<{
key?: string;
label: string;
value?: string;
checked?: 'on' | 'off' | undefined;
}>
{...selectableProps}
searchable
singleSelection="always"
options={indexPatternRefs.map(({ title, id }) => ({
key: id,
label: title,
value: id,
checked: id === indexPatternId ? 'on' : undefined,
}))}
onChange={(choices) => {
const choice = choices.find(({ checked }) => checked) as unknown as {
value: string;
};
trackUiEvent('indexpattern_changed');
onChangeIndexPattern(choice.value);
setPopoverIsOpen(false);
}}
searchProps={{
compressed: true,
...(selectableProps ? selectableProps.searchProps : undefined),
}}
>
{(list, search) => (
<>
{search}
{list}
</>
)}
</EuiSelectable>
</div>
</EuiPopover>
</>
);
}
105 changes: 65 additions & 40 deletions x-pack/plugins/lens/public/esdsl_datasource/datapanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { EuiFieldText, EuiSelect } from '@elastic/eui';
import { DatasourceDataPanelProps, DataType, StateSetter } from '../types';
import { IndexPattern, EsDSLPrivateState, IndexPatternField, IndexPatternRef } from './types';
import { esRawResponse } from '../../../../../src/plugins/data/common';
import { ChangeIndexPattern } from './change_indexpattern';

export type Props = DatasourceDataPanelProps<EsDSLPrivateState> & {
data: DataPublicPluginStart;
};
import { EuiFieldText, EuiSelect } from '@elastic/eui';
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
import { flatten } from './flatten';

Expand Down Expand Up @@ -211,46 +212,57 @@ export function EsDSLHorizontalDataPanel({
>
<EuiSpacer size="xxl" />
<EuiFlexGroup direction="column">
{Object.entries(layers).map(([id, layer]) => (
<EuiFlexItem key={id}>
<EuiPanel>
<EuiFieldText
value={layer.index}
onChange={(e) => {
setLocalState({
...state,
layers: {
...state.layers,
[id]: {
...layer,
index: e.target.value,
{Object.entries(layers).map(([id, layer]) => {
const ref = state.indexPatternRefs.find((r) => r.id === layer.index);
return (
<EuiFlexItem key={id}>
<EuiPanel>
<ChangeIndexPattern
data-test-subj="indexPattern-switcher"
trigger={{
label: ref?.title,
title: ref?.title,
'data-test-subj': 'indexPattern-switch-link',
fontWeight: 'bold',
}}
indexPatternId={layer.index}
indexPatternRefs={state.indexPatternRefs}
onChangeIndexPattern={(newId: string) => {
setState({
...state,
layers: {
...state.layers,
[id]: {
...layer,
index: newId,
},
},
},
});
}}
/>
<EuiCodeEditor
mode="json"
theme="github"
value={layer.query}
width="100%"
height="250px"
onChange={(val) => {
setLocalState({
...state,
layers: {
...state.layers,
[id]: {
...layer,
query: val,
});
}}
/>
<EuiCodeEditor
mode="json"
theme="github"
value={layer.query}
width="100%"
height="250px"
onChange={(val) => {
setLocalState({
...state,
layers: {
...state.layers,
[id]: {
...layer,
query: val,
},
},
},
});
}}
/>
</EuiPanel>
</EuiFlexItem>
))}
});
}}
/>
</EuiPanel>
</EuiFlexItem>
);
})}
{Object.entries(removedLayers).map(([id, { layer }]) => (
<EuiFlexItem key={id}>
<EuiPanel>
Expand Down Expand Up @@ -278,7 +290,9 @@ export function EsDSLHorizontalDataPanel({
.search({
params: {
size: 0,
index: layer.index,
index: [
state.indexPatternRefs.find((r) => r.id === layer.index)!.title,
],
body: JSON.parse(layer.query),
},
})
Expand All @@ -295,6 +309,17 @@ export function EsDSLHorizontalDataPanel({
const { rows, columns } = esRawResponse.to!.datatable({
body: response.rawResponse,
});
columns.forEach((col) => {
const testVal = rows[0][col.id];
if (typeof testVal === 'number' && Math.log10(testVal) > 11) {
// col.meta.type = 'date';
// col.meta.params = { id: 'date' };
localState.layers[layerId].overwrittenFieldTypes = {
...(localState.layers[layerId].overwrittenFieldTypes || {}),
[col.id]: 'date',
};
}
});
// todo hack some logic in for dates
cachedFieldList[layerId] = { fields: columns, singleRow: rows.length === 1 };
});
Expand Down
Loading

0 comments on commit 265b94a

Please sign in to comment.