Skip to content

Commit 94fcc10

Browse files
committed
Refactor logic
Signed-off-by: Huy Nguyen <73027756+huyaboo@users.noreply.github.com>
1 parent c927981 commit 94fcc10

File tree

3 files changed

+77
-76
lines changed

3 files changed

+77
-76
lines changed

src/core/server/saved_objects/import/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const updateDataSourceNameInVegaSpec = (
1616
props: UpdateDataSourceNameInVegaSpecProps
1717
): string => {
1818
const { spec, spacing } = props;
19-
const stringifiedSpacing = !!spacing ? spacing : 2;
19+
const stringifiedSpacing = spacing || 2;
2020

2121
let parsedSpec = parseJSONSpec(spec);
2222
const isJSONString = !!parsedSpec;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"saved_objects": [
3+
{
4+
"id": "935afa20-e0cd-11e7-9d07-1398ccfcefa3",
5+
"type": "visualization",
6+
"updated_at": "2018-08-29T13:22:17.617Z",
7+
"version": "1",
8+
"migrationVersion": {},
9+
"attributes": {
10+
"title": "[Logs] Heatmap",
11+
"visState": "{\"title\":\"[Logs] Heatmap\",\"type\":\"heatmap\",\"params\":{\"type\":\"heatmap\",\"addTooltip\":true,\"addLegend\":true,\"enableHover\":true,\"legendPosition\":\"right\",\"times\":[],\"colorsNumber\":10,\"colorSchema\":\"Reds\",\"setColorRange\":false,\"colorsRange\":[],\"invertColors\":false,\"percentageMode\":false,\"valueAxes\":[{\"show\":false,\"id\":\"ValueAxis-1\",\"type\":\"value\",\"scale\":{\"type\":\"linear\",\"defaultYExtents\":false},\"labels\":{\"show\":false,\"rotate\":0,\"color\":\"#555\",\"overwriteColor\":false}}]},\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"cardinality\",\"schema\":\"metric\",\"params\":{\"field\":\"clientip\"}},{\"id\":\"3\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"group\",\"params\":{\"field\":\"geo.src\",\"size\":5,\"order\":\"desc\",\"orderBy\":\"1\",\"otherBucket\":false,\"otherBucketLabel\":\"Other\",\"missingBucket\":false,\"missingBucketLabel\":\"Missing\",\"customLabel\":\"Country Source\"}},{\"id\":\"2\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"hour_of_day\",\"size\":25,\"order\":\"asc\",\"orderBy\":\"_key\",\"otherBucket\":false,\"otherBucketLabel\":\"Other\",\"missingBucket\":false,\"missingBucketLabel\":\"Missing\",\"customLabel\":\"Hour of Day\"}}]}",
12+
"uiStateJSON": "{}",
13+
"description": "",
14+
"version": 1,
15+
"kibanaSavedObjectMeta": {
16+
"searchSourceJSON": "{\"index\":\"90943e30-9a47-11e8-b64d-95841ca0b247\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"kuery\"}}"
17+
}
18+
},
19+
"references": []
20+
},
21+
{
22+
"id": "some-id",
23+
"type": "visualization",
24+
"references": [],
25+
"attributes": {
26+
"title": "Some non-OpenSearch query Vega visualization",
27+
"description": "This is a sample Vega visualization without an OpenSearch query.",
28+
"kibanaSavedObjectMeta": {},
29+
"visState": "{\"title\":\"Vega Visualization without OpenSearch Query\",\"type\":\"vega\",\"params\":{\"spec\":\"{\\\"$schema\\\":\\\"https://vega.github.io/schema/vega-lite/v5.json\\\",\\\"description\\\":\\\"A simple scatter plot.\\\",\\\"data\\\":{\\\"values\\\":[{\\\"x\\\":1,\\\"y\\\":2},{\\\"x\\\":2,\\\"y\\\":3},{\\\"x\\\":3,\\\"y\\\":4},{\\\"x\\\":4,\\\"y\\\":5},{\\\"x\\\":5,\\\"y\\\":6},{\\\"x\\\":6,\\\"y\\\":7},{\\\"x\\\":7,\\\"y\\\":8},{\\\"x\\\":8,\\\"y\\\":9},{\\\"x\\\":9,\\\"y\\\":10}],\\\"mark\\\":\\\"point\\\",\\\"encoding\\\":{\\\"x\\\":{\\\"field\\\":\\\"x\\\",\\\"type\\\":\\\"quantitative\\\"},\\\"y\\\":{\\\"field\\\":\\\"y\\\",\\\"type\\\":\\\"quantitative\\\"}}},\\\"width\\\":400,\\\"height\\\":200}\"},\"aggs\":[]}",
30+
"uiStateJSON": "{}",
31+
"version": 1
32+
}
33+
},
34+
{
35+
"attributes": {
36+
"description": "",
37+
"kibanaSavedObjectMeta": {
38+
"searchSourceJSON": "{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[]}"
39+
},
40+
"title": "(Vega) Top destination count",
41+
"uiStateJSON": "{}",
42+
"version": 1,
43+
"visState": "{\"title\":\"(Vega) Top destination count\",\"type\":\"vega\",\"aggs\":[],\"params\":{\"spec\":\"{\\n $schema: https://vega.github.io/schema/vega-lite/v5.json\\n data: {\\n url: {\\n %context%: true\\n %timefield%: @timestamp\\n index: opensearch_dashboards_sample_data_logs\\n body: {\\n aggs: {\\n 1: {\\n terms: {\\n field: geo.dest\\n order: {\\n _count: desc\\n }\\n size: 5\\n }\\n }\\n }\\n }\\n }\\n format: {\\n property: aggregations.1.buckets\\n }\\n }\\n transform: [\\n {\\n calculate: datum.key\\n as: dest\\n }\\n {\\n calculate: datum.doc_count\\n as: count\\n }\\n ]\\n layer: [\\n {\\n mark: {\\n type: bar\\n tooltip: true\\n }\\n }\\n ]\\n encoding: {\\n x: {\\n field: count\\n type: quantitative\\n axis: {\\n title: Count\\n }\\n }\\n y: {\\n field: dest\\n type: nominal\\n axis: {\\n title: Dest\\n }\\n sort: -x\\n }\\n }\\n}\"}}"
44+
},
45+
"id": "f0d162c0-227b-11ee-b88b-47a93b5c527c",
46+
"migrationVersion": {
47+
"visualization": "7.10.0"
48+
},
49+
"references": [],
50+
"type": "visualization",
51+
"updated_at": "2023-07-25T19:39:50.773Z",
52+
"version": "WzgxLDFd"
53+
},
54+
{
55+
"id": "06cf9c40-9ee8-11e7-8711-e7a007dcef99",
56+
"type": "visualization",
57+
"updated_at": "2018-08-29T13:22:17.617Z",
58+
"version": "1",
59+
"migrationVersion": {},
60+
"attributes": {
61+
"title": "[Logs] Visitors Map",
62+
"visState": "{\"title\":\"[Logs] Visitors Map\",\"type\":\"vega\",\"aggs\":[],\"params\":{\"spec\":\"{\\n $schema: https://vega.github.io/schema/vega/v5.json\\n config: {\\n kibana: {type: \\\"map\\\", latitude: 30, longitude: -120, zoom: 3}\\n }\\n data: [\\n {\\n name: table\\n url: {\\n index: opensearch_dashboards_sample_data_logs\\n %context%: true\\n %timefield%: timestamp\\n body: {\\n size: 0\\n aggs: {\\n gridSplit: {\\n geotile_grid: {field: \\\"geo.coordinates\\\", precision: 5, size: 10000}\\n aggs: {\\n gridCentroid: {\\n geo_centroid: {\\n field: \\\"geo.coordinates\\\"\\n }\\n }\\n }\\n }\\n }\\n }\\n }\\n format: {property: \\\"aggregations.gridSplit.buckets\\\"}\\n transform: [\\n {\\n type: geopoint\\n projection: projection\\n fields: [\\n gridCentroid.location.lon\\n gridCentroid.location.lat\\n ]\\n }\\n ]\\n }\\n ]\\n scales: [\\n {\\n name: gridSize\\n type: linear\\n domain: {data: \\\"table\\\", field: \\\"doc_count\\\"}\\n range: [\\n 50\\n 1000\\n ]\\n }\\n {\\n name: bubbleColor\\n type: linear\\n domain: {\\n data: table\\n field: doc_count\\n }\\n range: [\\\"rgb(255, 255, 255)\\\",\\\"rgb(249, 212, 204)\\\",\\\"rgb(238, 170, 156)\\\", \\\"rgb(223, 129, 110)\\\"]\\n }\\n ]\\n marks: [\\n {\\n name: gridMarker\\n type: symbol\\n from: {data: \\\"table\\\"}\\n encode: {\\n update: {\\n fill: {\\n scale: bubbleColor\\n field: doc_count\\n }\\n size: {scale: \\\"gridSize\\\", field: \\\"doc_count\\\"}\\n xc: {signal: \\\"datum.x\\\"}\\n yc: {signal: \\\"datum.y\\\"}\\n tooltip: {\\n signal: \\\"{flights: datum.doc_count}\\\"\\n }\\n }\\n }\\n }\\n ]\\n}\"}}",
63+
"uiStateJSON": "{}",
64+
"description": "",
65+
"version": 1,
66+
"kibanaSavedObjectMeta": {
67+
"searchSourceJSON": "{\"index\":\"90943e30-9a47-11e8-b64d-95841ca0b247\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"kuery\"}}"
68+
}
69+
},
70+
"references": []
71+
}
72+
]
73+
}

