diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.test.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.test.ts index 9e605a8fd30c0..7bf81fa6e5a1e 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.test.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.test.ts @@ -10,8 +10,8 @@ import { stubLogstashDataView } from '@kbn/data-views-plugin/common/data_view.st import { TSVB_METRIC_TYPES } from '../../../../common/enums'; import type { Metric } from '../../../../common/types'; import { createSeries } from '../__mocks__'; -import { convertToCounterRateFormulaColumn } from './counter_rate'; -import { CommonColumnsConverterArgs, FormulaColumn } from './types'; +import { convertToCounterRateColumn } from './counter_rate'; +import { CommonColumnsConverterArgs, CounterRateColumn, MaxColumn } from './types'; describe('convertToCounterRateFormulaColumn', () => { const dataView = stubLogstashDataView; @@ -21,33 +21,32 @@ describe('convertToCounterRateFormulaColumn', () => { type: TSVB_METRIC_TYPES.POSITIVE_RATE, }; - test.each<[string, CommonColumnsConverterArgs, Partial | null]>([ + test.each< + [string, CommonColumnsConverterArgs, [Partial, Partial] | null] + >([ ['null if metric contains empty field param', { series, metrics: [metric], dataView }, null], [ - 'formula column if metric contains field param', + 'max and cpunter rate columns if metric contains field param', { series, metrics: [{ ...metric, field: dataView.fields[0].name }], dataView }, - { - operationType: 'formula', - params: { formula: 'pick_max(differences(max(bytes)), 0)' }, - meta: { metricId: 'some-id' }, - }, - ], - [ - 'differences formula with shift if metric contains unit', - { series, metrics: [{ ...metric, field: dataView.fields[0].name, unit: '1h' }], dataView }, - { - operationType: 'formula', - params: { - formula: 'pick_max(differences(max(bytes), shift=1h), 0)', + [ + { + operationType: 'max', + sourceField: dataView.fields[0].name, + }, + { + operationType: 'counter_rate', }, - meta: { metricId: 'some-id' }, - }, + ], ], ])('should return %s', (_, input, expected) => { if (expected === null) { - expect(convertToCounterRateFormulaColumn(input)).toBeNull(); + expect(convertToCounterRateColumn(input)).toBeNull(); + } else if (Array.isArray(expected)) { + const results = convertToCounterRateColumn(input); + expect(results).toEqual(expected.map(expect.objectContaining)); + expect(results?.[1].references[0]).toEqual(results?.[0].columnId); } else { - expect(convertToCounterRateFormulaColumn(input)).toEqual(expect.objectContaining(expected)); + expect(convertToCounterRateColumn(input)).toEqual(expect.objectContaining(expected)); } }); }); diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.ts index 7030ddef33a60..aa64e478e99e0 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/counter_rate.ts @@ -6,15 +6,15 @@ * Side Public License, v 1. */ -import { buildCounterRateFormula } from '../metrics'; -import { createFormulaColumn } from './formula'; -import { CommonColumnsConverterArgs, FormulaColumn } from './types'; +import { Operations } from '@kbn/visualizations-plugin/common/convert_to_lens'; +import { createColumn, getFormat } from './column'; +import { CommonColumnsConverterArgs, CounterRateColumn, MaxColumn } from './types'; -export const convertToCounterRateFormulaColumn = ({ +export const convertToCounterRateColumn = ({ series, metrics, dataView, -}: CommonColumnsConverterArgs): FormulaColumn | null => { +}: CommonColumnsConverterArgs): [MaxColumn, CounterRateColumn] | null => { const metric = metrics[metrics.length - 1]; const field = metric.field ? dataView.getFieldByName(metric.field) : undefined; @@ -22,6 +22,20 @@ export const convertToCounterRateFormulaColumn = ({ return null; } - const formula = buildCounterRateFormula(metric, field.name); - return createFormulaColumn(formula, { series, metric, dataView }); + const maxColumn = { + operationType: Operations.MAX, + sourceField: field.name, + ...createColumn(series, metric, field), + params: { ...getFormat(series) }, + }; + + return [ + maxColumn, + { + operationType: Operations.COUNTER_RATE, + references: [maxColumn.columnId], + ...createColumn(series, metric, field), + params: { ...getFormat(series) }, + }, + ]; }; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/date_histogram.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/date_histogram.ts index da3768ba30ca1..f2173cf56b469 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/date_histogram.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/date_histogram.ts @@ -6,14 +6,6 @@ * Side Public License, v 1. */ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - import type { DataView } from '@kbn/data-views-plugin/common'; import uuid from 'uuid'; import { DateHistogramParams, DataType } from '@kbn/visualizations-plugin/common/convert_to_lens'; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts index f46000727fd3f..4d6135ee3d5b2 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts @@ -21,7 +21,7 @@ export { convertToStaticValueColumn } from './static_value'; export { convertToFiltersColumn } from './filters'; export { convertToDateHistogramColumn } from './date_histogram'; export { converToTermsColumn } from './terms'; -export { convertToCounterRateFormulaColumn } from './counter_rate'; +export { convertToCounterRateColumn } from './counter_rate'; export { convertToStandartDeviationColumn } from './std_deviation'; export * from './types'; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.test.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.test.ts index 61f48f4c94149..95f83d48d8258 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.test.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.test.ts @@ -268,26 +268,6 @@ describe('convertMetricAggregationToColumn', () => { reducedTimeRange: '10m', }, ], - [ - 'null for counter rate if field in metric is empty', - [ - SUPPORTED_METRICS.positive_rate, - { series, metric: { id, type: TSVB_METRIC_TYPES.POSITIVE_RATE }, dataView }, - ], - null, - ], - [ - 'formula column for counter rate', - [ - SUPPORTED_METRICS.positive_rate, - { series, metric: { id, field, type: TSVB_METRIC_TYPES.POSITIVE_RATE }, dataView }, - ], - { - meta: { metricId: 'some-id' }, - operationType: 'formula', - params: { formula: 'pick_max(differences(max(bytes)), 0)' }, - }, - ], [ 'null for last value (unsupported)', [ diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.ts index 99d277f6446e6..3b60fe55572a4 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/parent_pipeline.ts @@ -40,7 +40,6 @@ import { createFormulaColumn } from './formula'; import { convertToMovingAverageParams } from './moving_average'; import { convertToPercentileColumn } from './percentile'; import { convertToPercentileRankColumn } from './percentile_rank'; -import { convertToCounterRateFormulaColumn } from './counter_rate'; type MetricAggregationWithoutParams = | typeof Operations.AVERAGE @@ -92,6 +91,7 @@ const SUPPORTED_METRICS_AGGS_WITHOUT_PARAMS: MetricAggregationWithoutParams[] = Operations.MIN, Operations.SUM, Operations.STANDARD_DEVIATION, + Operations.COUNTER_RATE, ]; const SUPPORTED_METRIC_AGGS: MetricAggregation[] = [ @@ -159,9 +159,6 @@ export const convertMetricAggregationToColumn = ( reducedTimeRange, }); } - if (aggregation.name === Operations.COUNTER_RATE) { - return convertToCounterRateFormulaColumn({ series, metrics: [metric], dataView }); - } if (aggregation.name === Operations.LAST_VALUE) { return null; @@ -237,7 +234,7 @@ const convertMovingAvgOrDerivativeToColumns = ( const [nestedFieldId, _] = subMetricField?.split('[') ?? []; // support nested aggs with formula const additionalSubFunction = metrics.find(({ id }) => id === nestedFieldId); - if (additionalSubFunction) { + if (additionalSubFunction || pipelineAgg.name === 'counter_rate') { const formula = getParentPipelineSeriesFormula( metrics, subFunctionMetric, diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.test.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.test.ts index aa1d367be6c11..a0a995f5d2b45 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.test.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.test.ts @@ -10,6 +10,7 @@ import { stubLogstashDataView } from '@kbn/data-views-plugin/common/data_view.st import { TSVB_METRIC_TYPES } from '../../../../common/enums'; import { Metric } from '../../../../common/types'; import { buildCounterRateFormula } from './counter_rate_formula'; +import { SUPPORTED_METRICS } from './supported_metrics'; describe('buildCounterRateFormula', () => { test('should return correct formula for counter rate', () => { @@ -20,7 +21,10 @@ describe('buildCounterRateFormula', () => { unit: '1h', }; - const formula = buildCounterRateFormula(metric, dataView.fields[0].name); - expect(formula).toStrictEqual('pick_max(differences(max(bytes), shift=1h), 0)'); + const formula = buildCounterRateFormula( + SUPPORTED_METRICS[metric.type]!.name, + dataView.fields[0].name + ); + expect(formula).toStrictEqual('counter_rate(max(bytes))'); }); }); diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.ts index f19daa8225f1b..2d1b744c1eb14 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/counter_rate_formula.ts @@ -6,27 +6,17 @@ * Side Public License, v 1. */ -import type { Metric } from '../../../../common/types'; -import { TimeScaleValue, isTimeScaleValue } from './metrics_helpers'; - const buildMaxFormula = (selector: string) => { return `max(${selector})`; }; -const buildPickMaxPositiveFormula = (selector: string) => { - return `pick_max(${selector}, 0)`; -}; - -const buildDifferencesFormula = (selector: string, shift?: TimeScaleValue) => { - return `differences(${selector}${shift ? `, shift=${shift}` : ''})`; +const buildСounterRateFormula = (aggFormula: string, selector: string) => { + return `${aggFormula}(${selector})`; }; -export const buildCounterRateFormula = (metric: Metric, fieldName: string) => { +export const buildCounterRateFormula = (aggFormula: string, fieldName: string) => { const maxFormula = buildMaxFormula(fieldName); - const unit = metric.unit && isTimeScaleValue(metric.unit) ? metric.unit : undefined; - const diffOfMaxFormula = buildDifferencesFormula(maxFormula, unit); - - const counterRateFormula = buildPickMaxPositiveFormula(diffOfMaxFormula); + const counterRateFormula = buildСounterRateFormula(aggFormula, maxFormula); return counterRateFormula; }; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.test.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.test.ts index 994e1f77c37a0..2b8510d3d348a 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.test.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.test.ts @@ -6,14 +6,6 @@ * Side Public License, v 1. */ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - import { TimeRange } from '@kbn/data-plugin/common'; import { METRIC_TYPES } from '@kbn/data-plugin/public'; import type { Metric, Series, Panel } from '../../../../common/types'; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.ts index 7f5e036ef89d3..d6e5c0f8c74b8 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/metrics/metrics_helpers.ts @@ -131,7 +131,7 @@ export const getFormulaEquivalent = ( ); } case 'positive_rate': { - return buildCounterRateFormula(currentMetric, currentMetric.field!); + return buildCounterRateFormula(aggFormula, currentMetric.field!); } case 'filter_ratio': { return getFilterRatioFormula(currentMetric, reducedTimeRange); diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.test.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.test.ts index 9df7e23f67b92..76a453e3d9f8a 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.test.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.test.ts @@ -17,7 +17,7 @@ const mockConvertMathToFormulaColumn = jest.fn(); const mockConvertParentPipelineAggToColumns = jest.fn(); const mockConvertToCumulativeSumColumns = jest.fn(); const mockConvertFilterRatioToFormulaColumn = jest.fn(); -const mockConvertToCounterRateFormulaColumn = jest.fn(); +const mockConvertToCounterRateColumn = jest.fn(); const mockConvertOtherAggsToFormulaColumn = jest.fn(); const mockConvertToLastValueColumn = jest.fn(); const mockConvertToStaticValueColumn = jest.fn(); @@ -30,7 +30,7 @@ jest.mock('../convert', () => ({ convertParentPipelineAggToColumns: jest.fn(() => mockConvertParentPipelineAggToColumns()), convertToCumulativeSumColumns: jest.fn(() => mockConvertToCumulativeSumColumns()), convertFilterRatioToFormulaColumn: jest.fn(() => mockConvertFilterRatioToFormulaColumn()), - convertToCounterRateFormulaColumn: jest.fn(() => mockConvertToCounterRateFormulaColumn()), + convertToCounterRateColumn: jest.fn(() => mockConvertToCounterRateColumn()), convertOtherAggsToFormulaColumn: jest.fn(() => mockConvertOtherAggsToFormulaColumn()), convertToLastValueColumn: jest.fn(() => mockConvertToLastValueColumn()), convertToStaticValueColumn: jest.fn(() => mockConvertToStaticValueColumn()), @@ -111,13 +111,13 @@ describe('getMetricsColumns', () => { mockConvertFilterRatioToFormulaColumn, ], [ - 'call convertToCounterRateFormulaColumn if metric type is positive rate', + 'call convertToCounterRateColumn if metric type is positive rate', [ createSeries({ metrics: [{ type: TSVB_METRIC_TYPES.POSITIVE_RATE, id: '1' }] }), dataView, 1, ], - mockConvertToCounterRateFormulaColumn, + mockConvertToCounterRateColumn, ], [ 'call convertOtherAggsToFormulaColumn if metric type is positive only', diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.ts index 3ac6952b561c4..822e5158effe7 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/metrics_columns.ts @@ -23,7 +23,7 @@ import { convertToLastValueColumn, convertToStaticValueColumn, convertMetricAggregationColumnWithoutSpecialParams, - convertToCounterRateFormulaColumn, + convertToCounterRateColumn, convertToStandartDeviationColumn, } from '../convert'; import { getValidColumns } from './columns'; @@ -98,7 +98,7 @@ export const getMetricsColumns = ( return getValidColumns(formulaColumn); } case 'positive_rate': { - const formulaColumn = convertToCounterRateFormulaColumn(columnsConverterArgs); + const formulaColumn = convertToCounterRateColumn(columnsConverterArgs); return getValidColumns(formulaColumn); } case 'positive_only':