Skip to content

Commit

Permalink
[TSVB] Remove request facade and type server code (#92964)
Browse files Browse the repository at this point in the history
* Remove request facade and update search strategies

* Use typescript

* Type files

* Update structure

* Update tests

* Type annotations

* Fix type for infra

* Enhance types

* Remove generics

* Use constant

* Update docs

* Type capabilities
  • Loading branch information
Daniil authored Mar 9, 2021
1 parent af8d5eb commit c7fc78e
Show file tree
Hide file tree
Showing 55 changed files with 610 additions and 500 deletions.
10 changes: 9 additions & 1 deletion src/plugins/vis_type_timeseries/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
*/

import { TypeOf } from '@kbn/config-schema';
import { metricsItems, panel, seriesItems, visPayloadSchema, fieldObject } from './vis_schema';
import {
metricsItems,
panel,
seriesItems,
visPayloadSchema,
fieldObject,
annotationsItems,
} from './vis_schema';
import { PANEL_TYPES } from './panel_types';
import { TimeseriesUIRestrictions } from './ui_restrictions';

export type AnnotationItemsSchema = TypeOf<typeof annotationsItems>;
export type SeriesItemsSchema = TypeOf<typeof seriesItems>;
export type MetricsItemsSchema = TypeOf<typeof metricsItems>;
export type PanelSchema = TypeOf<typeof panel>;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vis_type_timeseries/common/vis_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const numberOptionalOrEmptyString = schema.maybe(

export const fieldObject = stringOptionalNullable;

const annotationsItems = schema.object({
export const annotationsItems = schema.object({
color: stringOptionalNullable,
fields: stringOptionalNullable,
hidden: schema.maybe(schema.boolean()),
icon: stringOptionalNullable,
id: stringOptionalNullable,
id: schema.string(),
ignore_global_filters: numberIntegerOptional,
ignore_panel_filters: numberIntegerOptional,
index_pattern: stringOptionalNullable,
Expand Down
55 changes: 10 additions & 45 deletions src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,18 @@
*/

import { uniqBy } from 'lodash';
import { first, map } from 'rxjs/operators';
import { KibanaRequest } from 'kibana/server';

import { Framework } from '../plugin';
import { IndexPatternsFetcher } from '../../../data/server';
import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy';
import { VisTypeTimeseriesRequestHandlerContext } from '../types';
import { VisTypeTimeseriesFieldsRequest, VisTypeTimeseriesRequestHandlerContext } from '../types';

export async function getFields(
requestContext: VisTypeTimeseriesRequestHandlerContext,
request: KibanaRequest,
request: VisTypeTimeseriesFieldsRequest,
framework: Framework,
indexPatternString: string
) {
const getIndexPatternsService = async () => {
const [, { data }] = await framework.core.getStartServices();

return await data.indexPatterns.indexPatternsServiceFactory(
requestContext.core.savedObjects.client,
requestContext.core.elasticsearch.client.asCurrentUser
);
};

const indexPatternsService = await getIndexPatternsService();

// NOTE / TODO: This facade has been put in place to make migrating to the New Platform easier. It
// 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<{}> = {
requestContext,
...request,
framework,
payload: {},
pre: {
indexPatternsFetcher: new IndexPatternsFetcher(
requestContext.core.elasticsearch.client.asCurrentUser
),
},
getUiSettingsService: () => requestContext.core.uiSettings.client,
getEsShardTimeout: async () => {
return await framework.globalConfig$
.pipe(
first(),
map((config) => config.elasticsearch.shardTimeout.asMilliseconds())
)
.toPromise();
},
getIndexPatternsService: async () => indexPatternsService,
};

if (!indexPatternString) {
const indexPatternsService = await framework.getIndexPatternsService(requestContext);
const defaultIndexPattern = await indexPatternsService.getDefault();

indexPatternString = defaultIndexPattern?.title ?? '';
Expand All @@ -67,10 +27,15 @@ export async function getFields(
const {
searchStrategy,
capabilities,
} = (await framework.searchStrategyRegistry.getViableStrategy(reqFacade, indexPatternString))!;
} = (await framework.searchStrategyRegistry.getViableStrategy(
requestContext,
request,
indexPatternString
))!;

const fields = await searchStrategy.getFieldsForWildcard(
reqFacade,
requestContext,
request,
indexPatternString,
capabilities
);
Expand Down
86 changes: 30 additions & 56 deletions src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,47 @@
* Side Public License, v 1.
*/

import { FakeRequest } from 'kibana/server';
import _ from 'lodash';
import { first, map } from 'rxjs/operators';

import { Filter, Query } from 'src/plugins/data/common';
import { getPanelData } from './vis_data/get_panel_data';
import { Framework } from '../plugin';
import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy';
import { TimeseriesVisData } from '../../common/types';
import type { VisTypeTimeseriesRequestHandlerContext } from '../types';
import { PANEL_TYPES } from '../../common/panel_types';
import type {
VisTypeTimeseriesVisDataRequest,
VisTypeTimeseriesRequestHandlerContext,
VisTypeTimeseriesRequestServices,
} from '../types';
import { getSeriesData } from './vis_data/get_series_data';
import { getTableData } from './vis_data/get_table_data';
import { getEsQueryConfig } from './vis_data/helpers/get_es_query_uisettings';

export interface GetVisDataOptions {
timerange: {
min: number | string;
max: number | string;
timezone?: string;
};
panels: unknown[];
filters?: Filter[];
state?: Record<string, unknown>;
query?: Query | Query[];
sessionId?: string;
}

export type GetVisData = (
export async function getVisData(
requestContext: VisTypeTimeseriesRequestHandlerContext,
options: GetVisDataOptions,
framework: Framework
) => Promise<TimeseriesVisData>;

export function getVisData(
requestContext: VisTypeTimeseriesRequestHandlerContext,
request: FakeRequest & { body: GetVisDataOptions },
request: VisTypeTimeseriesVisDataRequest,
framework: Framework
): Promise<TimeseriesVisData> {
// NOTE / TODO: This facade has been put in place to make migrating to the New Platform easier. It
// 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<GetVisDataOptions> = {
requestContext,
...request,
framework,
pre: {},
payload: request.body,
getUiSettingsService: () => requestContext.core.uiSettings.client,
getEsShardTimeout: async () => {
return await framework.globalConfig$
.pipe(
first(),
map((config) => config.elasticsearch.shardTimeout.asMilliseconds())
)
.toPromise();
},
getIndexPatternsService: async () => {
const [, { data }] = await framework.core.getStartServices();

return await data.indexPatterns.indexPatternsServiceFactory(
requestContext.core.savedObjects.client,
requestContext.core.elasticsearch.client.asCurrentUser
);
},
const uiSettings = requestContext.core.uiSettings.client;
const esShardTimeout = await framework.getEsShardTimeout();
const indexPatternsService = await framework.getIndexPatternsService(requestContext);
const esQueryConfig = await getEsQueryConfig(uiSettings);
const services: VisTypeTimeseriesRequestServices = {
esQueryConfig,
esShardTimeout,
indexPatternsService,
uiSettings,
searchStrategyRegistry: framework.searchStrategyRegistry,
};
const promises = reqFacade.payload.panels.map(getPanelData(reqFacade));

const promises = request.body.panels.map((panel) => {
if (panel.type === PANEL_TYPES.TABLE) {
return getTableData(requestContext, request, panel, services);
}
return getSeriesData(requestContext, request, panel, services);
});

return Promise.all(promises).then((res) => {
return res.reduce((acc, data) => {
return _.assign(acc as any, data);
return _.assign(acc, data);
}, {});
}) as Promise<TimeseriesVisData>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
*/

import { DefaultSearchCapabilities } from './default_search_capabilities';
import type { ReqFacade } from '../strategies/abstract_search_strategy';
import type { VisPayload } from '../../../../common/types';
import { VisTypeTimeseriesRequest } from '../../../types';

describe('DefaultSearchCapabilities', () => {
let defaultSearchCapabilities: DefaultSearchCapabilities;
let req: ReqFacade<VisPayload>;
let req: VisTypeTimeseriesRequest;

beforeEach(() => {
req = {} as ReqFacade<VisPayload>;
req = {} as VisTypeTimeseriesRequest;
defaultSearchCapabilities = new DefaultSearchCapabilities(req);
});

Expand All @@ -38,12 +37,12 @@ describe('DefaultSearchCapabilities', () => {

test('should return Search Timezone', () => {
defaultSearchCapabilities.request = ({
payload: {
body: {
timerange: {
timezone: 'UTC',
},
},
} as unknown) as ReqFacade<VisPayload>;
} as unknown) as VisTypeTimeseriesRequest;

expect(defaultSearchCapabilities.searchTimezone).toEqual('UTC');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ import {
getSuitableUnit,
} from '../../vis_data/helpers/unit_to_seconds';
import { RESTRICTIONS_KEYS } from '../../../../common/ui_restrictions';
import type { ReqFacade } from '../strategies/abstract_search_strategy';
import type { VisPayload } from '../../../../common/types';
import { VisTypeTimeseriesRequest, VisTypeTimeseriesVisDataRequest } from '../../../types';

const getTimezoneFromRequest = (request: ReqFacade<VisPayload>) => {
return request.payload.timerange.timezone;
const isVisDataRequest = (
request: VisTypeTimeseriesRequest
): request is VisTypeTimeseriesVisDataRequest => {
return !!(request as VisTypeTimeseriesVisDataRequest).body;
};

const getTimezoneFromRequest = (request: VisTypeTimeseriesRequest) => {
if (isVisDataRequest(request)) return request.body.timerange.timezone;
};

export class DefaultSearchCapabilities {
constructor(
public request: ReqFacade<VisPayload>,
public request: VisTypeTimeseriesRequest,
public fieldsCapabilities: Record<string, any> = {}
) {}

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

import { Unit } from '@elastic/datemath';
import { RollupSearchCapabilities } from './rollup_search_capabilities';

import type { VisPayload } from '../../../../common/types';
import type { ReqFacade } from '../strategies/abstract_search_strategy';
import { VisTypeTimeseriesRequest } from '../../../types';

describe('Rollup Search Capabilities', () => {
const testTimeZone = 'time_zone';
const testInterval = '10s';
const rollupIndex = 'rollupIndex';
const request = ({} as unknown) as ReqFacade<VisPayload>;
const request = ({} as unknown) as VisTypeTimeseriesRequest;

let fieldsCapabilities: Record<string, any>;
let rollupSearchCaps: RollupSearchCapabilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import { get, has } from 'lodash';
import { leastCommonInterval, isCalendarInterval } from '../lib/interval_helper';

import { DefaultSearchCapabilities } from './default_search_capabilities';

import type { VisPayload } from '../../../../common/types';
import type { ReqFacade } from '../strategies/abstract_search_strategy';
import { VisTypeTimeseriesRequest } from '../../../types';

export class RollupSearchCapabilities extends DefaultSearchCapabilities {
rollupIndex: string;
availableMetrics: Record<string, any>;

constructor(
req: ReqFacade<VisPayload>,
req: VisTypeTimeseriesRequest,
fieldsCapabilities: Record<string, any>,
rollupIndex: string
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,4 @@
export { SearchStrategyRegistry } from './search_strategy_registry';
export { DefaultSearchCapabilities } from './capabilities/default_search_capabilities';

export {
AbstractSearchStrategy,
ReqFacade,
RollupSearchStrategy,
DefaultSearchStrategy,
} from './strategies';
export { AbstractSearchStrategy, RollupSearchStrategy, DefaultSearchStrategy } from './strategies';
Loading

0 comments on commit c7fc78e

Please sign in to comment.