Skip to content

Commit

Permalink
Add interfaces for layering data on Visualizations (#3108)
Browse files Browse the repository at this point in the history
* Add vis layer interfaces

Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler authored Dec 30, 2022
1 parent ba6f9eb commit e78ccbf
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/plugins/vis_augmenter/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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

import { VisLayerTypes, VisLayer, isPointInTimeEventsVisLayer, isValidVisLayer } from './types';

describe('isPointInTimeEventsVisLayer()', function () {
it('should return false if type does not match', function () {
const visLayer = ({
type: 'incorrect-type',
name: 'visLayerName',
field1: 'value1',
field2: 'value2',
} as unknown) as VisLayer;
expect(isPointInTimeEventsVisLayer(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(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);
});
});
54 changes: 54 additions & 0 deletions src/plugins/vis_augmenter/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { ExpressionFunctionDefinition } from '../../expressions';

export enum VisLayerTypes {
PointInTimeEvents = 'PointInTimeEvents',
}

export interface VisLayer {
type: keyof typeof VisLayerTypes;
name: string;
}

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 EventMetadata {
resourceId: string;
resourceName: 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 VisLayerResponseValue {
visLayers: object;
}

export type VisLayerFunctionDefinition = ExpressionFunctionDefinition<
string,
VisLayerResponseValue,
any,
Promise<VisLayerResponseValue>
>;
2 changes: 1 addition & 1 deletion src/plugins/vis_augmenter/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"version": "opensearchDashboards",
"server": true,
"ui": true,
"requiredPlugins": ["data", "savedObjects", "opensearchDashboardsUtils"]
"requiredPlugins": ["data", "savedObjects", "opensearchDashboardsUtils", "expressions"]
}
6 changes: 6 additions & 0 deletions src/plugins/vis_augmenter/public/expressions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export * from './vis_layers';
33 changes: 33 additions & 0 deletions src/plugins/vis_augmenter/public/expressions/vis_layers.ts
Original file line number Diff line number Diff line change
@@ -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<typeof name, ExprVisLayers> = {
name,
from: {
null: () => {
return {
type: name,
layers: [] as VisLayers,
} as ExprVisLayers;
},
undefined: () => {
return {
type: name,
layers: [] as VisLayers,
} as ExprVisLayers;
},
},
};
6 changes: 5 additions & 1 deletion src/plugins/vis_augmenter/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -14,6 +16,7 @@ export interface VisAugmenterStart {}

export interface VisAugmenterSetupDeps {
data: DataPublicPluginSetup;
expressions: ExpressionsSetup;
}

export interface VisAugmenterStartDeps {
Expand All @@ -27,8 +30,9 @@ export class VisAugmenterPlugin

public setup(
core: CoreSetup<VisAugmenterStartDeps, VisAugmenterStart>,
{ data }: VisAugmenterSetupDeps
{ data, expressions }: VisAugmenterSetupDeps
): VisAugmenterSetup {
expressions.registerType(visLayers);
return {};
}

Expand Down

0 comments on commit e78ccbf

Please sign in to comment.