Skip to content

Commit

Permalink
🐛 fix duplicate suggestion issue + missing over time (elastic#113449)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
dej611 and kibanamachine authored Oct 5, 2021
1 parent 1e7d4e1 commit 35e9f6a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function getSuggestions({
} else {
dataSourceSuggestions = datasource.getDatasourceSuggestionsFromCurrentState(
datasourceState,
(layerId) => isLayerSupportedByVisualization(layerId, [layerTypes.DATA]),
activeData
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,103 @@ describe('IndexPattern Data Source suggestions', () => {
);
});

it('adds date histogram over default time field for tables without time dimension and a threshold', async () => {
const initialState = testInitialState();
const state: IndexPatternPrivateState = {
...initialState,
layers: {
first: {
indexPatternId: '1',
columnOrder: ['cola', 'colb'],
columns: {
cola: {
label: 'My Terms',
customLabel: true,
dataType: 'string',
isBucketed: true,
operationType: 'terms',
sourceField: 'source',
scale: 'ordinal',
params: {
orderBy: { type: 'alphabetical' },
orderDirection: 'asc',
size: 5,
},
},
colb: {
label: 'My Op',
customLabel: true,
dataType: 'number',
isBucketed: false,
operationType: 'average',
sourceField: 'bytes',
scale: 'ratio',
},
},
},
threshold: {
indexPatternId: '2',
columnOrder: ['thresholda'],
columns: {
thresholda: {
label: 'My Op',
customLabel: true,
dataType: 'number',
isBucketed: false,
operationType: 'average',
sourceField: 'bytes',
scale: 'ratio',
},
},
},
},
};

expect(
getSuggestionSubset(
getDatasourceSuggestionsFromCurrentState(state, (layerId) => layerId !== 'threshold')
)
).toContainEqual(
expect.objectContaining({
table: {
isMultiRow: true,
changeType: 'extended',
label: 'Over time',
columns: [
{
columnId: 'cola',
operation: {
label: 'My Terms',
dataType: 'string',
isBucketed: true,
scale: 'ordinal',
},
},
{
columnId: 'id1',
operation: {
label: 'timestampLabel',
dataType: 'date',
isBucketed: true,
scale: 'interval',
},
},
{
columnId: 'colb',
operation: {
label: 'My Op',
dataType: 'number',
isBucketed: false,
scale: 'ratio',
},
},
],
layerId: 'first',
},
})
);
});

it('does not create an over time suggestion if tables with numeric buckets with time dimension', async () => {
const initialState = testInitialState();
const state: IndexPatternPrivateState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ function createNewLayerWithMetricAggregation(
}

export function getDatasourceSuggestionsFromCurrentState(
state: IndexPatternPrivateState
state: IndexPatternPrivateState,
filterLayers: (layerId: string) => boolean = () => true
): Array<DatasourceSuggestion<IndexPatternPrivateState>> {
const layers = Object.entries(state.layers || {});
const layers = Object.entries(state.layers || {}).filter(([layerId]) => filterLayers(layerId));

if (layers.length > 1) {
// Return suggestions that reduce the data to each layer individually
return layers
Expand Down Expand Up @@ -394,7 +396,7 @@ export function getDatasourceSuggestionsFromCurrentState(
}

return flatten(
Object.entries(state.layers || {})
layers
.filter(([_id, layer]) => layer.columnOrder.length && layer.indexPatternId)
.map(([layerId, layer]) => {
const indexPattern = state.indexPatterns[layer.indexPatternId];
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export interface Datasource<T = unknown, P = unknown> {
) => Array<DatasourceSuggestion<T>>;
getDatasourceSuggestionsFromCurrentState: (
state: T,
filterFn?: (layerId: string) => boolean,
activeData?: Record<string, Datatable>
) => Array<DatasourceSuggestion<T>>;

Expand Down

0 comments on commit 35e9f6a

Please sign in to comment.