Skip to content

Commit

Permalink
use NP context savedObjectsClient for rollup true
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Feb 12, 2020
1 parent 0581dea commit 1d28a85
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { cloneDeep } from 'lodash';
import { Request } from 'src/legacy/server/kbn_server';
import { SavedObjectsClientContract } from 'kibana/server';
import {
Field,
Aggregation,
Expand Down Expand Up @@ -40,22 +40,27 @@ export function fieldServiceProvider(
indexPattern: string,
isRollup: boolean,
callWithRequest: any,
request: Request
savedObjectsClient: SavedObjectsClientContract
) {
return new FieldsService(indexPattern, isRollup, callWithRequest, request);
return new FieldsService(indexPattern, isRollup, callWithRequest, savedObjectsClient);
}

class FieldsService {
private _indexPattern: string;
private _isRollup: boolean;
private _callWithRequest: any;
private _request: Request;
private _savedObjectsClient: SavedObjectsClientContract;

constructor(indexPattern: string, isRollup: boolean, callWithRequest: any, request: Request) {
constructor(
indexPattern: string,
isRollup: boolean,
callWithRequest: any,
savedObjectsClient: any
) {
this._indexPattern = indexPattern;
this._isRollup = isRollup;
this._callWithRequest = callWithRequest;
this._request = request;
this._savedObjectsClient = savedObjectsClient;
}

private async loadFieldCaps(): Promise<any> {
Expand Down Expand Up @@ -104,7 +109,7 @@ class FieldsService {
const rollupService = await rollupServiceProvider(
this._indexPattern,
this._callWithRequest,
this._request
this._savedObjectsClient
);
const rollupConfigs: RollupJob[] | null = await rollupService.getRollupJobs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Request } from 'src/legacy/server/kbn_server';
import { SavedObjectsClientContract } from 'kibana/server';
import { Aggregation, Field, NewJobCaps } from '../../../../common/types/fields';
import { fieldServiceProvider } from './field_service';

Expand All @@ -15,9 +16,15 @@ interface NewJobCapsResponse {
export function newJobCapsProvider(callWithRequest: any, request: Request) {
async function newJobCaps(
indexPattern: string,
isRollup: boolean = false
isRollup: boolean = false,
savedObjectsClient: SavedObjectsClientContract
): Promise<NewJobCapsResponse> {
const fieldService = fieldServiceProvider(indexPattern, isRollup, callWithRequest, request);
const fieldService = fieldServiceProvider(
indexPattern,
isRollup,
callWithRequest,
savedObjectsClient
);
const { aggs, fields } = await fieldService.getData();
convertForStringify(aggs, fields);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Request } from 'src/legacy/server/kbn_server';
import { SavedObject } from 'src/core/server';
import { SavedObjectsClientContract } from 'kibana/server';
import { FieldId } from '../../../../common/types/fields';
import { ES_AGGREGATION } from '../../../../common/constants/aggregation_types';

Expand All @@ -21,9 +21,9 @@ export interface RollupJob {
export async function rollupServiceProvider(
indexPattern: string,
callWithRequest: any,
request: Request
savedObjectsClient: SavedObjectsClientContract
) {
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, request);
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, savedObjectsClient);
let jobIndexPatterns: string[] = [indexPattern];

async function getRollupJobs(): Promise<RollupJob[] | null> {
Expand Down Expand Up @@ -57,9 +57,8 @@ export async function rollupServiceProvider(

async function loadRollupIndexPattern(
indexPattern: string,
request: Request
savedObjectsClient: SavedObjectsClientContract
): Promise<SavedObject | null> {
const savedObjectsClient = request.getSavedObjectsClient();
const resp = await savedObjectsClient.find({
type: 'index-pattern',
fields: ['title', 'type', 'typeMeta'],
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,16 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
path: '/api/ml/jobs/new_job_caps/{indexPattern}',
validate: {
params: schema.object({ indexPattern: schema.string() }),
query: schema.maybe(schema.object({ rollup: schema.maybe(schema.string()) })),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { indexPattern } = request.params;
const isRollup = request.query.rollup === 'true';
const savedObjectsClient = context.core.savedObjects.client;
const { newJobCaps } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser, request);
const resp = await newJobCaps(indexPattern, isRollup);
const resp = await newJobCaps(indexPattern, isRollup, savedObjectsClient);

return response.ok({
body: resp,
Expand Down

0 comments on commit 1d28a85

Please sign in to comment.