Skip to content

Commit

Permalink
[Reporting/np-k8] Remove several oncePerServer usages (elastic#50997)
Browse files Browse the repository at this point in the history
* [Reporting/np-k8] Remove several oncePerServer usages

* ts fixes 1

* ts fixes 2

* more ts fixes

* more ts fixes

* more ts fixes

* ts simplification

* improve ts

* remove any type for jobParams and define JobParamsSavedObject and JobParamsUrl

* ts simplification

* Fix ts

* ts simplification

* fix ts

* bug fix

* align with joels pr
  • Loading branch information
tsullivan committed Nov 22, 2019
1 parent a38e5ed commit e523d33
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 123 deletions.
5 changes: 1 addition & 4 deletions x-pack/legacy/plugins/reporting/common/get_absolute_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/

import url from 'url';
import { oncePerServer } from '../server/lib/once_per_server';
import { ServerFacade } from '../types';

function getAbsoluteUrlFn(server: ServerFacade) {
export function getAbsoluteUrlFactory(server: ServerFacade) {
const config = server.config();

return function getAbsoluteUrl({
Expand All @@ -27,5 +26,3 @@ function getAbsoluteUrlFn(server: ServerFacade) {
});
};
}

export const getAbsoluteUrlFactory = oncePerServer(getAbsoluteUrlFn);
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { oncePerServer } from '../../../server/lib/once_per_server';
import { cryptoFactory } from '../../../server/lib/crypto';
import { ConditionalHeaders, CreateJobFactory, ServerFacade, RequestFacade } from '../../../types';
import { JobParamsDiscoverCsv, ESQueueCreateJobFnDiscoverCsv } from '../types';
import { ConditionalHeaders, ServerFacade, RequestFacade } from '../../../types';
import { JobParamsDiscoverCsv } from '../types';

function createJobFn(server: ServerFacade) {
export const createJobFactory = function createJobFn(server: ServerFacade) {
const crypto = cryptoFactory(server);

return async function createJob(
Expand All @@ -32,8 +31,4 @@ function createJobFn(server: ServerFacade) {
...jobParams,
};
};
}

export const createJobFactory: CreateJobFactory = oncePerServer(
createJobFn as (server: ServerFacade) => ESQueueCreateJobFnDiscoverCsv
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import { CSV_JOB_TYPE, PLUGIN_ID } from '../../../common/constants';
import { cryptoFactory, oncePerServer, LevelLogger } from '../../../server/lib';
import { cryptoFactory, LevelLogger } from '../../../server/lib';
import { createGenerateCsv } from './lib/generate_csv';
import { fieldFormatMapFactory } from './lib/field_format_map';
import { i18n } from '@kbn/i18n';

function executeJobFn(server) {
export function executeJobFactory(server) {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const crypto = cryptoFactory(server);
const config = server.config();
Expand Down Expand Up @@ -126,5 +126,3 @@ function executeJobFn(server) {
};
};
}

export const executeJobFactory = oncePerServer(executeJobFn);
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { notFound, notImplemented } from 'boom';
import { get } from 'lodash';
import { PLUGIN_ID, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../../common/constants';
import { cryptoFactory, LevelLogger, oncePerServer } from '../../../../server/lib';
import { ServerFacade, RequestFacade } from '../../../../types';
import { cryptoFactory, LevelLogger } from '../../../../server/lib';
import { ImmediateCreateJobFn, ServerFacade, RequestFacade } from '../../../../types';
import {
SavedObject,
SavedObjectServiceError,
Expand All @@ -27,13 +27,7 @@ interface VisData {
panel: SearchPanel;
}

type CreateJobFn = (
jobParams: JobParamsPanelCsv,
headers: any,
req: RequestFacade
) => Promise<JobDocPayloadPanelCsv>;

function createJobFn(server: ServerFacade): CreateJobFn {
export function createJobFactory(server: ServerFacade): ImmediateCreateJobFn {
const crypto = cryptoFactory(server);
const logger = LevelLogger.createForServer(server, [
PLUGIN_ID,
Expand Down Expand Up @@ -100,5 +94,3 @@ function createJobFn(server: ServerFacade): CreateJobFn {
};
};
}

export const createJobFactory = oncePerServer(createJobFn);
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
*/

import { i18n } from '@kbn/i18n';
import { cryptoFactory, LevelLogger, oncePerServer } from '../../../server/lib';
import { cryptoFactory, LevelLogger } from '../../../server/lib';
import {
ImmediateExecuteFn,
JobDocOutputExecuted,
ServerFacade,
ExecuteImmediateJobFactory,
RequestFacade,
} from '../../../types';
import {
CONTENT_TYPE_CSV,
CSV_FROM_SAVEDOBJECT_JOB_TYPE,
PLUGIN_ID,
} from '../../../common/constants';
import { CsvResultFromSearch, JobDocPayloadPanelCsv, FakeRequest } from '../types';
import {
CsvResultFromSearch,
JobParamsPanelCsv,
SearchPanel,
JobDocPayloadPanelCsv,
FakeRequest,
} from '../types';
import { createGenerateCsv } from './lib';

type ExecuteJobFn = (
jobId: string | null,
job: JobDocPayloadPanelCsv,
realRequest?: RequestFacade
) => Promise<JobDocOutputExecuted>;

function executeJobFactoryFn(server: ServerFacade): ExecuteJobFn {
export function executeJobFactory(server: ServerFacade): ImmediateExecuteFn {
const crypto = cryptoFactory(server);
const logger = LevelLogger.createForServer(server, [
PLUGIN_ID,
Expand All @@ -45,7 +45,14 @@ function executeJobFactoryFn(server: ServerFacade): ExecuteJobFn {
const jobLogger = logger.clone([jobId === null ? 'immediate' : jobId]);

const { jobParams } = job;
const { isImmediate, panel, visType } = jobParams;
const { isImmediate, panel, visType } = jobParams as JobParamsPanelCsv & { panel: SearchPanel };

if (!panel) {
i18n.translate(
'xpack.reporting.exportTypes.csv_from_savedobject.executeJob.failedToAccessPanel',
{ defaultMessage: 'Failed to access panel metadata for job execution' }
);
}

jobLogger.debug(`Execute job generating [${visType}] csv`);

Expand Down Expand Up @@ -112,7 +119,3 @@ function executeJobFactoryFn(server: ServerFacade): ExecuteJobFn {
};
};
}

export const executeJobFactory: ExecuteImmediateJobFactory = oncePerServer(
executeJobFactoryFn as ExecuteImmediateJobFactory
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { JobParamsPostPayloadPanelCsv, JobParamsPanelCsv } from '../../types';
export function getJobParamsFromRequest(
request: RequestFacade,
opts: { isImmediate: boolean }
): Partial<JobParamsPanelCsv> {
): JobParamsPanelCsv {
const { savedObjectType, savedObjectId } = request.params;
const { timerange, state } = request.payload as JobParamsPostPayloadPanelCsv;
const post = timerange || state ? { timerange, state } : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ export interface JobParamsPanelCsv {
savedObjectType: string;
savedObjectId: string;
isImmediate: boolean;
panel: SearchPanel;
panel?: SearchPanel;
post?: JobParamsPostPayloadPanelCsv;
visType?: string;
}

export interface JobDocPayloadPanelCsv extends JobDocPayload {
type: string | null;
objects: null;
jobParams: JobParamsPanelCsv;
}

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

import {
ServerFacade,
RequestFacade,
ConditionalHeaders,
CreateJobFactory,
} from '../../../../types';
import { ServerFacade, RequestFacade, ConditionalHeaders } from '../../../../types';
import { validateUrls } from '../../../../common/validate_urls';
import { cryptoFactory } from '../../../../server/lib/crypto';
import { oncePerServer } from '../../../../server/lib/once_per_server';
import { JobParamsPNG, ESQueueCreateJobFnPNG } from '../../types';
import { JobParamsPNG } from '../../types';

function createJobFn(server: ServerFacade) {
export const createJobFactory = function createJobFn(server: ServerFacade) {
const crypto = cryptoFactory(server);

return async function createJob(
Expand All @@ -38,8 +32,4 @@ function createJobFn(server: ServerFacade) {
forceNow: new Date().toISOString(),
};
};
}

export const createJobFactory: CreateJobFactory = oncePerServer(
createJobFn as (server: ServerFacade) => ESQueueCreateJobFnPNG
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Rx from 'rxjs';
import { i18n } from '@kbn/i18n';
import { mergeMap, catchError, map, takeUntil } from 'rxjs/operators';
import { PLUGIN_ID, PNG_JOB_TYPE } from '../../../../common/constants';
import { LevelLogger, oncePerServer } from '../../../../server/lib';
import { LevelLogger } from '../../../../server/lib';
import { generatePngObservableFactory } from '../lib/generate_png';
import {
decryptJobHeaders,
Expand All @@ -17,7 +17,7 @@ import {
getFullUrls,
} from '../../../common/execute_job/';

function executeJobFn(server) {
export function executeJobFactory(server) {
const generatePngObservable = generatePngObservableFactory(server);
const logger = LevelLogger.createForServer(server, [PLUGIN_ID, PNG_JOB_TYPE, 'execute']);

Expand Down Expand Up @@ -67,5 +67,3 @@ function executeJobFn(server) {
return process$.pipe(takeUntil(stop$)).toPromise();
};
}

export const executeJobFactory = oncePerServer(executeJobFn);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as Rx from 'rxjs';
import { toArray, mergeMap } from 'rxjs/operators';
import { LevelLogger } from '../../../../server/lib';
import { ServerFacade, ConditionalHeaders } from '../../../../types';
import { oncePerServer } from '../../../../server/lib/once_per_server';
import { screenshotsObservableFactory } from '../../../common/lib/screenshots';
import { PreserveLayout } from '../../../common/layouts/preserve_layout';
import { LayoutParams } from '../../../common/layouts/layout';
Expand All @@ -21,7 +20,7 @@ interface UrlScreenshot {
screenshots: ScreenshotData[];
}

function generatePngObservableFn(server: ServerFacade) {
export function generatePngObservableFactory(server: ServerFacade) {
const screenshotsObservable = screenshotsObservableFactory(server);
const captureConcurrency = 1;

Expand Down Expand Up @@ -68,5 +67,3 @@ function generatePngObservableFn(server: ServerFacade) {
);
};
}

export const generatePngObservableFactory = oncePerServer(generatePngObservableFn);
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import { PLUGIN_ID, PDF_JOB_TYPE } from '../../../../common/constants';
import { validateUrls } from '../../../../common/validate_urls';
import { LevelLogger, oncePerServer } from '../../../../server/lib';
import { LevelLogger } from '../../../../server/lib';
import { cryptoFactory } from '../../../../server/lib/crypto';
import { compatibilityShimFactory } from './compatibility_shim';

function createJobFactoryFn(server) {
export function createJobFactory(server) {
const logger = LevelLogger.createForServer(server, [PLUGIN_ID, PDF_JOB_TYPE, 'create']);
const compatibilityShim = compatibilityShimFactory(server, logger);
const crypto = cryptoFactory(server);
Expand All @@ -36,5 +36,3 @@ function createJobFactoryFn(server) {
};
});
}

export const createJobFactory = oncePerServer(createJobFactoryFn);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Rx from 'rxjs';
import { mergeMap, catchError, map, takeUntil } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { PLUGIN_ID, PDF_JOB_TYPE } from '../../../../common/constants';
import { LevelLogger, oncePerServer } from '../../../../server/lib';
import { LevelLogger } from '../../../../server/lib';
import { generatePdfObservableFactory } from '../lib/generate_pdf';
import {
decryptJobHeaders,
Expand All @@ -18,7 +18,7 @@ import {
getCustomLogo,
} from '../../../common/execute_job/';

function executeJobFn(server) {
export function executeJobFactory(server) {
const generatePdfObservable = generatePdfObservableFactory(server);
const logger = LevelLogger.createForServer(server, [PLUGIN_ID, PDF_JOB_TYPE, 'execute']);

Expand Down Expand Up @@ -71,5 +71,3 @@ function executeJobFn(server) {
return process$.pipe(takeUntil(stop$)).toPromise();
};
}

export const executeJobFactory = oncePerServer(executeJobFn);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { LevelLogger } from '../../../../server/lib';
import { ServerFacade, ConditionalHeaders } from '../../../../types';
// @ts-ignore untyped module
import { pdf } from './pdf';
import { oncePerServer } from '../../../../server/lib/once_per_server';
import { screenshotsObservableFactory } from '../../../common/lib/screenshots';
import { createLayout } from '../../../common/layouts';
import { TimeRange } from '../../../common/lib/screenshots/types';
Expand All @@ -38,7 +37,7 @@ const getTimeRange = (urlScreenshots: UrlScreenshot[]) => {
return null;
};

function generatePdfObservableFn(server: ServerFacade) {
export function generatePdfObservableFactory(server: ServerFacade) {
const screenshotsObservable = screenshotsObservableFactory(server);
const captureConcurrency = 1;

Expand Down Expand Up @@ -87,5 +86,3 @@ function generatePdfObservableFn(server: ServerFacade) {
);
};
}

export const generatePdfObservableFactory = oncePerServer(generatePdfObservableFn);
5 changes: 1 addition & 4 deletions x-pack/legacy/plugins/reporting/server/lib/create_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { ServerFacade, QueueConfig } from '../../types';
// @ts-ignore
import { Esqueue } from './esqueue';
import { createWorkerFactory } from './create_worker';
import { oncePerServer } from './once_per_server';
import { LevelLogger } from './level_logger';
// @ts-ignore
import { createTaggedLogger } from './create_tagged_logger'; // TODO remove createTaggedLogger once esqueue is removed

function createQueueFn(server: ServerFacade): Esqueue {
export function createQueueFactory(server: ServerFacade): Esqueue {
const queueConfig: QueueConfig = server.config().get('xpack.reporting.queue');
const index = server.config().get('xpack.reporting.index');

Expand Down Expand Up @@ -45,5 +44,3 @@ function createQueueFn(server: ServerFacade): Esqueue {

return queue;
}

export const createQueueFactory = oncePerServer(createQueueFn);
5 changes: 1 addition & 4 deletions x-pack/legacy/plugins/reporting/server/lib/create_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import {
// @ts-ignore untyped dependency
import { events as esqueueEvents } from './esqueue';
import { LevelLogger } from './level_logger';
import { oncePerServer } from './once_per_server';

function createWorkerFn(server: ServerFacade) {
export function createWorkerFactory(server: ServerFacade) {
const config = server.config();
const logger = LevelLogger.createForServer(server, [PLUGIN_ID, 'queue-worker']);
const queueConfig: QueueConfig = config.get('xpack.reporting.queue');
Expand Down Expand Up @@ -79,5 +78,3 @@ function createWorkerFn(server: ServerFacade) {
});
};
}

export const createWorkerFactory = oncePerServer(createWorkerFn);
Loading

0 comments on commit e523d33

Please sign in to comment.