From 9e1c51b0985a24e15e8c856f8eafcfbde7e48202 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Mon, 19 Dec 2022 11:18:37 -0800 Subject: [PATCH] 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 2ef7379fa0f..05913ce9c2c 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 00000000000..3902304919b --- /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 2062cb2a6fe..19d38d92987 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 00000000000..61c2c207ac9 --- /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 00000000000..6aa2ca8d96e --- /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 00000000000..c97980bc1b9 --- /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 +>;