-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Add types for setup_request (#23762)
- Loading branch information
Showing
7 changed files
with
84 additions
and
61 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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
/* | ||
* 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 { SearchParams, SearchResponse } from 'elasticsearch'; | ||
import moment from 'moment'; | ||
import { Url } from 'url'; | ||
import { StringMap } from '../../../typings/common'; | ||
|
||
function decodeEsQuery(esQuery?: string) { | ||
return esQuery ? JSON.parse(decodeURIComponent(esQuery)) : null; | ||
} | ||
|
||
// TODO: get these from hapi | ||
interface Request { | ||
query: StringMap; | ||
server: any; | ||
method: string; | ||
url: Url; | ||
} | ||
|
||
interface ServerConfig { | ||
get: (key: string) => string; | ||
} | ||
|
||
type Client<T> = (type: string, params: SearchParams) => SearchResponse<T>; | ||
|
||
export interface Setup<T = any> { | ||
start: number; | ||
end: number; | ||
esFilterQuery: string; | ||
client: Client<T>; | ||
config: ServerConfig; | ||
} | ||
|
||
export function setupRequest(req: Request, reply: (setup: Setup) => void) { | ||
const cluster = req.server.plugins.elasticsearch.getCluster('data'); | ||
|
||
function client<T>(type: string, params: SearchParams): SearchResponse<T> { | ||
if (req.query._debug) { | ||
console.log(`DEBUG ES QUERY:`); | ||
console.log( | ||
`${req.method.toUpperCase()} ${req.url.pathname} ${JSON.stringify( | ||
req.query | ||
)}` | ||
); | ||
console.log(`GET ${params.index}/_search`); | ||
console.log(JSON.stringify(params.body, null, 4)); | ||
} | ||
return cluster.callWithRequest(req, type, params); | ||
} | ||
|
||
const setup = { | ||
start: moment.utc(req.query.start).valueOf(), | ||
end: moment.utc(req.query.end).valueOf(), | ||
esFilterQuery: decodeEsQuery(req.query.esFilterQuery), | ||
client, | ||
config: req.server.config() | ||
}; | ||
|
||
reply(setup); | ||
} |
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