Skip to content

Commit

Permalink
Add migration logging for gauges (elastic#38764) (elastic#38969)
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal authored Jun 14, 2019
1 parent 70f321e commit 0b00499
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 121 deletions.
9 changes: 5 additions & 4 deletions src/legacy/core_plugins/kibana/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ function removeDateHistogramTimeZones(doc) {

// migrate gauge verticalSplit to alignment
// https://github.com/elastic/kibana/issues/34636
function migrateGaugeVerticalSplitToAlignment(doc) {
function migrateGaugeVerticalSplitToAlignment(doc, logger) {
const visStateJSON = get(doc, 'attributes.visState');

if (visStateJSON) {
try {
const visState = JSON.parse(visStateJSON);
if (visState && visState.type === 'gauge') {
if (visState && visState.type === 'gauge' && !visState.params.gauge.alignment) {

visState.params.gauge.alignment = visState.params.gauge.verticalSplit ? 'vertical' : 'horizontal';
delete visState.params.gauge.verticalSplit;
Expand All @@ -182,7 +182,8 @@ function migrateGaugeVerticalSplitToAlignment(doc) {
};
}
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
logger.warning(`Exception @ migrateGaugeVerticalSplitToAlignment! ${e}`);
logger.warning(`Exception @ migrateGaugeVerticalSplitToAlignment! Payload: ${visStateJSON}`);
}
}
return doc;
Expand Down Expand Up @@ -415,7 +416,7 @@ export const migrations = {
},
'7.0.1': removeDateHistogramTimeZones,
'7.2.0': doc => executeMigrations720(doc),
'7.3.0': executeMigrations730,
'7.3.0': executeMigrations730
},
dashboard: {
'7.0.0': (doc) => {
Expand Down
249 changes: 132 additions & 117 deletions src/legacy/core_plugins/kibana/migrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,78 +68,78 @@ describe('visualization', () => {
visState: JSON.stringify({
aggs: [
{
'enabled': true,
'id': '1',
'params': {
enabled: true,
id: '1',
params: {
// Doesn't make much sense but we want to test it's not removing it from anything else
time_zone: 'Europe/Berlin',
},
'schema': 'metric',
'type': 'count'
schema: 'metric',
type: 'count',
},
{
'enabled': true,
'id': '2',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'time_zone': 'Europe/Berlin',
'interval': 'auto',
'min_doc_count': 1,
'useNormalizedEsInterval': true
enabled: true,
id: '2',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
time_zone: 'Europe/Berlin',
interval: 'auto',
min_doc_count: 1,
useNormalizedEsInterval: true,
},
'schema': 'segment',
'type': 'date_histogram'
schema: 'segment',
type: 'date_histogram',
},
{
'enabled': true,
'id': '4',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'interval': 'auto',
'min_doc_count': 1,
'useNormalizedEsInterval': true
enabled: true,
id: '4',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
interval: 'auto',
min_doc_count: 1,
useNormalizedEsInterval: true,
},
'schema': 'segment',
'type': 'date_histogram'
schema: 'segment',
type: 'date_histogram',
},
{
'enabled': true,
'id': '3',
'params': {
'customBucket': {
'enabled': true,
'id': '1-bucket',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'interval': 'auto',
'min_doc_count': 1,
'time_zone': 'Europe/Berlin',
'useNormalizedEsInterval': true
enabled: true,
id: '3',
params: {
customBucket: {
enabled: true,
id: '1-bucket',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
interval: 'auto',
min_doc_count: 1,
time_zone: 'Europe/Berlin',
useNormalizedEsInterval: true,
},
'type': 'date_histogram'
type: 'date_histogram',
},
customMetric: {
enabled: true,
id: '1-metric',
params: {},
type: 'count',
},
'customMetric': {
'enabled': true,
'id': '1-metric',
'params': {},
'type': 'count'
}
},
'schema': 'metric',
'type': 'max_bucket'
schema: 'metric',
type: 'max_bucket',
},
]
],
}),
}
},
};
});

Expand Down Expand Up @@ -726,75 +726,75 @@ Object {
visState: JSON.stringify({
aggs: [
{
'enabled': true,
'id': '1',
'params': {
'customInterval': '1h'
enabled: true,
id: '1',
params: {
customInterval: '1h',
},
'schema': 'metric',
'type': 'count'
schema: 'metric',
type: 'count',
},
{
'enabled': true,
'id': '2',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'interval': 'auto',
'min_doc_count': 1,
'useNormalizedEsInterval': true
enabled: true,
id: '2',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
interval: 'auto',
min_doc_count: 1,
useNormalizedEsInterval: true,
},
'schema': 'segment',
'type': 'date_histogram'
schema: 'segment',
type: 'date_histogram',
},
{
'enabled': true,
'id': '4',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'interval': 'custom',
'min_doc_count': 1,
'useNormalizedEsInterval': true
enabled: true,
id: '4',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
interval: 'custom',
min_doc_count: 1,
useNormalizedEsInterval: true,
},
'schema': 'segment',
'type': 'date_histogram'
schema: 'segment',
type: 'date_histogram',
},
{
'enabled': true,
'id': '3',
'params': {
'customBucket': {
'enabled': true,
'id': '1-bucket',
'params': {
'customInterval': '2h',
'drop_partials': false,
'extended_bounds': {},
'field': 'timestamp',
'interval': 'custom',
'min_doc_count': 1,
'useNormalizedEsInterval': true
enabled: true,
id: '3',
params: {
customBucket: {
enabled: true,
id: '1-bucket',
params: {
customInterval: '2h',
drop_partials: false,
extended_bounds: {},
field: 'timestamp',
interval: 'custom',
min_doc_count: 1,
useNormalizedEsInterval: true,
},
'type': 'date_histogram'
type: 'date_histogram',
},
customMetric: {
enabled: true,
id: '1-metric',
params: {},
type: 'count',
},
'customMetric': {
'enabled': true,
'id': '1-metric',
'params': {},
'type': 'count'
}
},
'schema': 'metric',
'type': 'max_bucket'
schema: 'metric',
type: 'max_bucket',
},
]
],
}),
}
},
};
});

Expand All @@ -807,7 +807,9 @@ Object {
it('should not change interval from date_histogram aggregations', () => {
const migratedDoc = migrate(doc);
const aggs = JSON.parse(migratedDoc.attributes.visState).aggs;
expect(aggs[1].params.interval).toBe(JSON.parse(doc.attributes.visState).aggs[1].params.interval);
expect(aggs[1].params.interval).toBe(
JSON.parse(doc.attributes.visState).aggs[1].params.interval
);
});

it('should not remove customInterval from non date_histogram aggregations', () => {
Expand All @@ -819,7 +821,9 @@ Object {
it('should set interval with customInterval value and remove customInterval when interval equals "custom"', () => {
const migratedDoc = migrate(doc);
const aggs = JSON.parse(migratedDoc.attributes.visState).aggs;
expect(aggs[2].params.interval).toBe(JSON.parse(doc.attributes.visState).aggs[2].params.customInterval);
expect(aggs[2].params.interval).toBe(
JSON.parse(doc.attributes.visState).aggs[2].params.customInterval
);
expect(aggs[2]).not.toHaveProperty('params.customInterval');
});

Expand All @@ -832,8 +836,9 @@ Object {
it('should remove customInterval from nested aggregations and set interval with customInterval value', () => {
const migratedDoc = migrate(doc);
const aggs = JSON.parse(migratedDoc.attributes.visState).aggs;
expect(aggs[3].params.customBucket.params.interval)
.toBe(JSON.parse(doc.attributes.visState).aggs[3].params.customBucket.params.customInterval);
expect(aggs[3].params.customBucket.params.interval).toBe(
JSON.parse(doc.attributes.visState).aggs[3].params.customBucket.params.customInterval
);
expect(aggs[3]).not.toHaveProperty('params.customBucket.params.customInterval');
});

Expand All @@ -844,7 +849,11 @@ Object {
});
});
describe('7.3.0', () => {
const migrate = doc => migrations.visualization['7.3.0'](doc);
const logMsgArr = [];
const logger = {
warning: msg => logMsgArr.push(msg),
};
const migrate = doc => migrations.visualization['7.3.0'](doc, logger);

it('migrates type = gauge verticalSplit: false to alignment: vertical', () => {
const migratedDoc = migrate({
Expand Down Expand Up @@ -876,7 +885,7 @@ Object {
`);
});

it('doesnt migrate type = gauge containing invalid visState object', () => {
it('doesnt migrate type = gauge containing invalid visState object, adds message to log', () => {
const migratedDoc = migrate({
attributes: {
visState: JSON.stringify({ type: 'gauge' }),
Expand All @@ -888,6 +897,12 @@ Object {
"visState": "{\\"type\\":\\"gauge\\"}",
},
}
`);
expect(logMsgArr).toMatchInlineSnapshot(`
Array [
"Exception @ migrateGaugeVerticalSplitToAlignment! TypeError: Cannot read property 'gauge' of undefined",
"Exception @ migrateGaugeVerticalSplitToAlignment! Payload: {\\"type\\":\\"gauge\\"}",
]
`);
});

Expand Down

0 comments on commit 0b00499

Please sign in to comment.