Skip to content

Commit

Permalink
Fixes TSVB aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed Mar 4, 2022
1 parent 13ecb5f commit 10a7714
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('getSeries', () => {
field: 'day_of_week_i',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'average',
Expand All @@ -44,7 +44,7 @@ describe('getSeries', () => {
},
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand All @@ -71,7 +71,7 @@ describe('getSeries', () => {
field: '123456',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand All @@ -97,7 +97,7 @@ describe('getSeries', () => {
field: '123456',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand All @@ -122,7 +122,7 @@ describe('getSeries', () => {
field: '123456',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'cumulative_sum',
Expand All @@ -147,7 +147,7 @@ describe('getSeries', () => {
field: '123456',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand All @@ -174,7 +174,7 @@ describe('getSeries', () => {
unit: '1m',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'differences',
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('getSeries', () => {
window: 6,
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'moving_average',
Expand Down Expand Up @@ -246,7 +246,7 @@ describe('getSeries', () => {
window: 6,
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand Down Expand Up @@ -293,7 +293,7 @@ describe('getSeries', () => {
],
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'percentile',
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('getSeries', () => {
order_by: 'timestamp',
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'last_value',
Expand All @@ -357,7 +357,7 @@ describe('getSeries', () => {
size: 2,
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toBeNull();
});

Expand Down Expand Up @@ -415,7 +415,7 @@ describe('getSeries', () => {
],
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
getTimeScale,
} from './metrics_helpers';

export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metrics'] | null => {
export const getSeries = (
metrics: Metric[],
seriesNum: number
): VisualizeEditorLayersContext['metrics'] | null => {
const metricIdx = metrics.length - 1;
const aggregation = metrics[metricIdx].type;
const fieldName = metrics[metricIdx].field;
Expand Down Expand Up @@ -50,12 +53,10 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
const variables = metrics[mathMetricIdx].variables;
const layerMetricsArray = metrics;
if (!finalScript || !variables) return null;
const metricsWithoutMath = layerMetricsArray.filter((metric) => metric.type !== 'math');

// create the script
for (let layerMetricIdx = 0; layerMetricIdx < layerMetricsArray.length; layerMetricIdx++) {
if (layerMetricsArray[layerMetricIdx].type === 'math') {
continue;
}
for (let layerMetricIdx = 0; layerMetricIdx < metricsWithoutMath.length; layerMetricIdx++) {
const currentMetric = metrics[layerMetricIdx];
// We can only support top_hit with size 1
if (
Expand Down Expand Up @@ -102,7 +103,7 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
// percentile value is derived from the field Id. It has the format xxx-xxx-xxx-xxx[percentile]
const [fieldId, meta] = metrics[metricIdx]?.field?.split('[') ?? [];
const subFunctionMetric = metrics.find((metric) => metric.id === fieldId);
if (!subFunctionMetric) {
if (!subFunctionMetric || subFunctionMetric.type === 'static') {
return null;
}
const pipelineAgg = getPipelineAgg(subFunctionMetric);
Expand Down Expand Up @@ -185,12 +186,16 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
break;
}
case 'static': {
// Lens support reference lines only when at least one layer data exists
if (seriesNum === 1) {
return null;
}
const staticValue = metrics[metricIdx].value;
metricsArray = [
{
agg: aggregationMap.name,
isFullReference: aggregationMap.isFullReference,
fieldName: fieldName ?? 'document',
fieldName: 'document',
params: {
...(staticValue && { value: staticValue }),
},
Expand Down
11 changes: 4 additions & 7 deletions src/plugins/vis_types/timeseries/public/trigger_action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export const triggerTSVBtoLensConfiguration = async (
}
const layersConfiguration: { [key: string]: VisualizeEditorLayersContext } = {};
let seriesNum = 0;
model.series.forEach((series) => {
if (!series.hidden) seriesNum++;
});

// handle multiple layers/series
for (let layerIdx = 0; layerIdx < model.series.length; layerIdx++) {
const layer = model.series[layerIdx];
if (layer.hidden) continue;
seriesNum++;

const { indexPatternId, timeField } = await getDataSourceInfo(
model.index_pattern,
Expand All @@ -65,13 +67,8 @@ export const triggerTSVBtoLensConfiguration = async (
chartType = layerChartType !== 'line' ? `${layerChartType}_percentage_stacked` : 'line';
}

// Lens support reference lines only when at least one layer data exists
if (seriesNum === 1 && layer.metrics[layer.metrics.length - 1].type === 'static') {
return null;
}

// handle multiple metrics
let metricsArray = getSeries(layer.metrics);
let metricsArray = getSeries(layer.metrics, seriesNum);
if (!metricsArray) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const getParentPipelineSeries = (
// percentile value is derived from the field Id. It has the format xxx-xxx-xxx-xxx[percentile]
const [fieldId, meta] = currentMetric?.field?.split('[') ?? [];
const subFunctionMetric = metrics.find((metric) => metric.id === fieldId);
if (!subFunctionMetric) {
if (!subFunctionMetric || subFunctionMetric.type === 'static') {
return null;
}
const pipelineAgg = getPipelineAgg(subFunctionMetric);
Expand Down Expand Up @@ -184,7 +184,7 @@ export const getSiblingPipelineSeriesFormula = (
metrics: Metric[]
) => {
const subFunctionMetric = metrics.find((metric) => metric.id === currentMetric.field);
if (!subFunctionMetric) {
if (!subFunctionMetric || subFunctionMetric.type === 'static') {
return null;
}
const pipelineAggMap = SUPPORTED_METRICS[subFunctionMetric.type];
Expand Down Expand Up @@ -311,6 +311,9 @@ export const getFormulaEquivalent = (
case 'filter_ratio': {
return getFilterRatioFormula(currentMetric);
}
case 'static': {
return `${currentMetric.value}`;
}
default: {
return `${aggregation}(${currentMetric.field})`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { FieldFormatsStart } from 'src/plugins/field_formats/public';
import { ThemeServiceStart } from 'kibana/public';
import { KibanaThemeProvider } from '../../../../../src/plugins/kibana_react/public';
import { VIS_EVENT_TO_TRIGGER } from '../../../../../src/plugins/visualizations/public';
import { FillStyle } from '../../common/expressions/xy_chart';
import type { FillStyle } from '../../common/expressions/xy_chart';
import { getSuggestions } from './xy_suggestions';
import { XyToolbar } from './xy_config_panel';
import { DimensionEditor } from './xy_config_panel/dimension_editor';
Expand Down

0 comments on commit 10a7714

Please sign in to comment.