Skip to content

Commit

Permalink
Collect VisLayers in VisualizeEmbeddable render flow
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Feb 22, 2023
1 parent eeed599 commit 1687089
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 84 deletions.
60 changes: 0 additions & 60 deletions src/plugins/vis_augmenter/common/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/vis_augmenter/public/expressions/vis_layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { ExpressionTypeDefinition } from '../../../expressions';
import { VisLayers } from '../../common';
import { VisLayers } from '../';

const name = 'vis_layers';

Expand Down
12 changes: 11 additions & 1 deletion src/plugins/vis_augmenter/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ export {
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
} from './saved_augment_vis';

export { ISavedAugmentVis, VisLayerExpressionFn, AugmentVisSavedObject } from './types';
export {
ISavedAugmentVis,
VisLayerExpressionFn,
AugmentVisSavedObject,
VisLayerFunctionDefinition,
VisLayer,
VisLayers,
} from './types';

export * from './expressions';
export * from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { VisLayerExpressionFn } from '../types';
import { VisLayerTypes } from '../../common';
import { VisLayerExpressionFn, VisLayerTypes } from '../types';
import {
createSavedAugmentVisLoader,
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
Expand All @@ -19,15 +18,23 @@ describe('SavedObjectLoaderAugmentVis', () => {
testArg: 'test-value',
},
} as VisLayerExpressionFn;
const validObj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn);
const validObj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn);
const invalidFnTypeObj = generateAugmentVisSavedObject('invalid-fn-obj-id-1', {
...fn,
// @ts-ignore
type: 'invalid-type',
});
// @ts-ignore
const missingFnObj = generateAugmentVisSavedObject('missing-fn-obj-id-1', {});
const validObj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn, 'test-vis-id');
const validObj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn, 'test-vis-id');
const invalidFnTypeObj = generateAugmentVisSavedObject(
'invalid-fn-obj-id-1',
{
...fn,
// @ts-ignore
type: 'invalid-type',
},
'test-vis-id'
);

const missingFnObj = generateAugmentVisSavedObject(
'missing-fn-obj-id-1',
{} as VisLayerExpressionFn,
'test-vis-id'
);

