Skip to content

Commit

Permalink
back out some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jun 12, 2020
1 parent 8eef541 commit 26abb7d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
80 changes: 41 additions & 39 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,6 @@ export class ReportingCore {
this.pluginStart$.next(reportingStartDeps);
}

public setConfig(config: ReportingConfig) {
this.config = config;
this.pluginSetup$.next(true);
}

/*
* High-level module dependencies
*/

public getConfig(): ReportingConfig {
if (!this.config) {
throw new Error('Config is not yet initialized');
}
return this.config;
}

public getPluginSetupDeps() {
if (!this.pluginSetupDeps) {
throw new Error(`"pluginSetupDeps" dependencies haven't initialized yet`);
}
return this.pluginSetupDeps;
}

private async getPluginStartDeps() {
if (this.pluginStartDeps) {
return this.pluginStartDeps;
}
return await this.pluginStart$.pipe(first()).toPromise();
}

public async pluginIsSetup(): Promise<boolean> {
// use deps and config as a cached resolver
if (this.pluginSetupDeps && this.config) {
Expand All @@ -98,23 +68,19 @@ export class ReportingCore {
return await this.pluginSetup$.pipe(take(2)).toPromise(); // once for pluginSetupDeps (sync) and twice for config (async)
}

public async pluginIsStarted(): Promise<boolean> {
public async pluginHasStarted(): Promise<boolean> {
return await this.getPluginStartDeps().then(() => true);
}

/*
* Downstream dependencies
*/
public setConfig(config: ReportingConfig) {
this.config = config;
this.pluginSetup$.next(true);
}

public getExportTypesRegistry() {
return this.exportTypesRegistry;
}

public async getSavedObjectsClient(fakeRequest: KibanaRequest) {
const { savedObjects } = await this.getPluginStartDeps();
return savedObjects.getScopedClient(fakeRequest) as SavedObjectsClientContract;
}

public async getEsqueue() {
return (await this.getPluginStartDeps()).esqueue;
}
Expand All @@ -133,9 +99,45 @@ export class ReportingCore {
.toPromise();
}

public getConfig(): ReportingConfig {
if (!this.config) {
throw new Error('Config is not yet initialized');
}
return this.config;
}

public async getScreenshotsObservable(): Promise<ScreenshotsObservableFn> {
const config = this.getConfig();
const { browserDriverFactory } = await this.getPluginStartDeps();
return screenshotsObservableFactory(config.get('capture'), browserDriverFactory);
}

public getPluginSetupDeps() {
if (!this.pluginSetupDeps) {
throw new Error(`"pluginSetupDeps" dependencies haven't initialized yet`);
}
return this.pluginSetupDeps;
}

private async getPluginStartDeps() {
if (this.pluginStartDeps) {
return this.pluginStartDeps;
}
return await this.pluginStart$.pipe(first()).toPromise();
}

public getElasticsearchService() {
return this.getPluginSetupDeps().elasticsearch;
}

public async getSavedObjectsClient(fakeRequest: KibanaRequest) {
const { savedObjects } = await this.getPluginStartDeps();
return savedObjects.getScopedClient(fakeRequest) as SavedObjectsClientContract;
}

public async getUiSettingsServiceFactory(savedObjectsClient: SavedObjectsClientContract) {
const { uiSettings: uiSettingsService } = await this.getPluginStartDeps();
const scopedUiSettingsService = uiSettingsService.asScopedToClient(savedObjectsClient);
return scopedUiSettingsService;
}
}
16 changes: 8 additions & 8 deletions x-pack/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
const page = parseInt(queryPage, 10) || 0;
const size = Math.min(100, parseInt(querySize, 10) || 10);
const jobIds = queryIds ? queryIds.split(',') : null;
const queryProvider = jobsQueryFactory(reporting);
const results = await queryProvider.list(jobTypes, user, page, size, jobIds);
const jobsQuery = jobsQueryFactory(reporting);
const results = await jobsQuery.list(jobTypes, user, page, size, jobIds);

return res.ok({
body: results,
Expand All @@ -68,8 +68,8 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
management: { jobTypes = [] },
} = await reporting.getLicenseInfo();

const queryProvider = jobsQueryFactory(reporting);
const count = await queryProvider.count(jobTypes, user);
const jobsQuery = jobsQueryFactory(reporting);
const count = await jobsQuery.count(jobTypes, user);

return res.ok({
body: count.toString(),
Expand All @@ -96,8 +96,8 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
management: { jobTypes = [] },
} = await reporting.getLicenseInfo();

const queryProvider = jobsQueryFactory(reporting);
const result = await queryProvider.get(user, docId, { includeContent: true });
const jobsQuery = jobsQueryFactory(reporting);
const result = await jobsQuery.get(user, docId, { includeContent: true });

if (!result) {
throw Boom.notFound();
Expand Down Expand Up @@ -136,8 +136,8 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
management: { jobTypes = [] },
} = await reporting.getLicenseInfo();

const queryProvider = jobsQueryFactory(reporting);
const result = await queryProvider.get(user, docId);
const jobsQuery = jobsQueryFactory(reporting);
const result = await jobsQuery.get(user, docId);

if (!result) {
throw Boom.notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function registerReportingUsageCollector(
)
.toPromise();
};
const collectionIsReady = reporting.pluginIsStarted.bind(reporting);
const collectionIsReady = reporting.pluginHasStarted.bind(reporting);

const collector = getReportingUsageCollector(
reporting,
Expand Down

0 comments on commit 26abb7d

Please sign in to comment.