Skip to content

Commit

Permalink
Build more suggestions that work with metric/datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Oct 29, 2019
1 parent e019440 commit b171443
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe('IndexPattern Data Source suggestions', () => {
);
});

it('creates a new layer if no match is found', () => {
it('creates a new layer and replaces layer if no match is found', () => {
const suggestions = getDatasourceSuggestionsForField(stateWithEmptyLayer(), '2', {
name: 'source',
type: 'string',
Expand Down Expand Up @@ -593,6 +593,30 @@ describe('IndexPattern Data Source suggestions', () => {
keptLayerIds: ['previousLayer'],
})
);

expect(suggestions).toContainEqual(
expect.objectContaining({
state: expect.objectContaining({
layers: {
id1: expect.objectContaining({
indexPatternId: '2',
}),
},
}),
table: {
changeType: 'initial',
label: undefined,
isMultiRow: false,
columns: expect.arrayContaining([
expect.objectContaining({
columnId: expect.any(String),
}),
]),
layerId: 'id1',
},
keptLayerIds: [],
})
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ export function getDatasourceSuggestionsForField(
const layerIds = layers.filter(id => state.layers[id].indexPatternId === indexPatternId);

if (layerIds.length === 0) {
// The field we're suggesting on does not match any existing layer. This will add
// a new layer.
return getEmptyLayerSuggestionsForField(state, generateId(), indexPatternId, field);
// The field we're suggesting on does not match any existing layer.
// This generates a set of suggestions where we add a layer.
// A second set of suggestions is generated for visualizations that don't work with layers
const newId = generateId();
return getEmptyLayerSuggestionsForField(state, newId, indexPatternId, field).concat(
getEmptyLayerSuggestionsForField({ ...state, layers: {} }, newId, indexPatternId, field)
);
} else {
// The field we're suggesting on matches an existing layer. In this case we find the layer with
// the fewest configured columns and try to add the field to this table. If this layer does not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,18 @@ describe('metric_suggestions', () => {
}
`);
});

test('does not suggest for multiple layers', () => {
const suggestions = getSuggestions({
table: {
columns: [numCol('bytes')],
isMultiRow: false,
layerId: 'l1',
changeType: 'unchanged',
},
keptLayerIds: ['l1', 'l2'],
});

expect(suggestions).toHaveLength(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import chartMetricSVG from '../assets/chart_metric.svg';
export function getSuggestions({
table,
state,
keptLayerIds,
}: SuggestionRequest<State>): Array<VisualizationSuggestion<State>> {
// We only render metric charts for single-row queries. We require a single, numeric column.
if (
table.isMultiRow ||
keptLayerIds.length > 1 ||
table.columns.length !== 1 ||
table.columns[0].operation.dataType !== 'number'
) {
Expand Down

0 comments on commit b171443

Please sign in to comment.