-
Notifications
You must be signed in to change notification settings - Fork 867
/
util.ts
195 lines (172 loc) · 6.06 KB
/
util.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { SavedObject } from 'opensearch-dashboards/server';
import {
extractVegaSpecFromSavedObject,
extractTimelineExpression,
updateDataSourceNameInVegaSpec,
updateDataSourceNameInTimeline,
} from '../../../../../../core/server';
import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types';
export const appendDataSourceId = (id: string) => {
return (dataSourceId?: string, workspaceId?: string) => {
const idWithDataSource = dataSourceId ? `${dataSourceId}_` + id : id;
if (!workspaceId) {
return idWithDataSource;
}
return `${workspaceId}_${idWithDataSource}`;
};
};
const overrideSavedObjectId = (savedObject: SavedObject, idGenerator: (id: string) => string) => {
savedObject.id = idGenerator(savedObject.id);
// update reference
if (savedObject.type === 'dashboard' || savedObject.type === 'visualization-visbuilder') {
savedObject.references.map((reference) => {
if (reference.id) {
reference.id = idGenerator(reference.id);
}
});
}
// update reference
if (savedObject.type === 'visualization' || savedObject.type === 'search') {
const searchSourceString = savedObject.attributes?.kibanaSavedObjectMeta?.searchSourceJSON;
const visStateString = savedObject.attributes?.visState;
if (searchSourceString) {
const searchSource = JSON.parse(searchSourceString);
if (searchSource.index) {
searchSource.index = idGenerator(searchSource.index);
savedObject.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(
searchSource
);
}
}
if (visStateString) {
const visState = JSON.parse(visStateString);
const controlList = visState.params?.controls;
if (controlList) {
controlList.map((control) => {
if (control.indexPattern) {
control.indexPattern = idGenerator(control.indexPattern);
}
});
}
savedObject.attributes.visState = JSON.stringify(visState);
}
}
};
export const getSavedObjectsWithDataSource = (
saveObjectList: SavedObject[],
dataSourceId?: string,
dataSourceTitle?: string
): SavedObject[] => {
if (dataSourceId) {
const idGenerator = (id: string) => `${dataSourceId}_${id}`;
return saveObjectList.map((saveObject) => {
overrideSavedObjectId(saveObject, idGenerator);
// update reference
if (saveObject.type === 'index-pattern') {
saveObject.references = [
{
id: `${dataSourceId}`,
type: 'data-source',
name: 'dataSource',
},
];
}
if (dataSourceTitle) {
if (
saveObject.type === 'dashboard' ||
saveObject.type === 'visualization' ||
saveObject.type === 'search' ||
saveObject.type === 'visualization-visbuilder'
) {
saveObject.attributes.title = saveObject.attributes.title + `_${dataSourceTitle}`;
}
if (saveObject.type === 'visualization') {
const vegaSpec = extractVegaSpecFromSavedObject(saveObject);
const visualizationSavedObject = saveObject as SavedObject & {
attributes: { visState: string };
};
const visStateObject = JSON.parse(visualizationSavedObject.attributes.visState);
if (!!vegaSpec) {
const updatedVegaSpec = updateDataSourceNameInVegaSpec({
spec: vegaSpec,
newDataSourceName: dataSourceTitle,
// Spacing of 1 prevents the Sankey visualization in logs data from exceeding the default url length and breaking
spacing: 1,
});
visStateObject.params.spec = updatedVegaSpec;
// @ts-expect-error
saveObject.attributes.visState = JSON.stringify(visStateObject);
saveObject.references.push({
id: `${dataSourceId}`,
type: 'data-source',
name: 'dataSource',
});
}
const timelineExpression = extractTimelineExpression(saveObject);
if (!!timelineExpression) {
// Get the timeline expression with the updated data source name
const modifiedExpression = updateDataSourceNameInTimeline(
timelineExpression,
dataSourceTitle
);
// @ts-expect-error
const timelineStateObject = JSON.parse(saveObject.attributes?.visState);
timelineStateObject.params.expression = modifiedExpression;
// @ts-expect-error
saveObject.attributes.visState = JSON.stringify(timelineStateObject);
}
if (visStateObject.type === 'metrics') {
visStateObject.params.data_source_id = dataSourceId;
visualizationSavedObject.attributes.visState = JSON.stringify(visStateObject);
visualizationSavedObject.references.push({
id: `${dataSourceId}`,
type: 'data-source',
name: 'dataSource',
});
}
}
}
return saveObject;
});
}
return saveObjectList;
};
export const overwriteSavedObjectsWithWorkspaceId = (
savedObjectList: SavedObject[],
workspaceId: string
) => {
const idGenerator = (id: string) => `${workspaceId}_${id}`;
savedObjectList.forEach((savedObject) => {
overrideSavedObjectId(savedObject, idGenerator);
});
return savedObjectList;
};
export const getFinalSavedObjects = ({
dataset,
workspaceId,
dataSourceId,
dataSourceTitle,
}: {
dataset: SampleDatasetSchema;
workspaceId?: string;
dataSourceId?: string;
dataSourceTitle?: string;
}) => {
if (workspaceId && dataSourceId) {
return overwriteSavedObjectsWithWorkspaceId(
dataset.getDataSourceIntegratedSavedObjects(dataSourceId, dataSourceTitle),
workspaceId
);
}
if (workspaceId) {
return dataset.getWorkspaceIntegratedSavedObjects(workspaceId);
}
if (dataSourceId) {
return dataset.getDataSourceIntegratedSavedObjects(dataSourceId, dataSourceTitle);
}
return dataset.savedObjects;
};