mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
[Code] Use callWithRequest to send elasticsearch queries (#27563)
This commit is contained in:
parent
1c001af11c
commit
d17bf23cd9
8 changed files with 156 additions and 62 deletions
|
@ -40,7 +40,6 @@ import { socketRoute } from './server/routes/socket';
|
|||
import { userRoute } from './server/routes/user';
|
||||
import { workspaceRoute } from './server/routes/workspace';
|
||||
import { IndexScheduler, UpdateScheduler } from './server/scheduler';
|
||||
import { DocumentSearchClient, RepositorySearchClient, SymbolSearchClient } from './server/search';
|
||||
import { enableSecurity } from './server/security';
|
||||
import { ServerOptions } from './server/server_options';
|
||||
import { SocketService } from './server/socket_service';
|
||||
|
@ -92,7 +91,6 @@ export const code = (kibana: any) =>
|
|||
const queueIndex: string = server.config().get('xpack.code.queueIndex');
|
||||
const queueTimeout: number = server.config().get('xpack.code.queueTimeout');
|
||||
const adminCluster = server.plugins.elasticsearch.getCluster('admin');
|
||||
const dataCluster = server.plugins.elasticsearch.getCluster('data');
|
||||
const log = new Log(server);
|
||||
const serverOptions = new ServerOptions(options, server.config());
|
||||
|
||||
|
@ -109,14 +107,6 @@ export const code = (kibana: any) =>
|
|||
|
||||
const socketService = new SocketService(server, log);
|
||||
|
||||
// Initialize search clients
|
||||
// @ts-ignore
|
||||
const repoSearchClient = new RepositorySearchClient(dataCluster.getClient(), log);
|
||||
// @ts-ignore
|
||||
const documentSearchClient = new DocumentSearchClient(dataCluster.getClient(), log);
|
||||
// @ts-ignore
|
||||
const symbolSearchClient = new SymbolSearchClient(dataCluster.getClient(), log);
|
||||
|
||||
// @ts-ignore
|
||||
const esClient: EsClient = adminCluster.getClient();
|
||||
|
||||
|
@ -192,12 +182,12 @@ export const code = (kibana: any) =>
|
|||
indexWorker,
|
||||
repoIndexInitializerFactory
|
||||
);
|
||||
repositorySearchRoute(server, repoSearchClient);
|
||||
documentSearchRoute(server, documentSearchClient);
|
||||
symbolSearchRoute(server, symbolSearchClient);
|
||||
repositorySearchRoute(server, log);
|
||||
documentSearchRoute(server, log);
|
||||
symbolSearchRoute(server, log);
|
||||
fileRoute(server, serverOptions);
|
||||
workspaceRoute(server, serverOptions, esClient);
|
||||
symbolByQnameRoute(server, symbolSearchClient);
|
||||
workspaceRoute(server, serverOptions);
|
||||
symbolByQnameRoute(server, log);
|
||||
socketRoute(server, socketService, log);
|
||||
userRoute(server, serverOptions);
|
||||
installRoute(server, socketService, lspService, installManager, serverOptions);
|
||||
|
|
|
@ -25,6 +25,7 @@ import {
|
|||
mergeRanges,
|
||||
} from '../utils/composite_source_merger';
|
||||
import { detectLanguage } from '../utils/detect_language';
|
||||
import { EsClientWithRequest } from '../utils/esclient_with_request';
|
||||
import { promiseTimeout } from '../utils/timeout';
|
||||
|
||||
export function lspRoute(
|
||||
|
@ -171,12 +172,13 @@ export function lspRoute(
|
|||
});
|
||||
}
|
||||
|
||||
export function symbolByQnameRoute(server: hapi.Server, symbolSearchClient: SymbolSearchClient) {
|
||||
export function symbolByQnameRoute(server: hapi.Server, log: Log) {
|
||||
server.route({
|
||||
path: '/api/lsp/symbol/{qname}',
|
||||
method: 'GET',
|
||||
async handler(req, reply) {
|
||||
async handler(req) {
|
||||
try {
|
||||
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await symbolSearchClient.findByQname(req.params.qname);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
|
|
@ -15,6 +15,7 @@ import { Log } from '../log';
|
|||
import { CloneWorker, DeleteWorker, IndexWorker } from '../queue';
|
||||
import { RepositoryObjectClient } from '../search';
|
||||
import { ServerOptions } from '../server_options';
|
||||
import { EsClientWithRequest } from '../utils/esclient_with_request';
|
||||
|
||||
export function repositoryRoute(
|
||||
server: Server,
|
||||
|
@ -40,10 +41,7 @@ export function repositoryRoute(
|
|||
}
|
||||
|
||||
const repo = RepositoryUtils.buildRepository(repoUrl);
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
|
||||
try {
|
||||
// Check if the repository already exists
|
||||
|
@ -88,10 +86,7 @@ export function repositoryRoute(
|
|||
async handler(req, h) {
|
||||
const repoUri: string = req.params.uri as string;
|
||||
const log = new Log(req.server);
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
try {
|
||||
// Check if the repository already exists. If not, an error will be thrown.
|
||||
await repoObjectClient.getRepository(repoUri);
|
||||
|
@ -131,10 +126,7 @@ export function repositoryRoute(
|
|||
const repoUri = req.params.uri as string;
|
||||
const log = new Log(req.server);
|
||||
try {
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
return await repoObjectClient.getRepository(repoUri);
|
||||
} catch (error) {
|
||||
const msg = `Get repository ${repoUri} error: ${error}`;
|
||||
|
@ -151,10 +143,7 @@ export function repositoryRoute(
|
|||
const repoUri = req.params.uri as string;
|
||||
const log = new Log(req.server);
|
||||
try {
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
return await repoObjectClient.getRepositoryGitStatus(repoUri);
|
||||
} catch (error) {
|
||||
const msg = `Get repository clone status ${repoUri} error: ${error}`;
|
||||
|
@ -171,10 +160,7 @@ export function repositoryRoute(
|
|||
async handler(req) {
|
||||
const log = new Log(req.server);
|
||||
try {
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
return await repoObjectClient.getAllRepositories();
|
||||
} catch (error) {
|
||||
const msg = `Get all repositories error: ${error}`;
|
||||
|
@ -194,10 +180,7 @@ export function repositoryRoute(
|
|||
const repoUri = req.params.uri as string;
|
||||
const log = new Log(req.server);
|
||||
try {
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
const cloneStatus = await repoObjectClient.getRepositoryGitStatus(repoUri);
|
||||
|
||||
const payload = {
|
||||
|
@ -231,10 +214,7 @@ export function repositoryRoute(
|
|||
}
|
||||
|
||||
const repo = RepositoryUtils.buildRepository(repoUrl);
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
|
||||
try {
|
||||
// Check if the repository exists
|
||||
|
@ -265,10 +245,7 @@ export function repositoryRoute(
|
|||
const repoUri = req.params.uri as string;
|
||||
const log = new Log(req.server);
|
||||
try {
|
||||
const repoObjectClient = new RepositoryObjectClient(
|
||||
// @ts-ignore
|
||||
req.server.plugins.elasticsearch.getCluster('data').getClient()
|
||||
);
|
||||
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(req));
|
||||
return await repoObjectClient.getRepositoryConfig(repoUri);
|
||||
} catch (error) {
|
||||
const msg = `Get repository config ${repoUri} error: ${error}`;
|
||||
|
|
|
@ -8,12 +8,11 @@ import Boom from 'boom';
|
|||
|
||||
import hapi from 'hapi';
|
||||
import { DocumentSearchRequest, RepositorySearchRequest, SymbolSearchRequest } from '../../model';
|
||||
import { Log } from '../log';
|
||||
import { DocumentSearchClient, RepositorySearchClient, SymbolSearchClient } from '../search';
|
||||
import { EsClientWithRequest } from '../utils/esclient_with_request';
|
||||
|
||||
export function repositorySearchRoute(
|
||||
server: hapi.Server,
|
||||
repoSearchClient: RepositorySearchClient
|
||||
) {
|
||||
export function repositorySearchRoute(server: hapi.Server, log: Log) {
|
||||
server.route({
|
||||
path: '/api/code/search/repo',
|
||||
method: 'GET',
|
||||
|
@ -28,6 +27,7 @@ export function repositorySearchRoute(
|
|||
page,
|
||||
};
|
||||
try {
|
||||
const repoSearchClient = new RepositorySearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await repoSearchClient.search(searchReq);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
@ -50,6 +50,7 @@ export function repositorySearchRoute(
|
|||
page,
|
||||
};
|
||||
try {
|
||||
const repoSearchClient = new RepositorySearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await repoSearchClient.suggest(searchReq);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
@ -59,7 +60,7 @@ export function repositorySearchRoute(
|
|||
});
|
||||
}
|
||||
|
||||
export function documentSearchRoute(server: hapi.Server, docSearchClient: DocumentSearchClient) {
|
||||
export function documentSearchRoute(server: hapi.Server, log: Log) {
|
||||
server.route({
|
||||
path: '/api/code/search/doc',
|
||||
method: 'GET',
|
||||
|
@ -76,6 +77,7 @@ export function documentSearchRoute(server: hapi.Server, docSearchClient: Docume
|
|||
repoFileters: repos ? decodeURIComponent(repos as string).split(',') : [],
|
||||
};
|
||||
try {
|
||||
const docSearchClient = new DocumentSearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await docSearchClient.search(searchReq);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
@ -98,6 +100,7 @@ export function documentSearchRoute(server: hapi.Server, docSearchClient: Docume
|
|||
page,
|
||||
};
|
||||
try {
|
||||
const docSearchClient = new DocumentSearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await docSearchClient.suggest(searchReq);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
@ -107,7 +110,7 @@ export function documentSearchRoute(server: hapi.Server, docSearchClient: Docume
|
|||
});
|
||||
}
|
||||
|
||||
export function symbolSearchRoute(server: hapi.Server, symbolSearchClient: SymbolSearchClient) {
|
||||
export function symbolSearchRoute(server: hapi.Server, log: Log) {
|
||||
const symbolSearchHandler = async (req: hapi.Request) => {
|
||||
let page = 1;
|
||||
const { p, q } = req.query as hapi.RequestQuery;
|
||||
|
@ -119,6 +122,7 @@ export function symbolSearchRoute(server: hapi.Server, symbolSearchClient: Symbo
|
|||
page,
|
||||
};
|
||||
try {
|
||||
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(req), log);
|
||||
const res = await symbolSearchClient.suggest(searchReq);
|
||||
return res;
|
||||
} catch (error) {
|
||||
|
|
|
@ -7,18 +7,14 @@
|
|||
import Boom from 'boom';
|
||||
import hapi, { RequestQuery } from 'hapi';
|
||||
|
||||
import { EsClient } from '../lib/esqueue';
|
||||
import { Log } from '../log';
|
||||
import { WorkspaceCommand } from '../lsp/workspace_command';
|
||||
import { WorkspaceHandler } from '../lsp/workspace_handler';
|
||||
import { ServerOptions } from '../server_options';
|
||||
import { EsClientWithRequest } from '../utils/esclient_with_request';
|
||||
import { ServerLoggerFactory } from '../utils/server_logger_factory';
|
||||
|
||||
export function workspaceRoute(
|
||||
server: hapi.Server,
|
||||
serverOptions: ServerOptions,
|
||||
client: EsClient
|
||||
) {
|
||||
export function workspaceRoute(server: hapi.Server, serverOptions: ServerOptions) {
|
||||
server.route({
|
||||
path: '/api/code/workspace',
|
||||
method: 'GET',
|
||||
|
@ -40,7 +36,7 @@ export function workspaceRoute(
|
|||
const workspaceHandler = new WorkspaceHandler(
|
||||
serverOptions.repoPath,
|
||||
serverOptions.workspacePath,
|
||||
client,
|
||||
new EsClientWithRequest(req),
|
||||
new ServerLoggerFactory(server)
|
||||
);
|
||||
try {
|
||||
|
|
52
x-pack/plugins/code/server/utils/es_index_client.ts
Normal file
52
x-pack/plugins/code/server/utils/es_index_client.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { AnyObject } from '../lib/esqueue';
|
||||
import { WithRequest } from './with_request';
|
||||
|
||||
export class EsIndexClient {
|
||||
constructor(readonly self: WithRequest) {}
|
||||
|
||||
public exists(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.exists', params);
|
||||
}
|
||||
|
||||
public create(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.create', params);
|
||||
}
|
||||
|
||||
public refresh(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.refresh', params);
|
||||
}
|
||||
|
||||
public delete(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.delete', params);
|
||||
}
|
||||
|
||||
public existsAlias(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.existsAlias', params);
|
||||
}
|
||||
|
||||
public getAlias(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.getAlias', params);
|
||||
}
|
||||
|
||||
public putAlias(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.putAlias', params);
|
||||
}
|
||||
|
||||
public deleteAlias(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.deleteAlias', params);
|
||||
}
|
||||
|
||||
public updateAliases(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.updateAliases', params);
|
||||
}
|
||||
|
||||
public getMapping(params: AnyObject): Promise<any> {
|
||||
return this.self.callWithRequest('indices.getMapping', params);
|
||||
}
|
||||
}
|
55
x-pack/plugins/code/server/utils/esclient_with_request.ts
Normal file
55
x-pack/plugins/code/server/utils/esclient_with_request.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { Request } from 'hapi';
|
||||
// @ts-ignore
|
||||
import { AnyObject, EsClient } from '../lib/esqueue';
|
||||
import { EsIndexClient } from './es_index_client';
|
||||
import { WithRequest } from './with_request';
|
||||
|
||||
export class EsClientWithRequest extends WithRequest implements EsClient {
|
||||
public readonly indices = new EsIndexClient(this);
|
||||
|
||||
constructor(readonly req: Request) {
|
||||
super(req);
|
||||
}
|
||||
|
||||
public bulk(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('bulk', params);
|
||||
}
|
||||
|
||||
public delete(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('delete', params);
|
||||
}
|
||||
|
||||
public deleteByQuery(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('deleteByQuery', params);
|
||||
}
|
||||
|
||||
public get(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('get', params);
|
||||
}
|
||||
|
||||
public index(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('index', params);
|
||||
}
|
||||
|
||||
public ping(): Promise<void> {
|
||||
return this.callWithRequest('ping');
|
||||
}
|
||||
|
||||
public reindex(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('reindex', params);
|
||||
}
|
||||
|
||||
public search(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('search', params);
|
||||
}
|
||||
|
||||
public update(params: AnyObject): Promise<any> {
|
||||
return this.callWithRequest('update', params);
|
||||
}
|
||||
}
|
18
x-pack/plugins/code/server/utils/with_request.ts
Normal file
18
x-pack/plugins/code/server/utils/with_request.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { Request } from 'hapi';
|
||||
import { AnyObject } from '../lib/esqueue';
|
||||
|
||||
export class WithRequest {
|
||||
public readonly callWithRequest: (endpoint: string, clientOptions?: AnyObject) => Promise<any>;
|
||||
|
||||
constructor(readonly req: Request) {
|
||||
this.callWithRequest = req.server.plugins.elasticsearch
|
||||
.getCluster('data')
|
||||
.callWithRequest.bind(null, req);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue