-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
visualization_saved_object_migrations.ts
1309 lines (1182 loc) · 38.2 KB
/
visualization_saved_object_migrations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { cloneDeep, get, omit, has, flow, forOwn, mapValues } from 'lodash';
import type { SavedObjectMigrationFn, SavedObjectMigrationMap } from '@kbn/core/server';
import { mergeSavedObjectMigrationMaps } from '@kbn/core/server';
import { MigrateFunctionsObject, MigrateFunction } from '@kbn/kibana-utils-plugin/common';
import {
DEFAULT_QUERY_LANGUAGE,
isSerializedSearchSource,
SerializedSearchSourceFields,
} from '@kbn/data-plugin/common';
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common';
import {
commonAddSupportOfDualIndexSelectionModeInTSVB,
commonHideTSVBLastValueIndicator,
commonRemoveDefaultIndexPatternAndTimeFieldFromTSVBModel,
commonMigrateVislibPie,
commonAddEmptyValueColorRule,
commonMigrateTagCloud,
commonAddDropLastBucketIntoTSVBModel,
commonAddDropLastBucketIntoTSVBModel714Above,
commonRemoveMarkdownLessFromTSVB,
commonUpdatePieVisApi,
commonPreserveOldLegendSizeDefault,
commonRemoveExclamationCircleIcon,
} from './visualization_common_migrations';
import { VisualizationSavedObjectAttributes } from '../../common/content_management';
const migrateIndexPattern: SavedObjectMigrationFn<any, any> = (doc) => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
if (typeof searchSourceJSON !== 'string') {
return doc;
}
let searchSource;
try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}
if (searchSource.index && Array.isArray(doc.references)) {
searchSource.indexRefName = 'kibanaSavedObjectMeta.searchSourceJSON.index';
doc.references.push({
name: searchSource.indexRefName,
type: DATA_VIEW_SAVED_OBJECT_TYPE,
id: searchSource.index,
});
delete searchSource.index;
}
if (searchSource.filter) {
searchSource.filter.forEach((filterRow: any, i: number) => {
if (!filterRow.meta || !filterRow.meta.index || !Array.isArray(doc.references)) {
return;
}
filterRow.meta.indexRefName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${i}].meta.index`;
doc.references.push({
name: filterRow.meta.indexRefName,
type: DATA_VIEW_SAVED_OBJECT_TYPE,
id: filterRow.meta.index,
});
delete filterRow.meta.index;
});
}
doc.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
return doc;
};
// [TSVB] Migrate percentile-rank aggregation (value -> values)
const migratePercentileRankAggregation: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.type === 'metrics') {
const series: any[] = get(visState, 'params.series') || [];
series.forEach((part) => {
(part.metrics || []).forEach((metric: any) => {
if (metric.type === 'percentile_rank' && has(metric, 'value')) {
metric.values = [metric.value];
delete metric.value;
}
});
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
// [TSVB] Replace string query with object
const migrateFilterRatioQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.type === 'metrics') {
const series: any[] = get(visState, 'params.series') || [];
series.forEach((part) => {
(part.metrics || []).forEach((metric: any) => {
if (metric.type === 'filter_ratio') {
if (typeof metric.numerator === 'string') {
metric.numerator = { query: metric.numerator, language: 'lucene' };
}
if (typeof metric.denominator === 'string') {
metric.denominator = { query: metric.denominator, language: 'lucene' };
}
}
});
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
// [TSVB] Remove stale opperator key
const migrateOperatorKeyTypo: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.type === 'metrics') {
const gaugeColorRules: any[] = get(visState, 'params.gauge_color_rules') || [];
gaugeColorRules.forEach((colorRule) => {
if (colorRule.opperator) {
delete colorRule.opperator;
}
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
/**
* Moving setting wether to do a row or column split to vis.params
*
* @see https://github.com/elastic/kibana/pull/58462/files#diff-ae69fe15b20a5099d038e9bbe2ed3849
**/
const migrateSplitByChartRow: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState: any;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.aggs && visState.params) {
let row: boolean | undefined;
visState.aggs.forEach((agg: any) => {
if (agg.type === 'terms' && agg.schema === 'split' && 'row' in agg.params) {
row = agg.params.row;
delete agg.params.row;
}
});
if (row !== undefined) {
visState.params.row = row;
}
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
// Migrate date histogram aggregation (remove customInterval)
const migrateDateHistogramAggregation: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.aggs) {
visState.aggs.forEach((agg: any) => {
if (agg.type === 'date_histogram' && agg.params) {
if (agg.params.interval === 'custom') {
agg.params.interval = agg.params.customInterval;
}
delete agg.params.customInterval;
}
if (
get(agg, 'params.customBucket.type', null) === 'date_histogram' &&
agg.params.customBucket.params
) {
if (agg.params.customBucket.params.interval === 'custom') {
agg.params.customBucket.params.interval = agg.params.customBucket.params.customInterval;
}
delete agg.params.customBucket.params.customInterval;
}
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
// Migrate schemas inside aggregation (replace 'schema' object to name of the schema)
const migrateSchema: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
function replaceSchema(agg: any) {
forOwn(agg, (value: any, key: string) => {
if (typeof value === 'object') {
if (key === 'schema') {
agg[key] = value.name;
} else {
replaceSchema(value);
}
}
});
}
if (visState && visState.aggs) {
for (const agg of visState.aggs) {
if (typeof agg === 'object') {
replaceSchema(agg);
}
}
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
const removeDateHistogramTimeZones: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
let visState;
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.aggs) {
visState.aggs.forEach((agg: any) => {
// We're checking always for the existance of agg.params here. This should always exist, but better
// be safe then sorry during migrations.
if (agg.type === 'date_histogram' && agg.params) {
delete agg.params.time_zone;
}
if (
get(agg, 'params.customBucket.type', null) === 'date_histogram' &&
agg.params.customBucket.params
) {
delete agg.params.customBucket.params.time_zone;
}
});
doc.attributes.visState = JSON.stringify(visState);
}
}
return doc;
};
// migrate gauge verticalSplit to alignment
// https://github.com/elastic/kibana/issues/34636
const migrateGaugeVerticalSplitToAlignment: SavedObjectMigrationFn<any, any> = (doc, logger) => {
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
try {
const visState = JSON.parse(visStateJSON);
if (visState && visState.type === 'gauge' && !visState.params.gauge.alignment) {
visState.params.gauge.alignment = visState.params.gauge.verticalSplit
? 'vertical'
: 'horizontal';
delete visState.params.gauge.verticalSplit;
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
} catch (e) {
logger.log.warn(`Exception @ migrateGaugeVerticalSplitToAlignment! ${e}`);
logger.log.warn(`Exception @ migrateGaugeVerticalSplitToAlignment! Payload: ${visStateJSON}`);
}
}
return doc;
};
// Migrate filters (string -> { query: string, language: lucene })
/*
Enabling KQL in TSVB causes problems with savedObject visualizations when these are saved with filters.
In a visualisation type of saved object, if the visState param is of type metric, the filter is saved as a string that is not interpretted correctly as a lucene query in the visualization itself.
We need to transform the filter string into an object containing the original string as a query and specify the query language as lucene.
For Metrics visualizations (param.type === "metric"), filters can be applied to each series object in the series array within the SavedObject.visState.params object.
Path to the series array is thus:
attributes.visState.
*/
const transformFilterStringToQueryObject: SavedObjectMigrationFn<any, any> = (doc, logger) => {
// Migrate filters
// If any filters exist and they are a string, we assume it to be lucene and transform the filter into an object accordingly
const newDoc = cloneDeep(doc);
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
let visState;
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// let it go, the data is invalid and we'll leave it as is
}
if (visState) {
const visType = get(visState, 'params.type');
const tsvbTypes = ['metric', 'markdown', 'top_n', 'gauge', 'table', 'timeseries'];
if (tsvbTypes.indexOf(visType) === -1) {
// skip
return doc;
}
// migrate the params fitler
const params: any = get(visState, 'params');
if (params.filter && typeof params.filter === 'string') {
const paramsFilterObject = {
query: params.filter,
language: 'lucene',
};
params.filter = paramsFilterObject;
}
// migrate the annotations query string:
const annotations: any[] = get(visState, 'params.annotations') || [];
annotations.forEach((item) => {
if (!item.query_string) {
// we don't need to transform anything if there isn't a filter at all
return;
}
if (typeof item.query_string === 'string') {
const itemQueryStringObject = {
query: item.query_string,
language: 'lucene',
};
item.query_string = itemQueryStringObject;
}
});
// migrate the series filters
const series: any[] = get(visState, 'params.series') || [];
series.forEach((item) => {
if (!item.filter) {
// we don't need to transform anything if there isn't a filter at all
return;
}
// series item filter
if (typeof item.filter === 'string') {
const itemfilterObject = {
query: item.filter,
language: 'lucene',
};
item.filter = itemfilterObject;
}
// series item split filters filter
if (item.split_filters) {
const splitFilters: any[] = get(item, 'split_filters') || [];
splitFilters.forEach((filter) => {
if (!filter.filter) {
// we don't need to transform anything if there isn't a filter at all
return;
}
if (typeof filter.filter === 'string') {
const filterfilterObject = {
query: filter.filter,
language: 'lucene',
};
filter.filter = filterfilterObject;
}
});
}
});
newDoc.attributes.visState = JSON.stringify(visState);
}
}
return newDoc;
};
const transformSplitFiltersStringToQueryObject: SavedObjectMigrationFn<any, any> = (doc) => {
// Migrate split_filters in TSVB objects that weren't migrated in 7.3
// If any filters exist and they are a string, we assume them to be lucene syntax and transform the filter into an object accordingly
const newDoc = cloneDeep(doc);
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
let visState;
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// let it go, the data is invalid and we'll leave it as is
}
if (visState) {
const visType = get(visState, 'params.type');
const tsvbTypes = ['metric', 'markdown', 'top_n', 'gauge', 'table', 'timeseries'];
if (tsvbTypes.indexOf(visType) === -1) {
// skip
return doc;
}
// migrate the series split_filter filters
const series: any[] = get(visState, 'params.series') || [];
series.forEach((item) => {
// series item split filters filter
if (item.split_filters) {
const splitFilters: any[] = get(item, 'split_filters') || [];
if (splitFilters.length > 0) {
// only transform split_filter filters if we have filters
splitFilters.forEach((filter) => {
if (typeof filter.filter === 'string') {
const filterfilterObject = {
query: filter.filter,
language: 'lucene',
};
filter.filter = filterfilterObject;
}
});
}
}
});
newDoc.attributes.visState = JSON.stringify(visState);
}
}
return newDoc;
};
const migrateFiltersAggQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
try {
const visState = JSON.parse(visStateJSON);
if (visState && visState.aggs) {
visState.aggs.forEach((agg: any) => {
if (agg.type !== 'filters') return;
agg.params.filters.forEach((filter: any) => {
if (filter.input.language) return filter;
filter.input.language = 'lucene';
});
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
}
return doc;
};
const replaceMovAvgToMovFn: SavedObjectMigrationFn<any, any> = (doc, logger) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
if (visState && visState.type === 'metrics') {
const series: any[] = get(visState, 'params.series', []);
series.forEach((part) => {
if (part.metrics && Array.isArray(part.metrics)) {
part.metrics.forEach((metric: any) => {
if (metric.type === 'moving_average') {
metric.model_type = metric.model;
metric.alpha = get(metric, 'settings.alpha', 0.3);
metric.beta = get(metric, 'settings.beta', 0.1);
metric.gamma = get(metric, 'settings.gamma', 0.3);
metric.period = get(metric, 'settings.period', 1);
metric.multiplicative = get(metric, 'settings.type') === 'mult';
delete metric.minimize;
delete metric.model;
delete metric.settings;
delete metric.predict;
}
});
}
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
} catch (e) {
logger.log.warn(`Exception @ replaceMovAvgToMovFn! ${e}`);
logger.log.warn(`Exception @ replaceMovAvgToMovFn! Payload: ${visStateJSON}`);
}
}
return doc;
};
const migrateFiltersAggQueryStringQueries: SavedObjectMigrationFn<any, any> = (doc, logger) => {
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
try {
const visState = JSON.parse(visStateJSON);
if (visState && visState.aggs) {
visState.aggs.forEach((agg: any) => {
if (agg.type !== 'filters') return doc;
agg.params.filters.forEach((filter: any) => {
if (filter.input.query.query_string) {
filter.input.query = filter.input.query.query_string.query;
}
});
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
}
return doc;
};
const addDocReferences: SavedObjectMigrationFn<any, any> = (doc) => ({
...doc,
references: doc.references || [],
});
const migrateSavedSearch: SavedObjectMigrationFn<any, any> = (doc) => {
const savedSearchId = get(doc, 'attributes.savedSearchId');
if (savedSearchId && doc.references) {
doc.references.push({
type: 'search',
name: 'search_0',
id: savedSearchId,
});
doc.attributes.savedSearchRefName = 'search_0';
}
delete doc.attributes.savedSearchId;
return doc;
};
const migrateControls: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
if (visStateJSON) {
let visState;
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState) {
const controls: any[] = get(visState, 'params.controls') || [];
controls.forEach((control, i) => {
if (!control.indexPattern || !doc.references) {
return;
}
control.indexPatternRefName = `control_${i}_index_pattern`;
doc.references.push({
name: control.indexPatternRefName,
type: DATA_VIEW_SAVED_OBJECT_TYPE,
id: control.indexPattern,
});
delete control.indexPattern;
});
doc.attributes.visState = JSON.stringify(visState);
}
}
return doc;
};
const migrateTableSplits: SavedObjectMigrationFn<any, any> = (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
if (get(visState, 'type') !== 'table') {
return doc; // do nothing; we only want to touch tables
}
let splitCount = 0;
visState.aggs = visState.aggs.map((agg: any) => {
if (agg.schema !== 'split') {
return agg;
}
splitCount++;
if (splitCount === 1) {
return agg; // leave the first split agg unchanged
}
agg.schema = 'bucket';
// the `row` param is exclusively used by split aggs, so we remove it
agg.params = omit(agg.params, ['row']);
return agg;
});
if (splitCount <= 1) {
return doc; // do nothing; we only want to touch tables with multiple split aggs
}
const newDoc = cloneDeep(doc);
newDoc.attributes.visState = JSON.stringify(visState);
return newDoc;
} catch (e) {
throw new Error(`Failure attempting to migrate saved object '${doc.attributes.title}' - ${e}`);
}
};
/**
* This migration script is related to:
* @link https://github.com/elastic/kibana/pull/62194
* @link https://github.com/elastic/kibana/pull/14644
* This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3
*/
const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
if (searchSourceJSON) {
let searchSource: any;
try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}
if (searchSource.query?.match_all) {
return {
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
...searchSource,
query: {
query: '',
language: DEFAULT_QUERY_LANGUAGE,
},
}),
},
},
};
}
}
return doc;
};
// [TSVB] Default color palette is changing, keep the default for older viz
const migrateTsvbDefaultColorPalettes: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.type === 'metrics') {
const series: any[] = get(visState, 'params.series') || [];
series.forEach((part) => {
// The default value was not saved before
if (!part.split_color_mode) {
part.split_color_mode = 'gradient';
}
});
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
}
return doc;
};
// [TSVB] Remove serialized search source as it's not used in TSVB visualizations
const removeTSVBSearchSource: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState && visState.type === 'metrics' && searchSourceJSON !== '{}') {
return {
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
...get(doc, 'attributes.kibanaSavedObjectMeta'),
searchSourceJSON: '{}',
},
},
};
}
}
return doc;
};
const addSupportOfDualIndexSelectionModeInTSVB: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}
const newVisState = commonAddSupportOfDualIndexSelectionModeInTSVB(visState);
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(newVisState),
},
};
}
return doc;
};
// [Data table visualization] Enable toolbar by default
const enableDataTableVisToolbar: SavedObjectMigrationFn<any, any> = (doc) => {
let visState;
try {
visState = JSON.parse(doc.attributes.visState);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (visState?.type === 'table') {
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify({
...visState,
params: {
...visState.params,
showToolbar: true,
},
}),
},
};
}
return doc;
};
/**
* Decorate axes with default label filter value
*/
const decorateAxes = <T extends { labels: { filter?: boolean } }>(
axes: T[],
fallback: boolean
): T[] =>
axes.map((axis) => ({
...axis,
labels: {
...axis.labels,
filter: axis.labels.filter ?? fallback,
},
}));
/**
* Defaults circlesRadius to 1 if it is not configured
*/
const addCirclesRadius = <T extends { circlesRadius: number }>(axes: T[]): T[] =>
axes.map((axis) => {
const hasCircleRadiusAttribute = Number.isFinite(axis?.circlesRadius);
return {
...axis,
...(!hasCircleRadiusAttribute && {
circlesRadius: 1,
}),
};
});
// Inlined from vis_type_xy
const CHART_TYPE_AREA = 'area';
const CHART_TYPE_LINE = 'line';
const CHART_TYPE_HISTOGRAM = 'histogram';
/**
* Migrate vislib bar, line and area charts to use new vis_type_xy plugin
*/
const migrateVislibAreaLineBarTypes: SavedObjectMigrationFn<any, any> = (doc) => {
const visStateJSON = get(doc, 'attributes.visState');
let visState;
if (visStateJSON) {
try {
visState = JSON.parse(visStateJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
if (
visState &&
[CHART_TYPE_AREA, CHART_TYPE_LINE, CHART_TYPE_HISTOGRAM].includes(visState?.params?.type)
) {
const isHorizontalBar = visState.type === 'horizontal_bar';
const isLineOrArea =
visState?.params?.type === CHART_TYPE_AREA || visState?.params?.type === CHART_TYPE_LINE;
const hasPalette = visState?.params?.palette;
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify({
...visState,
params: {
...visState.params,
...(!hasPalette && {
palette: {
type: 'palette',
name: 'kibana_palette',
},
}),
categoryAxes:
visState.params.categoryAxes &&
decorateAxes(visState.params.categoryAxes, !isHorizontalBar),
valueAxes:
visState.params.valueAxes &&
decorateAxes(visState.params.valueAxes, isHorizontalBar),
seriesParams:
visState.params.seriesParams && addCirclesRadius(visState.params.seriesParams),
isVislibVis: true,
detailedTooltip: true,
...(isLineOrArea && {
fittingFunction: 'linear',
}),
},
}),
},
};
}
}
return doc;
};
/**
* [TSVB] Hide Last value indicator by default for all TSVB types except timeseries
*/
const hideTSVBLastValueIndicator: SavedObjectMigrationFn<any, any> = (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
const newVisState = commonHideTSVBLastValueIndicator(visState);
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(newVisState),
},
};
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
return doc;
};
const addDropLastBucketIntoTSVBModel: SavedObjectMigrationFn<any, any> = (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
const newVisState = commonAddDropLastBucketIntoTSVBModel(visState);
return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(newVisState),
},
};
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
return doc;
};
const addDropLastBucketIntoTSVBModel714Above: SavedObjectMigrationFn<any, any> = (doc) => {
try {
const visState = JSON.parse(doc.attributes.visState);
const newVisState = commonAddDropLastBucketIntoTSVBModel714Above(visState);
return {
...doc,