Skip to content

Commit

Permalink
[TSVB] Enable dual mode, support index patterns and strings (#92395)
Browse files Browse the repository at this point in the history
Part of #91367

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
alexwizp and kibanamachine authored Feb 24, 2021
1 parent 698a06a commit d2abc03
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 67 deletions.
1 change: 0 additions & 1 deletion src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export async function getFields(
),
},
getUiSettingsService: () => requestContext.core.uiSettings.client,
getSavedObjectsClient: () => requestContext.core.savedObjects.client,
getEsShardTimeout: async () => {
return await framework.globalConfig$
.pipe(
Expand Down
1 change: 0 additions & 1 deletion src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function getVisData(
pre: {},
payload: request.body,
getUiSettingsService: () => requestContext.core.uiSettings.client,
getSavedObjectsClient: () => requestContext.core.savedObjects.client,
getEsShardTimeout: async () => {
return await framework.globalConfig$
.pipe(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { IndexPatternsService, IndexPattern } from '../../../../../data/server';

interface IndexPatternObjectDependencies {
indexPatternsService: IndexPatternsService;
}
export async function getIndexPatternObject(
indexPatternString: string,
{ indexPatternsService }: IndexPatternObjectDependencies
) {
let indexPatternObject: IndexPattern | undefined | null;

if (!indexPatternString) {
indexPatternObject = await indexPatternsService.getDefault();
} else {
indexPatternObject = (await indexPatternsService.find(indexPatternString)).find(
(index) => index.title === indexPatternString
);
}

return {
indexPatternObject,
indexPatternString: indexPatternObject?.title || indexPatternString || '',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('AbstractSearchStrategy', () => {
getIndexPatternsService: jest.fn(() =>
Promise.resolve({
find: jest.fn(() => []),
getDefault: jest.fn(() => {}),
})
),
} as unknown) as ReqFacade<VisPayload>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Side Public License, v 1.
*/

import type { FakeRequest, IUiSettingsClient, SavedObjectsClientContract } from 'kibana/server';
import type { FakeRequest, IUiSettingsClient } from 'kibana/server';

import { indexPatterns, IndexPatternsFetcher } from '../../../../../data/server';

import type { Framework } from '../../../plugin';
import type { FieldSpec, IndexPatternsService } from '../../../../../data/common';
import type { VisPayload, SanitizedFieldType } from '../../../../common/types';
import type { VisTypeTimeseriesRequestHandlerContext } from '../../../types';
import { getIndexPatternObject } from '../lib/get_index_pattern';

/**
* ReqFacade is a regular KibanaRequest object extended with additional service
Expand All @@ -29,7 +30,6 @@ export interface ReqFacade<T = unknown> extends FakeRequest {
indexPatternsFetcher?: IndexPatternsFetcher;
};
getUiSettingsService: () => IUiSettingsClient;
getSavedObjectsClient: () => SavedObjectsClientContract;
getEsShardTimeout: () => Promise<number>;
getIndexPatternsService: () => Promise<IndexPatternsService>;
}
Expand Down Expand Up @@ -89,16 +89,14 @@ export abstract class AbstractSearchStrategy {
rollupIndex: string;
}>
) {
const { indexPatternsFetcher } = req.pre;
const indexPatternsService = await req.getIndexPatternsService();
const kibanaIndexPattern = (await indexPatternsService.find(indexPattern)).find(
(index) => index.title === indexPattern
);
const { indexPatternObject } = await getIndexPatternObject(indexPattern, {
indexPatternsService: await req.getIndexPatternsService(),
});

return toSanitizedFieldType(
kibanaIndexPattern
? kibanaIndexPattern.getNonScriptedFields()
: await indexPatternsFetcher!.getFieldsForWildcard({
indexPatternObject
? indexPatternObject.getNonScriptedFields()
: await req.pre.indexPatternsFetcher!.getFieldsForWildcard({
pattern: indexPattern,
fieldCapsOptions: { allow_no_indices: true },
metaFields: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { buildAnnotationRequest } from './build_request_body';
import { getEsShardTimeout } from '../helpers/get_es_shard_timeout';
import { getIndexPatternObject } from '../helpers/get_index_pattern';
import { getIndexPatternObject } from '../../search_strategies/lib/get_index_pattern';

export async function getAnnotationRequestParams(
req,
Expand All @@ -19,8 +19,13 @@ export async function getAnnotationRequestParams(
) {
const uiSettings = req.getUiSettingsService();
const esShardTimeout = await getEsShardTimeout(req);
const indexPattern = annotation.index_pattern;
const { indexPatternObject, indexPatternString } = await getIndexPatternObject(req, indexPattern);
const { indexPatternObject, indexPatternString } = await getIndexPatternObject(
annotation.index_pattern,
{
indexPatternsService: await req.getIndexPatternsService(),
}
);

const request = await buildAnnotationRequest(
req,
panel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { handleErrorResponse } from './handle_error_response';
import { get } from 'lodash';
import { processBucket } from './table/process_bucket';
import { getEsQueryConfig } from './helpers/get_es_query_uisettings';
import { getIndexPatternObject } from './helpers/get_index_pattern';
import { getIndexPatternObject } from '../search_strategies/lib/get_index_pattern';
import { createFieldsFetcher } from './helpers/fields_fetcher';
import { extractFieldLabel } from '../../../common/calculate_label';

Expand All @@ -23,7 +23,10 @@ export async function getTableData(req, panel) {
capabilities,
} = await req.framework.searchStrategyRegistry.getViableStrategy(req, panelIndexPattern);
const esQueryConfig = await getEsQueryConfig(req);
const { indexPatternObject } = await getIndexPatternObject(req, panelIndexPattern);
const { indexPatternObject } = await getIndexPatternObject(panelIndexPattern, {
indexPatternsService: await req.getIndexPatternsService(),
});

const extractFields = createFieldsFetcher(req, searchStrategy, capabilities);

const calculatePivotLabel = async () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import { buildRequestBody } from './build_request_body';
import { getEsShardTimeout } from '../helpers/get_es_shard_timeout';
import { getIndexPatternObject } from '../helpers/get_index_pattern';
import { getIndexPatternObject } from '../../../lib/search_strategies/lib/get_index_pattern';

export async function getSeriesRequestParams(req, panel, series, esQueryConfig, capabilities) {
const uiSettings = req.getUiSettingsService();
const indexPattern =
(series.override_index_pattern && series.series_index_pattern) || panel.index_pattern;
const { indexPatternObject, indexPatternString } = await getIndexPatternObject(req, indexPattern);

const { indexPatternObject, indexPatternString } = await getIndexPatternObject(indexPattern, {
indexPatternsService: await req.getIndexPatternsService(),
});

const request = await buildRequestBody(
req,
Expand Down

0 comments on commit d2abc03

Please sign in to comment.