Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interfaces for layering data on Visualizations #3108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[];
joshuarrrr marked this conversation as resolved.
Show resolved Hide resolved

// 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;
joshuarrrr marked this conversation as resolved.
Show resolved Hide resolved
}

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