Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting/NP Migration] Remove server.expose of ExportTypeRegistry #50973

Merged

Conversation

tsullivan
Copy link
Member

@tsullivan tsullivan commented Nov 18, 2019

Summary

This PR prepares the Reporting plugin for New Platform further, by taking away the export type registry from being "exposed" on the server. Previously, this was done as a utility to access a singleton. Now, the code follows the traditional flow of passing down the object to modules that depend on it.

Some of the modules that depend on export type registry were loaded with oncePerServer, which made the exported modules forced to be functions that could only receive the server object as a parameter. Since that doesn't work with the main idea behind the code change, there are unrelated modules that had to change by no longer being loaded with oncePerServer.

This PR is related to the dependent PRs:

The main changes of this PR is to pass the browserDriverFactory and exportTypesRegistry objects to the module that need them, instead of making those objects available throughout the plugin using server.expose.

Other cleanup-related work:

  • replaced all the export_type/server/index.ts files with export_type/index.ts. The new changes expose an export type definition object, which are imported by the export type registry class, and consumed in its getExportTypesRegistryFn.
    • Why: it was a weird pattern to pass a register function into a server function to have the export defs registered.
  • removed common/export_types_registry and moved its code to server/lib/export_types_registry
    • Why: previously the server/lib registry code would scan a directory for available export types. Now each export type definition is explicitly imported. The scan code is no longer needed and the registry code should be consolidated in server/lib/

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

@tsullivan tsullivan added Feature:NP Migration (Deprecated) Feature:Reporting Use Reporting:Screenshot, Reporting:CSV, or Reporting:Framework instead release_note:skip Skip the PR/issue when compiling release notes Team:Stack Services labels Nov 18, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-stack-services (Team:Stack Services)

@elasticmachine

This comment has been minimized.

@tsullivan tsullivan changed the title [Reporting] Remove server.expose of ExportTypeRegistry [WIP] [Reporting] Remove server.expose of ExportTypeRegistry Nov 18, 2019
@tsullivan tsullivan force-pushed the reporting/new-platform-shim-part-iv branch 5 times, most recently from 0365826 to 47efc80 Compare November 27, 2019 04:05
@elasticmachine

This comment has been minimized.

@tsullivan tsullivan force-pushed the reporting/new-platform-shim-part-iv branch from 47efc80 to 53320f1 Compare December 2, 2019 19:30
@elasticmachine

This comment has been minimized.

@tsullivan tsullivan force-pushed the reporting/new-platform-shim-part-iv branch from 53320f1 to 44166c6 Compare December 3, 2019 00:26
@elasticmachine

This comment has been minimized.

@tsullivan tsullivan force-pushed the reporting/new-platform-shim-part-iv branch from 0d7f588 to 446a9f8 Compare December 3, 2019 19:45
@elasticmachine

This comment has been minimized.

@tsullivan tsullivan force-pushed the reporting/new-platform-shim-part-iv branch from 446a9f8 to 723ab00 Compare December 3, 2019 21:28
@tsullivan tsullivan changed the title [WIP] [Reporting] Remove server.expose of ExportTypeRegistry [Reporting/NP Migration] Remove server.expose of ExportTypeRegistry Dec 3, 2019
@@ -1,64 +0,0 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this out to replace server/lib/export_types_registry

@@ -1,22 +0,0 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced all the export_type/server/index.ts files with export_type/index.ts

}

export const exportTypesRegistryFactory = oncePerServer(exportTypesRegistryFn);
// FIXME: is this the best way to return a singleton?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is no need to memoize this, since exportTypesRegistry is consumed in 1 place

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not instanciate ExportTypesRegistry in the module and export the singleton then? Maybe a little harder for tests though.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine

This comment has been minimized.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

@tsullivan tsullivan requested a review from Bargs December 10, 2019 00:30
@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

@@ -50,3 +50,9 @@ export const PNG_JOB_TYPE = 'PNG';
export const CSV_JOB_TYPE = 'csv';
export const CSV_FROM_SAVEDOBJECT_JOB_TYPE = 'csv_from_savedobject';
export const USES_HEADLESS_JOB_TYPES = [PDF_JOB_TYPE, PNG_JOB_TYPE];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@tsullivan tsullivan requested a review from a team December 10, 2019 21:31
@tsullivan
Copy link
Member Author

