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 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand All @@ -64,7 +63,6 @@ export const typeSpecs: AnyExpressionTypeDefinition[] = [
shape,
string,
style,
visLayers,
];

export * from './boolean';
Expand All @@ -83,4 +81,3 @@ export * from './render';
export * from './shape';
export * from './string';
export * from './style';
export * from './vis_layers';
1 change: 0 additions & 1 deletion src/plugins/expressions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,5 @@ export {
TypeString,
TypeToString,
UnmappedTypeStrings,
ExprVisLayers,
ExpressionValueRender as Render,
} from '../common';
41 changes: 34 additions & 7 deletions src/plugins/vis_augmenter/common/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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);
});
});
20 changes: 12 additions & 8 deletions src/plugins/vis_augmenter/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -17,25 +19,27 @@ 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;
joshuarrrr marked this conversation as resolved.
Show resolved Hide resolved
}

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;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, this seems much more clear to me.


export interface VisLayerResponseValue {
Expand Down
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';
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

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

const name = 'vis_layers';

Expand Down
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