🐛 fix duplicate suggestion issue + missing over time (#113449) (#113915)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-10-05 09:12:47 -04:00 committed by GitHub
parent a873767d62
commit e4a401dc00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 3 deletions

View file

@ -115,6 +115,7 @@ export function getSuggestions({
} else {
dataSourceSuggestions = datasource.getDatasourceSuggestionsFromCurrentState(
datasourceState,
(layerId) => isLayerSupportedByVisualization(layerId, [layerTypes.DATA]),
activeData
);
}

View file

@ -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 = {

View file

@ -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
@ -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];

View file

@ -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>>;