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

Switch to runtime references #9

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 13 additions & 14 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ describe('Lens App', () => {
state: {
query: 'fake query',
filters: [{ query: { match_phrase: { src: 'test' } } }],
datasourceMetaData: { numberFilterableIndexPatterns: 1 },
},
references: [{ type: 'index-pattern', id: '1', name: 'filterable-index-pattern-0' }],
});
Expand Down Expand Up @@ -478,7 +477,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: { id: initialDocId, ...lastKnownDoc } as Document,
isSaveable: true,
})
Expand Down Expand Up @@ -513,7 +512,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: 'will save this' } as unknown) as Document,
isSaveable: true,
})
Expand All @@ -533,7 +532,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: 'will save this' } as unknown) as Document,
isSaveable: true,
})
Expand Down Expand Up @@ -616,7 +615,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: undefined } as unknown) as Document,
isSaveable: true,
})
Expand Down Expand Up @@ -696,7 +695,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
await act(async () =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: '123' } as unknown) as Document,
isSaveable: true,
})
Expand Down Expand Up @@ -736,7 +735,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
await act(async () =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({} as unknown) as Document,
isSaveable: true,
})
Expand Down Expand Up @@ -806,7 +805,7 @@ describe('Lens App', () => {

await act(async () => {
onChange({
filterableIndexPatterns: ['1'],
indexPatternsByLayer: [{ layerId: 'a', indexPatternId: '1', refName: 'ref-1' }],
doc: ({ id: undefined } as unknown) as Document,
isSaveable: true,
});
Expand All @@ -825,7 +824,7 @@ describe('Lens App', () => {

await act(async () => {
onChange({
filterableIndexPatterns: ['2'],
indexPatternsByLayer: [{ layerId: 'a', indexPatternId: '2', refName: 'ref-2' }],
doc: ({ id: undefined } as unknown) as Document,
isSaveable: true,
});
Expand Down Expand Up @@ -1097,7 +1096,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: undefined } as unknown) as Document,
isSaveable: true,
})
Expand All @@ -1119,7 +1118,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: undefined, state: {} } as unknown) as Document,
isSaveable: true,
})
Expand All @@ -1144,7 +1143,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: '1234' } as unknown) as Document,
isSaveable: true,
})
Expand All @@ -1169,7 +1168,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({
id: '1234',
title: 'My cool doc',
Expand Down Expand Up @@ -1202,7 +1201,7 @@ describe('Lens App', () => {
const onChange = frame.mount.mock.calls[0][1].onChange;
act(() =>
onChange({
filterableIndexPatterns: [],
indexPatternsByLayer: [],
doc: ({ id: '1234' } as unknown) as Document,
isSaveable: true,
})
Expand Down
12 changes: 8 additions & 4 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,24 +519,28 @@ export function App({
doc: state.persistedDoc,
onError,
showNoDataPopover,
onChange: ({ filterableIndexPatterns, doc, isSaveable }) => {
onChange: ({ indexPatternsByLayer, doc, isSaveable }) => {
if (isSaveable !== state.isSaveable) {
setState((s) => ({ ...s, isSaveable }));
}
if (!_.isEqual(state.persistedDoc, doc)) {
setState((s) => ({ ...s, lastKnownDoc: doc }));
}

const indexPatternIds = _.uniq(
indexPatternsByLayer.map(({ indexPatternId }) => indexPatternId)
);

// Update the cached index patterns if the user made a change to any of them
if (
state.indexPatternsForTopNav.length !== filterableIndexPatterns.length ||
filterableIndexPatterns.some(
state.indexPatternsForTopNav.length !== indexPatternIds.length ||
indexPatternIds.some(
(id) =>
!state.indexPatternsForTopNav.find((indexPattern) => indexPattern.id === id)
)
) {
getAllIndexPatterns(
filterableIndexPatterns,
indexPatternIds,
data.indexPatterns,
core.notifications
).then((indexPatterns) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import { generateId } from '../../id_generator';
import { Filter, Query, SavedQuery } from '../../../../../../src/plugins/data/public';
import { EditorFrameStartPlugins } from '../service';

export interface OnChangeProp {
indexPatternsByLayer: DatasourceMetaData['indexPatternsByLayer'];
doc: Document;
isSaveable: boolean;
}

export interface EditorFrameProps {
doc?: Document;
datasourceMap: Record<string, Datasource>;
Expand All @@ -44,11 +50,7 @@ export interface EditorFrameProps {
query: Query;
filters: Filter[];
savedQuery?: SavedQuery;
onChange: (arg: {
filterableIndexPatterns: DatasourceMetaData['filterableIndexPatterns'];
doc: Document;
isSaveable: boolean;
}) => void;
onChange: (arg: OnChangeProp) => void;
showNoDataPopover: () => void;
}

Expand Down Expand Up @@ -220,20 +222,20 @@ export function EditorFrame(props: EditorFrameProps) {
return;
}

const { filterableIndexPatterns, doc, isSaveable } = getSavedObjectFormat({
activeDatasources: Object.keys(state.datasourceStates).reduce(
(datasourceMap, datasourceId) => ({
...datasourceMap,
[datasourceId]: props.datasourceMap[datasourceId],
}),
{}
),
visualization: activeVisualization,
state,
framePublicAPI,
});

props.onChange({ filterableIndexPatterns, doc, isSaveable });
props.onChange(
getSavedObjectFormat({
activeDatasources: Object.keys(state.datasourceStates).reduce(
(datasourceMap, datasourceId) => ({
...datasourceMap,
[datasourceId]: props.datasourceMap[datasourceId],
}),
{}
),
visualization: activeVisualization,
state,
framePublicAPI,
})
);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/

import { Ast, fromExpression, ExpressionFunctionAST } from '@kbn/interpreter/common';
import { DateRange } from '../../../common';
import { Visualization, Datasource, DatasourcePublicAPI } from '../../types';
import { Filter, TimeRange, Query } from '../../../../../../src/plugins/data/public';
import { Visualization, Datasource, FramePublicAPI } from '../../types';

export function prependDatasourceExpression(
visualizationExpression: Ast | string | null,
Expand All @@ -26,7 +24,7 @@ export function prependDatasourceExpression(
const state = datasourceStates[datasourceId].state;
const layers = datasource.getLayers(datasourceStates[datasourceId].state);

layers.forEach((layerId) => {
layers.forEach((layerId, index) => {
const result = datasource.toExpression(state, layerId);
if (result) {
datasourceExpressions.push([layerId, result]);
Expand Down Expand Up @@ -59,40 +57,16 @@ export function prependDatasourceExpression(
? fromExpression(visualizationExpression)
: visualizationExpression;

return {
type: 'expression',
chain: [datafetchExpression, ...parsedVisualizationExpression.chain],
};
}

export function prependKibanaContext(
expression: Ast | string,
{
timeRange,
query,
filters,
}: {
timeRange?: TimeRange;
query?: Query;
filters?: Filter[];
}
): Ast {
const parsedExpression = typeof expression === 'string' ? fromExpression(expression) : expression;

return {
type: 'expression',
chain: [
{ type: 'function', function: 'kibana', arguments: {} },
{
type: 'function',
function: 'kibana_context',
arguments: {
timeRange: timeRange ? [JSON.stringify(timeRange)] : [],
query: query ? [JSON.stringify(query)] : [],
filters: [JSON.stringify(filters || [])],
},
function: 'kibana',
arguments: {},
},
...parsedExpression.chain,
datafetchExpression,
...parsedVisualizationExpression.chain,
],
};
}
Expand All @@ -115,13 +89,7 @@ export function buildExpression({
state: unknown;
}
>;
framePublicAPI: {
datasourceLayers: Record<string, DatasourcePublicAPI>;
query: Query;
dateRange?: DateRange;
filters: Filter[];
};

framePublicAPI: FramePublicAPI;
removeDateRange?: boolean;
}): Ast | null {
if (visualization === null) {
Expand All @@ -132,28 +100,5 @@ export function buildExpression({
framePublicAPI.datasourceLayers
);

const expressionContext = removeDateRange
? { query: framePublicAPI.query, filters: framePublicAPI.filters }
: {
query: framePublicAPI.query,
timeRange: framePublicAPI.dateRange
? {
from: framePublicAPI.dateRange.fromDate,
to: framePublicAPI.dateRange.toDate,
}
: undefined,
filters: framePublicAPI.filters,
};

const completeExpression = prependDatasourceExpression(
visualizationExpression,
datasourceMap,
datasourceStates
);

if (completeExpression) {
return prependKibanaContext(completeExpression, expressionContext);
} else {
return null;
}
return prependDatasourceExpression(visualizationExpression, datasourceMap, datasourceStates);
}
Loading