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

[Watcher] Migrate to new ES client #97260

Merged
merged 14 commits into from
Apr 30, 2021
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
246 changes: 0 additions & 246 deletions x-pack/plugins/watcher/server/lib/elasticsearch_js_plugin.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import { elasticsearchServiceMock } from '../../../../../../src/core/server/mock
import { fetchAllFromScroll } from './fetch_all_from_scroll';

describe('fetch_all_from_scroll', () => {
let mockScopedClusterClient;
const mockScopedClusterClient = {};

beforeEach(() => {
mockScopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient();

elasticsearchServiceMock
.createLegacyClusterClient()
.asScoped.mockReturnValue(mockScopedClusterClient);
mockScopedClusterClient.asCurrentUser = elasticsearchServiceMock.createElasticsearchClient();
});

describe('#fetchAllFromScroll', () => {
Expand All @@ -33,9 +29,9 @@ describe('fetch_all_from_scroll', () => {
});
});

it('should not call callWithRequest', () => {
it('should not call asCurrentUser.scroll', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like how these tests became clearer as a result of this migration!

return fetchAllFromScroll(mockSearchResults, mockScopedClusterClient).then(() => {
expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled();
expect(mockScopedClusterClient.asCurrentUser.scroll).not.toHaveBeenCalled();
});
});
});
Expand All @@ -62,9 +58,9 @@ describe('fetch_all_from_scroll', () => {
},
};

mockScopedClusterClient.callAsCurrentUser
.mockReturnValueOnce(Promise.resolve(mockResponse1))
.mockReturnValueOnce(Promise.resolve(mockResponse2));
mockScopedClusterClient.asCurrentUser.scroll
.mockResolvedValueOnce({ body: mockResponse1 })
.mockResolvedValueOnce({ body: mockResponse2 });
});

it('should return the hits from the response', () => {
Expand All @@ -75,14 +71,14 @@ describe('fetch_all_from_scroll', () => {
);
});

it('should call callWithRequest', () => {
it('should call asCurrentUser.scroll', () => {
return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then(() => {
expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledTimes(2);
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenCalledTimes(2);

expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(1, 'scroll', {
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenNthCalledWith(1, {
body: { scroll: '30s', scroll_id: 'originalScrollId' },
});
expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(2, 'scroll', {
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenNthCalledWith(2, {
body: { scroll: '30s', scroll_id: 'newScrollId' },
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
* 2.0.
*/

import { ILegacyScopedClusterClient } from 'kibana/server';
import { ScrollResponse, Hit } from '@elastic/elasticsearch/api/types';
import { IScopedClusterClient } from 'kibana/server';
import { get } from 'lodash';
import { ES_SCROLL_SETTINGS } from '../../../common/constants';

export function fetchAllFromScroll(
searchResuls: any,
dataClient: ILegacyScopedClusterClient,
hits: any[] = []
): Promise<any> {
const newHits = get(searchResuls, 'hits.hits', []);
const scrollId = get(searchResuls, '_scroll_id');
searchResults: ScrollResponse<unknown>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice typo catch!

dataClient: IScopedClusterClient,
hits: Hit[] = []
): Promise<ScrollResponse['hits']['hits']> {
const newHits = get(searchResults, 'hits.hits', []);
const scrollId = get(searchResults, '_scroll_id');

if (newHits.length > 0) {
hits.push(...newHits);

return dataClient
.callAsCurrentUser('scroll', {
return dataClient.asCurrentUser
.scroll({
body: {
scroll: ES_SCROLL_SETTINGS.KEEPALIVE,
scroll_id: scrollId,
},
})
.then((innerResponse: any) => {
.then(({ body: innerResponse }) => {
return fetchAllFromScroll(innerResponse, dataClient, hits);
});
}
Expand Down
Loading