-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
mattkime
merged 5 commits into
elastic:main
from
mattkime:add_msearch_for_viz_lens_events
Jul 26, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c196534
add msearch to viz, lens, and event annotation group
mattkime 623535e
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine e32c588
Merge branch 'main' into add_msearch_for_viz_lens_events
kibanamachine 741cc53
Merge branch 'main' into add_msearch_for_viz_lens_events
stratoula c11c46c
Update msearch.ts
mattkime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>( | ||
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; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.