mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 10:23:14 -04:00
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
/*
|
|
* 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.
|
|
*/
|
|
|
|
/* tslint:disable no-console */
|
|
import { AggregationSearchResponse, SearchParams } from 'elasticsearch';
|
|
import { Request } from 'hapi';
|
|
import moment from 'moment';
|
|
|
|
function decodeEsQuery(esQuery?: string): object {
|
|
return esQuery ? JSON.parse(decodeURIComponent(esQuery)) : null;
|
|
}
|
|
|
|
interface KibanaConfig {
|
|
get: <T = void>(key: string) => T;
|
|
}
|
|
|
|
// Extend the defaults with the plugins and server methods we need.
|
|
declare module 'hapi' {
|
|
interface PluginProperties {
|
|
elasticsearch: any;
|
|
}
|
|
|
|
interface Server {
|
|
config: () => KibanaConfig;
|
|
}
|
|
}
|
|
|
|
export type ESClient = <T = void, U = void>(
|
|
type: string,
|
|
params: SearchParams
|
|
) => Promise<AggregationSearchResponse<T, U>>;
|
|
|
|
export interface Setup {
|
|
start: number;
|
|
end: number;
|
|
esFilterQuery?: any;
|
|
client: ESClient;
|
|
config: KibanaConfig;
|
|
}
|
|
|
|
interface APMRequestQuery {
|
|
_debug: string;
|
|
start: string;
|
|
end: string;
|
|
esFilterQuery: string;
|
|
}
|
|
|
|
export function setupRequest(req: Request): Setup {
|
|
const query = (req.query as unknown) as APMRequestQuery;
|
|
const cluster = req.server.plugins.elasticsearch.getCluster('data');
|
|
|
|
const client: ESClient = (type, params) => {
|
|
if (query._debug) {
|
|
console.log(`DEBUG ES QUERY:`);
|
|
console.log(
|
|
`${req.method.toUpperCase()} ${req.url.pathname} ${JSON.stringify(
|
|
query
|
|
)}`
|
|
);
|
|
console.log(`GET ${params.index}/_search`);
|
|
console.log(JSON.stringify(params.body, null, 4));
|
|
}
|
|
|
|
const nextParams = {
|
|
...params,
|
|
rest_total_hits_as_int: true // ensure that ES returns accurate hits.total with pre-6.6 format
|
|
};
|
|
return cluster.callWithRequest(req, type, nextParams);
|
|
};
|
|
|
|
return {
|
|
start: moment.utc(query.start).valueOf(),
|
|
end: moment.utc(query.end).valueOf(),
|
|
esFilterQuery: decodeEsQuery(query.esFilterQuery),
|
|
client,
|
|
config: req.server.config()
|
|
};
|
|
}
|