Skip to content

Commit 1687089

Browse files
committed
Collect VisLayers in VisualizeEmbeddable render flow
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent eeed599 commit 1687089

17 files changed

+356
-84
lines changed

src/plugins/vis_augmenter/common/types.ts

-60
This file was deleted.

src/plugins/vis_augmenter/public/expressions/vis_layers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { ExpressionTypeDefinition } from '../../../expressions';
7-
import { VisLayers } from '../../common';
7+
import { VisLayers } from '../';
88

99
const name = 'vis_layers';
1010

src/plugins/vis_augmenter/public/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ export {
1818
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
1919
} from './saved_augment_vis';
2020

21-
export { ISavedAugmentVis, VisLayerExpressionFn, AugmentVisSavedObject } from './types';
21+
export {
22+
ISavedAugmentVis,
23+
VisLayerExpressionFn,
24+
AugmentVisSavedObject,
25+
VisLayerFunctionDefinition,
26+
VisLayer,
27+
VisLayers,
28+
} from './types';
29+
30+
export * from './expressions';
31+
export * from './utils';

src/plugins/vis_augmenter/public/saved_augment_vis/saved_augment_vis.test.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { VisLayerExpressionFn } from '../types';
7-
import { VisLayerTypes } from '../../common';
6+
import { VisLayerExpressionFn, VisLayerTypes } from '../types';
87
import {
98
createSavedAugmentVisLoader,
109
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
@@ -19,15 +18,23 @@ describe('SavedObjectLoaderAugmentVis', () => {
1918
testArg: 'test-value',
2019
},
2120
} as VisLayerExpressionFn;
22-
const validObj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn);
23-
const validObj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn);
24-
const invalidFnTypeObj = generateAugmentVisSavedObject('invalid-fn-obj-id-1', {
25-
...fn,
26-
// @ts-ignore
27-
type: 'invalid-type',
28-
});
29-
// @ts-ignore
30-
const missingFnObj = generateAugmentVisSavedObject('missing-fn-obj-id-1', {});
21+
const validObj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn, 'test-vis-id');
22+
const validObj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn, 'test-vis-id');
23+
const invalidFnTypeObj = generateAugmentVisSavedObject(
24+
'invalid-fn-obj-id-1',
25+
{
26+
...fn,
27+
// @ts-ignore
28+
type: 'invalid-type',
29+
},
30+
'test-vis-id'
31+
);
32+
33+
const missingFnObj = generateAugmentVisSavedObject(
34+
'missing-fn-obj-id-1',
35+
{} as VisLayerExpressionFn,
36+
'test-vis-id'
37+
);
3138

