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.
- Loading branch information
Showing
5 changed files
with
180 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
"features", | ||
"usageCollection", | ||
"spaces", | ||
|
||
"data", | ||
"dataEnhanced", | ||
"visTypeTimeseries", | ||
|
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
122 changes: 122 additions & 0 deletions
122
x-pack/plugins/infra/server/search_strategy/provider.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,122 @@ | ||
/* | ||
* 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 { ISearchStrategy, PluginStart } from '../../../../../src/plugins/data/server'; | ||
import { IEsSearchResponse, ISearchRequestParams } from '../../../../../src/plugins/data/common'; | ||
|
||
// NOTE: Types are shown as super rough examples here. We usually stay away from enums, so they'd likely be | ||
// keyof via io-ts. And 'any' is used in some places as it just sped up putting the example together. | ||
interface ExampleRequestOptions { | ||
queryType: FactoryQueryTypes, | ||
params?: { | ||
size: 10, | ||
body: { | ||
query: {} | ||
} | ||
} | ||
} | ||
|
||
type LogEntriesEntriesReqestOptions = ExampleRequestOptions; | ||
type LogEntriesItemReqestOptions = ExampleRequestOptions; | ||
|
||
interface ExampleResponse extends IEsSearchResponse { | ||
results: { | ||
name: string | ||
}[] | ||
}; | ||
|
||
type LogEntriesEntriesStrategyResponse = ExampleResponse; | ||
type LogEntriesItemStrategyResponse = ExampleResponse; | ||
|
||
export enum LogEntriesQueries { | ||
entries = 'entries', | ||
highlights = 'highlights', | ||
summaryBuckets = 'summaryBuckets', | ||
item = 'item', | ||
} | ||
|
||
type FactoryQueryTypes = LogEntriesQueries; | ||
|
||
type StrategyRequestType<T extends FactoryQueryTypes> = T extends LogEntriesQueries.entries | ||
? LogEntriesEntriesReqestOptions | ||
: T extends LogEntriesQueries.item | ||
? LogEntriesItemReqestOptions | ||
: never; | ||
|
||
export type StrategyResponseType<T extends FactoryQueryTypes> = T extends LogEntriesQueries.entries | ||
? LogEntriesEntriesStrategyResponse | ||
: T extends LogEntriesQueries.item | ||
? LogEntriesItemStrategyResponse | ||
: never; | ||
|
||
|
||
|
||
export interface InfraQueryTypeFactory<T extends FactoryQueryTypes> { | ||
buildDsl: (options: StrategyRequestType<T>) => ISearchRequestParams; | ||
parse: ( | ||
options: StrategyRequestType<T>, | ||
response: IEsSearchResponse | ||
) => Promise<StrategyResponseType<T>>; | ||
} | ||
|
||
|
||
// NOTE: Switch harcoded ID to something you know exists to try this out | ||
const queryTypeFactories = { | ||
[LogEntriesQueries.item]: { | ||
buildDsl: (options: any) => { | ||
return { | ||
"index":"logs-*,filebeat-*,kibana_sample_data_logs*", | ||
"terminate_after":1, | ||
"body":{ | ||
"size":1, | ||
"sort":[ | ||
{ | ||
"@timestamp":"desc" | ||
}, | ||
{ | ||
"_doc":"desc" | ||
} | ||
], | ||
"query":{ | ||
"ids":{ | ||
"values":[ | ||
"ww5WbXQBHyE_1lJ-g4vn" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
parse: async ( | ||
options: any, | ||
response: any, | ||
): Promise<any> => { | ||
return { ...response } | ||
}, | ||
} | ||
} | ||
export const infraSearchStrategyProvider = <T extends FactoryQueryTypes>( | ||
data: PluginStart | ||
): ISearchStrategy<StrategyRequestType<T>, StrategyResponseType<T>> => { | ||
const es = data.search.getSearchStrategy('es'); | ||
|
||
return { | ||
search: async (context, request, options) => { | ||
if (request.queryType == null) { | ||
throw new Error('queryType is required'); | ||
} | ||
const queryFactory: InfraQueryTypeFactory<T> = queryTypeFactories[request.queryType]; | ||
const dsl = queryFactory.buildDsl(request); | ||
const esSearchRes = await es.search(context, { ...request, params: dsl }, options); | ||
return queryFactory.parse(request, esSearchRes); | ||
}, | ||
cancel: async (context, id) => { | ||
if (es.cancel) { | ||
es.cancel(context, id); | ||
} | ||
}, | ||
}; | ||
}; |