From 9e1c51b0985a24e15e8c856f8eafcfbde7e48202 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Mon, 19 Dec 2022 11:18:37 -0800 Subject: [PATCH 1/4] Add vis layer interfaces Signed-off-by: Tyler Ohlsen --- .../common/expression_types/specs/index.ts | 3 ++ .../expression_types/specs/vis_layers.ts | 33 +++++++++++++ src/plugins/expressions/public/index.ts | 1 + src/plugins/vis_augmenter/common/index.ts | 15 ++++++ .../vis_augmenter/common/types.test.ts | 33 +++++++++++++ src/plugins/vis_augmenter/common/types.ts | 47 +++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 src/plugins/expressions/common/expression_types/specs/vis_layers.ts create mode 100644 src/plugins/vis_augmenter/common/index.ts create mode 100644 src/plugins/vis_augmenter/common/types.test.ts create mode 100644 src/plugins/vis_augmenter/common/types.ts diff --git a/src/plugins/expressions/common/expression_types/specs/index.ts b/src/plugins/expressions/common/expression_types/specs/index.ts index 2ef7379fa0fd..05913ce9c2ca 100644 --- a/src/plugins/expressions/common/expression_types/specs/index.ts +++ b/src/plugins/expressions/common/expression_types/specs/index.ts @@ -44,6 +44,7 @@ import { render } from './render'; import { shape } from './shape'; import { string } from './string'; import { style } from './style'; +import { visLayers } from './vis_layers'; import { AnyExpressionTypeDefinition } from '../types'; export const typeSpecs: AnyExpressionTypeDefinition[] = [ @@ -63,6 +64,7 @@ export const typeSpecs: AnyExpressionTypeDefinition[] = [ shape, string, style, + visLayers, ]; export * from './boolean'; @@ -81,3 +83,4 @@ export * from './render'; export * from './shape'; export * from './string'; export * from './style'; +export * from './vis_layers'; diff --git a/src/plugins/expressions/common/expression_types/specs/vis_layers.ts b/src/plugins/expressions/common/expression_types/specs/vis_layers.ts new file mode 100644 index 000000000000..3902304919b6 --- /dev/null +++ b/src/plugins/expressions/common/expression_types/specs/vis_layers.ts @@ -0,0 +1,33 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { ExpressionTypeDefinition } from '../types'; +import { VisLayers } from '../../../../vis_augmenter/common'; + +const name = 'vis_layers'; + +export interface ExprVisLayers { + type: typeof name; + layers: VisLayers; +} + +// Setting default empty arrays for null & undefined edge cases +export const visLayers: ExpressionTypeDefinition = { + name, + from: { + null: () => { + return { + type: name, + layers: [] as VisLayers, + } as ExprVisLayers; + }, + undefined: () => { + return { + type: name, + layers: [] as VisLayers, + } as ExprVisLayers; + }, + }, +}; diff --git a/src/plugins/expressions/public/index.ts b/src/plugins/expressions/public/index.ts index 2062cb2a6fe7..19d38d929879 100644 --- a/src/plugins/expressions/public/index.ts +++ b/src/plugins/expressions/public/index.ts @@ -132,5 +132,6 @@ export { TypeString, TypeToString, UnmappedTypeStrings, + ExprVisLayers, ExpressionValueRender as Render, } from '../common'; diff --git a/src/plugins/vis_augmenter/common/index.ts b/src/plugins/vis_augmenter/common/index.ts new file mode 100644 index 000000000000..61c2c207ac97 --- /dev/null +++ b/src/plugins/vis_augmenter/common/index.ts @@ -0,0 +1,15 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export { + VisLayer, + VisLayers, + PointInTimeEventsVisLayer, + PointInTimeEvent, + PointInTimeEventMetadata, + isPointInTimeEventsVisLayer, + VisLayerResponseValue, + VisLayerFunctionDefinition, +} from './types'; diff --git a/src/plugins/vis_augmenter/common/types.test.ts b/src/plugins/vis_augmenter/common/types.test.ts new file mode 100644 index 000000000000..6aa2ca8d96e4 --- /dev/null +++ b/src/plugins/vis_augmenter/common/types.test.ts @@ -0,0 +1,33 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { VisLayer, isPointInTimeEventsVisLayer } from './types'; + +describe('isPointInTimeEventsVisLayer()', function () { + it('should return false if no events field', function () { + const visLayer = { + id: 'visLayerId', + name: 'visLayerName', + field1: 'value1', + field2: 'value2', + } as VisLayer; + expect(isPointInTimeEventsVisLayer(visLayer)).toBe(false); + }); + + it('should return true if events field exists', function () { + const visLayer = { + id: 'testId', + name: 'testName', + events: [ + { + timestamp: 123, + resourceId: 'testId', + resourceName: 'testName', + }, + ], + } as VisLayer; + expect(isPointInTimeEventsVisLayer(visLayer)).toBe(true); + }); +}); diff --git a/src/plugins/vis_augmenter/common/types.ts b/src/plugins/vis_augmenter/common/types.ts new file mode 100644 index 000000000000..c97980bc1b9d --- /dev/null +++ b/src/plugins/vis_augmenter/common/types.ts @@ -0,0 +1,47 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { ExpressionFunctionDefinition } from '../../expressions'; + +export interface VisLayer { + // will be used as the column ID + id: string; + // will be used as the name when hovering over the tooltip + name: string; +} + +export type VisLayers = VisLayer[]; + +export interface PointInTimeEventMetadata { + resourceId: string; + resourceName: string; + tooltip?: string; +} + +export interface PointInTimeEvent { + timestamp: number; + metadata: PointInTimeEventMetadata; +} + +export interface PointInTimeEventsVisLayer extends VisLayer { + events: PointInTimeEvent[]; +} + +// used to determine what vis layer's interface is being implemented. +// currently PointInTimeEventsLayer is the only interface extending VisLayer +export const isPointInTimeEventsVisLayer = (obj: any) => { + return 'events' in obj; +}; + +export interface VisLayerResponseValue { + visLayers: object; +} + +export type VisLayerFunctionDefinition = ExpressionFunctionDefinition< + string, + VisLayerResponseValue, + any, + Promise +>; From 4ff4451a2b8397d2325696733763117762a93c78 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Thu, 22 Dec 2022 13:05:31 -0800 Subject: [PATCH 2/4] Minor cleanup Signed-off-by: Tyler Ohlsen --- src/plugins/vis_augmenter/common/index.ts | 11 +---------- src/plugins/vis_augmenter/common/types.ts | 3 +++ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/plugins/vis_augmenter/common/index.ts b/src/plugins/vis_augmenter/common/index.ts index 61c2c207ac97..9f269633f307 100644 --- a/src/plugins/vis_augmenter/common/index.ts +++ b/src/plugins/vis_augmenter/common/index.ts @@ -3,13 +3,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -export { - VisLayer, - VisLayers, - PointInTimeEventsVisLayer, - PointInTimeEvent, - PointInTimeEventMetadata, - isPointInTimeEventsVisLayer, - VisLayerResponseValue, - VisLayerFunctionDefinition, -} from './types'; +export * from './types'; diff --git a/src/plugins/vis_augmenter/common/types.ts b/src/plugins/vis_augmenter/common/types.ts index c97980bc1b9d..eac6a49a77c7 100644 --- a/src/plugins/vis_augmenter/common/types.ts +++ b/src/plugins/vis_augmenter/common/types.ts @@ -14,6 +14,9 @@ export interface VisLayer { export type VisLayers = VisLayer[]; +// resourceId & resourceName are required so that the +// events flyout can partition data based on these attributes +// (e.g., partitioning anomalies based on the detector they came from) export interface PointInTimeEventMetadata { resourceId: string; resourceName: string; From d7bb8bd3468aaced6535f9d6c011630ff498c720 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Fri, 23 Dec 2022 16:28:00 -0800 Subject: [PATCH 3/4] Move new expressions type to vis_augmenter; clean up vis layer types Signed-off-by: Tyler Ohlsen --- .../common/expression_types/specs/index.ts | 2 - src/plugins/expressions/public/index.ts | 1 - .../vis_augmenter/common/types.test.ts | 41 +++++++++++++++---- src/plugins/vis_augmenter/common/types.ts | 20 +++++---- .../vis_augmenter/opensearch_dashboards.json | 2 +- .../vis_augmenter/public/expressions/index.ts | 6 +++ .../public/expressions/vis_layers.ts | 33 +++++++++++++++ src/plugins/vis_augmenter/public/plugin.ts | 6 ++- 8 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 src/plugins/vis_augmenter/public/expressions/index.ts create mode 100644 src/plugins/vis_augmenter/public/expressions/vis_layers.ts diff --git a/src/plugins/expressions/common/expression_types/specs/index.ts b/src/plugins/expressions/common/expression_types/specs/index.ts index 05913ce9c2ca..6c7de351bf4c 100644 --- a/src/plugins/expressions/common/expression_types/specs/index.ts +++ b/src/plugins/expressions/common/expression_types/specs/index.ts @@ -44,7 +44,6 @@ import { render } from './render'; import { shape } from './shape'; import { string } from './string'; import { style } from './style'; -import { visLayers } from './vis_layers'; import { AnyExpressionTypeDefinition } from '../types'; export const typeSpecs: AnyExpressionTypeDefinition[] = [ @@ -64,7 +63,6 @@ export const typeSpecs: AnyExpressionTypeDefinition[] = [ shape, string, style, - visLayers, ]; export * from './boolean'; diff --git a/src/plugins/expressions/public/index.ts b/src/plugins/expressions/public/index.ts index 19d38d929879..2062cb2a6fe7 100644 --- a/src/plugins/expressions/public/index.ts +++ b/src/plugins/expressions/public/index.ts @@ -132,6 +132,5 @@ export { TypeString, TypeToString, UnmappedTypeStrings, - ExprVisLayers, ExpressionValueRender as Render, } from '../common'; diff --git a/src/plugins/vis_augmenter/common/types.test.ts b/src/plugins/vis_augmenter/common/types.test.ts index 6aa2ca8d96e4..9e1282f40062 100644 --- a/src/plugins/vis_augmenter/common/types.test.ts +++ b/src/plugins/vis_augmenter/common/types.test.ts @@ -3,22 +3,22 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { VisLayer, isPointInTimeEventsVisLayer } from './types'; +import { VisLayerTypes, VisLayer, isPointInTimeEventsVisLayer, isValidVisLayer } from './types'; describe('isPointInTimeEventsVisLayer()', function () { - it('should return false if no events field', function () { - const visLayer = { - id: 'visLayerId', + it('should return false if type does not match', function () { + const visLayer = ({ + type: 'incorrect-type', name: 'visLayerName', field1: 'value1', field2: 'value2', - } as VisLayer; + } as unknown) as VisLayer; expect(isPointInTimeEventsVisLayer(visLayer)).toBe(false); }); - it('should return true if events field exists', function () { + it('should return true if type matches', function () { const visLayer = { - id: 'testId', + type: VisLayerTypes.PointInTimeEvents, name: 'testName', events: [ { @@ -31,3 +31,30 @@ describe('isPointInTimeEventsVisLayer()', function () { expect(isPointInTimeEventsVisLayer(visLayer)).toBe(true); }); }); + +describe('isValidVisLayer()', function () { + it('should return false if no valid type', function () { + const visLayer = ({ + type: 'incorrect-type', + name: 'visLayerName', + field1: 'value1', + field2: 'value2', + } as unknown) as VisLayer; + expect(isValidVisLayer(visLayer)).toBe(false); + }); + + it('should return true if type matches', function () { + const visLayer = { + type: VisLayerTypes.PointInTimeEvents, + name: 'testName', + events: [ + { + timestamp: 123, + resourceId: 'testId', + resourceName: 'testName', + }, + ], + } as VisLayer; + expect(isValidVisLayer(visLayer)).toBe(true); + }); +}); diff --git a/src/plugins/vis_augmenter/common/types.ts b/src/plugins/vis_augmenter/common/types.ts index eac6a49a77c7..ea4561991488 100644 --- a/src/plugins/vis_augmenter/common/types.ts +++ b/src/plugins/vis_augmenter/common/types.ts @@ -5,10 +5,12 @@ import { ExpressionFunctionDefinition } from '../../expressions'; +export enum VisLayerTypes { + PointInTimeEvents = 'PointInTimeEvents', +} + export interface VisLayer { - // will be used as the column ID - id: string; - // will be used as the name when hovering over the tooltip + type: keyof typeof VisLayerTypes; name: string; } @@ -17,7 +19,7 @@ export type VisLayers = VisLayer[]; // resourceId & resourceName are required so that the // events flyout can partition data based on these attributes // (e.g., partitioning anomalies based on the detector they came from) -export interface PointInTimeEventMetadata { +export interface EventMetadata { resourceId: string; resourceName: string; tooltip?: string; @@ -25,17 +27,19 @@ export interface PointInTimeEventMetadata { export interface PointInTimeEvent { timestamp: number; - metadata: PointInTimeEventMetadata; + metadata: EventMetadata; } export interface PointInTimeEventsVisLayer extends VisLayer { events: PointInTimeEvent[]; } -// used to determine what vis layer's interface is being implemented. -// currently PointInTimeEventsLayer is the only interface extending VisLayer export const isPointInTimeEventsVisLayer = (obj: any) => { - return 'events' in obj; + return obj?.type === VisLayerTypes.PointInTimeEvents; +}; + +export const isValidVisLayer = (obj: any) => { + return obj?.type in VisLayerTypes; }; export interface VisLayerResponseValue { diff --git a/src/plugins/vis_augmenter/opensearch_dashboards.json b/src/plugins/vis_augmenter/opensearch_dashboards.json index e9cd25618d01..ac78ec9c0281 100644 --- a/src/plugins/vis_augmenter/opensearch_dashboards.json +++ b/src/plugins/vis_augmenter/opensearch_dashboards.json @@ -3,5 +3,5 @@ "version": "opensearchDashboards", "server": true, "ui": true, - "requiredPlugins": ["data", "savedObjects", "opensearchDashboardsUtils"] + "requiredPlugins": ["data", "savedObjects", "opensearchDashboardsUtils", "expressions"] } diff --git a/src/plugins/vis_augmenter/public/expressions/index.ts b/src/plugins/vis_augmenter/public/expressions/index.ts new file mode 100644 index 000000000000..f7bcfbd083fe --- /dev/null +++ b/src/plugins/vis_augmenter/public/expressions/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export * from './vis_layers'; diff --git a/src/plugins/vis_augmenter/public/expressions/vis_layers.ts b/src/plugins/vis_augmenter/public/expressions/vis_layers.ts new file mode 100644 index 000000000000..f99ead0407eb --- /dev/null +++ b/src/plugins/vis_augmenter/public/expressions/vis_layers.ts @@ -0,0 +1,33 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { ExpressionTypeDefinition } from '../../../expressions'; +import { VisLayers } from '../../common'; + +const name = 'vis_layers'; + +export interface ExprVisLayers { + type: typeof name; + layers: VisLayers; +} + +// Setting default empty arrays for null & undefined edge cases +export const visLayers: ExpressionTypeDefinition = { + name, + from: { + null: () => { + return { + type: name, + layers: [] as VisLayers, + } as ExprVisLayers; + }, + undefined: () => { + return { + type: name, + layers: [] as VisLayers, + } as ExprVisLayers; + }, + }, +}; diff --git a/src/plugins/vis_augmenter/public/plugin.ts b/src/plugins/vis_augmenter/public/plugin.ts index cbc7a24800de..d53116bdd12d 100644 --- a/src/plugins/vis_augmenter/public/plugin.ts +++ b/src/plugins/vis_augmenter/public/plugin.ts @@ -3,8 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { ExpressionsSetup } from '../../expressions/public'; import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/public'; import { DataPublicPluginSetup, DataPublicPluginStart } from '../../data/public'; +import { visLayers } from './expressions'; // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface VisAugmenterSetup {} @@ -14,6 +16,7 @@ export interface VisAugmenterStart {} export interface VisAugmenterSetupDeps { data: DataPublicPluginSetup; + expressions: ExpressionsSetup; } export interface VisAugmenterStartDeps { @@ -27,8 +30,9 @@ export class VisAugmenterPlugin public setup( core: CoreSetup, - { data }: VisAugmenterSetupDeps + { data, expressions }: VisAugmenterSetupDeps ): VisAugmenterSetup { + expressions.registerType(visLayers); return {}; } From 25276e45aa0d76ddcb84902569e42b369f1afa38 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Fri, 23 Dec 2022 16:32:31 -0800 Subject: [PATCH 4/4] Remove leftover expression types after moving Signed-off-by: Tyler Ohlsen --- .../common/expression_types/specs/index.ts | 1 - .../expression_types/specs/vis_layers.ts | 33 ------------------- 2 files changed, 34 deletions(-) delete mode 100644 src/plugins/expressions/common/expression_types/specs/vis_layers.ts diff --git a/src/plugins/expressions/common/expression_types/specs/index.ts b/src/plugins/expressions/common/expression_types/specs/index.ts index 6c7de351bf4c..2ef7379fa0fd 100644 --- a/src/plugins/expressions/common/expression_types/specs/index.ts +++ b/src/plugins/expressions/common/expression_types/specs/index.ts @@ -81,4 +81,3 @@ export * from './render'; export * from './shape'; export * from './string'; export * from './style'; -export * from './vis_layers'; diff --git a/src/plugins/expressions/common/expression_types/specs/vis_layers.ts b/src/plugins/expressions/common/expression_types/specs/vis_layers.ts deleted file mode 100644 index 3902304919b6..000000000000 --- a/src/plugins/expressions/common/expression_types/specs/vis_layers.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { ExpressionTypeDefinition } from '../types'; -import { VisLayers } from '../../../../vis_augmenter/common'; - -const name = 'vis_layers'; - -export interface ExprVisLayers { - type: typeof name; - layers: VisLayers; -} - -// Setting default empty arrays for null & undefined edge cases -export const visLayers: ExpressionTypeDefinition = { - name, - from: { - null: () => { - return { - type: name, - layers: [] as VisLayers, - } as ExprVisLayers; - }, - undefined: () => { - return { - type: name, - layers: [] as VisLayers, - } as ExprVisLayers; - }, - }, -};