Skip to content

Commit

Permalink
Fixed pipeline and terms aggs. Added timeShift arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Sep 12, 2022
1 parent f98f3c9 commit 3e39342
Show file tree
Hide file tree
Showing 32 changed files with 322 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('getConfigurationForTimeseries', () => {
},
yLeftExtent: { mode: 'full' },
yRightExtent: { mode: 'full' },
yLeftScale: 'linear',
yRightScale: 'linear',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const getConfigurationForTimeseries = (
},
yLeftExtent: extents.yLeftExtent,
yRightExtent: extents.yRightExtent,
yLeftScale: model.axis_scale === 'log' ? 'log' : 'linear',
yRightScale: model.axis_scale === 'log' ? 'log' : 'linear',
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const isSupportedFormat = (format: string) => ['bytes', 'number', 'percent'].inc
export const getFormat = (series: Series): FormatParams => {
let suffix;

if (!series.formatter) {
if (!series.formatter || series.formatter === 'default') {
return {};
}

Expand All @@ -44,10 +44,12 @@ export const getFormat = (series: Series): FormatParams => {

// not supported formatters should be converted to number
if (!isSupportedFormat(series.formatter)) {
return { format: { id: DATA_FORMATTERS.NUMBER, ...(suffix && { params: { suffix } }) } };
return {
format: { id: DATA_FORMATTERS.NUMBER, ...(suffix && { params: { suffix, decimals: 2 } }) },
};
}

return { format: { id: series.formatter, ...(suffix && { params: { suffix } }) } };
return { format: { id: series.formatter, ...(suffix && { params: { suffix, decimals: 2 } }) } };
};

export const createColumn = (
Expand All @@ -63,6 +65,7 @@ export const createColumn = (
isSplit,
reducedTimeRange,
filter: series.filter,
timeShift: series.offset_time,
timeScale: getTimeScale(metric),
meta: { metricId: metric.id },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { METRIC_TYPES } from '@kbn/data-plugin/public';
import { SUPPORTED_METRICS, getParentPipelineSeriesFormula } from '../metrics';
import { SUPPORTED_METRICS, getPipelineSeriesFormula } from '../metrics';
import { createFormulaColumn } from './formula';
import { computeParentPipelineColumns } from './parent_pipeline';
import { CommonColumnsConverterArgs } from './types';
Expand All @@ -33,9 +33,10 @@ export const convertToCumulativeSumColumns = (
// and everything else as formula
if (pipelineAgg.name !== 'count' && pipelineAgg.name !== 'sum') {
const metaValue = Number(meta?.replace(']', ''));
formula = getParentPipelineSeriesFormula(metrics, subFunctionMetric, pipelineAgg, metric.type, {
formula = getPipelineSeriesFormula(metric, metrics, subFunctionMetric, {
metaValue,
reducedTimeRange,
timeShift: series.offset_time,
});
if (!formula) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const convertFilterRatioToFormulaColumn = (
reducedTimeRange?: string
): FormulaColumn | null => {
const metric = metrics[metrics.length - 1];
const formula = getFilterRatioFormula(metric, reducedTimeRange);
const formula = getFilterRatioFormula(metric, {
reducedTimeRange,
timeShift: series.offset_time,
});

if (!formula) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { FormulaParams } from '@kbn/visualizations-plugin/common/convert_to_lens
import { CommonColumnConverterArgs, CommonColumnsConverterArgs, FormulaColumn } from './types';
import { TSVB_METRIC_TYPES } from '../../../../common/enums';
import type { Metric } from '../../../../common/types';
import { getFormulaEquivalent, getSiblingPipelineSeriesFormula } from '../metrics';
import { getFormulaEquivalent, getPipelineSeriesFormula } from '../metrics';
import { createColumn, getFormat } from './column';
import { AdditionalFormulaArgs } from '../../types';

type OtherFormulaAggregations =
| typeof TSVB_METRIC_TYPES.POSITIVE_ONLY
Expand Down Expand Up @@ -43,15 +44,15 @@ const convertFormulaScriptForPercentileAggs = (
variables: Exclude<Metric['variables'], undefined>,
metric: Metric,
allAggMetrics: Metric[],
reducedTimeRange?: string
additionalArgs: AdditionalFormulaArgs
) => {
variables.forEach((variable) => {
const [_, meta] = variable?.field?.split('[') ?? [];
const metaValue = Number(meta?.replace(']', ''));
if (!metaValue) {
return;
}
const script = getFormulaEquivalent(metric, allAggMetrics, { metaValue, reducedTimeRange });
const script = getFormulaEquivalent(metric, allAggMetrics, { metaValue, ...additionalArgs });
if (!script) {
return;
}
Expand All @@ -65,9 +66,9 @@ const convertFormulaScriptForAggs = (
variables: Exclude<Metric['variables'], undefined>,
metric: Metric,
allAggMetrics: Metric[],
reducedTimeRange?: string
additionalArgs: AdditionalFormulaArgs
) => {
const script = getFormulaEquivalent(metric, allAggMetrics, { reducedTimeRange });
const script = getFormulaEquivalent(metric, allAggMetrics, { ...additionalArgs });
if (!script) {
return null;
}
Expand Down Expand Up @@ -107,21 +108,15 @@ export const convertMathToFormulaColumn = (

// should treat percentiles differently
if (notMathMetric.type === 'percentile' || notMathMetric.type === 'percentile_rank') {
script = convertFormulaScriptForPercentileAggs(
script!,
variables,
notMathMetric,
metrics,
reducedTimeRange
);
script = convertFormulaScriptForPercentileAggs(script!, variables, notMathMetric, metrics, {
reducedTimeRange,
timeShift: series.offset_time,
});
} else {
script = convertFormulaScriptForAggs(
script!,
variables,
notMathMetric,
metrics,
reducedTimeRange
);
script = convertFormulaScriptForAggs(script!, variables, notMathMetric, metrics, {
reducedTimeRange,
timeShift: series.offset_time,
});
}
}

Expand All @@ -143,8 +138,19 @@ export const convertOtherAggsToFormulaColumn = (
reducedTimeRange?: string
): FormulaColumn | null => {
const metric = metrics[metrics.length - 1];
const [fieldId, meta] = metric?.field?.split('[') ?? [];
const subFunctionMetric = metrics.find(({ id }) => id === fieldId);
const metaValue = meta ? Number(meta?.replace(']', '')) : undefined;

if (!subFunctionMetric) {
return null;
}

const formula = getSiblingPipelineSeriesFormula(aggregation, metric, metrics, reducedTimeRange);
const formula = getPipelineSeriesFormula(metric, metrics, subFunctionMetric, {
metaValue,
reducedTimeRange,
timeShift: series.offset_time,
});
if (!formula) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { convertToLastValueColumn } from './last_value';
export { convertToStaticValueColumn } from './static_value';
export { convertToFiltersColumn } from './filters';
export { convertToDateHistogramColumn } from './date_histogram';
export { converToTermsColumn } from './terms';
export { convertToTermsColumn } from './terms';
export { convertToCounterRateColumn } from './counter_rate';
export { convertToStandartDeviationColumn } from './std_deviation';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,16 +467,16 @@ describe('convertParentPipelineAggToColumns', () => {
series,
metrics: [
{ id, field, type: METRIC_TYPES.MAX },
{ id: id1, field: `${id}[75]`, type: METRIC_TYPES.AVG },
{ id: id2, field: `${id1}[50]`, type: TSVB_METRIC_TYPES.MOVING_AVERAGE },
{ id: id1, field: `${id}`, type: METRIC_TYPES.DERIVATIVE },
{ id: id2, field: `${id1}`, type: TSVB_METRIC_TYPES.MOVING_AVERAGE },
],
dataView,
},
],
{
meta: { metricId: 'some-id-2' },
operationType: 'formula',
params: { formula: 'moving_average(average(max(bytes)))' },
params: { formula: 'moving_average(differences(max(bytes)), window=5)' },
},
],
[
Expand All @@ -486,16 +486,16 @@ describe('convertParentPipelineAggToColumns', () => {
series,
metrics: [
{ id, field, type: METRIC_TYPES.MAX },
{ id: id1, field: `${id}[75]`, type: METRIC_TYPES.AVG },
{ id: id2, field: `${id1}[50]`, type: METRIC_TYPES.DERIVATIVE },
{ id: id1, field: `${id}`, type: TSVB_METRIC_TYPES.MOVING_AVERAGE },
{ id: id2, field: `${id1}`, type: METRIC_TYPES.DERIVATIVE },
],
dataView,
},
],
{
meta: { metricId: 'some-id-2' },
operationType: 'formula',
params: { formula: 'differences(average(max(bytes)))' },
params: { formula: 'differences(moving_average(max(bytes), window=5))' },
},
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import { Metric } from '../../../../common/types';
import {
getFilterRatioFormula,
getFormulaFromMetric,
getParentPipelineSeriesFormula,
SupportedMetric,
SUPPORTED_METRICS,
getPipelineSeriesFormula
} from '../metrics';
import { createColumn, getFormat } from './column';
import { createFormulaColumn } from './formula';
Expand Down Expand Up @@ -186,7 +186,10 @@ export const computeParentPipelineColumns = (
const aggFormula = getFormulaFromMetric(agg);

if (subFunctionMetric.type === 'filter_ratio') {
const script = getFilterRatioFormula(subFunctionMetric, reducedTimeRange);
const script = getFilterRatioFormula(subFunctionMetric, {
reducedTimeRange,
timeShift: series.offset_time,
});
if (!script) {
return null;
}
Expand Down Expand Up @@ -235,13 +238,11 @@ const convertMovingAvgOrDerivativeToColumns = (
// support nested aggs with formula
const additionalSubFunction = metrics.find(({ id }) => id === nestedFieldId);
if (additionalSubFunction || pipelineAgg.name === 'counter_rate') {
const formula = getParentPipelineSeriesFormula(
metrics,
subFunctionMetric,
pipelineAgg,
metric.type,
{ metaValue, reducedTimeRange }
);
const formula = getPipelineSeriesFormula(metric, metrics, subFunctionMetric, {
metaValue,
reducedTimeRange,
timeShift: series.offset_time,
});
if (!formula) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const createStandartDeviationFormulaColumn = (
metrics: Metric[],
reducedTimeRange?: string
) => {
const script = getFormulaEquivalent(metric, metrics, { reducedTimeRange });
const script = getFormulaEquivalent(metric, metrics, {
reducedTimeRange,
timeShift: series.offset_time,
});
if (!script) return null;
return createFormulaColumn(script, { series, metric, dataView });
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TermsParams,
} from '@kbn/visualizations-plugin/common/convert_to_lens';
import { createSeries } from '../__mocks__';
import { converToTermsColumn, convertToTermsParams } from './terms';
import { convertToTermsColumn, convertToTermsParams } from './terms';
import { Column } from './types';

describe('convertToTermsParams', () => {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('converToTermsColumn', () => {
test.each<
[
string,
Parameters<typeof converToTermsColumn>,
Parameters<typeof convertToTermsColumn>,
Partial<
Omit<TermsColumn, 'params'> & {
params: Partial<Omit<TermsColumn['params'], 'orderAgg'>> & {
Expand Down Expand Up @@ -275,9 +275,9 @@ describe('converToTermsColumn', () => {
],
])('should return %s', (_, input, expected) => {
if (expected === null) {
expect(converToTermsColumn(...input)).toBeNull();
expect(convertToTermsColumn(...input)).toBeNull();
} else {
expect(converToTermsColumn(...input)).toEqual(
expect(convertToTermsColumn(...input)).toEqual(
expect.objectContaining({
...expected,
params: expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const getOrderByWithAgg = (series: Series, columns: Column[]): OrderByWithAgg |
}

if (series.terms_order_by === '_count' || !series.terms_order_by) {
const columnId = uuid();
return {
orderBy: { type: 'column', columnId },
orderBy: { type: 'custom' },
orderAgg: {
operationType: 'count',
sourceField: 'document',
columnId,
columnId: uuid(),
isBucketed: true,
isSplit: false,
dataType: 'number',
Expand Down Expand Up @@ -83,7 +82,7 @@ export const convertToTermsParams = (
};
};

export const converToTermsColumn = (
export const convertToTermsColumn = (
termFields: [string, ...string[]],
series: Series,
columns: Column[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('buildCounterRateFormula', () => {

const formula = buildCounterRateFormula(
SUPPORTED_METRICS[metric.type]!.name,
dataView.fields[0].name
dataView.fields[0].name,
{}
);
expect(formula).toStrictEqual('counter_rate(max(bytes))');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
* Side Public License, v 1.
*/

const buildMaxFormula = (selector: string) => {
return `max(${selector})`;
import { addAdditionalArgs } from '.';
import { AdditionalFormulaArgs } from '../../types';

const buildMaxFormula = (selector: string, additionalArgs: AdditionalFormulaArgs) => {
return `max(${selector}${addAdditionalArgs(additionalArgs)})`;
};

const buildСounterRateFormula = (aggFormula: string, selector: string) => {
return `${aggFormula}(${selector})`;
};

export const buildCounterRateFormula = (aggFormula: string, fieldName: string) => {
const maxFormula = buildMaxFormula(fieldName);
export const buildCounterRateFormula = (
aggFormula: string,
fieldName: string,
additionalArgs: AdditionalFormulaArgs
) => {
const maxFormula = buildMaxFormula(fieldName, additionalArgs);

const counterRateFormula = buildСounterRateFormula(aggFormula, maxFormula);
return counterRateFormula;
Expand Down
Loading

0 comments on commit 3e39342

Please sign in to comment.