Skip to content

Commit

Permalink
[Discover] Initial implementation of saved searches by value
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Dec 2, 2022
1 parent 16ad022 commit fbb86ec
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 135 deletions.
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,
});
},
});
}
Loading

0 comments on commit fbb86ec

Please sign in to comment.