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 indexpattern checks for missing references #88840

Merged
merged 13 commits into from
Jan 27, 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 @@ -367,11 +367,14 @@ export function getIndexPatternDatasource({
return;
}

// Forward the indexpattern as well, as it is required by some operationType checks
const layerErrors = Object.values(state.layers).map((layer) =>
(getErrorMessages(layer) ?? []).map((message) => ({
shortMessage: '', // Not displayed currently
longMessage: message,
}))
(getErrorMessages(layer, state.indexPatterns[layer.indexPatternId]) ?? []).map(
(message) => ({
shortMessage: '', // Not displayed currently
longMessage: message,
})
)
);

// Single layer case, no need to explain more
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2203,5 +2203,33 @@ describe('state_helpers', () => {

delete operationDefinitionMap.testIncompleteReference;
});

it('should forward the indexpattern when available', () => {
const mock = jest.fn();
operationDefinitionMap.testReference.getErrorMessage = mock;
getErrorMessages(
{
indexPatternId: '1',
columnOrder: [],
columns: {
col1:
// @ts-expect-error not statically analyzed
{ operationType: 'testReference', references: [] },
},
},
indexPattern
);
expect(mock).toHaveBeenCalledWith(
{
indexPatternId: '1',
columnOrder: [],
columns: {
col1: { operationType: 'testReference', references: [] },
},
},
'col1',
indexPattern
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -864,18 +864,24 @@ export function updateLayerIndexPattern(
* - All column references are valid
* - All prerequisites are met
*/
export function getErrorMessages(layer: IndexPatternLayer): string[] | undefined {
const errors: string[] = [];
Object.entries(layer.columns).forEach(([columnId, column]) => {
// If we're transitioning to another operation, check for "new" incompleteColumns rather
// than "old" saved operation on the layer
const columnFinalRef =
layer.incompleteColumns?.[columnId]?.operationType || column.operationType;
const def = operationDefinitionMap[columnFinalRef];
if (def.getErrorMessage) {
errors.push(...(def.getErrorMessage(layer, columnId) ?? []));
}
});
export function getErrorMessages(
layer: IndexPatternLayer,
indexPattern?: IndexPattern
): string[] | undefined {
const errors: string[] = Object.entries(layer.columns)
.flatMap(([columnId, column]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in terms of style, you're using flatMap and then filter as opposed to forEach- seems fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just hoping to use a simple .filter(Boolean) at the end, but Typescript wasn't happy about that., therefore I had to god for an explicit typeguard.

// If we're transitioning to another operation, check for "new" incompleteColumns rather
// than "old" saved operation on the layer
const columnFinalRef =
layer.incompleteColumns?.[columnId]?.operationType || column.operationType;
const def = operationDefinitionMap[columnFinalRef];
Comment on lines +873 to +877
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk could cause a merge conflict with my other open PR, but it should be fine to merge as-is and then fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is unchanged from master

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, checked your PR #88916 together with this one and it works as expected in all the various scenarios. So I'll skip to refactor this and approve your so the merge will provide a fix for both issues.


if (def.getErrorMessage) {
return def.getErrorMessage(layer, columnId, indexPattern);
}
})
// remove the undefined values
.filter((v: string | undefined): v is string => v != null);

return errors.length ? errors : undefined;
}
Expand Down