Skip to content

Commit

Permalink
[Lens] Fix formula validation issue with non-default locale (elastic#…
Browse files Browse the repository at this point in the history
…149806)

## Summary

Fix elastic#149803 

This PR addresses the problem with i18n validation on formula's content,
centralising the default node type into a locale-based type.

I've also added some i18n functional tests as suggested by @stratoula :
one for this specific bug and some smokescreen ones for Lens.

### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Stratoula Kalafateli <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
3 people authored and kqualters-elastic committed Feb 6, 2023
1 parent a28e262 commit f4bdf38
Show file tree
Hide file tree
Showing 8 changed files with 1,019 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function getOperationParams(
}, {});
}

function getTypeI18n(type: string) {
export function getTypeI18n(type: string) {
if (type === 'number') {
return i18n.translate('xpack.lens.formula.number', { defaultMessage: 'number' });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
findMathNodes,
findVariables,
getOperationParams,
getTypeI18n,
getValueOrName,
groupArgsByType,
isMathNode,
Expand Down Expand Up @@ -121,6 +122,8 @@ export interface ErrorWrapper {
severity?: 'error' | 'warning';
}

const DEFAULT_RETURN_TYPE = getTypeI18n('number');

function getNodeLocation(node: TinymathFunction): TinymathLocation[] {
return [node.location].filter(nonNullable);
}
Expand All @@ -131,11 +134,11 @@ function getArgumentType(arg: TinymathAST, operations: Record<string, GenericOpe
}
if (arg.type === 'function') {
if (tinymathFunctions[arg.name]) {
return tinymathFunctions[arg.name].outputType ?? 'number';
return tinymathFunctions[arg.name].outputType ?? DEFAULT_RETURN_TYPE;
}
// Assume it's a number for now
if (operations[arg.name]) {
return 'number';
return DEFAULT_RETURN_TYPE;
}
}
// leave for now other argument types
Expand Down Expand Up @@ -708,7 +711,6 @@ function validateNameArguments(
return errors;
}

const DEFAULT_RETURN_TYPE = 'number';
function checkTopNodeReturnType(ast: TinymathAST): ErrorWrapper[] {
if (
isObject(ast) &&
Expand Down Expand Up @@ -1175,7 +1177,7 @@ export function validateMathNodes(
values: {
operation: node.name,
name: positionalArguments[wrongTypeArgumentIndex].name,
type: getArgumentType(arg, operations) || 'number',
type: getArgumentType(arg, operations) || DEFAULT_RETURN_TYPE,
expectedType: positionalArguments[wrongTypeArgumentIndex].type || '',
},
locations: getNodeLocation(node),
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/localization/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import type { FtrProviderContext } from '../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('Sanity checks', () => {
loadTestFile(require.resolve('./login_page'));
loadTestFile(require.resolve('./lens'));
});
}
33 changes: 33 additions & 0 deletions x-pack/test/localization/tests/lens/formula.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService, getPageObjects }: FtrProviderContext) {
const PageObjects = getPageObjects(['visualize', 'lens']);
const elasticChart = getService('elasticChart');

describe('lens formula tests', () => {
it('should allow creation of a lens chart via formula', async () => {
await PageObjects.visualize.navigateToNewVisualization();
await PageObjects.visualize.clickVisType('lens');
await elasticChart.setNewChartUiDebugFlag(true);
await PageObjects.lens.goToTimeRange();

await PageObjects.lens.configureDimension({
dimension: 'lnsXY_yDimensionPanel > lns-empty-dimension',
operation: 'formula',
formula: `count() + average(bytes)`,
});

expect(await PageObjects.lens.getWorkspaceErrorCount()).to.eql(0);
const data = await PageObjects.lens.getCurrentChartDebugState('xyVisChart');
expect(data).to.be.ok();
});
});
}
77 changes: 77 additions & 0 deletions x-pack/test/localization/tests/lens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EsArchiver } from '@kbn/es-archiver';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService, getPageObjects, loadTestFile }: FtrProviderContext) {
const PageObjects = getPageObjects(['timePicker']);
const browser = getService('browser');
const config = getService('config');
const log = getService('log');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
let remoteEsArchiver;

describe('lens app', () => {
const esArchive = 'x-pack/test/functional/es_archives/logstash_functional';
const localIndexPatternString = 'logstash-*';
const remoteIndexPatternString = 'ftr-remote:logstash-*';
const localFixtures = {
lensBasic: 'x-pack/test/functional/fixtures/kbn_archiver/lens/lens_basic.json',
lensDefault: 'x-pack/test/functional/fixtures/kbn_archiver/lens/default',
};

const remoteFixtures = {
lensBasic: 'x-pack/test/functional/fixtures/kbn_archiver/lens/ccs/lens_basic.json',
lensDefault: 'x-pack/test/functional/fixtures/kbn_archiver/lens/ccs/default',
};
let esNode: EsArchiver;
let fixtureDirs: {
lensBasic: string;
lensDefault: string;
};
let indexPatternString: string;
before(async () => {
log.debug('Starting lens before method');
await browser.setWindowSize(1280, 1200);
await kibanaServer.savedObjects.cleanStandardList();
try {
config.get('esTestCluster.ccs');
remoteEsArchiver = getService('remoteEsArchiver' as 'esArchiver');
esNode = remoteEsArchiver;
fixtureDirs = remoteFixtures;
indexPatternString = remoteIndexPatternString;
} catch (error) {
esNode = esArchiver;
fixtureDirs = localFixtures;
indexPatternString = localIndexPatternString;
}

await esNode.load(esArchive);
// changing the timepicker default here saves us from having to set it in Discover (~8s)
await PageObjects.timePicker.setDefaultAbsoluteRangeViaUiSettings();
await kibanaServer.uiSettings.update({
defaultIndex: indexPatternString,
'dateFormat:tz': 'UTC',
});
await kibanaServer.importExport.load(fixtureDirs.lensBasic);
await kibanaServer.importExport.load(fixtureDirs.lensDefault);
});

after(async () => {
await esArchiver.unload(esArchive);
await PageObjects.timePicker.resetDefaultAbsoluteRangeViaUiSettings();
await kibanaServer.importExport.unload(fixtureDirs.lensBasic);
await kibanaServer.importExport.unload(fixtureDirs.lensDefault);
await kibanaServer.savedObjects.cleanStandardList();
});

loadTestFile(require.resolve('./smokescreen'));
loadTestFile(require.resolve('./formula'));
});
}
Loading

0 comments on commit f4bdf38

Please sign in to comment.