3239
it('find returns single saved obj', async () => {
3340
const loader = createSavedAugmentVisLoader({

src/plugins/vis_augmenter/public/saved_augment_vis/saved_augment_vis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
SavedObjectOpenSearchDashboardsServices,
1010
} from '../../../saved_objects/public';
1111
import { createSavedAugmentVisClass } from './_saved_augment_vis';
12-
import { VisLayerTypes } from '../../common';
12+
import { VisLayerTypes } from '../types';
1313

1414
// eslint-disable-next-line @typescript-eslint/no-empty-interface
1515
export interface SavedObjectOpenSearchDashboardsServicesWithAugmentVis

src/plugins/vis_augmenter/public/saved_augment_vis/saved_augment_vis_references.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { extractReferences, injectReferences } from './saved_augment_vis_references';
6+
import {
7+
extractReferences,
8+
injectReferences,
9+
VIS_REFERENCE_NAME,
10+
} from './saved_augment_vis_references';
711
import { AugmentVisSavedObject } from '../types';
8-
import { VIS_REFERENCE_NAME } from './saved_augment_vis_references';
912

1013
describe('extractReferences()', () => {
1114
test('extracts nothing if visId is null', () => {

src/plugins/vis_augmenter/public/saved_augment_vis/utils/test_helpers.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ import { VisLayerExpressionFn, ISavedAugmentVis } from '../../types';
88
import { VIS_REFERENCE_NAME } from '../saved_augment_vis_references';
99

1010
const pluginResourceId = 'test-plugin-resource-id';
11-
const visId = 'test-vis-id';
11+
const title = 'test-title';
1212
const version = 1;
1313

14-
export const generateAugmentVisSavedObject = (idArg: string, exprFnArg: VisLayerExpressionFn) => {
14+
export const generateAugmentVisSavedObject = (
15+
idArg: string,
16+
exprFnArg: VisLayerExpressionFn,
17+
visIdArg: string
18+
) => {
1519
return {
1620
id: idArg,
21+
title,
1722
pluginResourceId,
1823
visLayerExpressionFn: exprFnArg,
1924
VIS_REFERENCE_NAME,
20-
visId,
25+
visId: visIdArg,
2126
version,
2227
} as ISavedAugmentVis;
2328
};

src/plugins/vis_augmenter/public/types.ts

+56-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,54 @@
44
*/
55

66
import { SavedObject } from '../../saved_objects/public';
7-
import { VisLayerTypes } from '../common';
7+
import { ExpressionFunctionDefinition } from '../../expressions';
8+
9+
export enum VisLayerTypes {
10+
PointInTimeEvents = 'PointInTimeEvents',
11+
}
12+
13+
export type PluginResourceType = string;
14+
15+
export interface PluginResource {
16+
type: PluginResourceType;
17+
id: string;
18+
name: string;
19+
urlPath: string;
20+
}
21+
22+
export interface VisLayer {
23+
type: keyof typeof VisLayerTypes;
24+
originPlugin: string;
25+
pluginResource: PluginResource;
26+
}
27+
28+
export type VisLayers = VisLayer[];
29+
30+
export interface EventMetadata {
31+
pluginResourceId: string;
32+
tooltip?: string;
33+
}
34+
35+
export interface PointInTimeEvent {
36+
timestamp: number;
37+
metadata: EventMetadata;
38+
}
39+
40+
export interface PointInTimeEventsVisLayer extends VisLayer {
41+
events: PointInTimeEvent[];
42+
}
43+
44+
export const isPointInTimeEventsVisLayer = (obj: any) => {
45+
return obj?.type === VisLayerTypes.PointInTimeEvents;
46+
};
47+
48+
export const isValidVisLayer = (obj: any) => {
49+
return obj?.type in VisLayerTypes;
50+
};
851

952
export interface ISavedAugmentVis {
1053
id?: string;
54+
title: string;
1155
description?: string;
1256
pluginResourceId: string;
1357
visName?: string;
@@ -24,3 +68,14 @@ export interface VisLayerExpressionFn {
2468
}
2569

2670
export interface AugmentVisSavedObject extends SavedObject, ISavedAugmentVis {}
71+
72+
export interface VisLayerResponseValue {
73+
visLayers: object;
74+
}
75+
76+
export type VisLayerFunctionDefinition = ExpressionFunctionDefinition<
77+
string,
78+
VisLayerResponseValue,
79+
any,
80+
Promise<VisLayerResponseValue>
81+
>;

src/plugins/vis_augmenter/common/index.ts src/plugins/vis_augmenter/public/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
export * from './types';
6+
export * from './utils';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { Vis } from '../../../visualizations/public';
7+
import {
8+
buildPipelineFromAugmentVisSavedObjs,
9+
getAugmentVisSavedObjs,
10+
isEligibleForVisLayers,
11+
} from './utils';
12+
import { VisLayerTypes, ISavedAugmentVis, VisLayerExpressionFn } from '../types';
13+
import {
14+
createSavedAugmentVisLoader,
15+
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
16+
getMockAugmentVisSavedObjectClient,
17+
generateAugmentVisSavedObject,
18+
} from '../saved_augment_vis';
19+
20+
describe('utils', () => {
21+
// TODO: redo / update this test suite when eligibility is finalized.
22+
// Tracked in https://github.com/opensearch-project/OpenSearch-Dashboards/issues/3268
23+
describe('isEligibleForVisLayers', () => {
24+
it('vis is ineligible with invalid type', async () => {
25+
const vis = ({
26+
params: {
27+
type: 'not-line',
28+
},
29+
} as unknown) as Vis;
30+
expect(isEligibleForVisLayers(vis)).toEqual(false);
31+
});
32+
it('vis is eligible with valid type', async () => {
33+
const vis = ({
34+
params: {
35+
type: 'line',
36+
},
37+
} as unknown) as Vis;
38+
expect(isEligibleForVisLayers(vis)).toEqual(true);
39+
});
40+
});
41+
42+
describe('getAugmentVisSavedObjs', () => {
43+
const fn = {
44+
type: VisLayerTypes.PointInTimeEvents,
45+
name: 'test-fn',
46+
args: {
47+
testArg: 'test-value',
48+
},
49+
} as VisLayerExpressionFn;
50+
const visId1 = 'test-vis-id-1';
51+
const visId2 = 'test-vis-id-2';
52+
const visId3 = 'test-vis-id-3';
53+
const obj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn, visId1);
54+
const obj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn, visId1);
55+
const obj3 = generateAugmentVisSavedObject('valid-obj-id-3', fn, visId2);
56+
57+
it('returns no matching saved objs with filtering', async () => {
58+
const loader = createSavedAugmentVisLoader({
59+
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]),
60+
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
61+
expect((await getAugmentVisSavedObjs(visId3, loader)).length).toEqual(0);
62+
});
63+
it('returns no matching saved objs when client returns empty list', async () => {
64+
const loader = createSavedAugmentVisLoader({
65+
savedObjectsClient: getMockAugmentVisSavedObjectClient([]),
66+
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
67+
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(0);
68+
});
69+
it('returns one matching saved obj', async () => {
70+
const loader = createSavedAugmentVisLoader({
71+
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1]),
72+
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
73+
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(1);
74+
});
75+
it('returns multiple matching saved objs without filtering', async () => {
76+
const loader = createSavedAugmentVisLoader({
77+
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2]),
78+
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
79+
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2);
80+
});
81+
it('returns multiple matching saved objs with filtering', async () => {
82+
const loader = createSavedAugmentVisLoader({
83+
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]),
84+
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
85+
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2);
86+
});
87+
});
88+
89+
describe('buildPipelineFromAugmentVisSavedObjs', () => {
90+
const obj1 = {
91+
title: 'obj1',
92+
pluginResourceId: 'obj-1-resource-id',
93+
visLayerExpressionFn: {
94+
type: VisLayerTypes.PointInTimeEvents,
95+
name: 'fn-1',
96+
args: {
97+
arg1: 'value-1',
98+
},
99+
},
100+
} as ISavedAugmentVis;
101+
const obj2 = {
102+
title: 'obj2',
103+
pluginResourceId: 'obj-2-resource-id',
104+
visLayerExpressionFn: {
105+
type: VisLayerTypes.PointInTimeEvents,
106+
name: 'fn-2',
107+
args: {
108+
arg2: 'value-2',
109+
},
110+
},
111+
} as ISavedAugmentVis;
112+
it('catches error with empty array', async () => {
113+
try {
114+
buildPipelineFromAugmentVisSavedObjs([]);
115+
} catch (e: any) {
116+
expect(
117+
e.message.includes(
118+
'Expression function from augment-vis saved objects could not be generated'
119+
)
120+
);
121+
}
122+
});
123+
it('builds with one saved obj', async () => {
124+
const str = buildPipelineFromAugmentVisSavedObjs([obj1]);
125+
expect(str).toEqual('fn-1 arg1="value-1"');
126+
});
127+
it('builds with multiple saved objs', async () => {
128+
const str = buildPipelineFromAugmentVisSavedObjs([obj1, obj2]);
129+
expect(str).toEqual(`fn-1 arg1="value-1"\n| fn-2 arg2="value-2"`);
130+
});
131+
});
132+
});

0 commit comments

Comments
 (0)