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 content management mSearch to viz, lens, and event annotation group #162450

Merged
merged 5 commits into from
Jul 26, 2023
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
1 change: 1 addition & 0 deletions packages/kbn-content-management-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './src/types';
export * from './src/schema';
export * from './src/saved_object_content_storage';
export * from './src/utils';
export * from './src/msearch';
96 changes: 96 additions & 0 deletions packages/kbn-content-management-utils/src/msearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Boom from '@hapi/boom';
import { pick } from 'lodash';

import type { StorageContext } from '@kbn/content-management-plugin/server';

import type {
SavedObjectsFindResult,
SavedObject,
SavedObjectReference,
} from '@kbn/core-saved-objects-api-server';

import type { ServicesDefinitionSet, SOWithMetadata, SOWithMetadataPartial } from './types';

type PartialSavedObject<T> = Omit<SavedObject<Partial<T>>, 'references'> & {
references: SavedObjectReference[] | undefined;
};

interface GetMSearchParams {
savedObjectType: string;
cmServicesDefinition: ServicesDefinitionSet;
allowedSavedObjectAttributes: string[];
}

function savedObjectToItem<Attributes extends object>(
Copy link
Contributor

@Dosant Dosant Jul 25, 2023

Choose a reason for hiding this comment

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

Just want to understand better, is this function always applicable for any saved object, or is there a scenario where it will have to be changed for a specific saved object type later (maybe with some data changes)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, I had run into a case where a malformed saved object could throw validation errors since all SO attributes were being added to the resulting object. Therefore I decided it was best to treat the SO doc itself like an api that needs to specify its inputs.

savedObject: SavedObject<Attributes> | PartialSavedObject<Attributes>,
allowedSavedObjectAttributes: string[]
): SOWithMetadata | SOWithMetadataPartial {
const {
id,
type,
updated_at: updatedAt,
created_at: createdAt,
attributes,
references,
error,
namespaces,
version,
} = savedObject;

return {
id,
type,
updatedAt,
createdAt,
attributes: pick(attributes, allowedSavedObjectAttributes),
references,
error,
namespaces,
version,
};
}

export interface GetMSearchType<ReturnItem> {
savedObjectType: string;
toItemResult: (ctx: StorageContext, savedObject: SavedObjectsFindResult) => ReturnItem;
}

export const getMSearch = <ReturnItem, SOAttributes extends object>({
savedObjectType,
cmServicesDefinition,
allowedSavedObjectAttributes,
}: GetMSearchParams) => {
return {
savedObjectType,
toItemResult: (ctx: StorageContext, savedObject: SavedObjectsFindResult): ReturnItem => {
const transforms = ctx.utils.getTransforms(cmServicesDefinition);

// Validate DB response and DOWN transform to the request version
const { value, error: resultError } = transforms.mSearch.out.result.down<
ReturnItem,
ReturnItem
>(
// Ran into a case where a schema was broken by a SO attribute that wasn't part of the definition
// so we specify which attributes are allowed
savedObjectToItem<SOAttributes>(
savedObject as SavedObjectsFindResult<SOAttributes>,
allowedSavedObjectAttributes
)
);

if (resultError) {
throw Boom.badRequest(`Invalid response. ${resultError.message}`);
}

return value;
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
SavedObjectsFindOptions,
} from '@kbn/core-saved-objects-api-server';

import { getMSearch, type GetMSearchType } from '@kbn/content-management-utils';

import { EVENT_ANNOTATION_GROUP_TYPE } from '@kbn/event-annotation-common';
import { cmServicesDefinition } from '../../common/content_management/cm_services';
import type {
Expand Down Expand Up @@ -96,7 +98,20 @@ export class EventAnnotationGroupStorage
implements
ContentStorage<EventAnnotationGroupSavedObject, PartialEventAnnotationGroupSavedObject>
{
constructor() {}
mSearch: GetMSearchType<EventAnnotationGroupSavedObject>;
constructor() {
this.mSearch = getMSearch<EventAnnotationGroupSavedObject, EventAnnotationGroupSearchOut>({
savedObjectType: SO_TYPE,
cmServicesDefinition,
allowedSavedObjectAttributes: [
'title',
'description',
'ignoreGlobalFilters',
'annotations',
'dataViewSpec',
],
});
}

async get(ctx: StorageContext, id: string): Promise<EventAnnotationGroupGetOut> {
const {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/event_annotation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"@kbn/core-lifecycle-browser",
"@kbn/saved-objects-tagging-oss-plugin",
"@kbn/dom-drag-drop",
"@kbn/kibana-utils-plugin"
"@kbn/kibana-utils-plugin",
"@kbn/content-management-utils"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
SavedObjectsFindOptions,
} from '@kbn/core-saved-objects-api-server';

import { getMSearch, type GetMSearchType } from '@kbn/content-management-utils';

import { CONTENT_ID } from '../../common/content_management';
import { cmServicesDefinition } from '../../common/content_management/cm_services';
import type {
Expand Down Expand Up @@ -103,7 +105,23 @@ const SO_TYPE: VisualizationContentType = 'visualization';
export class VisualizationsStorage
implements ContentStorage<VisualizationSavedObject, PartialVisualizationSavedObject>
{
constructor() {}
mSearch: GetMSearchType<VisualizationSavedObject>;

constructor() {
this.mSearch = getMSearch<VisualizationSavedObject, VisualizationSearchOut>({
savedObjectType: SO_TYPE,
cmServicesDefinition,
allowedSavedObjectAttributes: [
'title',
'description',
'version',
'kibanaSavedObjectMeta',
'uiStateJSON',
'visState',
'savedSearchRefName',
],
});
}

async get(ctx: StorageContext, id: string): Promise<VisualizationGetOut> {
const {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/visualizations/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"@kbn/core-saved-objects-utils-server",
"@kbn/content-management-table-list-view-table",
"@kbn/content-management-tabbed-table-list-view",
"@kbn/content-management-table-list-view"
"@kbn/content-management-table-list-view",
"@kbn/content-management-utils"
],
"exclude": [
"target/**/*",
Expand Down
11 changes: 10 additions & 1 deletion x-pack/plugins/lens/server/content_management/lens_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
SavedObjectsFindOptions,
} from '@kbn/core-saved-objects-api-server';

import { getMSearch, type GetMSearchType } from '@kbn/content-management-utils';

import { CONTENT_ID } from '../../common/content_management';
import { cmServicesDefinition } from '../../common/content_management/cm_services';
import type {
Expand Down Expand Up @@ -91,7 +93,14 @@ function savedObjectToLensSavedObject(
const SO_TYPE: LensContentType = 'lens';

export class LensStorage implements ContentStorage<LensSavedObject, PartialLensSavedObject> {
constructor() {}
mSearch: GetMSearchType<LensSavedObject>;
constructor() {
this.mSearch = getMSearch<LensSavedObject, LensSearchOut>({
savedObjectType: SO_TYPE,
cmServicesDefinition,
allowedSavedObjectAttributes: ['title', 'description', 'visualizationType', 'state'],
});
}

async get(ctx: StorageContext, id: string): Promise<LensGetOut> {
const {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@kbn/core-overlays-browser-mocks",
"@kbn/core-theme-browser-mocks",
"@kbn/event-annotation-components",
"@kbn/content-management-utils",
],
"exclude": [
"target/**/*",
Expand Down