[8.8] [Lens] Enabled tsdb counter fields for last_value (#156183) (#156487)

# Backport

This will backport the following commits from `main` to `8.8`:
- [[Lens] Enabled tsdb counter fields for last_value
(#156183)](https://github.com/elastic/kibana/pull/156183)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Marco
Liberati","email":"dej611@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-05-03T09:05:06Z","message":"[Lens]
Enabled tsdb counter fields for last_value (#156183)\n\n##
Summary\r\n\r\nFixes #156168 \r\n\r\nEnables the `last_value` operation
for TSDB `counter` field types.\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n- [ ] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"36c31f5c3f3face5417c044fbc3238f9a42b24a0","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","Team:Visualizations","Feature:Lens","backport:prev-minor","v8.8.0","v8.9.0"],"number":156183,"url":"https://github.com/elastic/kibana/pull/156183","mergeCommit":{"message":"[Lens]
Enabled tsdb counter fields for last_value (#156183)\n\n##
Summary\r\n\r\nFixes #156168 \r\n\r\nEnables the `last_value` operation
for TSDB `counter` field types.\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n- [ ] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"36c31f5c3f3face5417c044fbc3238f9a42b24a0"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"8.8","label":"v8.8.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/156183","number":156183,"mergeCommit":{"message":"[Lens]
Enabled tsdb counter fields for last_value (#156183)\n\n##
Summary\r\n\r\nFixes #156168 \r\n\r\nEnables the `last_value` operation
for TSDB `counter` field types.\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n- [ ] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"36c31f5c3f3face5417c044fbc3238f9a42b24a0"}}]}]
BACKPORT-->

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-05-03 06:10:11 -04:00 committed by GitHub
parent f6bce10870
commit cdd9cb4ea5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 24 deletions

View file

@ -18,7 +18,6 @@ import { createMockedIndexPattern } from '../../mocks';
import { LastValueIndexPatternColumn } from './last_value';
import { lastValueOperation } from '.';
import type { FormBasedLayer } from '../../types';
import type { IndexPattern } from '../../../../types';
import { TermsIndexPatternColumn } from './terms';
import { EuiSwitch, EuiSwitchEvent } from '@elastic/eui';
import { buildExpression, parseExpression } from '@kbn/expressions-plugin/common';
@ -35,10 +34,9 @@ const defaultProps = {
dataViews: dataViewPluginMocks.createStartContract(),
data: dataPluginMock.createStartContract(),
http: {} as HttpSetup,
indexPattern: {
...createMockedIndexPattern(),
indexPattern: createMockedIndexPattern({
hasRestrictions: false,
} as IndexPattern,
}),
operationDefinitionMap: {},
isFullscreen: false,
toggleFullscreen: jest.fn(),
@ -87,7 +85,7 @@ describe('last_value', () => {
const esAggsFn = lastValueOperation.toEsAggsFn(
{ ...lastValueColumn, params: { ...lastValueColumn.params } },
'col1',
{} as IndexPattern,
createMockedIndexPattern(),
layer,
uiSettingsMock,
[]
@ -110,7 +108,7 @@ describe('last_value', () => {
const esAggsFn = lastValueOperation.toEsAggsFn(
{ ...lastValueColumn, params: { ...lastValueColumn.params, showArrayValues: true } },
'col1',
{} as IndexPattern,
createMockedIndexPattern(),
layer,
uiSettingsMock,
[]
@ -398,6 +396,23 @@ describe('last_value', () => {
})
).toEqual({ dataType: 'string', isBucketed: false, scale: 'ordinal' });
});
it('should return operation with the right type also for tsdb counter types', () => {
expect(
lastValueOperation.getPossibleOperationForField({
aggregatable: true,
searchable: true,
name: 'test',
displayName: 'test',
type: 'number',
timeSeriesMetric: 'counter',
})
).toEqual({
dataType: 'number',
isBucketed: false,
scale: 'ratio',
});
});
});
describe('buildColumn', () => {
@ -882,10 +897,8 @@ describe('last_value', () => {
});
describe('getErrorMessage', () => {
let indexPattern: IndexPattern;
let errorLayer: FormBasedLayer;
beforeEach(() => {
indexPattern = createMockedIndexPattern();
errorLayer = {
columns: {
col1: {
@ -903,9 +916,9 @@ describe('last_value', () => {
};
});
it('returns undefined if sourceField exists and sortField is of type date ', () => {
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', indexPattern)).toEqual(
undefined
);
expect(
lastValueOperation.getErrorMessage!(errorLayer, 'col1', createMockedIndexPattern())
).toEqual(undefined);
});
it('shows error message if the sourceField does not exist in index pattern', () => {
errorLayer = {
@ -917,7 +930,7 @@ describe('last_value', () => {
} as LastValueIndexPatternColumn,
},
};
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', indexPattern))
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', createMockedIndexPattern()))
.toMatchInlineSnapshot(`
Array [
Object {
@ -968,7 +981,7 @@ describe('last_value', () => {
} as LastValueIndexPatternColumn,
},
};
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', indexPattern))
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', createMockedIndexPattern()))
.toMatchInlineSnapshot(`
Array [
Object {
@ -1014,10 +1027,13 @@ describe('last_value', () => {
} as LastValueIndexPatternColumn,
},
};
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', indexPattern)).toHaveLength(2);
expect(
lastValueOperation.getErrorMessage!(errorLayer, 'col1', createMockedIndexPattern())
).toHaveLength(2);
});
it('shows error message if the sourceField is of unsupported type', () => {
const indexPattern = createMockedIndexPattern();
indexPattern.getFieldByName('start_date')!.type = 'unsupported_type';
errorLayer = {
...errorLayer,
@ -1045,9 +1061,9 @@ describe('last_value', () => {
} as LastValueIndexPatternColumn,
},
};
expect(lastValueOperation.getErrorMessage!(errorLayer, 'col1', indexPattern)).toEqual([
'Field bytes is not a date field and cannot be used for sorting',
]);
expect(
lastValueOperation.getErrorMessage!(errorLayer, 'col1', createMockedIndexPattern())
).toEqual(['Field bytes is not a date field and cannot be used for sorting']);
});
});

View file

@ -199,8 +199,8 @@ export const lastValueOperation: OperationDefinition<
: oldColumn.filter,
};
},
getPossibleOperationForField: ({ aggregationRestrictions, type, timeSeriesMetric }) => {
if (supportedTypes.has(type) && !aggregationRestrictions && timeSeriesMetric !== 'counter') {
getPossibleOperationForField: ({ aggregationRestrictions, type }) => {
if (supportedTypes.has(type) && !aggregationRestrictions) {
return {
dataType: type as DataType,
isBucketed: false,
@ -285,9 +285,12 @@ export const lastValueOperation: OperationDefinition<
// time shift is added to wrapping aggFilteredMetric if filter is set
timeShift: column.filter ? undefined : column.timeShift,
} as const;
// do not use unsupported top hits when using a counter field type
const isCounterMetricFieldUsed =
indexPattern.getFieldByName(column.sourceField)?.timeSeriesMetric === 'counter';
return (
column.params.showArrayValues
column.params.showArrayValues && !isCounterMetricFieldUsed
? buildExpressionFunction<AggFunctionsMapping['aggTopHit']>('aggTopHit', {
...initialArgs,
aggregate: 'concat',

View file

@ -171,7 +171,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
// skip count for now as it's a special function and will
// change automatically the unsupported field to Records when detected
const operationsByFieldSupport = [
const allOperations = [
'average',
'max',
'last_value',
@ -181,11 +181,16 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
'standard_deviation',
'sum',
'unique_count',
].map((name) => ({
];
const counterFieldsSupportedOps = ['min', 'max', 'counter_rate', 'last_value'];
const gaugeFieldsSupportedOps = allOperations;
const operationsByFieldSupport = allOperations.map((name) => ({
name,
// Quick way to make it match the UI name
label: `${name[0].toUpperCase()}${name.slice(1).replace('_', ' ')}`,
counter: ['max', 'counter_rate'].includes(name),
gauge: true,
counter: counterFieldsSupportedOps.includes(name),
gauge: gaugeFieldsSupportedOps.includes(name),
}));
for (const fieldType of ['counter', 'gauge'] as const) {