Skip to content

Commit

Permalink
fix rollup and add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Jan 29, 2020
1 parent f00d2d9 commit 2447388
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 67 deletions.
4 changes: 3 additions & 1 deletion src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getIndexPatternObject } from './vis_data/helpers/get_index_pattern';
import { isNestedField } from '../../../data/server';
import { Framework } from '../plugin';
import { FieldDescriptor, IndexPatternsFetcher } from '../../../data/server';
import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy';

export async function getFields(
requestContext: RequestHandlerContext,
Expand All @@ -36,9 +37,10 @@ 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: any = {
const reqFacade: ReqFacade = {
...request,
framework,
payload: {},
pre: {
indexPatternsService: new IndexPatternsFetcher(
requestContext.core.elasticsearch.dataClient.callAsCurrentUser
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
* under the License.
*/

import { KibanaRequest, RequestHandlerContext } from 'kibana/server';
import { FakeRequest, RequestHandlerContext } from 'kibana/server';
import _ from 'lodash';
import { first, map } from 'rxjs/operators';
import { getPanelData } from './vis_data/get_panel_data';
import { Framework } from '../index';
import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy';

interface GetVisDataResponse {
[key: string]: GetVisDataPanel;
Expand Down Expand Up @@ -56,16 +57,17 @@ export type GetVisData = (

export function getVisData(
requestContext: RequestHandlerContext,
request: KibanaRequest<{}, {}, GetVisDataOptions>,
request: FakeRequest & { body: GetVisDataOptions },
framework: Framework
): Promise<GetVisDataResponse> {
// 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: any = {
const reqFacade: ReqFacade = {
...request,
framework,
pre: {},
payload: request.body,
getUiSettingsService: () => requestContext.core.uiSettings.client,
getSavedObjectsClient: () => requestContext.core.savedObjects.client,
Expand Down Expand Up @@ -94,7 +96,7 @@ export function getVisData(
.toPromise();
},
};
const promises = reqFacade.payload.panels.map(getPanelData(reqFacade));
const promises = (reqFacade.payload as GetVisDataOptions).panels.map(getPanelData(reqFacade));
return Promise.all(promises).then(res => {
return res.reduce((acc, data) => {
return _.assign(acc as any, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('SearchStrategyRegister', () => {
});

test('should add a strategy if it is an instance of AbstractSearchStrategy', () => {
const anotherSearchStrategy = new MockSearchStrategy();
const anotherSearchStrategy = new MockSearchStrategy({}, {} as any, {});
const addedStrategies = registry.addStrategy(anotherSearchStrategy);

expect(addedStrategies.length).toEqual(2);
Expand All @@ -75,7 +75,7 @@ describe('SearchStrategyRegister', () => {
test('should return a MockSearchStrategy instance', async () => {
const req = {};
const indexPattern = '*';
const anotherSearchStrategy = new MockSearchStrategy();
const anotherSearchStrategy = new MockSearchStrategy({}, {} as any, {});
registry.addStrategy(anotherSearchStrategy);

const { searchStrategy, capabilities } = (await registry.getViableStrategy(req, indexPattern))!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

// @ts-ignore
import { AbstractSearchStrategy } from './strategies/abstract_search_strategy';
// @ts-ignore
import { DefaultSearchStrategy } from './strategies/default_search_strategy';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {
APICaller,
FakeRequest,
IUiSettingsClient,
SavedObjectsClientContract,
} from 'kibana/server';
import { Framework } from '../../../plugin';
import { IndexPatternsFetcher } from '../../../../../data/server';

/**
* ReqFacade is a regular KibanaRequest object extended with additional service
* references to ensure backwards compatibility for existing integrations.
*
* This will be replaced by standard KibanaRequest and RequestContext objects in a later version.
*/
export type ReqFacade = FakeRequest & {
framework: Framework;
payload: unknown;
pre: {
indexPatternsService?: IndexPatternsFetcher;
};
getUiSettingsService: () => IUiSettingsClient;
getSavedObjectsClient: () => SavedObjectsClientContract;
server: {
plugins: {
elasticsearch: {
getCluster: () => {
callWithRequest: (req: ReqFacade, endpoint: string, params: any) => Promise<any>;
};
};
};
};
getEsShardTimeout: () => Promise<number>;
};

export class AbstractSearchStrategy {
public getCallWithRequestInstance: (req: ReqFacade) => APICaller;
public getSearchRequest: (req: ReqFacade) => any;

constructor(
server: any,
callWithRequestFactory: (server: any, req: ReqFacade) => APICaller,
SearchRequest: any
) {
this.getCallWithRequestInstance = req => callWithRequestFactory(server, req);

this.getSearchRequest = req => {
const callWithRequest = this.getCallWithRequestInstance(req);

return new SearchRequest(req, callWithRequest);
};
}

async getFieldsForWildcard(req: ReqFacade, indexPattern: string, capabilities: any) {
const { indexPatternsService } = req.pre;

return await indexPatternsService!.getFieldsForWildcard({
pattern: indexPattern,
});
}

checkForViability(
req: ReqFacade,
indexPattern: string
): { isViable: boolean; capabilities: any } {
throw new TypeError('Must override method');
}
}
8 changes: 5 additions & 3 deletions src/plugins/vis_type_timeseries/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RequestHandlerContext,
Logger,
IRouter,
KibanaRequest,
FakeRequest,
} from 'src/core/server';
import { Observable } from 'rxjs';
import { Server } from 'hapi';
Expand All @@ -49,6 +49,7 @@ interface VisTypeTimeseriesPluginSetupDependencies {
export interface VisTypeTimeseriesSetup {
getVisData: (
requestContext: RequestHandlerContext,
fakeRequest: FakeRequest,
options: GetVisDataOptions
) => ReturnType<GetVisData>;
addSearchStrategy: SearchStrategyRegistry['addStrategy'];
Expand Down Expand Up @@ -104,9 +105,10 @@ export class VisTypeTimeseriesPlugin implements Plugin<VisTypeTimeseriesSetup> {
return {
getVisData: async (
requestContext: RequestHandlerContext,
request: KibanaRequest<{}, {}, GetVisDataOptions>
fakeRequest: FakeRequest,
options: GetVisDataOptions
) => {
return await getVisData(requestContext, request, framework);
return await getVisData(requestContext, { ...fakeRequest, body: options }, framework);
},
addSearchStrategy: searchStrategyRegistry.addStrategy.bind(searchStrategyRegistry),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export class KibanaFramework {

public async makeTSVBRequest(
requestContext: RequestHandlerContext,
rawRequest: KibanaRequest,
model: TSVBMetricModel,
timerange: { min: number; max: number },
filters: any[]
Expand All @@ -254,6 +255,6 @@ export class KibanaFramework {
panels: [model],
filters,
};
return getVisData(requestContext, options);
return getVisData(requestContext, rawRequest, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ export const populateSeriesWithTSVBData = (
}

// Get TSVB results using the model, timerange and filters
const tsvbResults = await framework.makeTSVBRequest(requestContext, model, timerange, filters);
const tsvbResults = await framework.makeTSVBRequest(
requestContext,
request,
model,
timerange,
filters
);

// If there is no data `custom` will not exist.
if (!tsvbResults.custom) {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/rollup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function rollup(kibana) {
server.plugins.index_management.addIndexManagementDataEnricher(rollupDataEnricher);
}

registerRollupSearchStrategy(this.kbnServer, server);
registerRollupSearchStrategy(this.kbnServer);
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { once } from 'lodash';
import { elasticsearchJsPlugin } from '../../client/elasticsearch_rollup';

const callWithRequest = once(server => {
console.log('>>>>>>>>>>>>');
console.log(server);
const client = server.newPlatform.setup.core.elasticsearch.createClient('rollup', {
plugins: [elasticsearchJsPlugin],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
AbstractSearchStrategy,
} from '../../../../../../../src/plugins/vis_type_timeseries/server';

export const registerRollupSearchStrategy = (kbnServer, server) =>
export const registerRollupSearchStrategy = kbnServer =>
kbnServer.afterPluginsInit(() => {
if (!kbnServer.newPlatform.setup.plugins.metrics) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ describe('Register Rollup Search Strategy', () => {
});

test('should run initialization on "afterPluginsInit" hook', () => {
registerRollupSearchStrategy(kbnServer, {});
registerRollupSearchStrategy(kbnServer);

expect(kbnServer.afterPluginsInit).toHaveBeenCalled();
});

test('should run initialization if metrics plugin available', () => {
registerRollupSearchStrategy(
{ ...kbnServer, newPlatform: { setup: { plugins: { metrics } } } },
{}
);
registerRollupSearchStrategy({
...kbnServer,
newPlatform: { setup: { plugins: { metrics } } },
});

expect(metrics.addSearchStrategy).toHaveBeenCalled();
});

test('should not run initialization if metrics plugin unavailable', () => {
registerRollupSearchStrategy(kbnServer, {
plugins: {},
});
registerRollupSearchStrategy(kbnServer);

expect(metrics.addSearchStrategy).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit 2447388

Please sign in to comment.