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] Calculation operations #83789

Merged
merged 20 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -161,7 +161,7 @@ export function WorkspacePanel({

const expression = useMemo(
() => {
if (!configurationValidationError) {
if (!configurationValidationError || configurationValidationError.length === 0) {
try {
return buildExpression({
visualization: activeVisualization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,24 +660,30 @@ describe('IndexPattern Data Source', () => {

it('should skip columns that are being referenced', () => {
publicAPI = indexPatternDatasource.getPublicAPI({
state: {
state: ({
layers: {
first: {
indexPatternId: '1',
columnOrder: ['col1', 'col2'],
columns: {
// @ts-ignore this is too little information for a real column
col1: {
dataType: 'number',
operationType: 'sum',
label: 'Sum',
isBucketed: false,
sourceField: 'test',
},
col2: {
// @ts-expect-error update once we have a reference operation outside tests
operationType: 'cumulative_sum',
dataType: 'number',
isBucketed: false,
label: 'Cumulative sum',
references: ['col1'],
},
},
},
},
},
} as unknown) as IndexPatternPrivateState,
layerId: 'first',
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types';
import { IndexPatternLayer } from '../../../types';
import { checkForDateHistogram, dateBasedOperationToExpression, hasDateField } from './utils';
import { OperationDefinition } from '..';

const ofName = (name: string) => {
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
return i18n.translate('xpack.lens.indexPattern.CounterRateOf', {
defaultMessage: 'Counter rate of {name}',
values: { name },
});
};

export type CounterRateIndexPatternColumn = FormattedIndexPatternColumn &
ReferenceBasedIndexPatternColumn & {
operationType: 'counter_rate';
};

export const counterRateOperation: OperationDefinition<
CounterRateIndexPatternColumn,
'fullReference'
> = {
type: 'counter_rate',
priority: 2,
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
displayName: i18n.translate('xpack.lens.indexPattern.counterRate', {
defaultMessage: 'Counter rate',
}),
input: 'fullReference',
selectionStyle: 'field',
requiredReferences: [
{
input: ['field'],
specificOperations: ['max'],
validateMetadata: (metadata) =>
metadata.dataType === 'number' && metadata.isBucketed === false,
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
},
],
getPossibleOperation: () => {
return {
dataType: 'number',
isBucketed: false,
scale: 'ratio',
};
},
getDefaultLabel: () => {
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
return i18n.translate('xpack.lens.indexPattern.counterRate', {
defaultMessage: 'Counter rate',
});
},
toExpression: (layer, columnId) => {
return dateBasedOperationToExpression(layer, columnId, 'lens_counter_rate');
},
buildColumn: ({ referenceIds, previousColumn, layer }) => {
const metric = layer.columns[referenceIds[0]];
return {
label: ofName(metric.label),
dataType: 'number',
operationType: 'counter_rate',
isBucketed: false,
scale: 'ratio',
references: referenceIds,
params:
previousColumn?.dataType === 'number' &&
previousColumn.params &&
'format' in previousColumn.params &&
previousColumn.params.format
? { format: previousColumn.params.format }
: undefined,
};
},
isTransferable: (column, newIndexPattern) => {
return hasDateField(newIndexPattern);
},
getErrorMessage: (layer: IndexPatternLayer) => {
return checkForDateHistogram(layer);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types';
import { IndexPatternLayer } from '../../../types';
import { checkForDateHistogram, dateBasedOperationToExpression } from './utils';
import { OperationDefinition } from '..';

const ofName = (name: string) => {
return i18n.translate('xpack.lens.indexPattern.cumulativeSumOf', {
defaultMessage: 'Cumulative sum of {name}',
values: { name },
});
};

export type CumulativeSumIndexPatternColumn = FormattedIndexPatternColumn &
ReferenceBasedIndexPatternColumn & {
operationType: 'cumulative_sum';
};

export const cumulativeSumOperation: OperationDefinition<
CumulativeSumIndexPatternColumn,
'fullReference'
> = {
type: 'cumulative_sum',
priority: 2,
displayName: i18n.translate('xpack.lens.indexPattern.cumulativeSum', {
defaultMessage: 'Cumulative sum',
}),
input: 'fullReference',
selectionStyle: 'field',
requiredReferences: [
{
input: ['field'],
specificOperations: ['count', 'sum'],
validateMetadata: (metadata) =>
metadata.dataType === 'number' && metadata.isBucketed === false,
},
],
getPossibleOperation: () => {
return {
dataType: 'number',
isBucketed: false,
scale: 'ratio',
};
},
getDefaultLabel: () => {
return i18n.translate('xpack.lens.indexPattern.cumulativeSum', {
defaultMessage: 'Cumulative sum',
});
},
toExpression: (layer, columnId) => {
return dateBasedOperationToExpression(layer, columnId, 'cumulative_sum');
},
buildColumn: ({ referenceIds, previousColumn, layer }) => {
const metric = layer.columns[referenceIds[0]];
return {
label: ofName(metric.label),
dataType: 'number',
operationType: 'cumulative_sum',
isBucketed: false,
scale: 'ratio',
references: referenceIds,
params:
previousColumn?.dataType === 'number' &&
previousColumn.params &&
'format' in previousColumn.params &&
previousColumn.params.format
? { format: previousColumn.params.format }
: undefined,
};
},
isTransferable: () => {
return true;
},
getErrorMessage: (layer: IndexPatternLayer) => {
return checkForDateHistogram(layer);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types';
import { IndexPatternLayer } from '../../../types';
import { checkForDateHistogram, dateBasedOperationToExpression, hasDateField } from './utils';
import { OperationDefinition } from '..';

const ofName = (name: string) => {
return i18n.translate('xpack.lens.indexPattern.derivativeOf', {
defaultMessage: 'Derivative of {name}',
values: { name },
});
};

export type DerivativeIndexPatternColumn = FormattedIndexPatternColumn &
ReferenceBasedIndexPatternColumn & {
operationType: 'derivative';
};

export const derivativeOperation: OperationDefinition<
DerivativeIndexPatternColumn,
'fullReference'
> = {
type: 'derivative',
priority: 2,
displayName: i18n.translate('xpack.lens.indexPattern.derivative', {
defaultMessage: 'Derivative',
flash1293 marked this conversation as resolved.
Show resolved Hide resolved
}),
input: 'fullReference',
selectionStyle: 'full',
requiredReferences: [
{
input: ['field'],
validateMetadata: (metadata) =>
metadata.dataType === 'number' && metadata.isBucketed === false,
},
],
getPossibleOperation: () => {
return {
dataType: 'number',
isBucketed: false,
scale: 'ratio',
};
},
getDefaultLabel: () => {
return i18n.translate('xpack.lens.indexPattern.derivative', {
defaultMessage: 'Derivative',
});
},
toExpression: (layer, columnId) => {
return dateBasedOperationToExpression(layer, columnId, 'derivative');
},
buildColumn: ({ referenceIds, previousColumn, layer }) => {
const metric = layer.columns[referenceIds[0]];
return {
label: ofName(metric.label),
dataType: 'number',
operationType: 'derivative',
isBucketed: false,
scale: 'ratio',
references: referenceIds,
params:
previousColumn?.dataType === 'number' &&
previousColumn.params &&
'format' in previousColumn.params &&
previousColumn.params.format
? { format: previousColumn.params.format }
: undefined,
};
},
isTransferable: (column, newIndexPattern) => {
return hasDateField(newIndexPattern);
},
getErrorMessage: (layer: IndexPatternLayer) => {
return checkForDateHistogram(layer);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { counterRateOperation, CounterRateIndexPatternColumn } from './counter_rate';
export { cumulativeSumOperation, CumulativeSumIndexPatternColumn } from './cumulative_sum';
export { derivativeOperation, DerivativeIndexPatternColumn } from './derivative';
export { movingAverageOperation, MovingAverageIndexPatternColumn } from './moving_average';
Loading