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 a new augment-vis saved object type #3109

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -4,7 +4,14 @@
"server": true,
"ui": true,
"requiredPlugins": ["management", "data"],
"optionalPlugins": ["dashboard", "visualizations", "discover", "home", "visBuilder"],
"optionalPlugins": [
"dashboard",
"visualizations",
"discover",
"home",
"visBuilder",
"visAugmenter"
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
],
"extraPublicDirs": ["public/lib"],
"requiredBundles": ["opensearchDashboardsReact", "home"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function canViewInApp(uiCapabilities: Capabilities, type: string): boolea
case 'visualization':
case 'visualizations':
return uiCapabilities.visualize.show as boolean;
case 'augment-vis':
return uiCapabilities.visAugmenter.show as boolean;
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
case 'index-pattern':
case 'index-patterns':
case 'indexPatterns':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class SavedObjectEdition extends Component<
);
if (confirmed) {
await savedObjectsClient.delete(type, id);
notifications.toasts.addSuccess(`Deleted '${object!.attributes.title}' ${type} object`);
notifications.toasts.addSuccess(`Deleted ${this.formatTitle(object)} ${type} object`);
this.redirectToListing();
}
}
Expand All @@ -179,10 +179,14 @@ export class SavedObjectEdition extends Component<
const { object, type } = this.state;

await savedObjectsClient.update(object!.type, object!.id, attributes, { references });
notifications.toasts.addSuccess(`Updated '${attributes.title}' ${type} object`);
notifications.toasts.addSuccess(`Updated ${this.formatTitle(object)} ${type} object`);
this.redirectToListing();
};

formatTitle = (object: SimpleSavedObject<any> | undefined) => {
return object?.attributes?.title ? `'${object.attributes.title}'` : '';
};

