forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Onboarding] delete document action (elastic#195263)
## Summary Ability to delete documents. I had to reduce the capability of the search_documents hook so the delete document hook can optimistically update the change. These capabilities were not in use. ![image](https://github.com/user-attachments/assets/85df2146-e6d5-4812-b825-bbbca6049215) ### Checklist Delete any items that are not applicable to this PR. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
- Loading branch information
1 parent
b5bc989
commit faa74d2
Showing
16 changed files
with
266 additions
and
23 deletions.
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
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
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/search_indices/public/hooks/api/use_delete_document.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,72 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { AcknowledgedResponseBase } from '@elastic/elasticsearch/lib/api/types'; | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { MutationKeys, QueryKeys } from '../../constants'; | ||
import { useKibana } from '../use_kibana'; | ||
import { INDEX_SEARCH_POLLING, IndexDocuments } from './use_document_search'; | ||
|
||
interface DeleteDocumentParams { | ||
id: string; | ||
} | ||
|
||
export const useDeleteDocument = (indexName: string) => { | ||
const { http } = useKibana().services; | ||
const queryClient = useQueryClient(); | ||
|
||
const result = useMutation( | ||
async ({ id }: DeleteDocumentParams) => { | ||
const response = await http.delete<AcknowledgedResponseBase>( | ||
`/internal/search_indices/${indexName}/documents/${id}` | ||
); | ||
return response.acknowledged; | ||
}, | ||
{ | ||
mutationKey: [MutationKeys.SearchIndicesDeleteDocument, indexName], | ||
onMutate: async ({ id }: DeleteDocumentParams) => { | ||
await queryClient.cancelQueries([QueryKeys.SearchDocuments, indexName]); | ||
|
||
const previousData = queryClient.getQueryData<IndexDocuments>([ | ||
QueryKeys.SearchDocuments, | ||
indexName, | ||
]); | ||
|
||
queryClient.setQueryData( | ||
[QueryKeys.SearchDocuments, indexName], | ||
(snapshot: IndexDocuments | undefined) => { | ||
const oldData = snapshot ?? { results: { data: [] } }; | ||
return { | ||
...oldData, | ||
results: { | ||
...oldData.results, | ||
data: oldData.results.data.filter((doc: SearchHit) => doc._id !== id), | ||
}, | ||
} as IndexDocuments; | ||
} | ||
); | ||
|
||
return { previousData }; | ||
}, | ||
|
||
onSuccess: () => { | ||
setTimeout(() => { | ||
queryClient.invalidateQueries([QueryKeys.SearchDocuments, indexName]); | ||
}, INDEX_SEARCH_POLLING); | ||
}, | ||
|
||
onError: (error, _, context) => { | ||
if (context?.previousData) { | ||
queryClient.setQueryData([QueryKeys.SearchDocuments, indexName], context.previousData); | ||
} | ||
return error; | ||
}, | ||
} | ||
); | ||
return { ...result }; | ||
}; |
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
56 changes: 56 additions & 0 deletions
56
x-pack/plugins/search_indices/server/lib/documents.test.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,56 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { deleteDocument } from './documents'; | ||
import { Logger } from '@kbn/logging'; | ||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
|
||
describe('deleteDocument', () => { | ||
let mockClient: jest.Mocked<ElasticsearchClient>; | ||
let mockLogger: jest.Mocked<Logger>; | ||
|
||
beforeEach(() => { | ||
mockClient = { | ||
delete: jest.fn().mockResolvedValue({}), | ||
} as unknown as jest.Mocked<ElasticsearchClient>; | ||
|
||
mockLogger = { | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
debug: jest.fn(), | ||
trace: jest.fn(), | ||
fatal: jest.fn(), | ||
log: jest.fn(), | ||
} as unknown as jest.Mocked<Logger>; | ||
}); | ||
|
||
it('should delete a document and return true', async () => { | ||
const index = 'test-index'; | ||
const id = 'test-id'; | ||
|
||
const result = await deleteDocument(mockClient, mockLogger, index, id); | ||
|
||
expect(mockClient.delete).toHaveBeenCalledWith({ | ||
index, | ||
id, | ||
}); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should log an error and throw when delete fails', async () => { | ||
const index = 'test-index'; | ||
const id = 'test-id'; | ||
const error = new Error('Delete failed'); | ||
|
||
mockClient.delete.mockRejectedValue(error); | ||
|
||
await expect(deleteDocument(mockClient, mockLogger, index, id)).rejects.toThrow( | ||
'Delete failed' | ||
); | ||
}); | ||
}); |
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,22 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import type { Logger } from '@kbn/logging'; | ||
|
||
export async function deleteDocument( | ||
client: ElasticsearchClient, | ||
logger: Logger, | ||
index: string, | ||
id: string | ||
): Promise<boolean> { | ||
await client.delete({ | ||
index, | ||
id, | ||
}); | ||
return true; | ||
} |
Oops, something went wrong.