mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
* [ML] Switching to new es client * further conversions * fixing tests * updating responses * test commit * refactoring shared services to removed context parameter * removing last scoped clients * removing ml js client * udating file data viz errors * fixing jest tests * fixing types after merge with master * error response changes * adding default sizes to some requests * adding custom error types for license checks * tidying up shared function checks * removing test data * removing need for DummyKibanaRequest * updating comment * fixing functional api tests * removing comments * fixing types after master merge * throw error rather than return it * removing placeholder error * changes based on review comments * fixing types after merge with master * fixing missing return Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
131 lines
3.6 KiB
TypeScript
131 lines
3.6 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.
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
import { UI_SETTINGS } from '../../../../../../src/plugins/data/common';
|
|
import { KibanaRequest } from '../../../../../../src/core/server';
|
|
import { APMConfig } from '../..';
|
|
import {
|
|
getApmIndices,
|
|
ApmIndicesConfig,
|
|
} from '../settings/apm_indices/get_apm_indices';
|
|
import { ESFilter } from '../../../typings/elasticsearch';
|
|
import { getUiFiltersES } from './convert_ui_filters/get_ui_filters_es';
|
|
import { APMRequestHandlerContext } from '../../routes/typings';
|
|
import { ProcessorEvent } from '../../../common/processor_event';
|
|
import {
|
|
APMEventClient,
|
|
createApmEventClient,
|
|
} from './create_es_client/create_apm_event_client';
|
|
import {
|
|
APMInternalClient,
|
|
createInternalESClient,
|
|
} from './create_es_client/create_internal_es_client';
|
|
|
|
function decodeUiFilters(uiFiltersEncoded?: string) {
|
|
if (!uiFiltersEncoded) {
|
|
return [];
|
|
}
|
|
const uiFilters = JSON.parse(uiFiltersEncoded);
|
|
return getUiFiltersES(uiFilters);
|
|
}
|
|
// Explicitly type Setup to prevent TS initialization errors
|
|
// https://github.com/microsoft/TypeScript/issues/34933
|
|
|
|
export interface Setup {
|
|
apmEventClient: APMEventClient;
|
|
internalClient: APMInternalClient;
|
|
ml?: ReturnType<typeof getMlSetup>;
|
|
config: APMConfig;
|
|
indices: ApmIndicesConfig;
|
|
}
|
|
|
|
export interface SetupTimeRange {
|
|
start: number;
|
|
end: number;
|
|
}
|
|
|
|
export interface SetupUIFilters {
|
|
uiFiltersES: ESFilter[];
|
|
}
|
|
|
|
interface SetupRequestParams {
|
|
query?: {
|
|
_debug?: boolean;
|
|
start?: string;
|
|
end?: string;
|
|
uiFilters?: string;
|
|
processorEvent?: ProcessorEvent;
|
|
};
|
|
}
|
|
|
|
type InferSetup<TParams extends SetupRequestParams> = Setup &
|
|
(TParams extends { query: { start: string } } ? { start: number } : {}) &
|
|
(TParams extends { query: { end: string } } ? { end: number } : {}) &
|
|
(TParams extends { query: { uiFilters: string } }
|
|
? { uiFiltersES: ESFilter[] }
|
|
: {});
|
|
|
|
export async function setupRequest<TParams extends SetupRequestParams>(
|
|
context: APMRequestHandlerContext<TParams>,
|
|
request: KibanaRequest
|
|
): Promise<InferSetup<TParams>> {
|
|
const { config } = context;
|
|
const { query } = context.params;
|
|
|
|
const [indices, includeFrozen] = await Promise.all([
|
|
getApmIndices({
|
|
savedObjectsClient: context.core.savedObjects.client,
|
|
config,
|
|
}),
|
|
context.core.uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
|
|
]);
|
|
|
|
const uiFiltersES = decodeUiFilters(query.uiFilters);
|
|
|
|
const coreSetupRequest = {
|
|
indices,
|
|
apmEventClient: createApmEventClient({
|
|
context,
|
|
request,
|
|
indices,
|
|
options: { includeFrozen },
|
|
}),
|
|
internalClient: createInternalESClient({
|
|
context,
|
|
request,
|
|
}),
|
|
ml: getMlSetup(
|
|
context.plugins.ml,
|
|
context.core.savedObjects.client,
|
|
request
|
|
),
|
|
config,
|
|
};
|
|
|
|
return {
|
|
...('start' in query ? { start: moment.utc(query.start).valueOf() } : {}),
|
|
...('end' in query ? { end: moment.utc(query.end).valueOf() } : {}),
|
|
...('uiFilters' in query ? { uiFiltersES } : {}),
|
|
...coreSetupRequest,
|
|
} as InferSetup<TParams>;
|
|
}
|
|
|
|
function getMlSetup(
|
|
ml: APMRequestHandlerContext['plugins']['ml'],
|
|
savedObjectsClient: APMRequestHandlerContext['core']['savedObjects']['client'],
|
|
request: KibanaRequest
|
|
) {
|
|
if (!ml) {
|
|
return;
|
|
}
|
|
|
|
return {
|
|
mlSystem: ml.mlSystemProvider(request),
|
|
anomalyDetectors: ml.anomalyDetectorsProvider(request),
|
|
modules: ml.modulesProvider(request, savedObjectsClient),
|
|
};
|
|
}
|