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

[8.9] [Lens] Relax counter field checks for saved visualizations with unsupported operations (#163515) - manual backport #164191

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -71,6 +71,7 @@ import {
isColumnInvalid,
cloneLayer,
getNotifiableFeatures,
getUnsupportedOperationsWarningMessage,
} from './utils';
import { getUniqueLabelGenerator, isDraggedDataViewField } from '../../utils';
import { hasField, normalizeOperationDataType } from './pure_utils';
Expand Down Expand Up @@ -891,6 +892,7 @@ export function getFormBasedDatasource({
core.docLinks,
setState
),
...getUnsupportedOperationsWarningMessage(state, frameDatasourceAPI, core.docLinks),
];

const infoMessages = getNotifiableFeatures(state, frameDatasourceAPI, visualizationInfo);
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/lens/public/datasources/form_based/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,15 @@ export function createMockedDragDropContext(): jest.Mocked<DragContextState> {
registerDropTarget: jest.fn(),
};
}

export const createMockedIndexPatternWithAdditionalFields = (
newFields: IndexPatternField[]
): IndexPattern => {
const { fields, ...otherIndexPatternProps } = createMockedIndexPattern();
const completeFields = fields.concat(newFields);
return {
...otherIndexPatternProps,
fields: completeFields,
getFieldByName: getFieldByNameFactory(completeFields),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ function buildMetricOperation<T extends MetricColumn<string>>({
newField &&
supportedTypes.includes(newField.type) &&
newField.aggregatable &&
isTimeSeriesCompatible(type, newField.timeSeriesMetric) &&
(!newField.aggregationRestrictions || newField.aggregationRestrictions![type])
);
},
Expand Down
200 changes: 198 additions & 2 deletions x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
import React from 'react';
import { shallow } from 'enzyme';
import { createDatatableUtilitiesMock } from '@kbn/data-plugin/common/mocks';
import { getPrecisionErrorWarningMessages, cloneLayer } from './utils';
import {
getPrecisionErrorWarningMessages,
cloneLayer,
getUnsupportedOperationsWarningMessage,
} from './utils';
import type { FormBasedPrivateState, GenericIndexPatternColumn } from './types';
import type { FramePublicAPI } from '../../types';
import type { FramePublicAPI, IndexPattern } from '../../types';
import type { DocLinksStart } from '@kbn/core/public';
import { EuiLink } from '@elastic/eui';
import { TermsIndexPatternColumn } from './operations';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { FormattedMessage } from '@kbn/i18n-react';
import { FormBasedLayer } from './types';
import { createMockedIndexPatternWithAdditionalFields } from './mocks';

describe('indexpattern_datasource utils', () => {
describe('getPrecisionErrorWarningMessages', () => {
Expand Down Expand Up @@ -240,4 +245,195 @@ describe('indexpattern_datasource utils', () => {
).toMatchSnapshot();
});
});

describe('getUnsupportedOperationsWarningMessage', () => {
let docLinks: DocLinksStart;
const affectedOperations = [
'sum',
'average',
'percentile',
'percentile_rank',
'count',
'unique_count',
'standard_deviation',
];

function createColumnsForField(field: string, colOffset: number = 0) {
return Object.fromEntries(
affectedOperations.map((operationType, i) => [
`col_${i + colOffset}`,
{ operationType, sourceField: field, label: `${operationType} of ${field}` },
])
);
}

function createState(fields: string[]) {
return {
layers: {
id: {
indexPatternId: '0',
columns: Object.assign(
{},
...fields.map((field, i) =>
createColumnsForField(field, i * affectedOperations.length)
)
),
},
},
} as unknown as FormBasedPrivateState;
}

function createFramePublic(indexPattern: IndexPattern): FramePublicAPI {
return {
dataViews: {
indexPatterns: Object.fromEntries([indexPattern].map((dataView, i) => [i, dataView])),
},
} as unknown as FramePublicAPI;
}

function createFormulaColumns(formulaParts: string[], field: string, colOffset: number = 0) {
const fullFormula = formulaParts.map((part) => `${part}(${field})`).join(' + ');
// just assume it's a sum of all the parts for testing
const rootId = `col-formula${colOffset}`;
return Object.fromEntries([
[
rootId,
{
operationType: 'formula',
label: `Formula: ${fullFormula}`,
params: { formula: fullFormula },
},
],
...formulaParts.map((part, i) => [
`${rootId}X${i}`,
{ operationType: part, sourceField: field, label: 'Part of formula' },
]),
[
`${rootId}X${formulaParts.length}`,
{ operationType: 'math', references: formulaParts.map((_, i) => `${rootId}X${i}`) },
],
]);
}

beforeEach(() => {
docLinks = {
links: {
fleet: {
datastreamsTSDSMetrics: 'http://tsdb_metric_doc',
},
},
} as DocLinksStart;
});

it.each([['bytes'], ['bytes_gauge']])(
'should return no warning for non-counter fields: %s',
(fieldName: string) => {
const warnings = getUnsupportedOperationsWarningMessage(
createState([fieldName]),
createFramePublic(
createMockedIndexPatternWithAdditionalFields([
{
name: 'bytes_gauge',
displayName: 'bytes_gauge',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'gauge',
},
])
),
docLinks
);
expect(warnings).toHaveLength(0);
}
);

it('should return a warning for a counter field grouped by field', () => {
const warnings = getUnsupportedOperationsWarningMessage(
createState(['bytes_counter']),
createFramePublic(
createMockedIndexPatternWithAdditionalFields([
{
name: 'bytes_counter',
displayName: 'bytes_counter',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'counter',
},
])
),
docLinks
);
expect(warnings).toHaveLength(1);
});

it('should group multiple warnings by field', () => {
const warnings = getUnsupportedOperationsWarningMessage(
createState(['bytes_counter', 'bytes_counter2']),
createFramePublic(
createMockedIndexPatternWithAdditionalFields([
{
name: 'bytes_counter',
displayName: 'bytes_counter',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'counter',
},
{
name: 'bytes_counter2',
displayName: 'bytes_counter2',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'counter',
},
])
),
docLinks
);
expect(warnings).toHaveLength(2);
});

it('should handle formula reporting only the top visible dimension', () => {
const warnings = getUnsupportedOperationsWarningMessage(
{
layers: {
id: {
indexPatternId: '0',
columns: Object.assign(
{},
...['bytes_counter', 'bytes_counter2'].map((field, i) =>
createFormulaColumns(affectedOperations, field, i * affectedOperations.length)
)
),
},
},
} as unknown as FormBasedPrivateState,
createFramePublic(
createMockedIndexPatternWithAdditionalFields([
{
name: 'bytes_counter',
displayName: 'bytes_counter',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'counter',
},
{
name: 'bytes_counter2',
displayName: 'bytes_counter2',
type: 'number',
aggregatable: true,
searchable: true,
timeSeriesMetric: 'counter',
},
])
),
docLinks
);
expect(warnings).toHaveLength(2);
});
});
});
Loading