@elastic/kibana-platform would you be able to lend someone to give a 2nd review on this PR?

As it has a lot of refactoring, there is some context required, but the main part of the changes are to support getting rid of our server.expose usage.

Copy link
Contributor

@pgayvallet pgayvallet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have all the functional context, but PR does what is described and it seems to be going in the right direction for NP migration.

Some comments:

Comment on lines +54 to +58
export const LICENSE_TYPE_TRIAL = 'trial';
export const LICENSE_TYPE_BASIC = 'basic';
export const LICENSE_TYPE_STANDARD = 'standard';
export const LICENSE_TYPE_GOLD = 'gold';
export const LICENSE_TYPE_PLATINUM = 'platinum';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use LicenseType from x-pack/plugins/licensing/server instead or redeclaring them

// => ExportTypeDefinition<T, U, V, W>

export class ExportTypesRegistry {
private _map: Map<string, ExportTypeDefinition<any, any, any, any>> = new Map();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: should probably be unknown instead of any

}

export const exportTypesRegistryFactory = oncePerServer(exportTypesRegistryFn);
// FIXME: is this the best way to return a singleton?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not instanciate ExportTypesRegistry in the module and export the singleton then? Maybe a little harder for tests though.

Comment on lines +29 to +30
const config = server.config();
const DOWNLOAD_BASE_URL = config.get('server.basePath') + `${API_BASE_URL}/jobs/download`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could access this from NP with core.http.basePath.serverBasePath instead of relying on the legacy config.

Comment on lines +62 to +73
function handleError(exportTypeId: string, err: Error) {
if (err instanceof esErrors['401']) {
return boom.unauthorized(`Sorry, you aren't authenticated`);
}
if (err instanceof esErrors['403']) {
return boom.forbidden(`Sorry, you are not authorized to create ${exportTypeId} reports`);
}
if (err instanceof esErrors['404']) {
return boom.boomify(err, { statusCode: 404 });
}
return err;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info for later: Boom errors are not handled by default by NP router. You will have to user router.handleLegacyErrors when you'll migrate your routes (or use proper NP response errors)

@tsullivan tsullivan merged commit 711b44b into elastic:master Dec 11, 2019
@tsullivan
Copy link
Member Author

Thanks for the insightful feedback, @pgayvallet ! I think your comments should come in as changes in forthcoming PRs

@tsullivan tsullivan deleted the reporting/new-platform-shim-part-iv branch December 11, 2019 19:42
tsullivan added a commit to tsullivan/kibana that referenced this pull request Dec 11, 2019
…lastic#50973)

* [Reporting/NPMigration] typescriptify ExportTypeRegistry, remove from server.expose

* Minor routes registration cleanup

* move the ETR test file

* Re-pack the route registration, reduce LOC changes

* add EnqueueJobFn type

* Fix usage collector test

* remove a throw error used for development/debugging

* fix imports error

* Fix execute job tests

* wip test fixes

* test fixes for real

* fix more tests

* fix diffs

* Add TODOs about the ExportTypesRegistry.register unwrap the factory functions.

* really make headlessbrowserdriver required as an execute job factory option

* fix tests

* Use constants for license type keywords
tsullivan added a commit that referenced this pull request Dec 12, 2019
…50973) (#52813)

* [Reporting/NPMigration] typescriptify ExportTypeRegistry, remove from server.expose

* Minor routes registration cleanup

* move the ETR test file

* Re-pack the route registration, reduce LOC changes

* add EnqueueJobFn type

* Fix usage collector test

* remove a throw error used for development/debugging

* fix imports error

* Fix execute job tests

* wip test fixes

* test fixes for real

* fix more tests

* fix diffs

* Add TODOs about the ExportTypesRegistry.register unwrap the factory functions.

* really make headlessbrowserdriver required as an execute job factory option

* fix tests

* Use constants for license type keywords
@tsullivan
Copy link
Member Author

#53898

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
(Deprecated) Feature:Reporting Use Reporting:Screenshot, Reporting:CSV, or Reporting:Framework instead Feature:NP Migration release_note:skip Skip the PR/issue when compiling release notes v7.6.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants