-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] Initial implementation of saved searches by value
- Loading branch information
1 parent
16ad022
commit fbb86ec
Showing
9 changed files
with
398 additions
and
135 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
src/plugins/discover/public/embeddable/saved_search_attribute_service.ts
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,129 @@ | ||
/* | ||
* 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 type { AttributeService } from '@kbn/embeddable-plugin/public'; | ||
import { OnSaveProps } from '@kbn/saved-objects-plugin/public'; | ||
import { | ||
SavedSearch, | ||
SavedSearchAttributes, | ||
} from '@kbn/saved-search-plugin/public/services/saved_searches/types'; | ||
import { | ||
checkForDuplicateTitle, | ||
getSavedSearch, | ||
saveSavedSearch, | ||
} from '@kbn/saved-search-plugin/public'; | ||
import { | ||
throwErrorOnSavedSearchUrlConflict, | ||
fromSavedSearchAttributes, | ||
toSavedSearchAttributes, | ||
} from '@kbn/saved-search-plugin/public'; | ||
import { injectSearchSourceReferences, parseSearchSourceJSON } from '@kbn/data-plugin/public'; | ||
import { SearchByReferenceInput, SearchByValueInput } from './types'; | ||
import { SEARCH_EMBEDDABLE_TYPE } from './constants'; | ||
import { DiscoverServices } from '../build_services'; | ||
|
||
export interface SavedSearchUnwrapMetaInfo { | ||
sharingSavedObjectProps: SavedSearch['sharingSavedObjectProps']; | ||
tags: SavedSearch['tags']; | ||
searchSource: SavedSearch['searchSource']; | ||
} | ||
|
||
export interface SavedSearchUnwrapResult { | ||
attributes: SavedSearchAttributes; | ||
metaInfo?: SavedSearchUnwrapMetaInfo; | ||
} | ||
|
||
export type SavedSearchAttributeService = AttributeService< | ||
SavedSearchAttributes, | ||
SearchByValueInput, | ||
SearchByReferenceInput, | ||
SavedSearchUnwrapMetaInfo | ||
>; | ||
|
||
export function getSavedSearchAttributeService( | ||
services: DiscoverServices | ||
): SavedSearchAttributeService { | ||
return services.embeddable.getAttributeService< | ||
SavedSearchAttributes, | ||
SearchByValueInput, | ||
SearchByReferenceInput, | ||
SavedSearchUnwrapMetaInfo | ||
>(SEARCH_EMBEDDABLE_TYPE, { | ||
saveMethod: async (attributes: SavedSearchAttributes, savedObjectId?: string) => { | ||
let tags: string[] | undefined; | ||
|
||
if (savedObjectId) { | ||
const savedSearch = await getSavedSearch(savedObjectId, { | ||
search: services.data.search, | ||
savedObjectsClient: services.core.savedObjects.client, | ||
spaces: services.spaces, | ||
savedObjectsTagging: services.savedObjectsTagging, | ||
}); | ||
|
||
tags = savedSearch.tags; | ||
} | ||
|
||
const parsedSearchSourceJSON = parseSearchSourceJSON( | ||
attributes.kibanaSavedObjectMeta?.searchSourceJSON ?? '{}' | ||
); | ||
|
||
const searchSourceValues = injectSearchSourceReferences( | ||
parsedSearchSourceJSON as Parameters<typeof injectSearchSourceReferences>[0], | ||
[] | ||
); | ||
|
||
const searchSource = await services.data.search.searchSource.create(searchSourceValues); | ||
|
||
const savedSearch = fromSavedSearchAttributes( | ||
savedObjectId, | ||
attributes, | ||
tags, | ||
searchSource, | ||
undefined | ||
); | ||
|
||
const id = await saveSavedSearch( | ||
savedSearch, | ||
{ isTitleDuplicateConfirmed: true }, | ||
services.core.savedObjects.client, | ||
services.savedObjectsTagging | ||
); | ||
|
||
return { id }; | ||
}, | ||
unwrapMethod: async (savedObjectId: string): Promise<SavedSearchUnwrapResult> => { | ||
const savedSearch = await getSavedSearch(savedObjectId, { | ||
search: services.data.search, | ||
savedObjectsClient: services.core.savedObjects.client, | ||
spaces: services.spaces, | ||
savedObjectsTagging: services.savedObjectsTagging, | ||
}); | ||
|
||
await throwErrorOnSavedSearchUrlConflict(savedSearch); | ||
|
||
const { searchSourceJSON } = savedSearch.searchSource.serialize(); | ||
|
||
return { | ||
attributes: toSavedSearchAttributes(savedSearch, searchSourceJSON), | ||
metaInfo: { | ||
sharingSavedObjectProps: savedSearch.sharingSavedObjectProps, | ||
tags: savedSearch.tags, | ||
searchSource: savedSearch.searchSource, | ||
}, | ||
}; | ||
}, | ||
checkForDuplicateTitle: (props: OnSaveProps) => { | ||
return checkForDuplicateTitle({ | ||
title: props.newTitle, | ||
isTitleDuplicateConfirmed: props.isTitleDuplicateConfirmed, | ||
onTitleDuplicate: props.onTitleDuplicate, | ||
savedObjectsClient: services.core.savedObjects.client, | ||
}); | ||
}, | ||
}); | ||
} |
Oops, something went wrong.