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

[Lens] Fix wrong suggestions from Datatable to other visualizations #93920

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const validateDatasourceAndVisualization = (
: undefined;

const visualizationValidationErrors = currentVisualizationState
? currentVisualization?.getErrorMessages(currentVisualizationState)
? currentVisualization?.getErrorMessages(currentVisualizationState, frameAPI.datasourceLayers)
: undefined;

if (datasourceValidationErrors?.length || visualizationValidationErrors?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const lastValueOperation: OperationDefinition<LastValueIndexPatternColumn
label: ofName(field.displayName),
sourceField: field.name,
params: newParams,
scale: field.type === 'string' ? 'ordinal' : 'ratio',
};
},
getPossibleOperationForField: ({ aggregationRestrictions, type }) => {
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ export interface Visualization<T = unknown> {
* The frame will call this function on all visualizations at few stages (pre-build/build error) in order
* to provide more context to the error and show it to the user
*/
getErrorMessages: (state: T) => Array<{ shortMessage: string; longMessage: string }> | undefined;
getErrorMessages: (
state: T,
datasourceLayers?: Record<string, DatasourcePublicAPI>
) => Array<{ shortMessage: string; longMessage: string }> | undefined;

/**
* The frame calls this function to display warnings about visualization
Expand Down
43 changes: 43 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/visualization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,23 @@ describe('xy_visualization', () => {
});

describe('#getErrorMessages', () => {
let mockDatasource: ReturnType<typeof createMockDatasource>;
let frame: ReturnType<typeof createMockFramePublicAPI>;

beforeEach(() => {
frame = createMockFramePublicAPI();
mockDatasource = createMockDatasource('testDatasource');

mockDatasource.publicAPIMock.getOperationForColumnId.mockReturnValue({
dataType: 'string',
label: 'MyOperation',
} as Operation);

frame.datasourceLayers = {
first: mockDatasource.publicAPIMock,
};
});

it("should not return an error when there's only one dimension (X or Y)", () => {
expect(
xyVisualization.getErrorMessages({
Expand Down Expand Up @@ -775,6 +792,32 @@ describe('xy_visualization', () => {
},
]);
});

it('should return an error when accessor type is of the wrong type', () => {
expect(
xyVisualization.getErrorMessages(
{
...exampleState(),
layers: [
{
layerId: 'first',
seriesType: 'area',
splitAccessor: 'd',
xAccessor: 'a',
accessors: ['b'], // just use a single accessor to avoid too much noise
},
],
},
frame.datasourceLayers
)
).toEqual([
{
shortMessage: 'Wrong data type for Vertical axis.',
longMessage:
'The dimension MyOperation provided for the Vertical axis has the wrong data type. Expected number but have string',
},
]);
});
});

describe('#getWarningMessages', () => {
Expand Down
31 changes: 30 additions & 1 deletion x-pack/plugins/lens/public/xy_visualization/visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export const getXyVisualization = ({
toExpression(state, layers, paletteService, attributes),
toPreviewExpression: (state, layers) => toPreviewExpression(state, layers, paletteService),

getErrorMessages(state) {
getErrorMessages(state, datasourceLayers) {
// Data error handling below here
const hasNoAccessors = ({ accessors }: XYLayerConfig) =>
accessors == null || accessors.length === 0;
Expand Down Expand Up @@ -373,6 +373,35 @@ export const getXyVisualization = ({
}
}

if (datasourceLayers && state) {
for (const layer of state.layers) {
const datasourceAPI = datasourceLayers[layer.layerId];
if (datasourceAPI) {
for (const accessor of layer.accessors) {
const operation = datasourceAPI.getOperationForColumnId(accessor);
if (operation && operation.dataType !== 'number') {
errors.push({
shortMessage: i18n.translate('xpack.lens.xyVisualization.dataTypeFailureYShort', {
defaultMessage: `Wrong data type for {axis}.`,
values: {
axis: getAxisName('y', { isHorizontal: isHorizontalChart(state.layers) }),
},
}),
longMessage: i18n.translate('xpack.lens.xyVisualization.dataTypeFailureYLong', {
defaultMessage: `The dimension {label} provided for the {axis} has the wrong data type. Expected number but have {dataType}`,
values: {
label: operation.label,
dataType: operation.dataType,
axis: getAxisName('y', { isHorizontal: isHorizontalChart(state.layers) }),
},
}),
});
}
}
}
}
}

return errors.length ? errors : undefined;
},

Expand Down