[TSVB] Update the series and metrics Ids that are numbers to strings (#113619) (#113772)

* [TSVB] Update the series and metrics Ids that are numbers to strings

* Minor changes

* Adds a unit test to TSVB plugin to test this case

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
This commit is contained in:
Kibana Machine 2021-10-04 13:21:34 -04:00 committed by GitHub
parent 525aa24d65
commit af114bf336
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 237 additions and 0 deletions

View file

@ -0,0 +1,126 @@
/*
* 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 { updateOldState } from '../../../visualizations/public';
/**
* The reason we add this test is to ensure that `convertNumIdsToStringsForTSVB` of the updateOldState runs correctly
* for the TSVB vis state. As the `updateOldState` runs on the visualizations plugin. a change to our objects structure can
* result to forget this case.
* Just for reference the `convertNumIdsToStringsForTSVB` finds and converts the series and metrics ids that have only digits to strings
* by adding an x prefix. Number ids are never been generated from the editor, only programmatically.
* See https://github.com/elastic/kibana/issues/113601.
*/
describe('TimeseriesVisState', () => {
test('should format the TSVB visState correctly', () => {
const visState = {
title: 'test',
type: 'metrics',
aggs: [],
params: {
time_range_mode: 'entire_time_range',
id: '0ecc58b1-30ba-43b9-aa3f-9ac32b482497',
type: 'timeseries',
series: [
{
id: '1',
color: '#68BC00',
split_mode: 'terms',
palette: {
type: 'palette',
name: 'default',
},
metrics: [
{
id: '10',
type: 'count',
},
],
separate_axis: 0,
axis_position: 'right',
formatter: 'default',
chart_type: 'line',
line_width: 1,
point_size: 1,
fill: 0.5,
stacked: 'none',
terms_field: 'Cancelled',
},
],
time_field: '',
use_kibana_indexes: true,
interval: '',
axis_position: 'left',
axis_formatter: 'number',
axis_scale: 'normal',
show_legend: 1,
truncate_legend: 1,
max_lines_legend: 1,
show_grid: 1,
tooltip_mode: 'show_all',
drop_last_bucket: 0,
isModelInvalid: false,
index_pattern: {
id: '665cd2c0-21d6-11ec-b42f-f7077c64d21b',
},
},
};
const newVisState = updateOldState(visState);
expect(newVisState).toEqual({
aggs: [],
params: {
axis_formatter: 'number',
axis_position: 'left',
axis_scale: 'normal',
drop_last_bucket: 0,
id: '0ecc58b1-30ba-43b9-aa3f-9ac32b482497',
index_pattern: {
id: '665cd2c0-21d6-11ec-b42f-f7077c64d21b',
},
interval: '',
isModelInvalid: false,
max_lines_legend: 1,
series: [
{
axis_position: 'right',
chart_type: 'line',
color: '#68BC00',
fill: 0.5,
formatter: 'default',
id: 'x1',
line_width: 1,
metrics: [
{
id: 'x10',
type: 'count',
},
],
palette: {
name: 'default',
type: 'palette',
},
point_size: 1,
separate_axis: 0,
split_mode: 'terms',
stacked: 'none',
terms_field: 'Cancelled',
},
],
show_grid: 1,
show_legend: 1,
time_field: '',
time_range_mode: 'entire_time_range',
tooltip_mode: 'show_all',
truncate_legend: 1,
type: 'timeseries',
use_kibana_indexes: true,
},
title: 'test',
type: 'metrics',
});
});
});

View file

@ -136,6 +136,30 @@ function convertSeriesParams(visState) {
];
}
/**
* This function is responsible for updating old TSVB visStates.
* Specifically, it identifies if the series and metrics ids are numbers
* and convert them to string with an x prefix. Number ids are never been generated
* from the editor, only programmatically. See https://github.com/elastic/kibana/issues/113601.
*/
function convertNumIdsToStringsForTSVB(visState) {
if (visState.params.series) {
visState.params.series.forEach((s) => {
const seriesId = s.id;
const metrics = s.metrics;
if (!isNaN(seriesId)) {
s.id = `x${seriesId}`;
}
metrics?.forEach((m) => {
const metricId = m.id;
if (!isNaN(metricId)) {
m.id = `x${metricId}`;
}
});
});
}
}
/**
* This function is responsible for updating old visStates - the actual saved object
* object - into the format, that will be required by the current Kibana version.
@ -155,6 +179,10 @@ export const updateOldState = (visState) => {
convertSeriesParams(newState);
}
if (visState.params && visState.type === 'metrics') {
convertNumIdsToStringsForTSVB(newState);
}
if (visState.type === 'gauge' && visState.fontSize) {
delete newState.fontSize;
set(newState, 'gauge.style.fontSize', visState.fontSize);

View file

@ -93,4 +93,87 @@ describe('updateOldState', () => {
expect(state.params.showMeticsAtAllLevels).toBe(undefined);
});
});
describe('TSVB ids conversion', () => {
it('should update the seriesId from number to string with x prefix', () => {
const oldState = {
type: 'metrics',
params: {
series: [
{
id: '10',
},
{
id: 'ABC',
},
{
id: 1,
},
],
},
};
const state = updateOldState(oldState);
expect(state.params.series).toEqual([
{
id: 'x10',
},
{
id: 'ABC',
},
{
id: 'x1',
},
]);
});
it('should update the metrics ids from number to string with x prefix', () => {
const oldState = {
type: 'metrics',
params: {
series: [
{
id: '10',
metrics: [
{
id: '1000',
},
{
id: '74a66e70-ac44-11eb-9865-6b616e971cf8',
},
],
},
{
id: 'ABC',
metrics: [
{
id: null,
},
],
},
],
},
};
const state = updateOldState(oldState);
expect(state.params.series).toEqual([
{
id: 'x10',
metrics: [
{
id: 'x1000',
},
{
id: '74a66e70-ac44-11eb-9865-6b616e971cf8',
},
],
},
{
id: 'ABC',
metrics: [
{
id: 'xnull',
},
],
},
]);
});
});
});