redirectToListing() {
this.props.history.push('/');
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/saved_objects_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { DashboardStart } from '../../dashboard/public';
import { DiscoverStart } from '../../discover/public';
import { HomePublicPluginSetup, FeatureCatalogueCategory } from '../../home/public';
import { VisualizationsStart } from '../../visualizations/public';
import { VisAugmenterStart } from '../../vis_augmenter/public';
import {
SavedObjectsManagementActionService,
SavedObjectsManagementActionServiceSetup,
Expand Down Expand Up @@ -75,6 +76,7 @@ export interface StartDependencies {
data: DataPublicPluginStart;
dashboard?: DashboardStart;
visualizations?: VisualizationsStart;
visAugmenter?: VisAugmenterStart;
discover?: DiscoverStart;
visBuilder?: VisBuilderStart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const registerServices = async (
registry: ISavedObjectsManagementServiceRegistry,
getStartServices: StartServicesAccessor<StartDependencies, SavedObjectsManagementPluginStart>
) => {
const [, { dashboard, visualizations, discover, visBuilder }] = await getStartServices();
const [
,
{ dashboard, visualizations, visAugmenter, discover, visBuilder },
] = await getStartServices();

if (dashboard) {
registry.register({
Expand All @@ -54,6 +57,14 @@ export const registerServices = async (
});
}

if (visAugmenter) {
registry.register({
id: 'savedAugmentVis',
title: 'augmentVis',
service: visAugmenter.savedAugmentVisLoader,
});
}

if (discover) {
registry.register({
id: 'savedSearches',
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/vis_augmenter/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ export function plugin(initializerContext: PluginInitializerContext) {
return new VisAugmenterPlugin(initializerContext);
}
export { VisAugmenterSetup, VisAugmenterStart };

export {
createSavedAugmentVisLoader,
createAugmentVisSavedObject,
SavedAugmentVisLoader,
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
} from './saved_augment_vis';

export { ISavedAugmentVis, VisLayerExpressionFn, AugmentVisSavedObject } from './types';
17 changes: 14 additions & 3 deletions src/plugins/vis_augmenter/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { ExpressionsSetup } from '../../expressions/public';
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/public';
import { DataPublicPluginSetup, DataPublicPluginStart } from '../../data/public';
import { visLayers } from './expressions';
import { setSavedAugmentVisLoader } from './services';
import { createSavedAugmentVisLoader, SavedAugmentVisLoader } from './saved_augment_vis';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface VisAugmenterSetup {}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface VisAugmenterStart {}
export interface VisAugmenterStart {
savedAugmentVisLoader: SavedAugmentVisLoader;
}

export interface VisAugmenterSetupDeps {
data: DataPublicPluginSetup;
Expand All @@ -37,7 +40,15 @@ export class VisAugmenterPlugin
}

public start(core: CoreStart, { data }: VisAugmenterStartDeps): VisAugmenterStart {
return {};
const savedAugmentVisLoader = createSavedAugmentVisLoader({
savedObjectsClient: core.savedObjects.client,
indexPatterns: data.indexPatterns,
search: data.search,
chrome: core.chrome,
overlays: core.overlays,
});
setSavedAugmentVisLoader(savedAugmentVisLoader);
return { savedAugmentVisLoader };
}

public stop() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @name SavedAugmentVis
*
* @extends SavedObject.
*/
import { get } from 'lodash';
import {
createSavedObjectClass,
SavedObject,
SavedObjectOpenSearchDashboardsServices,
} from '../../../saved_objects/public';
import { IIndexPattern } from '../../../data/public';
import { extractReferences, injectReferences } from './saved_augment_vis_references';

const name = 'augment-vis';

export function createSavedAugmentVisClass(services: SavedObjectOpenSearchDashboardsServices) {
const SavedObjectClass = createSavedObjectClass(services);

class SavedAugmentVis extends SavedObjectClass {
public static type: string = name;
public static mapping: Record<string, string> = {
description: 'text',
pluginResourceId: 'text',
visId: 'keyword',
visLayerExpressionFn: 'text',
version: 'integer',
};

constructor(opts: Record<string, unknown> | string = {}) {
if (typeof opts !== 'object') {
opts = { id: opts };
}
super({
type: SavedAugmentVis.type,
mapping: SavedAugmentVis.mapping,
extractReferences,
injectReferences,
id: (opts.id as string) || '',
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
indexPattern: opts.indexPattern as IIndexPattern,
defaults: {
description: get(opts, 'description', ''),
pluginResourceId: get(opts, 'pluginResourceId', ''),
visId: get(opts, 'visId', ''),
visLayerExpressionFn: get(opts, 'visLayerExpressionFn', {}),
version: 1,
},
});
this.showInRecentlyAccessed = false;
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
}
}

return SavedAugmentVis as new (opts: Record<string, unknown> | string) => SavedObject;
}
7 changes: 7 additions & 0 deletions src/plugins/vis_augmenter/public/saved_augment_vis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export * from './saved_augment_vis';
export * from './utils';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { VisLayerExpressionFn } from '../types';
import { VisLayerTypes } from '../../common';
import {
createSavedAugmentVisLoader,
SavedObjectOpenSearchDashboardsServicesWithAugmentVis,
} from './saved_augment_vis';
import { generateAugmentVisSavedObject, getMockAugmentVisSavedObjectClient } from './utils';

describe('SavedObjectLoaderAugmentVis', () => {
const fn = {
type: VisLayerTypes.PointInTimeEvents,
name: 'test-fn',
args: {
testArg: 'test-value',
},
} as VisLayerExpressionFn;
const obj1 = generateAugmentVisSavedObject('test-id-1', fn);
const obj2 = generateAugmentVisSavedObject('test-id-2', fn);

it('findAll returns single saved obj', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
const resp = await loader.findAll();
expect(resp.hits.length === 1);
});

// TODO: once done rebasing after VisLayer PR, can finish creating test cases here.
// right now they are failing since there is missing imports

// add test for empty response
// add test for multi obj response
// add test for invalid VisLayerType
// add test for missing reference
// add test for missing visLayerExpressionFn
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { get, isEmpty } from 'lodash';
import {
SavedObjectLoader,
SavedObjectOpenSearchDashboardsServices,
} from '../../../saved_objects/public';
import { createSavedAugmentVisClass } from './_saved_augment_vis';
import { VisLayerTypes } from '../../common';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SavedObjectOpenSearchDashboardsServicesWithAugmentVis
extends SavedObjectOpenSearchDashboardsServices {}
export type SavedAugmentVisLoader = ReturnType<typeof createSavedAugmentVisLoader>;
export function createSavedAugmentVisLoader(
services: SavedObjectOpenSearchDashboardsServicesWithAugmentVis
) {
const { savedObjectsClient } = services;

class SavedObjectLoaderAugmentVis extends SavedObjectLoader {
mapHitSource = (source: Record<string, any>, id: string) => {
source.id = id;
source.visId = get(source, 'visReference.id', '');

if (isEmpty(source.visReference)) {
source.error = 'visReference is missing in augment-vis saved object';
}
if (isEmpty(source.visLayerExpressionFn)) {
source.error = 'visLayerExpressionFn is missing in augment-vis saved object';
}
if (!(get(source, 'visLayerExpressionFn.type', '') in VisLayerTypes)) {
source.error = 'Unknown VisLayer expression function type';
}

delete source.visReference;
delete source.visName;
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
return source;
};

/**
* Updates hit.attributes to contain an id related to the referenced visualization
* (visId) and returns the updated attributes object.
* @param hit
* @returns {hit.attributes} The modified hit.attributes object, with an id and url field.
*/
mapSavedObjectApiHits(hit: {
references: any[];
attributes: Record<string, unknown>;
id: string;
}) {
// For now we are assuming only one vis reference per saved object.
// If we change to multiple, we will need to dynamically handle that
const visReference = hit.references[0];
return this.mapHitSource({ ...hit.attributes, visReference }, hit.id);
}
}
const SavedAugmentVis = createSavedAugmentVisClass(services);
return new SavedObjectLoaderAugmentVis(SavedAugmentVis, savedObjectsClient) as SavedObjectLoader;
}
Loading