From 0be57972227b0d903d07f728d35402b38172e637 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 9 Nov 2020 18:14:10 +0300 Subject: [PATCH 01/10] [TSVB] use new Search API for rollup search Closes: #82710 --- .../vis_type_timeseries/server/index.ts | 2 +- ...ties.js => default_search_capabilities.ts} | 34 +++--- .../search_strategies_registry.test.ts | 4 +- .../strategies/abstract_search_strategy.ts | 12 +- ...strategy.js => default_search_strategy.ts} | 8 +- ...{unit_to_seconds.js => unit_to_seconds.ts} | 14 ++- x-pack/plugins/rollup/server/index.ts | 2 + .../register_rollup_search_strategy.test.js | 22 ---- .../register_rollup_search_strategy.ts | 28 ----- .../rollup_search_capabilities.ts | 115 ------------------ .../rollup_search_strategy.ts | 94 -------------- x-pack/plugins/rollup/server/plugin.ts | 25 ++-- .../vis_type_timeseries_enhanced/kibana.json | 11 ++ .../server}/index.ts | 6 +- .../server/plugin.ts | 39 ++++++ .../lib/interval_helper.test.ts} | 0 .../search_strategies/lib/interval_helper.ts | 0 .../rollup_search_capabilities.test.ts} | 16 +-- .../rollup_search_capabilities.ts | 114 +++++++++++++++++ .../rollup_search_strategy.test.js | 53 +++----- .../rollup_search_strategy.ts | 94 ++++++++++++++ 21 files changed, 340 insertions(+), 353 deletions(-) rename src/plugins/vis_type_timeseries/server/lib/search_strategies/{default_search_capabilities.js => default_search_capabilities.ts} (73%) rename src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/{default_search_strategy.js => default_search_strategy.ts} (87%) rename src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/{unit_to_seconds.js => unit_to_seconds.ts} (83%) delete mode 100644 x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.test.js delete mode 100644 x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.ts delete mode 100644 x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.ts delete mode 100644 x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.ts create mode 100644 x-pack/plugins/vis_type_timeseries_enhanced/kibana.json rename x-pack/plugins/{rollup/server/lib/search_strategies => vis_type_timeseries_enhanced/server}/index.ts (50%) create mode 100644 x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts rename x-pack/plugins/{rollup/server/lib/search_strategies/lib/interval_helper.test.js => vis_type_timeseries_enhanced/server/search_strategies/lib/interval_helper.test.ts} (100%) rename x-pack/plugins/{rollup/server/lib => vis_type_timeseries_enhanced/server}/search_strategies/lib/interval_helper.ts (100%) rename x-pack/plugins/{rollup/server/lib/search_strategies/rollup_search_capabilities.test.js => vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts} (88%) create mode 100644 x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts rename x-pack/plugins/{rollup/server/lib => vis_type_timeseries_enhanced/server}/search_strategies/rollup_search_strategy.test.js (75%) create mode 100644 x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts diff --git a/src/plugins/vis_type_timeseries/server/index.ts b/src/plugins/vis_type_timeseries/server/index.ts index 333ed0ff64fdb..78b9344227ed5 100644 --- a/src/plugins/vis_type_timeseries/server/index.ts +++ b/src/plugins/vis_type_timeseries/server/index.ts @@ -43,7 +43,7 @@ export { AbstractSearchStrategy, ReqFacade, } from './lib/search_strategies/strategies/abstract_search_strategy'; -// @ts-ignore + export { DefaultSearchCapabilities } from './lib/search_strategies/default_search_capabilities'; export function plugin(initializerContext: PluginInitializerContext) { diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.js b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts similarity index 73% rename from src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.js rename to src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts index 02a710fef897f..be9b912138a47 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.js +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts @@ -20,36 +20,38 @@ import { convertIntervalToUnit, parseInterval, getSuitableUnit, + IntervalUnits, } from '../vis_data/helpers/unit_to_seconds'; import { RESTRICTIONS_KEYS } from '../../../common/ui_restrictions'; +import { ReqFacade } from './strategies/abstract_search_strategy'; -const getTimezoneFromRequest = (request) => { +const getTimezoneFromRequest = (request: ReqFacade) => { return request.payload.timerange.timezone; }; export class DefaultSearchCapabilities { - constructor(request, fieldsCapabilities = {}) { - this.request = request; - this.fieldsCapabilities = fieldsCapabilities; - } + constructor( + protected request: ReqFacade, + protected fieldsCapabilities: Record = {} + ) {} - get defaultTimeInterval() { + public get defaultTimeInterval() { return null; } - get whiteListedMetrics() { + public get whiteListedMetrics() { return this.createUiRestriction(); } - get whiteListedGroupByFields() { + public get whiteListedGroupByFields() { return this.createUiRestriction(); } - get whiteListedTimerangeModes() { + public get whiteListedTimerangeModes() { return this.createUiRestriction(); } - get uiRestrictions() { + public get uiRestrictions() { return { [RESTRICTIONS_KEYS.WHITE_LISTED_METRICS]: this.whiteListedMetrics, [RESTRICTIONS_KEYS.WHITE_LISTED_GROUP_BY_FIELDS]: this.whiteListedGroupByFields, @@ -57,26 +59,26 @@ export class DefaultSearchCapabilities { }; } - get searchTimezone() { + public get searchTimezone() { return getTimezoneFromRequest(this.request); } - createUiRestriction(restrictionsObject) { + createUiRestriction(restrictionsObject?: Record) { return { '*': !restrictionsObject, ...(restrictionsObject || {}), }; } - parseInterval(interval) { + parseInterval(interval: string) { return parseInterval(interval); } - getSuitableUnit(intervalInSeconds) { + getSuitableUnit(intervalInSeconds: string) { return getSuitableUnit(intervalInSeconds); } - convertIntervalToUnit(intervalString, unit) { + convertIntervalToUnit(intervalString: string, unit: IntervalUnits) { const parsedInterval = this.parseInterval(intervalString); if (parsedInterval.unit !== unit) { @@ -86,7 +88,7 @@ export class DefaultSearchCapabilities { return parsedInterval; } - getValidTimeInterval(intervalString) { + getValidTimeInterval(intervalString: string) { // Default search capabilities doesn't have any restrictions for the interval string return intervalString; } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts index 66ea4f017dd90..4ae8a97d8abb8 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts @@ -27,10 +27,10 @@ import { DefaultSearchCapabilities } from './default_search_capabilities'; class MockSearchStrategy extends AbstractSearchStrategy { checkForViability() { - return { + return Promise.resolve({ isViable: true, capabilities: {}, - }; + }); } } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index b1e21edf8b588..ba8aa673ebceb 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -46,7 +46,7 @@ export interface ReqFacade extends FakeRequest { getEsShardTimeout: () => Promise; } -export class AbstractSearchStrategy { +export abstract class AbstractSearchStrategy { public indexType?: string; public additionalParams: any; @@ -81,7 +81,11 @@ export class AbstractSearchStrategy { return Promise.all(requests); } - async getFieldsForWildcard(req: ReqFacade, indexPattern: string, capabilities: any) { + async getFieldsForWildcard( + req: ReqFacade, + indexPattern: string, + capabilities?: Record + ) { const { indexPatternsService } = req.pre; return await indexPatternsService!.getFieldsForWildcard({ @@ -93,7 +97,9 @@ export class AbstractSearchStrategy { checkForViability( req: ReqFacade, indexPattern: string - ): { isViable: boolean; capabilities: any } { + ): + | { isViable: boolean; capabilities: Record } + | Promise<{ isViable: boolean; capabilities: Record }> { throw new TypeError('Must override method'); } } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.js b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts similarity index 87% rename from src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.js rename to src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts index 8e57c117637bf..0e70b9978bf5c 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.js +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts @@ -17,16 +17,16 @@ * under the License. */ -import { AbstractSearchStrategy } from './abstract_search_strategy'; +import { AbstractSearchStrategy, ReqFacade } from './abstract_search_strategy'; import { DefaultSearchCapabilities } from '../default_search_capabilities'; export class DefaultSearchStrategy extends AbstractSearchStrategy { name = 'default'; - checkForViability(req) { - return { + checkForViability(req: ReqFacade) { + return Promise.resolve({ isViable: true, capabilities: new DefaultSearchCapabilities(req), - }; + }); } } diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts similarity index 83% rename from src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.js rename to src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts index be8f1741627ba..d14b38717c723 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts @@ -16,8 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -import { INTERVAL_STRING_RE } from '../../../../common/interval_regexp'; import { sortBy, isNumber } from 'lodash'; +import { INTERVAL_STRING_RE } from '../../../../common/interval_regexp'; export const ASCENDING_UNIT_ORDER = ['ms', 's', 'm', 'h', 'd', 'w', 'M', 'y']; @@ -32,9 +32,11 @@ const units = { y: 86400 * 7 * 4 * 12, // Leap year? }; -const sortedUnits = sortBy(Object.keys(units), (key) => units[key]); +export type IntervalUnits = keyof typeof units; + +const sortedUnits = sortBy(Object.keys(units), (key: IntervalUnits) => units[key]); -export const parseInterval = (intervalString) => { +export const parseInterval = (intervalString: string) => { let value; let unit; @@ -50,7 +52,7 @@ export const parseInterval = (intervalString) => { return { value, unit }; }; -export const convertIntervalToUnit = (intervalString, newUnit) => { +export const convertIntervalToUnit = (intervalString: string, newUnit: IntervalUnits) => { const parsedInterval = parseInterval(intervalString); let value; let unit; @@ -65,7 +67,7 @@ export const convertIntervalToUnit = (intervalString, newUnit) => { return { value, unit }; }; -export const getSuitableUnit = (intervalInSeconds) => +export const getSuitableUnit = (intervalInSeconds: string) => sortedUnits.find((key, index, array) => { const nextUnit = array[index + 1]; const isValidInput = isNumber(intervalInSeconds) && intervalInSeconds > 0; @@ -77,4 +79,4 @@ export const getSuitableUnit = (intervalInSeconds) => ); }); -export const getUnitValue = (unit) => units[unit]; +export const getUnitValue = (unit: IntervalUnits) => units[unit]; diff --git a/x-pack/plugins/rollup/server/index.ts b/x-pack/plugins/rollup/server/index.ts index 78859a959a1e0..1c5145451f13c 100644 --- a/x-pack/plugins/rollup/server/index.ts +++ b/x-pack/plugins/rollup/server/index.ts @@ -8,6 +8,8 @@ import { PluginInitializerContext, PluginConfigDescriptor } from 'src/core/serve import { RollupPlugin } from './plugin'; import { configSchema, RollupConfig } from './config'; +export { RollupPluginSetup } from './plugin'; + export const plugin = (pluginInitializerContext: PluginInitializerContext) => new RollupPlugin(pluginInitializerContext); diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.test.js b/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.test.js deleted file mode 100644 index 8672a8b8f6849..0000000000000 --- a/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.test.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 { registerRollupSearchStrategy } from './register_rollup_search_strategy'; - -describe('Register Rollup Search Strategy', () => { - let addSearchStrategy; - let getRollupService; - - beforeEach(() => { - addSearchStrategy = jest.fn().mockName('addSearchStrategy'); - getRollupService = jest.fn().mockName('getRollupService'); - }); - - test('should run initialization', () => { - registerRollupSearchStrategy(addSearchStrategy, getRollupService); - - expect(addSearchStrategy).toHaveBeenCalled(); - }); -}); diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.ts b/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.ts deleted file mode 100644 index 22dafbb71d802..0000000000000 --- a/x-pack/plugins/rollup/server/lib/search_strategies/register_rollup_search_strategy.ts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 { ILegacyScopedClusterClient } from 'src/core/server'; -import { - DefaultSearchCapabilities, - AbstractSearchStrategy, - ReqFacade, -} from '../../../../../../src/plugins/vis_type_timeseries/server'; -import { getRollupSearchStrategy } from './rollup_search_strategy'; -import { getRollupSearchCapabilities } from './rollup_search_capabilities'; - -export const registerRollupSearchStrategy = ( - addSearchStrategy: (searchStrategy: any) => void, - getRollupService: (reg: ReqFacade) => Promise -) => { - const RollupSearchCapabilities = getRollupSearchCapabilities(DefaultSearchCapabilities); - const RollupSearchStrategy = getRollupSearchStrategy( - AbstractSearchStrategy, - RollupSearchCapabilities, - getRollupService - ); - - addSearchStrategy(new RollupSearchStrategy()); -}; diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.ts b/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.ts deleted file mode 100644 index 354bf641114c7..0000000000000 --- a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.ts +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 { get, has } from 'lodash'; -import { KibanaRequest } from 'src/core/server'; -import { leastCommonInterval, isCalendarInterval } from './lib/interval_helper'; - -export const getRollupSearchCapabilities = (DefaultSearchCapabilities: any) => - class RollupSearchCapabilities extends DefaultSearchCapabilities { - constructor( - req: KibanaRequest, - fieldsCapabilities: { [key: string]: any }, - rollupIndex: string - ) { - super(req, fieldsCapabilities); - - this.rollupIndex = rollupIndex; - this.availableMetrics = get(fieldsCapabilities, `${rollupIndex}.aggs`, {}); - } - - public get dateHistogram() { - const [dateHistogram] = Object.values(this.availableMetrics.date_histogram); - - return dateHistogram; - } - - public get defaultTimeInterval() { - return ( - this.dateHistogram.fixed_interval || - this.dateHistogram.calendar_interval || - /* - Deprecation: [interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future. - We can remove the following line only for versions > 8.x - */ - this.dateHistogram.interval || - null - ); - } - - public get searchTimezone() { - return get(this.dateHistogram, 'time_zone', null); - } - - public get whiteListedMetrics() { - const baseRestrictions = this.createUiRestriction({ - count: this.createUiRestriction(), - }); - - const getFields = (fields: { [key: string]: any }) => - Object.keys(fields).reduce( - (acc, item) => ({ - ...acc, - [item]: true, - }), - this.createUiRestriction({}) - ); - - return Object.keys(this.availableMetrics).reduce( - (acc, item) => ({ - ...acc, - [item]: getFields(this.availableMetrics[item]), - }), - baseRestrictions - ); - } - - public get whiteListedGroupByFields() { - return this.createUiRestriction({ - everything: true, - terms: has(this.availableMetrics, 'terms'), - }); - } - - public get whiteListedTimerangeModes() { - return this.createUiRestriction({ - last_value: true, - }); - } - - getValidTimeInterval(userIntervalString: string) { - const parsedRollupJobInterval = this.parseInterval(this.defaultTimeInterval); - const inRollupJobUnit = this.convertIntervalToUnit( - userIntervalString, - parsedRollupJobInterval.unit - ); - - const getValidCalendarInterval = () => { - let unit = parsedRollupJobInterval.unit; - - if (inRollupJobUnit.value > parsedRollupJobInterval.value) { - const inSeconds = this.convertIntervalToUnit(userIntervalString, 's'); - - unit = this.getSuitableUnit(inSeconds.value); - } - - return { - value: 1, - unit, - }; - }; - - const getValidFixedInterval = () => ({ - value: leastCommonInterval(inRollupJobUnit.value, parsedRollupJobInterval.value), - unit: parsedRollupJobInterval.unit, - }); - - const { value, unit } = (isCalendarInterval(parsedRollupJobInterval) - ? getValidCalendarInterval - : getValidFixedInterval)(); - - return `${value}${unit}`; - } - }; diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.ts deleted file mode 100644 index dcf6629d35397..0000000000000 --- a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 { keyBy, isString } from 'lodash'; -import { ILegacyScopedClusterClient } from 'src/core/server'; -import { ReqFacade } from '../../../../../../src/plugins/vis_type_timeseries/server'; - -import { - mergeCapabilitiesWithFields, - getCapabilitiesForRollupIndices, -} from '../../../../../../src/plugins/data/server'; - -const getRollupIndices = (rollupData: { [key: string]: any }) => Object.keys(rollupData); - -const isIndexPatternContainsWildcard = (indexPattern: string) => indexPattern.includes('*'); -const isIndexPatternValid = (indexPattern: string) => - indexPattern && isString(indexPattern) && !isIndexPatternContainsWildcard(indexPattern); - -export const getRollupSearchStrategy = ( - AbstractSearchStrategy: any, - RollupSearchCapabilities: any, - getRollupService: (reg: ReqFacade) => Promise -) => - class RollupSearchStrategy extends AbstractSearchStrategy { - name = 'rollup'; - - constructor() { - super('rollup', { rest_total_hits_as_int: true }); - } - - async search(req: ReqFacade, bodies: any[], options = {}) { - const rollupService = await getRollupService(req); - const requests: any[] = []; - bodies.forEach((body) => { - requests.push( - rollupService.callAsCurrentUser('rollup.search', { - ...body, - rest_total_hits_as_int: true, - }) - ); - }); - return Promise.all(requests); - } - - async getRollupData(req: ReqFacade, indexPattern: string) { - const rollupService = await getRollupService(req); - return rollupService - .callAsCurrentUser('rollup.rollupIndexCapabilities', { - indexPattern, - }) - .catch(() => Promise.resolve({})); - } - - async checkForViability(req: ReqFacade, indexPattern: string) { - let isViable = false; - let capabilities = null; - - if (isIndexPatternValid(indexPattern)) { - const rollupData = await this.getRollupData(req, indexPattern); - const rollupIndices = getRollupIndices(rollupData); - - isViable = rollupIndices.length === 1; - - if (isViable) { - const [rollupIndex] = rollupIndices; - const fieldsCapabilities = getCapabilitiesForRollupIndices(rollupData); - - capabilities = new RollupSearchCapabilities(req, fieldsCapabilities, rollupIndex); - } - } - - return { - isViable, - capabilities, - }; - } - - async getFieldsForWildcard( - req: ReqFacade, - indexPattern: string, - { - fieldsCapabilities, - rollupIndex, - }: { fieldsCapabilities: { [key: string]: any }; rollupIndex: string } - ) { - const fields = await super.getFieldsForWildcard(req, indexPattern); - const fieldsFromFieldCapsApi = keyBy(fields, 'name'); - const rollupIndexCapabilities = fieldsCapabilities[rollupIndex].aggs; - - return mergeCapabilitiesWithFields(rollupIndexCapabilities, fieldsFromFieldCapsApi); - } - }; diff --git a/x-pack/plugins/rollup/server/plugin.ts b/x-pack/plugins/rollup/server/plugin.ts index 51920af7c8cbc..b8468354e0015 100644 --- a/x-pack/plugins/rollup/server/plugin.ts +++ b/x-pack/plugins/rollup/server/plugin.ts @@ -24,7 +24,6 @@ import { import { i18n } from '@kbn/i18n'; import { schema } from '@kbn/config-schema'; -import { ReqFacade } from '../../../../src/plugins/vis_type_timeseries/server'; import { PLUGIN, CONFIG_ROLLUPS } from '../common'; import { Dependencies } from './types'; import { registerApiRoutes } from './routes'; @@ -32,7 +31,6 @@ import { License } from './services'; import { registerRollupUsageCollector } from './collectors'; import { rollupDataEnricher } from './rollup_data_enricher'; import { IndexPatternsFetcher } from './shared_imports'; -import { registerRollupSearchStrategy } from './lib/search_strategies'; import { elasticsearchJsPlugin } from './client/elasticsearch_rollup'; import { isEsError } from './shared_imports'; import { formatEsError } from './lib/format_es_error'; @@ -45,10 +43,15 @@ async function getCustomEsClient(getStartServices: CoreSetup['getStartServices'] const [core] = await getStartServices(); // Extend the elasticsearchJs client with additional endpoints. const esClientConfig = { plugins: [elasticsearchJsPlugin] }; + return core.elasticsearch.legacy.createClient('rollup', esClientConfig); } -export class RollupPlugin implements Plugin { +export interface RollupPluginSetup { + getRollupEsClient: () => Promise; +} + +export class RollupPlugin implements Plugin { private readonly logger: Logger; private readonly globalConfig$: Observable; private readonly license: License; @@ -128,15 +131,6 @@ export class RollupPlugin implements Plugin { }, }); - if (visTypeTimeseries) { - const getRollupService = async (request: ReqFacade) => { - this.rollupEsClient = this.rollupEsClient ?? (await getCustomEsClient(getStartServices)); - return this.rollupEsClient.asScoped(request); - }; - const { addSearchStrategy } = visTypeTimeseries; - registerRollupSearchStrategy(addSearchStrategy, getRollupService); - } - if (usageCollection) { this.globalConfig$ .pipe(first()) @@ -152,6 +146,13 @@ export class RollupPlugin implements Plugin { if (indexManagement && indexManagement.indexDataEnricher) { indexManagement.indexDataEnricher.add(rollupDataEnricher); } + + return { + getRollupEsClient: async () => + this.rollupEsClient + ? Promise.resolve(this.rollupEsClient) + : getCustomEsClient(getStartServices), + }; } start() {} diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json b/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json new file mode 100644 index 0000000000000..d077347512da9 --- /dev/null +++ b/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json @@ -0,0 +1,11 @@ +{ + "id": "visTypeTimeseriesEnhanced", + "version": "8.0.0", + "kibanaVersion": "kibana", + "server": true, + "ui": false, + "requiredPlugins": [ + "visTypeTimeseries", + "rollup" + ] +} diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/index.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/index.ts similarity index 50% rename from x-pack/plugins/rollup/server/lib/search_strategies/index.ts rename to x-pack/plugins/vis_type_timeseries_enhanced/server/index.ts index 7db0b38ea29dd..d2665ec1e2813 100644 --- a/x-pack/plugins/rollup/server/lib/search_strategies/index.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/index.ts @@ -4,4 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -export { registerRollupSearchStrategy } from './register_rollup_search_strategy'; +import { PluginInitializerContext } from 'src/core/server'; +import { VisTypeTimeseriesEnhanced } from './plugin'; + +export const plugin = (initializerContext: PluginInitializerContext) => + new VisTypeTimeseriesEnhanced(initializerContext); diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts new file mode 100644 index 0000000000000..03f86599594fb --- /dev/null +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts @@ -0,0 +1,39 @@ +/* + * 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 { Plugin, PluginInitializerContext, Logger, CoreSetup } from 'src/core/server'; +import { ReqFacade, VisTypeTimeseriesSetup } from 'src/plugins/vis_type_timeseries/server'; +import { RollupPluginSetup } from '../../rollup/server'; +import { RollupSearchStrategy } from './search_strategies/rollup_search_strategy'; + +interface VisTypeTimeseriesEnhancedSetupDependencies { + visTypeTimeseries: VisTypeTimeseriesSetup; + rollup: RollupPluginSetup; +} + +export class VisTypeTimeseriesEnhanced + implements Plugin { + private logger: Logger; + + constructor(initializerContext: PluginInitializerContext) { + this.logger = initializerContext.logger.get('vis_type_timeseries_enhanced'); + } + + public async setup( + core: CoreSetup, + { visTypeTimeseries, rollup }: VisTypeTimeseriesEnhancedSetupDependencies + ) { + this.logger.debug('Starting plugin'); + + const getRollupService = async (request: ReqFacade) => + (await rollup.getRollupEsClient()).asScoped(request); + const rollupSearchStrategy = new RollupSearchStrategy(getRollupService); + + visTypeTimeseries.addSearchStrategy(rollupSearchStrategy); + } + + public start() {} +} diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/lib/interval_helper.test.js b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/lib/interval_helper.test.ts similarity index 100% rename from x-pack/plugins/rollup/server/lib/search_strategies/lib/interval_helper.test.js rename to x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/lib/interval_helper.test.ts diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/lib/interval_helper.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/lib/interval_helper.ts similarity index 100% rename from x-pack/plugins/rollup/server/lib/search_strategies/lib/interval_helper.ts rename to x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/lib/interval_helper.ts diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.test.js b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts similarity index 88% rename from x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.test.js rename to x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts index 977601247594f..aa82b3d1cca7c 100644 --- a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_capabilities.test.js +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts @@ -3,14 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { getRollupSearchCapabilities } from './rollup_search_capabilities'; - -class DefaultSearchCapabilities { - constructor(request, fieldsCapabilities = {}) { - this.fieldsCapabilities = fieldsCapabilities; - this.parseInterval = jest.fn((interval) => interval); - } -} +import { RollupSearchCapabilities } from './rollup_search_capabilities'; describe('Rollup Search Capabilities', () => { const testTimeZone = 'time_zone'; @@ -18,12 +11,10 @@ describe('Rollup Search Capabilities', () => { const rollupIndex = 'rollupIndex'; const request = {}; - let RollupSearchCapabilities; - let fieldsCapabilities; - let rollupSearchCaps; + let fieldsCapabilities: Record; + let rollupSearchCaps: RollupSearchCapabilities; beforeEach(() => { - RollupSearchCapabilities = getRollupSearchCapabilities(DefaultSearchCapabilities); fieldsCapabilities = { [rollupIndex]: { aggs: { @@ -41,7 +32,6 @@ describe('Rollup Search Capabilities', () => { }); test('should create instance of RollupSearchRequest', () => { - expect(rollupSearchCaps).toBeInstanceOf(DefaultSearchCapabilities); expect(rollupSearchCaps.fieldsCapabilities).toBe(fieldsCapabilities); expect(rollupSearchCaps.rollupIndex).toBe(rollupIndex); }); diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts new file mode 100644 index 0000000000000..ba0569756fdc9 --- /dev/null +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts @@ -0,0 +1,114 @@ +/* + * 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 { get, has } from 'lodash'; +import { leastCommonInterval, isCalendarInterval } from './lib/interval_helper'; + +import { + ReqFacade, + DefaultSearchCapabilities, +} from '../../../../../src/plugins/vis_type_timeseries/server'; + +export class RollupSearchCapabilities extends DefaultSearchCapabilities { + constructor(req: ReqFacade, fieldsCapabilities: Record, rollupIndex: string) { + super(req, fieldsCapabilities); + + this.rollupIndex = rollupIndex; + this.availableMetrics = get(fieldsCapabilities, `${rollupIndex}.aggs`, {}); + } + + public get dateHistogram() { + const [dateHistogram] = Object.values(this.availableMetrics.date_histogram); + + return dateHistogram; + } + + public get defaultTimeInterval() { + return ( + this.dateHistogram.fixed_interval || + this.dateHistogram.calendar_interval || + /* + Deprecation: [interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future. + We can remove the following line only for versions > 8.x + */ + this.dateHistogram.interval || + null + ); + } + + public get searchTimezone() { + return get(this.dateHistogram, 'time_zone', null); + } + + public get whiteListedMetrics() { + const baseRestrictions = this.createUiRestriction({ + count: this.createUiRestriction(), + }); + + const getFields = (fields: { [key: string]: any }) => + Object.keys(fields).reduce( + (acc, item) => ({ + ...acc, + [item]: true, + }), + this.createUiRestriction({}) + ); + + return Object.keys(this.availableMetrics).reduce( + (acc, item) => ({ + ...acc, + [item]: getFields(this.availableMetrics[item]), + }), + baseRestrictions + ); + } + + public get whiteListedGroupByFields() { + return this.createUiRestriction({ + everything: true, + terms: has(this.availableMetrics, 'terms'), + }); + } + + public get whiteListedTimerangeModes() { + return this.createUiRestriction({ + last_value: true, + }); + } + + getValidTimeInterval(userIntervalString: string) { + const parsedRollupJobInterval = this.parseInterval(this.defaultTimeInterval); + const inRollupJobUnit = this.convertIntervalToUnit( + userIntervalString, + parsedRollupJobInterval.unit + ); + + const getValidCalendarInterval = () => { + let unit = parsedRollupJobInterval.unit; + + if (inRollupJobUnit.value > parsedRollupJobInterval.value) { + const inSeconds = this.convertIntervalToUnit(userIntervalString, 's'); + + unit = this.getSuitableUnit(inSeconds.value); + } + + return { + value: 1, + unit, + }; + }; + + const getValidFixedInterval = () => ({ + value: leastCommonInterval(inRollupJobUnit.value, parsedRollupJobInterval.value), + unit: parsedRollupJobInterval.unit, + }); + + const { value, unit } = (isCalendarInterval(parsedRollupJobInterval) + ? getValidCalendarInterval + : getValidFixedInterval)(); + + return `${value}${unit}`; + } +} diff --git a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.test.js b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js similarity index 75% rename from x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.test.js rename to x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js index f3da7ed3fdd17..e5ce19bac83b5 100644 --- a/x-pack/plugins/rollup/server/lib/search_strategies/rollup_search_strategy.test.js +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js @@ -3,11 +3,9 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { getRollupSearchStrategy } from './rollup_search_strategy'; +import { RollupSearchStrategy } from './rollup_search_strategy'; describe('Rollup Search Strategy', () => { - let RollupSearchStrategy; - let RollupSearchCapabilities; let callWithRequest; let rollupResolvedData; @@ -26,6 +24,7 @@ describe('Rollup Search Strategy', () => { }, }, }; + const getRollupService = jest.fn().mockImplementation(() => { return { callAsCurrentUser: async () => { @@ -33,36 +32,24 @@ describe('Rollup Search Strategy', () => { }, }; }); + const indexPattern = 'indexPattern'; beforeEach(() => { - class AbstractSearchStrategy { - getCallWithRequestInstance = jest.fn(() => callWithRequest); - - getFieldsForWildcard() { - return [ - { - name: 'day_of_week.terms.value', - type: 'object', - esTypes: ['object'], - searchable: false, - aggregatable: false, - }, - ]; - } - } - - RollupSearchCapabilities = jest.fn(() => 'capabilities'); - - RollupSearchStrategy = getRollupSearchStrategy( - AbstractSearchStrategy, - RollupSearchCapabilities, - getRollupService - ); + RollupSearchStrategy.prototype.getCallWithRequestInstance = jest.fn(() => callWithRequest); + RollupSearchStrategy.prototype.getFieldsForWildcard = jest.fn(() => [ + { + name: 'day_of_week.terms.value', + type: 'object', + esTypes: ['object'], + searchable: false, + aggregatable: false, + }, + ]); }); test('should create instance of RollupSearchRequest', () => { - const rollupSearchStrategy = new RollupSearchStrategy(); + const rollupSearchStrategy = new RollupSearchStrategy(getRollupService); expect(rollupSearchStrategy.name).toBe('rollup'); }); @@ -72,7 +59,7 @@ describe('Rollup Search Strategy', () => { const rollupIndex = 'rollupIndex'; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(); + rollupSearchStrategy = new RollupSearchStrategy(getRollupService); rollupSearchStrategy.getRollupData = jest.fn(() => ({ [rollupIndex]: { rollup_jobs: [ @@ -109,19 +96,13 @@ describe('Rollup Search Strategy', () => { capabilities: null, }); }); - - test('should get RollupSearchCapabilities for valid rollup index ', async () => { - await rollupSearchStrategy.checkForViability(request, rollupIndex); - - expect(RollupSearchCapabilities).toHaveBeenCalled(); - }); }); describe('getRollupData', () => { let rollupSearchStrategy; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(); + rollupSearchStrategy = new RollupSearchStrategy(getRollupService); }); test('should return rollup data', async () => { @@ -149,7 +130,7 @@ describe('Rollup Search Strategy', () => { const rollupIndex = 'rollupIndex'; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(); + rollupSearchStrategy = new RollupSearchStrategy(getRollupService); fieldsCapabilities = { [rollupIndex]: { aggs: { diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts new file mode 100644 index 0000000000000..25abed776b389 --- /dev/null +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -0,0 +1,94 @@ +/* + * 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 { keyBy, isString } from 'lodash'; +import { ILegacyScopedClusterClient } from 'src/core/server'; +import { + AbstractSearchStrategy, + ReqFacade, +} from '../../../../../src/plugins/vis_type_timeseries/server'; + +import { + mergeCapabilitiesWithFields, + getCapabilitiesForRollupIndices, +} from '../../../../../src/plugins/data/server'; + +import { RollupSearchCapabilities } from './rollup_search_capabilities'; + +const getRollupIndices = (rollupData: { [key: string]: any }) => Object.keys(rollupData); +const isIndexPatternContainsWildcard = (indexPattern: string) => indexPattern.includes('*'); +const isIndexPatternValid = (indexPattern: string) => + indexPattern && isString(indexPattern) && !isIndexPatternContainsWildcard(indexPattern); + +export class RollupSearchStrategy extends AbstractSearchStrategy { + name = 'rollup'; + + constructor(private getRollupService: (reg: ReqFacade) => Promise) { + super('rollup', { rest_total_hits_as_int: true }); + } + + async search(req: ReqFacade, bodies: any[], options = {}) { + const rollupService = await this.getRollupService(req); + const requests: any[] = []; + + bodies.forEach((body) => { + requests.push( + rollupService.callAsCurrentUser('rollup.search', { + ...body, + rest_total_hits_as_int: true, + }) + ); + }); + return Promise.all(requests); + } + + async getRollupData(req: ReqFacade, indexPattern: string) { + const rollupService = await this.getRollupService(req); + return rollupService + .callAsCurrentUser('rollup.rollupIndexCapabilities', { + indexPattern, + }) + .catch(() => Promise.resolve({})); + } + + async checkForViability(req: ReqFacade, indexPattern: string) { + let isViable = false; + let capabilities = null; + + if (isIndexPatternValid(indexPattern)) { + const rollupData = await this.getRollupData(req, indexPattern); + const rollupIndices = getRollupIndices(rollupData); + + isViable = rollupIndices.length === 1; + + if (isViable) { + const [rollupIndex] = rollupIndices; + const fieldsCapabilities = getCapabilitiesForRollupIndices(rollupData); + + capabilities = new RollupSearchCapabilities(req, fieldsCapabilities, rollupIndex); + } + } + + return { + isViable, + capabilities, + }; + } + + async getFieldsForWildcard( + req: ReqFacade, + indexPattern: string, + { + fieldsCapabilities, + rollupIndex, + }: { fieldsCapabilities: { [key: string]: any }; rollupIndex: string } + ) { + const fields = await super.getFieldsForWildcard(req, indexPattern); + const fieldsFromFieldCapsApi = keyBy(fields, 'name'); + const rollupIndexCapabilities = fieldsCapabilities[rollupIndex].aggs; + + return mergeCapabilitiesWithFields(rollupIndexCapabilities, fieldsFromFieldCapsApi); + } +} From 4dcf3a43f6459ac6d4e2b496190fa695f0a983d9 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Thu, 12 Nov 2020 16:08:30 +0300 Subject: [PATCH 02/10] remove unused code --- docs/developer/plugin-list.asciidoc | 4 ++ .../default_search_capabilities.ts | 13 ++--- .../strategies/abstract_search_strategy.ts | 20 +------ .../default_search_strategy.test.js | 4 +- .../lib/vis_data/helpers/get_bucket_size.js | 10 +++- ...econds.test.js => unit_to_seconds.test.ts} | 29 ++-------- .../lib/vis_data/helpers/unit_to_seconds.ts | 57 ++++++++++--------- .../vis_type_timeseries_enhanced/README.md | 10 ++++ .../rollup_search_capabilities.test.ts | 23 ++++---- .../rollup_search_capabilities.ts | 20 ++++--- .../rollup_search_strategy.ts | 2 +- 11 files changed, 93 insertions(+), 99 deletions(-) rename src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/{unit_to_seconds.test.js => unit_to_seconds.test.ts} (86%) create mode 100644 x-pack/plugins/vis_type_timeseries_enhanced/README.md diff --git a/docs/developer/plugin-list.asciidoc b/docs/developer/plugin-list.asciidoc index e89b6d86361c7..952dfa0c7ac62 100644 --- a/docs/developer/plugin-list.asciidoc +++ b/docs/developer/plugin-list.asciidoc @@ -554,6 +554,10 @@ in their infrastructure. |NOTE: This plugin contains implementation of URL drilldown. For drilldowns infrastructure code refer to ui_actions_enhanced plugin. +|{kib-repo}blob/{branch}/x-pack/plugins/vis_type_timeseries_enhanced/README.md[visTypeTimeseriesEnhanced] +|The vis_type_timeseries_enhanced plugin is the x-pack counterpart to the OSS vis_type_timeseries plugin. + + |{kib-repo}blob/{branch}/x-pack/plugins/watcher/README.md[watcher] |This plugins adopts some conventions in addition to or in place of conventions in Kibana (at the time of the plugin's creation): diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts index be9b912138a47..9261e84653aeb 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts @@ -16,11 +16,11 @@ * specific language governing permissions and limitations * under the License. */ +import { Unit } from '@elastic/datemath'; import { convertIntervalToUnit, parseInterval, getSuitableUnit, - IntervalUnits, } from '../vis_data/helpers/unit_to_seconds'; import { RESTRICTIONS_KEYS } from '../../../common/ui_restrictions'; import { ReqFacade } from './strategies/abstract_search_strategy'; @@ -30,10 +30,7 @@ const getTimezoneFromRequest = (request: ReqFacade) => { }; export class DefaultSearchCapabilities { - constructor( - protected request: ReqFacade, - protected fieldsCapabilities: Record = {} - ) {} + constructor(protected request: ReqFacade, public fieldsCapabilities: Record = {}) {} public get defaultTimeInterval() { return null; @@ -74,14 +71,14 @@ export class DefaultSearchCapabilities { return parseInterval(interval); } - getSuitableUnit(intervalInSeconds: string) { + getSuitableUnit(intervalInSeconds: string | number) { return getSuitableUnit(intervalInSeconds); } - convertIntervalToUnit(intervalString: string, unit: IntervalUnits) { + convertIntervalToUnit(intervalString: string, unit: Unit) { const parsedInterval = this.parseInterval(intervalString); - if (parsedInterval.unit !== unit) { + if (parsedInterval?.unit !== unit) { return convertIntervalToUnit(intervalString, unit); } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index ba8aa673ebceb..af2b4fe3322d0 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -47,14 +47,6 @@ export interface ReqFacade extends FakeRequest { } export abstract class AbstractSearchStrategy { - public indexType?: string; - public additionalParams: any; - - constructor(type?: string, additionalParams: any = {}) { - this.indexType = type; - this.additionalParams = additionalParams; - } - async search(req: ReqFacade, bodies: any[], options = {}) { const requests: any[] = []; const { sessionId } = req.payload; @@ -66,9 +58,7 @@ export abstract class AbstractSearchStrategy { { params: { ...body, - ...this.additionalParams, }, - indexType: this.indexType, }, { sessionId, @@ -81,11 +71,7 @@ export abstract class AbstractSearchStrategy { return Promise.all(requests); } - async getFieldsForWildcard( - req: ReqFacade, - indexPattern: string, - capabilities?: Record - ) { + async getFieldsForWildcard(req: ReqFacade, indexPattern: string, capabilities?: unknown) { const { indexPatternsService } = req.pre; return await indexPatternsService!.getFieldsForWildcard({ @@ -97,9 +83,7 @@ export abstract class AbstractSearchStrategy { checkForViability( req: ReqFacade, indexPattern: string - ): - | { isViable: boolean; capabilities: Record } - | Promise<{ isViable: boolean; capabilities: Record }> { + ): Promise<{ isViable: boolean; capabilities: unknown }> { throw new TypeError('Must override method'); } } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js index a9994ba3e1f75..40f3841b7d166 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js @@ -34,8 +34,8 @@ describe('DefaultSearchStrategy', () => { expect(defaultSearchStrategy.getFieldsForWildcard).toBeDefined(); }); - test('should check a strategy for viability', () => { - const value = defaultSearchStrategy.checkForViability(req); + test('should check a strategy for viability', async () => { + const value = await defaultSearchStrategy.checkForViability(req); expect(value.isViable).toBe(true); expect(value.capabilities).toEqual({ diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js index 53f0b84b8ec3b..c021ba3cebc66 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js @@ -42,14 +42,18 @@ const calculateBucketData = (timeInterval, capabilities) => { } // Check decimal - if (parsedInterval.value % 1 !== 0) { + if (parsedInterval && parsedInterval.value % 1 !== 0) { if (parsedInterval.unit !== 'ms') { - const { value, unit } = convertIntervalToUnit( + const converted = convertIntervalToUnit( intervalString, ASCENDING_UNIT_ORDER[ASCENDING_UNIT_ORDER.indexOf(parsedInterval.unit) - 1] ); - intervalString = value + unit; + if (converted) { + intervalString = converted.value + converted.unit; + } + + intervalString = undefined; } else { intervalString = '1ms'; } diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.ts similarity index 86% rename from src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.js rename to src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.ts index 5b533178949f1..278e557209a21 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.test.ts @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ +import { Unit } from '@elastic/datemath'; import { getUnitValue, @@ -51,22 +52,13 @@ describe('unit_to_seconds', () => { })); test('should not parse "gm" interval (negative)', () => - expect(parseInterval('gm')).toEqual({ - value: undefined, - unit: undefined, - })); + expect(parseInterval('gm')).toBeUndefined()); test('should not parse "-1d" interval (negative)', () => - expect(parseInterval('-1d')).toEqual({ - value: undefined, - unit: undefined, - })); + expect(parseInterval('-1d')).toBeUndefined()); test('should not parse "M" interval (negative)', () => - expect(parseInterval('M')).toEqual({ - value: undefined, - unit: undefined, - })); + expect(parseInterval('M')).toBeUndefined()); }); describe('convertIntervalToUnit()', () => { @@ -95,16 +87,10 @@ describe('unit_to_seconds', () => { })); test('should not convert "30m" interval to "0" unit (positive)', () => - expect(convertIntervalToUnit('30m', 'o')).toEqual({ - value: undefined, - unit: undefined, - })); + expect(convertIntervalToUnit('30m', 'o' as Unit)).toBeUndefined()); test('should not convert "m" interval to "s" unit (positive)', () => - expect(convertIntervalToUnit('m', 's')).toEqual({ - value: undefined, - unit: undefined, - })); + expect(convertIntervalToUnit('m', 's')).toBeUndefined()); }); describe('getSuitableUnit()', () => { @@ -155,8 +141,5 @@ describe('unit_to_seconds', () => { expect(getSuitableUnit(stringValue)).toBeUndefined(); }); - - test('should return "undefined" in case of no input value(negative)', () => - expect(getSuitableUnit()).toBeUndefined()); }); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts index d14b38717c723..22dfc590245ac 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts @@ -17,11 +17,14 @@ * under the License. */ import { sortBy, isNumber } from 'lodash'; +import { Unit } from '@elastic/datemath'; + +/** @ts-ignore */ import { INTERVAL_STRING_RE } from '../../../../common/interval_regexp'; export const ASCENDING_UNIT_ORDER = ['ms', 's', 'm', 'h', 'd', 'w', 'M', 'y']; -const units = { +const units: Record = { ms: 0.001, s: 1, m: 60, @@ -32,51 +35,53 @@ const units = { y: 86400 * 7 * 4 * 12, // Leap year? }; -export type IntervalUnits = keyof typeof units; - -const sortedUnits = sortBy(Object.keys(units), (key: IntervalUnits) => units[key]); +const sortedUnits = sortBy(Object.keys(units), (key: Unit) => units[key]); -export const parseInterval = (intervalString: string) => { - let value; - let unit; +export interface ParsedInterval { + value: number; + unit: Unit; +} +export const parseInterval = (intervalString: string): ParsedInterval | undefined => { if (intervalString) { const matches = intervalString.match(INTERVAL_STRING_RE); if (matches) { - value = Number(matches[1]); - unit = matches[2]; + return { + value: Number(matches[1]), + unit: matches[2] as Unit, + }; } } - - return { value, unit }; }; -export const convertIntervalToUnit = (intervalString: string, newUnit: IntervalUnits) => { +export const convertIntervalToUnit = ( + intervalString: string, + newUnit: Unit +): ParsedInterval | undefined => { const parsedInterval = parseInterval(intervalString); - let value; - let unit; - if (parsedInterval.value && units[newUnit]) { - value = Number( - ((parsedInterval.value * units[parsedInterval.unit]) / units[newUnit]).toFixed(2) - ); - unit = newUnit; + if (parsedInterval && units[newUnit]) { + return { + value: Number( + ((parsedInterval.value * units[parsedInterval.unit!]) / units[newUnit]).toFixed(2) + ), + unit: newUnit, + }; } - - return { value, unit }; }; -export const getSuitableUnit = (intervalInSeconds: string) => +export const getSuitableUnit = (intervalInSeconds: string | number) => sortedUnits.find((key, index, array) => { - const nextUnit = array[index + 1]; + const nextUnit = array[index + 1] as Unit; const isValidInput = isNumber(intervalInSeconds) && intervalInSeconds > 0; const isLastItem = index + 1 === array.length; return ( isValidInput && - ((intervalInSeconds >= units[key] && intervalInSeconds < units[nextUnit]) || isLastItem) + ((intervalInSeconds >= units[key as Unit] && intervalInSeconds < units[nextUnit]) || + isLastItem) ); - }); + }) as Unit; -export const getUnitValue = (unit: IntervalUnits) => units[unit]; +export const getUnitValue = (unit: Unit) => units[unit]; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/README.md b/x-pack/plugins/vis_type_timeseries_enhanced/README.md new file mode 100644 index 0000000000000..e77f0da4da5ca --- /dev/null +++ b/x-pack/plugins/vis_type_timeseries_enhanced/README.md @@ -0,0 +1,10 @@ +# vis_type_timeseries_enhanced + +The `vis_type_timeseries_enhanced` plugin is the x-pack counterpart to the OSS `vis_type_timeseries` plugin. + +It exists to provide Elastic-licensed services, or parts of services, which +enhance existing OSS functionality from `vis_type_timeseries_enhanced`. + +Currently the `vis_type_timeseries_enhanced` plugin doesn't return any APIs which you can +consume directly. + diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts index aa82b3d1cca7c..bd836f28b6638 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts @@ -3,13 +3,16 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { Unit } from '@elastic/datemath'; import { RollupSearchCapabilities } from './rollup_search_capabilities'; +import { ReqFacade } from '../../../../../src/plugins/vis_type_timeseries/server'; + describe('Rollup Search Capabilities', () => { const testTimeZone = 'time_zone'; const testInterval = '10s'; const rollupIndex = 'rollupIndex'; - const request = {}; + const request = ({} as unknown) as ReqFacade; let fieldsCapabilities: Record; let rollupSearchCaps: RollupSearchCapabilities; @@ -45,9 +48,9 @@ describe('Rollup Search Capabilities', () => { }); describe('getValidTimeInterval', () => { - let rollupJobInterval; - let userInterval; - let getSuitableUnit; + let rollupJobInterval: { value: number; unit: Unit }; + let userInterval: { value: number; unit: Unit }; + let getSuitableUnit: Unit; beforeEach(() => { rollupSearchCaps.parseInterval = jest @@ -71,7 +74,7 @@ describe('Rollup Search Capabilities', () => { getSuitableUnit = 'd'; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('1d'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('1d'); }); test('should return 1w as common interval for 7d(user interval) and 1d(rollup interval) - calendar intervals', () => { @@ -86,7 +89,7 @@ describe('Rollup Search Capabilities', () => { getSuitableUnit = 'w'; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('1w'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('1w'); }); test('should return 1w as common interval for 1d(user interval) and 1w(rollup interval) - calendar intervals', () => { @@ -101,7 +104,7 @@ describe('Rollup Search Capabilities', () => { getSuitableUnit = 'w'; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('1w'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('1w'); }); test('should return 2y as common interval for 0.1y(user interval) and 2y(rollup interval) - fixed intervals', () => { @@ -114,7 +117,7 @@ describe('Rollup Search Capabilities', () => { unit: 'y', }; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('2y'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('2y'); }); test('should return 3h as common interval for 2h(user interval) and 3h(rollup interval) - fixed intervals', () => { @@ -127,7 +130,7 @@ describe('Rollup Search Capabilities', () => { unit: 'h', }; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('3h'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('3h'); }); test('should return 6m as common interval for 4m(user interval) and 3m(rollup interval) - fixed intervals', () => { @@ -140,7 +143,7 @@ describe('Rollup Search Capabilities', () => { unit: 'm', }; - expect(rollupSearchCaps.getValidTimeInterval()).toBe('6m'); + expect(rollupSearchCaps.getValidTimeInterval('')).toBe('6m'); }); }); }); diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts index ba0569756fdc9..48df91bec9d5f 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts @@ -12,6 +12,9 @@ import { } from '../../../../../src/plugins/vis_type_timeseries/server'; export class RollupSearchCapabilities extends DefaultSearchCapabilities { + rollupIndex: string; + availableMetrics: Record; + constructor(req: ReqFacade, fieldsCapabilities: Record, rollupIndex: string) { super(req, fieldsCapabilities); @@ -82,16 +85,17 @@ export class RollupSearchCapabilities extends DefaultSearchCapabilities { const parsedRollupJobInterval = this.parseInterval(this.defaultTimeInterval); const inRollupJobUnit = this.convertIntervalToUnit( userIntervalString, - parsedRollupJobInterval.unit + parsedRollupJobInterval!.unit ); const getValidCalendarInterval = () => { - let unit = parsedRollupJobInterval.unit; + let unit = parsedRollupJobInterval!.unit; - if (inRollupJobUnit.value > parsedRollupJobInterval.value) { + if (inRollupJobUnit!.value > parsedRollupJobInterval!.value) { const inSeconds = this.convertIntervalToUnit(userIntervalString, 's'); - - unit = this.getSuitableUnit(inSeconds.value); + if (inSeconds?.value) { + unit = this.getSuitableUnit(inSeconds.value); + } } return { @@ -101,11 +105,11 @@ export class RollupSearchCapabilities extends DefaultSearchCapabilities { }; const getValidFixedInterval = () => ({ - value: leastCommonInterval(inRollupJobUnit.value, parsedRollupJobInterval.value), - unit: parsedRollupJobInterval.unit, + value: leastCommonInterval(inRollupJobUnit?.value, parsedRollupJobInterval?.value), + unit: parsedRollupJobInterval!.unit, }); - const { value, unit } = (isCalendarInterval(parsedRollupJobInterval) + const { value, unit } = (isCalendarInterval(parsedRollupJobInterval!) ? getValidCalendarInterval : getValidFixedInterval)(); diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts index 25abed776b389..369f994af578b 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -26,7 +26,7 @@ export class RollupSearchStrategy extends AbstractSearchStrategy { name = 'rollup'; constructor(private getRollupService: (reg: ReqFacade) => Promise) { - super('rollup', { rest_total_hits_as_int: true }); + super(); } async search(req: ReqFacade, bodies: any[], options = {}) { From 5cd4024a054b14d92cbeaeb066722074f9f602cd Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Fri, 13 Nov 2020 13:18:55 +0300 Subject: [PATCH 03/10] rollup_search_strategy.test.js -> rollup_search_strategy.test.ts --- .../search_strategies_registry.test.ts | 4 +- ...test.js => rollup_search_strategy.test.ts} | 103 ++++++++++-------- 2 files changed, 59 insertions(+), 48 deletions(-) rename x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/{rollup_search_strategy.test.js => rollup_search_strategy.test.ts} (65%) diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts index 4ae8a97d8abb8..4c3dcbd17bbd9 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategies_registry.test.ts @@ -65,7 +65,7 @@ describe('SearchStrategyRegister', () => { }); test('should add a strategy if it is an instance of AbstractSearchStrategy', () => { - const anotherSearchStrategy = new MockSearchStrategy('es'); + const anotherSearchStrategy = new MockSearchStrategy(); const addedStrategies = registry.addStrategy(anotherSearchStrategy); expect(addedStrategies.length).toEqual(2); @@ -75,7 +75,7 @@ describe('SearchStrategyRegister', () => { test('should return a MockSearchStrategy instance', async () => { const req = {}; const indexPattern = '*'; - const anotherSearchStrategy = new MockSearchStrategy('es'); + const anotherSearchStrategy = new MockSearchStrategy(); registry.addStrategy(anotherSearchStrategy); const { searchStrategy, capabilities } = (await registry.getViableStrategy(req, indexPattern))!; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts similarity index 65% rename from x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js rename to x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts index e5ce19bac83b5..be52b620a84a9 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.js +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts @@ -4,12 +4,34 @@ * you may not use this file except in compliance with the Elastic License. */ import { RollupSearchStrategy } from './rollup_search_strategy'; +import type { ReqFacade } from '../../../../../src/plugins/vis_type_timeseries/server'; + +jest.mock('../../../../../src/plugins/vis_type_timeseries/server', () => { + const actual = jest.requireActual('../../../../../src/plugins/vis_type_timeseries/server'); + class AbstractSearchStrategyMock { + getFieldsForWildcard() { + return [ + { + name: 'day_of_week.terms.value', + type: 'object', + esTypes: ['object'], + searchable: false, + aggregatable: false, + }, + ]; + } + } + + return { + ...actual, + AbstractSearchStrategy: AbstractSearchStrategyMock, + }; +}); describe('Rollup Search Strategy', () => { - let callWithRequest; - let rollupResolvedData; + let rollupResolvedData: Promise; - const request = { + const request = ({ requestContext: { core: { elasticsearch: { @@ -23,7 +45,7 @@ describe('Rollup Search Strategy', () => { }, }, }, - }; + } as unknown) as ReqFacade; const getRollupService = jest.fn().mockImplementation(() => { return { @@ -35,19 +57,6 @@ describe('Rollup Search Strategy', () => { const indexPattern = 'indexPattern'; - beforeEach(() => { - RollupSearchStrategy.prototype.getCallWithRequestInstance = jest.fn(() => callWithRequest); - RollupSearchStrategy.prototype.getFieldsForWildcard = jest.fn(() => [ - { - name: 'day_of_week.terms.value', - type: 'object', - esTypes: ['object'], - searchable: false, - aggregatable: false, - }, - ]); - }); - test('should create instance of RollupSearchRequest', () => { const rollupSearchStrategy = new RollupSearchStrategy(getRollupService); @@ -55,37 +64,39 @@ describe('Rollup Search Strategy', () => { }); describe('checkForViability', () => { - let rollupSearchStrategy; + let rollupSearchStrategy: RollupSearchStrategy; const rollupIndex = 'rollupIndex'; beforeEach(() => { rollupSearchStrategy = new RollupSearchStrategy(getRollupService); - rollupSearchStrategy.getRollupData = jest.fn(() => ({ - [rollupIndex]: { - rollup_jobs: [ - { - job_id: 'test', - rollup_index: rollupIndex, - index_pattern: 'kibana*', - fields: { - order_date: [ - { - agg: 'date_histogram', - delay: '1m', - interval: '1m', - time_zone: 'UTC', - }, - ], - day_of_week: [ - { - agg: 'terms', - }, - ], + rollupSearchStrategy.getRollupData = jest.fn(() => + Promise.resolve({ + [rollupIndex]: { + rollup_jobs: [ + { + job_id: 'test', + rollup_index: rollupIndex, + index_pattern: 'kibana*', + fields: { + order_date: [ + { + agg: 'date_histogram', + delay: '1m', + interval: '1m', + time_zone: 'UTC', + }, + ], + day_of_week: [ + { + agg: 'terms', + }, + ], + }, }, - }, - ], - }, - })); + ], + }, + }) + ); }); test('isViable should be false for invalid index', async () => { @@ -99,7 +110,7 @@ describe('Rollup Search Strategy', () => { }); describe('getRollupData', () => { - let rollupSearchStrategy; + let rollupSearchStrategy: RollupSearchStrategy; beforeEach(() => { rollupSearchStrategy = new RollupSearchStrategy(getRollupService); @@ -124,8 +135,8 @@ describe('Rollup Search Strategy', () => { }); describe('getFieldsForWildcard', () => { - let rollupSearchStrategy; - let fieldsCapabilities; + let rollupSearchStrategy: RollupSearchStrategy; + let fieldsCapabilities: Record; const rollupIndex = 'rollupIndex'; From dbe960982072f3ccdc6c498041f290e60a6668e2 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Fri, 13 Nov 2020 15:09:07 +0300 Subject: [PATCH 04/10] default_search_capabilities.test.js -> default_search_capabilities.test.ts --- ...es.test.js => default_search_capabilities.test.ts} | 11 ++++++----- .../search_strategies/default_search_capabilities.ts | 2 +- ...rategy.test.js => default_search_strategy.test.ts} | 7 ++++--- .../search_strategies/rollup_search_strategy.test.ts | 5 ++++- 4 files changed, 15 insertions(+), 10 deletions(-) rename src/plugins/vis_type_timeseries/server/lib/search_strategies/{default_search_capabilities.test.js => default_search_capabilities.test.ts} (92%) rename src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/{default_search_strategy.test.js => default_search_strategy.test.ts} (91%) diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.js b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts similarity index 92% rename from src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.js rename to src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts index b9b7759711567..9dddd04e2b423 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.js +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts @@ -17,13 +17,14 @@ * under the License. */ import { DefaultSearchCapabilities } from './default_search_capabilities'; +import { ReqFacade } from './strategies/abstract_search_strategy'; describe('DefaultSearchCapabilities', () => { - let defaultSearchCapabilities; - let req; + let defaultSearchCapabilities: DefaultSearchCapabilities; + let req: ReqFacade; beforeEach(() => { - req = {}; + req = {} as ReqFacade; defaultSearchCapabilities = new DefaultSearchCapabilities(req); }); @@ -45,13 +46,13 @@ describe('DefaultSearchCapabilities', () => { }); test('should return Search Timezone', () => { - defaultSearchCapabilities.request = { + defaultSearchCapabilities.request = ({ payload: { timerange: { timezone: 'UTC', }, }, - }; + } as unknown) as ReqFacade; expect(defaultSearchCapabilities.searchTimezone).toEqual('UTC'); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts index 9261e84653aeb..ccb048cfbc6e7 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts @@ -30,7 +30,7 @@ const getTimezoneFromRequest = (request: ReqFacade) => { }; export class DefaultSearchCapabilities { - constructor(protected request: ReqFacade, public fieldsCapabilities: Record = {}) {} + constructor(public request: ReqFacade, public fieldsCapabilities: Record = {}) {} public get defaultTimeInterval() { return null; diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts similarity index 91% rename from src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js rename to src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts index 40f3841b7d166..76efc9bf708f5 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.js +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts @@ -17,13 +17,14 @@ * under the License. */ import { DefaultSearchStrategy } from './default_search_strategy'; +import { ReqFacade } from './abstract_search_strategy'; describe('DefaultSearchStrategy', () => { - let defaultSearchStrategy; - let req; + let defaultSearchStrategy: DefaultSearchStrategy; + let req: ReqFacade; beforeEach(() => { - req = {}; + req = {} as ReqFacade; defaultSearchStrategy = new DefaultSearchStrategy(); }); diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts index be52b620a84a9..355a2f72e28be 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts @@ -100,7 +100,10 @@ describe('Rollup Search Strategy', () => { }); test('isViable should be false for invalid index', async () => { - const result = await rollupSearchStrategy.checkForViability(request, null); + const result = await rollupSearchStrategy.checkForViability( + request, + (null as unknown) as string + ); expect(result).toEqual({ isViable: false, From 488fe5bfa000581812501595cbdac2d565584fd7 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Fri, 13 Nov 2020 18:08:24 +0300 Subject: [PATCH 05/10] remove getRollupService --- .../strategies/abstract_search_strategy.ts | 4 +-- x-pack/plugins/rollup/server/index.ts | 2 -- x-pack/plugins/rollup/server/plugin.ts | 13 +-------- .../vis_type_timeseries_enhanced/kibana.json | 3 +- .../server/plugin.ts | 11 ++------ .../rollup_search_strategy.test.ts | 19 ++++--------- .../rollup_search_strategy.ts | 28 ++++--------------- 7 files changed, 18 insertions(+), 62 deletions(-) diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index af2b4fe3322d0..6c2130c3cf213 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -47,7 +47,7 @@ export interface ReqFacade extends FakeRequest { } export abstract class AbstractSearchStrategy { - async search(req: ReqFacade, bodies: any[], options = {}) { + async search(req: ReqFacade, bodies: any[], indexType?: string) { const requests: any[] = []; const { sessionId } = req.payload; @@ -56,13 +56,13 @@ export abstract class AbstractSearchStrategy { req.requestContext .search!.search( { + indexType, params: { ...body, }, }, { sessionId, - ...options, } ) .toPromise() diff --git a/x-pack/plugins/rollup/server/index.ts b/x-pack/plugins/rollup/server/index.ts index 1c5145451f13c..78859a959a1e0 100644 --- a/x-pack/plugins/rollup/server/index.ts +++ b/x-pack/plugins/rollup/server/index.ts @@ -8,8 +8,6 @@ import { PluginInitializerContext, PluginConfigDescriptor } from 'src/core/serve import { RollupPlugin } from './plugin'; import { configSchema, RollupConfig } from './config'; -export { RollupPluginSetup } from './plugin'; - export const plugin = (pluginInitializerContext: PluginInitializerContext) => new RollupPlugin(pluginInitializerContext); diff --git a/x-pack/plugins/rollup/server/plugin.ts b/x-pack/plugins/rollup/server/plugin.ts index b8468354e0015..3c670f56c7d8f 100644 --- a/x-pack/plugins/rollup/server/plugin.ts +++ b/x-pack/plugins/rollup/server/plugin.ts @@ -47,11 +47,7 @@ async function getCustomEsClient(getStartServices: CoreSetup['getStartServices'] return core.elasticsearch.legacy.createClient('rollup', esClientConfig); } -export interface RollupPluginSetup { - getRollupEsClient: () => Promise; -} - -export class RollupPlugin implements Plugin { +export class RollupPlugin implements Plugin { private readonly logger: Logger; private readonly globalConfig$: Observable; private readonly license: License; @@ -146,13 +142,6 @@ export class RollupPlugin implements Plugin { if (indexManagement && indexManagement.indexDataEnricher) { indexManagement.indexDataEnricher.add(rollupDataEnricher); } - - return { - getRollupEsClient: async () => - this.rollupEsClient - ? Promise.resolve(this.rollupEsClient) - : getCustomEsClient(getStartServices), - }; } start() {} diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json b/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json index d077347512da9..4b296856c3f97 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json +++ b/x-pack/plugins/vis_type_timeseries_enhanced/kibana.json @@ -5,7 +5,6 @@ "server": true, "ui": false, "requiredPlugins": [ - "visTypeTimeseries", - "rollup" + "visTypeTimeseries" ] } diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts index 03f86599594fb..4e7935363f49d 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts @@ -5,13 +5,12 @@ */ import { Plugin, PluginInitializerContext, Logger, CoreSetup } from 'src/core/server'; -import { ReqFacade, VisTypeTimeseriesSetup } from 'src/plugins/vis_type_timeseries/server'; +import { VisTypeTimeseriesSetup } from 'src/plugins/vis_type_timeseries/server'; import { RollupPluginSetup } from '../../rollup/server'; import { RollupSearchStrategy } from './search_strategies/rollup_search_strategy'; interface VisTypeTimeseriesEnhancedSetupDependencies { visTypeTimeseries: VisTypeTimeseriesSetup; - rollup: RollupPluginSetup; } export class VisTypeTimeseriesEnhanced @@ -24,15 +23,11 @@ export class VisTypeTimeseriesEnhanced public async setup( core: CoreSetup, - { visTypeTimeseries, rollup }: VisTypeTimeseriesEnhancedSetupDependencies + { visTypeTimeseries }: VisTypeTimeseriesEnhancedSetupDependencies ) { this.logger.debug('Starting plugin'); - const getRollupService = async (request: ReqFacade) => - (await rollup.getRollupEsClient()).asScoped(request); - const rollupSearchStrategy = new RollupSearchStrategy(getRollupService); - - visTypeTimeseries.addSearchStrategy(rollupSearchStrategy); + visTypeTimeseries.addSearchStrategy(new RollupSearchStrategy()); } public start() {} diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts index 355a2f72e28be..bbe90a460311f 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts @@ -47,18 +47,10 @@ describe('Rollup Search Strategy', () => { }, } as unknown) as ReqFacade; - const getRollupService = jest.fn().mockImplementation(() => { - return { - callAsCurrentUser: async () => { - return rollupResolvedData; - }, - }; - }); - const indexPattern = 'indexPattern'; test('should create instance of RollupSearchRequest', () => { - const rollupSearchStrategy = new RollupSearchStrategy(getRollupService); + const rollupSearchStrategy = new RollupSearchStrategy(); expect(rollupSearchStrategy.name).toBe('rollup'); }); @@ -68,7 +60,7 @@ describe('Rollup Search Strategy', () => { const rollupIndex = 'rollupIndex'; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(getRollupService); + rollupSearchStrategy = new RollupSearchStrategy(); rollupSearchStrategy.getRollupData = jest.fn(() => Promise.resolve({ [rollupIndex]: { @@ -116,15 +108,14 @@ describe('Rollup Search Strategy', () => { let rollupSearchStrategy: RollupSearchStrategy; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(getRollupService); + rollupSearchStrategy = new RollupSearchStrategy(); }); test('should return rollup data', async () => { - rollupResolvedData = Promise.resolve('data'); + rollupResolvedData = Promise.resolve({ body: 'data' }); const rollupData = await rollupSearchStrategy.getRollupData(request, indexPattern); - expect(getRollupService).toHaveBeenCalled(); expect(rollupData).toBe('data'); }); @@ -144,7 +135,7 @@ describe('Rollup Search Strategy', () => { const rollupIndex = 'rollupIndex'; beforeEach(() => { - rollupSearchStrategy = new RollupSearchStrategy(getRollupService); + rollupSearchStrategy = new RollupSearchStrategy(); fieldsCapabilities = { [rollupIndex]: { aggs: { diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts index 369f994af578b..f31e26c7c725d 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ import { keyBy, isString } from 'lodash'; -import { ILegacyScopedClusterClient } from 'src/core/server'; import { AbstractSearchStrategy, ReqFacade, @@ -25,31 +24,16 @@ const isIndexPatternValid = (indexPattern: string) => export class RollupSearchStrategy extends AbstractSearchStrategy { name = 'rollup'; - constructor(private getRollupService: (reg: ReqFacade) => Promise) { - super(); - } - - async search(req: ReqFacade, bodies: any[], options = {}) { - const rollupService = await this.getRollupService(req); - const requests: any[] = []; - - bodies.forEach((body) => { - requests.push( - rollupService.callAsCurrentUser('rollup.search', { - ...body, - rest_total_hits_as_int: true, - }) - ); - }); - return Promise.all(requests); + async search(req: ReqFacade, bodies: any[]) { + return super.search(req, bodies, 'rollup'); } async getRollupData(req: ReqFacade, indexPattern: string) { - const rollupService = await this.getRollupService(req); - return rollupService - .callAsCurrentUser('rollup.rollupIndexCapabilities', { - indexPattern, + return req.requestContext.core.elasticsearch.client.asCurrentUser.rollup + .getRollupIndexCaps({ + index: indexPattern, }) + .then((data) => data.body) .catch(() => Promise.resolve({})); } From 20d51cb802ef9bc81a650e4e6a44cfe446f70e94 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Sat, 14 Nov 2020 19:49:34 +0300 Subject: [PATCH 06/10] fix CI --- src/plugins/vis_type_timeseries/server/index.ts | 2 ++ x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts | 1 - .../server/search_strategies/rollup_search_strategy.ts | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugins/vis_type_timeseries/server/index.ts b/src/plugins/vis_type_timeseries/server/index.ts index 78b9344227ed5..1037dc81b2b17 100644 --- a/src/plugins/vis_type_timeseries/server/index.ts +++ b/src/plugins/vis_type_timeseries/server/index.ts @@ -44,6 +44,8 @@ export { ReqFacade, } from './lib/search_strategies/strategies/abstract_search_strategy'; +export { VisPayload } from '../common/types'; + export { DefaultSearchCapabilities } from './lib/search_strategies/default_search_capabilities'; export function plugin(initializerContext: PluginInitializerContext) { diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts index 4e7935363f49d..0598a691ab7c5 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/plugin.ts @@ -6,7 +6,6 @@ import { Plugin, PluginInitializerContext, Logger, CoreSetup } from 'src/core/server'; import { VisTypeTimeseriesSetup } from 'src/plugins/vis_type_timeseries/server'; -import { RollupPluginSetup } from '../../rollup/server'; import { RollupSearchStrategy } from './search_strategies/rollup_search_strategy'; interface VisTypeTimeseriesEnhancedSetupDependencies { diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts index f31e26c7c725d..6e48e69d9537f 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -7,6 +7,7 @@ import { keyBy, isString } from 'lodash'; import { AbstractSearchStrategy, ReqFacade, + VisPayload, } from '../../../../../src/plugins/vis_type_timeseries/server'; import { @@ -24,7 +25,7 @@ const isIndexPatternValid = (indexPattern: string) => export class RollupSearchStrategy extends AbstractSearchStrategy { name = 'rollup'; - async search(req: ReqFacade, bodies: any[]) { + async search(req: ReqFacade, bodies: any[]) { return super.search(req, bodies, 'rollup'); } From af81f5c4165248a5f0bf008a2b7c6aacd1aedfbb Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Sun, 15 Nov 2020 21:27:46 +0300 Subject: [PATCH 07/10] fix some types --- src/plugins/vis_type_timeseries/server/lib/get_fields.ts | 3 ++- src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts | 3 ++- .../lib/search_strategies/default_search_capabilities.ts | 2 +- .../search_strategies/strategies/abstract_search_strategy.ts | 4 ++-- .../server/search_strategies/rollup_search_strategy.ts | 3 +-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts index dc49e280a2bb7..8b8eca77a2e41 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts @@ -27,6 +27,7 @@ import { IndexPatternsFetcher, } from '../../../data/server'; import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy'; +import { VisPayload } from '../../common/types'; export async function getFields( requestContext: RequestHandlerContext, @@ -42,7 +43,7 @@ export async function getFields( requestContext, ...request, framework, - payload: {}, + payload: {} as VisPayload, pre: { indexPatternsService: new IndexPatternsFetcher( requestContext.core.elasticsearch.client.asCurrentUser diff --git a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts index 5eef2b53e2431..445036cc3f7b2 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts @@ -23,6 +23,7 @@ import { first, map } from 'rxjs/operators'; import { getPanelData } from './vis_data/get_panel_data'; import { Framework } from '../plugin'; import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy'; +import { VisPayload } from '../../common/types'; interface GetVisDataResponse { [key: string]: GetVisDataPanel; @@ -69,7 +70,7 @@ export function getVisData( ...request, framework, pre: {}, - payload: request.body, + payload: request.body as VisPayload, getUiSettingsService: () => requestContext.core.uiSettings.client, getSavedObjectsClient: () => requestContext.core.savedObjects.client, getEsShardTimeout: async () => { diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts index ccb048cfbc6e7..cb64673143fcb 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts @@ -25,7 +25,7 @@ import { import { RESTRICTIONS_KEYS } from '../../../common/ui_restrictions'; import { ReqFacade } from './strategies/abstract_search_strategy'; -const getTimezoneFromRequest = (request: ReqFacade) => { +const getTimezoneFromRequest = (request: ReqFacade) => { return request.payload.timerange.timezone; }; diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index 6c2130c3cf213..2372f9ebd7885 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -34,7 +34,7 @@ import { VisPayload } from '../../../../common/types'; * * This will be replaced by standard KibanaRequest and RequestContext objects in a later version. */ -export interface ReqFacade extends FakeRequest { +export interface ReqFacade extends FakeRequest { requestContext: RequestHandlerContext; framework: Framework; payload: T; @@ -47,7 +47,7 @@ export interface ReqFacade extends FakeRequest { } export abstract class AbstractSearchStrategy { - async search(req: ReqFacade, bodies: any[], indexType?: string) { + async search(req: ReqFacade, bodies: any[], indexType?: string) { const requests: any[] = []; const { sessionId } = req.payload; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts index 6e48e69d9537f..f31e26c7c725d 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -7,7 +7,6 @@ import { keyBy, isString } from 'lodash'; import { AbstractSearchStrategy, ReqFacade, - VisPayload, } from '../../../../../src/plugins/vis_type_timeseries/server'; import { @@ -25,7 +24,7 @@ const isIndexPatternValid = (indexPattern: string) => export class RollupSearchStrategy extends AbstractSearchStrategy { name = 'rollup'; - async search(req: ReqFacade, bodies: any[]) { + async search(req: ReqFacade, bodies: any[]) { return super.search(req, bodies, 'rollup'); } From d4e943be95707008e0fd9e0ed98ea58a523321e9 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 16 Nov 2020 14:07:00 +0300 Subject: [PATCH 08/10] update types --- .../server/lib/get_fields.ts | 5 ++-- .../server/lib/get_vis_data.ts | 5 ++-- .../default_search_capabilities.test.ts | 7 +++--- .../default_search_capabilities.ts | 8 +++++-- .../strategies/abstract_search_strategy.ts | 24 +++++++++++-------- .../default_search_strategy.test.ts | 5 ++-- .../strategies/default_search_strategy.ts | 3 ++- .../rollup_search_capabilities.test.ts | 4 ++-- .../rollup_search_capabilities.ts | 7 +++++- .../rollup_search_strategy.test.ts | 4 ++-- .../rollup_search_strategy.ts | 9 +++---- 11 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts index 8b8eca77a2e41..8f87318222f2b 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts @@ -27,7 +27,6 @@ import { IndexPatternsFetcher, } from '../../../data/server'; import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy'; -import { VisPayload } from '../../common/types'; export async function getFields( requestContext: RequestHandlerContext, @@ -39,11 +38,11 @@ export async function getFields( // removes the need to refactor many layers of dependencies on "req", and instead just augments the top // level object passed from here. The layers should be refactored fully at some point, but for now // this works and we are still using the New Platform services for these vis data portions. - const reqFacade: ReqFacade = { + const reqFacade: ReqFacade<{}> = { requestContext, ...request, framework, - payload: {} as VisPayload, + payload: {}, pre: { indexPatternsService: new IndexPatternsFetcher( requestContext.core.elasticsearch.client.asCurrentUser diff --git a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts index 445036cc3f7b2..fcb66d2e12fd1 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts @@ -23,7 +23,6 @@ import { first, map } from 'rxjs/operators'; import { getPanelData } from './vis_data/get_panel_data'; import { Framework } from '../plugin'; import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy'; -import { VisPayload } from '../../common/types'; interface GetVisDataResponse { [key: string]: GetVisDataPanel; @@ -65,12 +64,12 @@ export function getVisData( // removes the need to refactor many layers of dependencies on "req", and instead just augments the top // level object passed from here. The layers should be refactored fully at some point, but for now // this works and we are still using the New Platform services for these vis data portions. - const reqFacade: ReqFacade = { + const reqFacade: ReqFacade = { requestContext, ...request, framework, pre: {}, - payload: request.body as VisPayload, + payload: request.body, getUiSettingsService: () => requestContext.core.uiSettings.client, getSavedObjectsClient: () => requestContext.core.savedObjects.client, getEsShardTimeout: async () => { diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts index 9dddd04e2b423..a570e02ada8d1 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.test.ts @@ -18,13 +18,14 @@ */ import { DefaultSearchCapabilities } from './default_search_capabilities'; import { ReqFacade } from './strategies/abstract_search_strategy'; +import { VisPayload } from '../../../common/types'; describe('DefaultSearchCapabilities', () => { let defaultSearchCapabilities: DefaultSearchCapabilities; - let req: ReqFacade; + let req: ReqFacade; beforeEach(() => { - req = {} as ReqFacade; + req = {} as ReqFacade; defaultSearchCapabilities = new DefaultSearchCapabilities(req); }); @@ -52,7 +53,7 @@ describe('DefaultSearchCapabilities', () => { timezone: 'UTC', }, }, - } as unknown) as ReqFacade; + } as unknown) as ReqFacade; expect(defaultSearchCapabilities.searchTimezone).toEqual('UTC'); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts index cb64673143fcb..73b701379aee0 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/default_search_capabilities.ts @@ -24,13 +24,17 @@ import { } from '../vis_data/helpers/unit_to_seconds'; import { RESTRICTIONS_KEYS } from '../../../common/ui_restrictions'; import { ReqFacade } from './strategies/abstract_search_strategy'; +import { VisPayload } from '../../../common/types'; -const getTimezoneFromRequest = (request: ReqFacade) => { +const getTimezoneFromRequest = (request: ReqFacade) => { return request.payload.timerange.timezone; }; export class DefaultSearchCapabilities { - constructor(public request: ReqFacade, public fieldsCapabilities: Record = {}) {} + constructor( + public request: ReqFacade, + public fieldsCapabilities: Record = {} + ) {} public get defaultTimeInterval() { return null; diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index 2372f9ebd7885..71461d319f2b6 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -34,7 +34,7 @@ import { VisPayload } from '../../../../common/types'; * * This will be replaced by standard KibanaRequest and RequestContext objects in a later version. */ -export interface ReqFacade extends FakeRequest { +export interface ReqFacade extends FakeRequest { requestContext: RequestHandlerContext; framework: Framework; payload: T; @@ -47,7 +47,7 @@ export interface ReqFacade extends FakeRequest { } export abstract class AbstractSearchStrategy { - async search(req: ReqFacade, bodies: any[], indexType?: string) { + async search(req: ReqFacade, bodies: any[], indexType?: string) { const requests: any[] = []; const { sessionId } = req.payload; @@ -71,7 +71,18 @@ export abstract class AbstractSearchStrategy { return Promise.all(requests); } - async getFieldsForWildcard(req: ReqFacade, indexPattern: string, capabilities?: unknown) { + checkForViability( + req: ReqFacade, + indexPattern: string + ): Promise<{ isViable: boolean; capabilities: unknown }> { + throw new TypeError('Must override method'); + } + + async getFieldsForWildcard( + req: ReqFacade, + indexPattern: string, + capabilities?: unknown + ) { const { indexPatternsService } = req.pre; return await indexPatternsService!.getFieldsForWildcard({ @@ -79,11 +90,4 @@ export abstract class AbstractSearchStrategy { fieldCapsOptions: { allow_no_indices: true }, }); } - - checkForViability( - req: ReqFacade, - indexPattern: string - ): Promise<{ isViable: boolean; capabilities: unknown }> { - throw new TypeError('Must override method'); - } } diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts index 76efc9bf708f5..d8ea6c9c8a526 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.test.ts @@ -18,13 +18,14 @@ */ import { DefaultSearchStrategy } from './default_search_strategy'; import { ReqFacade } from './abstract_search_strategy'; +import { VisPayload } from '../../../../common/types'; describe('DefaultSearchStrategy', () => { let defaultSearchStrategy: DefaultSearchStrategy; - let req: ReqFacade; + let req: ReqFacade; beforeEach(() => { - req = {} as ReqFacade; + req = {} as ReqFacade; defaultSearchStrategy = new DefaultSearchStrategy(); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts index 0e70b9978bf5c..e1f519456d373 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/default_search_strategy.ts @@ -19,11 +19,12 @@ import { AbstractSearchStrategy, ReqFacade } from './abstract_search_strategy'; import { DefaultSearchCapabilities } from '../default_search_capabilities'; +import { VisPayload } from '../../../../common/types'; export class DefaultSearchStrategy extends AbstractSearchStrategy { name = 'default'; - checkForViability(req: ReqFacade) { + checkForViability(req: ReqFacade) { return Promise.resolve({ isViable: true, capabilities: new DefaultSearchCapabilities(req), diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts index bd836f28b6638..6c30895635fe5 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.test.ts @@ -6,13 +6,13 @@ import { Unit } from '@elastic/datemath'; import { RollupSearchCapabilities } from './rollup_search_capabilities'; -import { ReqFacade } from '../../../../../src/plugins/vis_type_timeseries/server'; +import { ReqFacade, VisPayload } from '../../../../../src/plugins/vis_type_timeseries/server'; describe('Rollup Search Capabilities', () => { const testTimeZone = 'time_zone'; const testInterval = '10s'; const rollupIndex = 'rollupIndex'; - const request = ({} as unknown) as ReqFacade; + const request = ({} as unknown) as ReqFacade; let fieldsCapabilities: Record; let rollupSearchCaps: RollupSearchCapabilities; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts index 48df91bec9d5f..015a371bd2a35 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_capabilities.ts @@ -9,13 +9,18 @@ import { leastCommonInterval, isCalendarInterval } from './lib/interval_helper'; import { ReqFacade, DefaultSearchCapabilities, + VisPayload, } from '../../../../../src/plugins/vis_type_timeseries/server'; export class RollupSearchCapabilities extends DefaultSearchCapabilities { rollupIndex: string; availableMetrics: Record; - constructor(req: ReqFacade, fieldsCapabilities: Record, rollupIndex: string) { + constructor( + req: ReqFacade, + fieldsCapabilities: Record, + rollupIndex: string + ) { super(req, fieldsCapabilities); this.rollupIndex = rollupIndex; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts index bbe90a460311f..ec6c91b616f5b 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { RollupSearchStrategy } from './rollup_search_strategy'; -import type { ReqFacade } from '../../../../../src/plugins/vis_type_timeseries/server'; +import type { ReqFacade, VisPayload } from '../../../../../src/plugins/vis_type_timeseries/server'; jest.mock('../../../../../src/plugins/vis_type_timeseries/server', () => { const actual = jest.requireActual('../../../../../src/plugins/vis_type_timeseries/server'); @@ -45,7 +45,7 @@ describe('Rollup Search Strategy', () => { }, }, }, - } as unknown) as ReqFacade; + } as unknown) as ReqFacade; const indexPattern = 'indexPattern'; diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts index f31e26c7c725d..f1c20c318d109 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts +++ b/x-pack/plugins/vis_type_timeseries_enhanced/server/search_strategies/rollup_search_strategy.ts @@ -7,6 +7,7 @@ import { keyBy, isString } from 'lodash'; import { AbstractSearchStrategy, ReqFacade, + VisPayload, } from '../../../../../src/plugins/vis_type_timeseries/server'; import { @@ -24,7 +25,7 @@ const isIndexPatternValid = (indexPattern: string) => export class RollupSearchStrategy extends AbstractSearchStrategy { name = 'rollup'; - async search(req: ReqFacade, bodies: any[]) { + async search(req: ReqFacade, bodies: any[]) { return super.search(req, bodies, 'rollup'); } @@ -37,7 +38,7 @@ export class RollupSearchStrategy extends AbstractSearchStrategy { .catch(() => Promise.resolve({})); } - async checkForViability(req: ReqFacade, indexPattern: string) { + async checkForViability(req: ReqFacade, indexPattern: string) { let isViable = false; let capabilities = null; @@ -61,8 +62,8 @@ export class RollupSearchStrategy extends AbstractSearchStrategy { }; } - async getFieldsForWildcard( - req: ReqFacade, + async getFieldsForWildcard( + req: ReqFacade, indexPattern: string, { fieldsCapabilities, From 67988ab6cd05a1545b86b457224e24edd787fccf Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Tue, 17 Nov 2020 11:39:07 +0300 Subject: [PATCH 09/10] update codeowners --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index b7fb3ff04db71..62b41bb26da22 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -9,6 +9,7 @@ /x-pack/plugins/discover_enhanced/ @elastic/kibana-app /x-pack/plugins/lens/ @elastic/kibana-app /x-pack/plugins/graph/ @elastic/kibana-app +/x-pack/plugins/vis_type_timeseries_enhanced/ @elastic/kibana-app /src/plugins/advanced_settings/ @elastic/kibana-app /src/plugins/charts/ @elastic/kibana-app /src/plugins/discover/ @elastic/kibana-app From 52f43f481554d8c0718ecf9ee8f77f9d9cc7c422 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Wed, 18 Nov 2020 15:19:08 +0300 Subject: [PATCH 10/10] fix PR comments --- .../server/lib/vis_data/helpers/unit_to_seconds.ts | 2 +- x-pack/plugins/vis_type_timeseries_enhanced/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts index 22dfc590245ac..8950e05c85d4f 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/unit_to_seconds.ts @@ -37,7 +37,7 @@ const units: Record = { const sortedUnits = sortBy(Object.keys(units), (key: Unit) => units[key]); -export interface ParsedInterval { +interface ParsedInterval { value: number; unit: Unit; } diff --git a/x-pack/plugins/vis_type_timeseries_enhanced/README.md b/x-pack/plugins/vis_type_timeseries_enhanced/README.md index e77f0da4da5ca..33aa16d8574ae 100644 --- a/x-pack/plugins/vis_type_timeseries_enhanced/README.md +++ b/x-pack/plugins/vis_type_timeseries_enhanced/README.md @@ -3,7 +3,7 @@ The `vis_type_timeseries_enhanced` plugin is the x-pack counterpart to the OSS `vis_type_timeseries` plugin. It exists to provide Elastic-licensed services, or parts of services, which -enhance existing OSS functionality from `vis_type_timeseries_enhanced`. +enhance existing OSS functionality from `vis_type_timeseries`. Currently the `vis_type_timeseries_enhanced` plugin doesn't return any APIs which you can consume directly.