src/plugins/home/server/services/sample_data/data_sets/util.test.ts

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,12 @@
55

66
import { getSavedObjectsWithDataSource } from './util';
77
import { SavedObject, updateDataSourceNameInVegaSpec } from '../../../../../../core/server';
8+
import visualizationObjects from './test_utils/visualization_objects.json';
89

910
describe('getSavedObjectsWithDataSource()', () => {
1011
const getVisualizationSavedObjects = (): Array<SavedObject<any>> => {
11-
return [
12-
{
13-
id: '935afa20-e0cd-11e7-9d07-1398ccfcefa3',
14-
type: 'visualization',
15-
updated_at: '2018-08-29T13:22:17.617Z',
16-
version: '1',
17-
migrationVersion: {},
18-
attributes: {
19-
title: '[Logs] Heatmap',
20-
visState:
21-
'{"title":"[Logs] Heatmap","type":"heatmap","params":{"type":"heatmap","addTooltip":true,"addLegend":true,"enableHover":true,"legendPosition":"right","times":[],"colorsNumber":10,"colorSchema":"Reds","setColorRange":false,"colorsRange":[],"invertColors":false,"percentageMode":false,"valueAxes":[{"show":false,"id":"ValueAxis-1","type":"value","scale":{"type":"linear","defaultYExtents":false},"labels":{"show":false,"rotate":0,"color":"#555","overwriteColor":false}}]},"aggs":[{"id":"1","enabled":true,"type":"cardinality","schema":"metric","params":{"field":"clientip"}},{"id":"3","enabled":true,"type":"terms","schema":"group","params":{"field":"geo.src","size":5,"order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","missingBucket":false,"missingBucketLabel":"Missing","customLabel":"Country Source"}},{"id":"2","enabled":true,"type":"terms","schema":"segment","params":{"field":"hour_of_day","size":25,"order":"asc","orderBy":"_key","otherBucket":false,"otherBucketLabel":"Other","missingBucket":false,"missingBucketLabel":"Missing","customLabel":"Hour of Day"}}]}',
22-
uiStateJSON: '{}',
23-
description: '',
24-
version: 1,
25-
kibanaSavedObjectMeta: {
26-
searchSourceJSON:
27-
'{"index":"90943e30-9a47-11e8-b64d-95841ca0b247","filter":[],"query":{"query":"","language":"kuery"}}',
28-
},
29-
},
30-
references: [],
31-
},
32-
{
33-
id: 'some-id',
34-
type: 'visualization',
35-
references: [],
36-
attributes: {
37-
title: 'Some non-OpenSearch query Vega visualization',
38-
description: 'This is a sample Vega visualization without an OpenSearch query.',
39-
kibanaSavedObjectMeta: {},
40-
visState:
41-
'{"title":"Vega Visualization without OpenSearch Query","type":"vega","params":{"spec":"{\\"$schema\\":\\"https://vega.github.io/schema/vega-lite/v5.json\\",\\"description\\":\\"A simple scatter plot.\\",\\"data\\":{\\"values\\":[{\\"x\\":1,\\"y\\":2},{\\"x\\":2,\\"y\\":3},{\\"x\\":3,\\"y\\":4},{\\"x\\":4,\\"y\\":5},{\\"x\\":5,\\"y\\":6},{\\"x\\":6,\\"y\\":7},{\\"x\\":7,\\"y\\":8},{\\"x\\":8,\\"y\\":9},{\\"x\\":9,\\"y\\":10}],\\"mark\\":\\"point\\",\\"encoding\\":{\\"x\\":{\\"field\\":\\"x\\",\\"type\\":\\"quantitative\\"},\\"y\\":{\\"field\\":\\"y\\",\\"type\\":\\"quantitative\\"}}},\\"width\\":400,\\"height\\":200}"},"aggs":[]}',
42-
uiStateJSON: '{}',
43-
version: 1,
44-
},
45-
},
46-
{
47-
attributes: {
48-
description: '',
49-
kibanaSavedObjectMeta: {
50-
searchSourceJSON: '{"query":{"query":"","language":"kuery"},"filter":[]}',
51-
},
52-
title: '(Vega) Top destination count',
53-
uiStateJSON: '{}',
54-
version: 1,
55-
visState:
56-
'{"title":"(Vega) Top destination count","type":"vega","aggs":[],"params":{"spec":"{\\n $schema: https://vega.github.io/schema/vega-lite/v5.json\\n data: {\\n url: {\\n %context%: true\\n %timefield%: @timestamp\\n index: opensearch_dashboards_sample_data_logs\\n body: {\\n aggs: {\\n 1: {\\n terms: {\\n field: geo.dest\\n order: {\\n _count: desc\\n }\\n size: 5\\n }\\n }\\n }\\n }\\n }\\n format: {\\n property: aggregations.1.buckets\\n }\\n }\\n transform: [\\n {\\n calculate: datum.key\\n as: dest\\n }\\n {\\n calculate: datum.doc_count\\n as: count\\n }\\n ]\\n layer: [\\n {\\n mark: {\\n type: bar\\n tooltip: true\\n }\\n }\\n ]\\n encoding: {\\n x: {\\n field: count\\n type: quantitative\\n axis: {\\n title: Count\\n }\\n }\\n y: {\\n field: dest\\n type: nominal\\n axis: {\\n title: Dest\\n }\\n sort: -x\\n }\\n }\\n}"}}',
57-
},
58-
id: 'f0d162c0-227b-11ee-b88b-47a93b5c527c',
59-
migrationVersion: { visualization: '7.10.0' },
60-
references: [],
61-
type: 'visualization',
62-
updated_at: '2023-07-25T19:39:50.773Z',
63-
version: 'WzgxLDFd',
64-
},
65-
{
66-
id: '06cf9c40-9ee8-11e7-8711-e7a007dcef99',
67-
type: 'visualization',
68-
updated_at: '2018-08-29T13:22:17.617Z',
69-
version: '1',
70-
migrationVersion: {},
71-
attributes: {
72-
title: '[Logs] Visitors Map',
73-
visState:
74-
'{"title":"[Logs] Visitors Map","type":"vega","aggs":[],"params":{"spec":"{\\n $schema: https://vega.github.io/schema/vega/v5.json\\n config: {\\n kibana: {type: \\"map\\", latitude: 30, longitude: -120, zoom: 3}\\n }\\n data: [\\n {\\n name: table\\n url: {\\n index: opensearch_dashboards_sample_data_logs\\n %context%: true\\n %timefield%: timestamp\\n body: {\\n size: 0\\n aggs: {\\n gridSplit: {\\n geotile_grid: {field: \\"geo.coordinates\\", precision: 5, size: 10000}\\n aggs: {\\n gridCentroid: {\\n geo_centroid: {\\n field: \\"geo.coordinates\\"\\n }\\n }\\n }\\n }\\n }\\n }\\n }\\n format: {property: \\"aggregations.gridSplit.buckets\\"}\\n transform: [\\n {\\n type: geopoint\\n projection: projection\\n fields: [\\n gridCentroid.location.lon\\n gridCentroid.location.lat\\n ]\\n }\\n ]\\n }\\n ]\\n scales: [\\n {\\n name: gridSize\\n type: linear\\n domain: {data: \\"table\\", field: \\"doc_count\\"}\\n range: [\\n 50\\n 1000\\n ]\\n }\\n {\\n name: bubbleColor\\n type: linear\\n domain: {\\n data: table\\n field: doc_count\\n }\\n range: [\\"rgb(255, 255, 255)\\",\\"rgb(249, 212, 204)\\",\\"rgb(238, 170, 156)\\", \\"rgb(223, 129, 110)\\"]\\n }\\n ]\\n marks: [\\n {\\n name: gridMarker\\n type: symbol\\n from: {data: \\"table\\"}\\n encode: {\\n update: {\\n fill: {\\n scale: bubbleColor\\n field: doc_count\\n }\\n size: {scale: \\"gridSize\\", field: \\"doc_count\\"}\\n xc: {signal: \\"datum.x\\"}\\n yc: {signal: \\"datum.y\\"}\\n tooltip: {\\n signal: \\"{flights: datum.doc_count}\\"\\n }\\n }\\n }\\n }\\n ]\\n}"}}',
75-
uiStateJSON: '{}',
76-
description: '',
77-
version: 1,
78-
kibanaSavedObjectMeta: {
79-
searchSourceJSON:
80-
'{"index":"90943e30-9a47-11e8-b64d-95841ca0b247","filter":[],"query":{"query":"","language":"kuery"}}',
81-
},
82-
},
83-
references: [],
84-
},
85-
];
12+
// @ts-expect-error
13+
return visualizationObjects.saved_objects;
8614
};
8715

8816
test('when processing Vega Visualization saved objects, it should attach data_source_name to each OpenSearch query', () => {

0 commit comments

Comments
 (0)