it('find returns single saved obj', async () => {
const loader = createSavedAugmentVisLoader({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SavedObjectOpenSearchDashboardsServices,
} from '../../../saved_objects/public';
import { createSavedAugmentVisClass } from './_saved_augment_vis';
import { VisLayerTypes } from '../../common';
import { VisLayerTypes } from '../types';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SavedObjectOpenSearchDashboardsServicesWithAugmentVis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { extractReferences, injectReferences } from './saved_augment_vis_references';
import {
extractReferences,
injectReferences,
VIS_REFERENCE_NAME,
} from './saved_augment_vis_references';
import { AugmentVisSavedObject } from '../types';
import { VIS_REFERENCE_NAME } from './saved_augment_vis_references';

describe('extractReferences()', () => {
test('extracts nothing if visId is null', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import { VisLayerExpressionFn, ISavedAugmentVis } from '../../types';
import { VIS_REFERENCE_NAME } from '../saved_augment_vis_references';

const pluginResourceId = 'test-plugin-resource-id';
const visId = 'test-vis-id';
const title = 'test-title';
const version = 1;

export const generateAugmentVisSavedObject = (idArg: string, exprFnArg: VisLayerExpressionFn) => {
export const generateAugmentVisSavedObject = (
idArg: string,
exprFnArg: VisLayerExpressionFn,
visIdArg: string
) => {
return {
id: idArg,
title,
pluginResourceId,
visLayerExpressionFn: exprFnArg,
VIS_REFERENCE_NAME,
visId,
visId: visIdArg,
version,
} as ISavedAugmentVis;
};
Expand Down
57 changes: 56 additions & 1 deletion src/plugins/vis_augmenter/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,54 @@
*/

import { SavedObject } from '../../saved_objects/public';
import { VisLayerTypes } from '../common';
import { ExpressionFunctionDefinition } from '../../expressions';

export enum VisLayerTypes {
PointInTimeEvents = 'PointInTimeEvents',
}

export type PluginResourceType = string;

export interface PluginResource {
type: PluginResourceType;
id: string;
name: string;
urlPath: string;
}

export interface VisLayer {
type: keyof typeof VisLayerTypes;
originPlugin: string;
pluginResource: PluginResource;
}

export type VisLayers = VisLayer[];

export interface EventMetadata {
pluginResourceId: string;
tooltip?: string;
}

export interface PointInTimeEvent {
timestamp: number;
metadata: EventMetadata;
}

export interface PointInTimeEventsVisLayer extends VisLayer {
events: PointInTimeEvent[];
}

export const isPointInTimeEventsVisLayer = (obj: any) => {
return obj?.type === VisLayerTypes.PointInTimeEvents;
};

export const isValidVisLayer = (obj: any) => {
return obj?.type in VisLayerTypes;
};

export interface ISavedAugmentVis {
id?: string;
title: string;
description?: string;
pluginResourceId: string;
visName?: string;
Expand All @@ -24,3 +68,14 @@ export interface VisLayerExpressionFn {
}

export interface AugmentVisSavedObject extends SavedObject, ISavedAugmentVis {}

export interface VisLayerResponseValue {
visLayers: object;
}

export type VisLayerFunctionDefinition = ExpressionFunctionDefinition<
string,
VisLayerResponseValue,
any,
Promise<VisLayerResponseValue>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export * from './types';
export * from './utils';
132 changes: 132 additions & 0 deletions src/plugins/vis_augmenter/public/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Vis } from '../../../visualizations/public';
import {
buildPipelineFromAugmentVisSavedObjs,
getAugmentVisSavedObjs,
isEligibleForVisLayers,
} from './utils';
import { VisLayerTypes, ISavedAugmentVis, VisLayerExpressionFn } from '../types';
import {
createSavedAugmentVisLoader,
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
getMockAugmentVisSavedObjectClient,
generateAugmentVisSavedObject,
} from '../saved_augment_vis';

describe('utils', () => {
// TODO: redo / update this test suite when eligibility is finalized.
// Tracked in https://github.com/opensearch-project/OpenSearch-Dashboards/issues/3268
describe('isEligibleForVisLayers', () => {
it('vis is ineligible with invalid type', async () => {
const vis = ({
params: {
type: 'not-line',
},
} as unknown) as Vis;
expect(isEligibleForVisLayers(vis)).toEqual(false);
});
it('vis is eligible with valid type', async () => {
const vis = ({
params: {
type: 'line',
},
} as unknown) as Vis;
expect(isEligibleForVisLayers(vis)).toEqual(true);
});
});

describe('getAugmentVisSavedObjs', () => {
const fn = {
type: VisLayerTypes.PointInTimeEvents,
name: 'test-fn',
args: {
testArg: 'test-value',
},
} as VisLayerExpressionFn;
const visId1 = 'test-vis-id-1';
const visId2 = 'test-vis-id-2';
const visId3 = 'test-vis-id-3';
const obj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn, visId1);
const obj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn, visId1);
const obj3 = generateAugmentVisSavedObject('valid-obj-id-3', fn, visId2);

it('returns no matching saved objs with filtering', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId3, loader)).length).toEqual(0);
});
it('returns no matching saved objs when client returns empty list', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(0);
});
it('returns one matching saved obj', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(1);
});
it('returns multiple matching saved objs without filtering', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2);
});
it('returns multiple matching saved objs with filtering', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2);
});
});

describe('buildPipelineFromAugmentVisSavedObjs', () => {
const obj1 = {
title: 'obj1',
pluginResourceId: 'obj-1-resource-id',
visLayerExpressionFn: {
type: VisLayerTypes.PointInTimeEvents,
name: 'fn-1',
args: {
arg1: 'value-1',
},
},
} as ISavedAugmentVis;
const obj2 = {
title: 'obj2',
pluginResourceId: 'obj-2-resource-id',
visLayerExpressionFn: {
type: VisLayerTypes.PointInTimeEvents,
name: 'fn-2',
args: {
arg2: 'value-2',
},
},
} as ISavedAugmentVis;
it('catches error with empty array', async () => {
try {
buildPipelineFromAugmentVisSavedObjs([]);
} catch (e: any) {
expect(
e.message.includes(
'Expression function from augment-vis saved objects could not be generated'
)
);
}
});
it('builds with one saved obj', async () => {
const str = buildPipelineFromAugmentVisSavedObjs([obj1]);
expect(str).toEqual('fn-1 arg1="value-1"');
});
it('builds with multiple saved objs', async () => {
const str = buildPipelineFromAugmentVisSavedObjs([obj1, obj2]);
expect(str).toEqual(`fn-1 arg1="value-1"\n| fn-2 arg2="value-2"`);
});
});
});
Loading

0 comments on commit 1687089

Please sign in to comment.