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

[TSVB2Lens] Supports static values #126903

Merged
merged 6 commits into from
Mar 9, 2022
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 @@ -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,10 +357,43 @@ describe('getSeries', () => {
size: 2,
},
] as Metric[];
const config = getSeries(metric);
const config = getSeries(metric, 1);
expect(config).toBeNull();
});

test('should return null for a static aggregation with 1 layer', () => {
const metric = [
{
id: '12345',
type: 'static',
value: '10',
},
] as Metric[];
const config = getSeries(metric, 1);
expect(config).toBeNull();
});

test('should return the correct config for a static aggregation with 2 layers', () => {
const metric = [
{
id: '12345',
type: 'static',
value: '10',
},
] as Metric[];
const config = getSeries(metric, 2);
expect(config).toStrictEqual([
{
agg: 'static_value',
fieldName: 'document',
isFullReference: true,
params: {
value: '10',
},
},
]);
});

test('should return the correct formula for the math aggregation with percentiles as variables', () => {
const metric = [
{
Expand Down Expand Up @@ -415,7 +448,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[],
totalSeriesNum: 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 @@ -184,6 +185,24 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
];
break;
}
case 'static': {
// Lens support reference lines only when at least one layer data exists
if (totalSeriesNum === 1) {
return null;
}
const staticValue = metrics[metricIdx].value;
metricsArray = [
{
agg: aggregationMap.name,
isFullReference: aggregationMap.isFullReference,
fieldName: 'document',
params: {
...(staticValue && { value: staticValue }),
},
},
];
break;
}
default: {
const timeScale = getTimeScale(metrics[metricIdx]);
metricsArray = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const triggerTSVBtoLensConfiguration = async (
return null;
}
const layersConfiguration: { [key: string]: VisualizeEditorLayersContext } = {};
// get the active series number
let seriesNum = 0;
model.series.forEach((series) => {
if (!series.hidden) seriesNum++;
});

// handle multiple layers/series
for (let layerIdx = 0; layerIdx < model.series.length; layerIdx++) {
Expand Down Expand Up @@ -64,7 +69,7 @@ export const triggerTSVBtoLensConfiguration = async (
}

// 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 @@ -92,4 +92,8 @@ export const SUPPORTED_METRICS: { [key: string]: AggOptions } = {
name: 'clamp',
isFullReference: true,
},
static: {
name: 'static_value',
isFullReference: true,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ export function getSuggestions({
},
currentVisualizationState,
subVisualizationId,
palette
palette,
visualizeTriggerFieldContext && 'isVisualizeAction' in visualizeTriggerFieldContext
);
});
})
Expand Down Expand Up @@ -205,7 +206,8 @@ function getVisualizationSuggestions(
datasourceSuggestion: DatasourceSuggestion & { datasourceId: string },
currentVisualizationState: unknown,
subVisualizationId?: string,
mainPalette?: PaletteOutput
mainPalette?: PaletteOutput,
isFromContext?: boolean
) {
return visualization
.getSuggestions({
Expand All @@ -214,6 +216,7 @@ function getVisualizationSuggestions(
keptLayerIds: datasourceSuggestion.keptLayerIds,
subVisualizationId,
mainPalette,
isFromContext,
})
.map(({ state, ...visualizationSuggestion }) => ({
...visualizationSuggestion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,61 @@ describe('IndexPattern Data Source suggestions', () => {
})
);
});

it('should apply a static layer if it is provided', () => {
const updatedContext = [
{
...context[0],
metrics: [
{
agg: 'static_value',
isFullReference: true,
fieldName: 'document',
params: {
value: '10',
},
color: '#68BC00',
},
],
},
];
const suggestions = getDatasourceSuggestionsForVisualizeCharts(
stateWithoutLayer(),
updatedContext
);

expect(suggestions).toContainEqual(
expect.objectContaining({
state: expect.objectContaining({
layers: {
id1: expect.objectContaining({
columnOrder: ['id2'],
columns: {
id2: expect.objectContaining({
operationType: 'static_value',
isStaticValue: true,
params: expect.objectContaining({
value: '10',
}),
}),
},
}),
},
}),
table: {
changeType: 'initial',
label: undefined,
isMultiRow: false,
columns: [
expect.objectContaining({
columnId: 'id2',
}),
],
layerId: 'id1',
},
})
);
});
});

describe('#getDatasourceSuggestionsForVisualizeField', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ function createNewTimeseriesLayerWithMetricAggregationFromVizEditor(
layer.format,
layer.label
);
// static values layers do not need a date histogram column
if (Object.values(computedLayer.columns)[0].isStaticValue) {
return computedLayer;
}

return insertNewColumn({
op: 'date_histogram',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,12 @@ export function insertNewColumn({
const possibleOperation = operationDefinition.getPossibleOperation();
const isBucketed = Boolean(possibleOperation?.isBucketed);
const addOperationFn = isBucketed ? addBucket : addMetric;
const buildColumnFn = columnParams
? operationDefinition.buildColumn({ ...baseOptions, layer }, columnParams)
: operationDefinition.buildColumn({ ...baseOptions, layer });

return updateDefaultLabels(
addOperationFn(
layer,
operationDefinition.buildColumn({ ...baseOptions, layer }),
columnId,
visualizationGroups,
targetGroup
),
addOperationFn(layer, buildColumnFn, columnId, visualizationGroups, targetGroup),
indexPattern
);
}
Expand Down
Loading