Skip to content

Commit

Permalink
Refactor PngV2 to export type class (#159853)
Browse files Browse the repository at this point in the history
## Summary

Partially addresses #158092

Following the Csv and Pdf reports, this PR continues the process with a
PngExportType class instance vs the getExportType() function. This PR
concludes refactoring the export types to class instances. The next
steps will be migrating these to the reporting export types plugin
#161291 and clean up of tests.

Diagnostic tool is timing out for waitForElements - in this PR this is
commented out since the png v1 endpoint has not been refactored yet.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Timothy Sullivan <[email protected]>
  • Loading branch information
3 people committed Jul 13, 2023
1 parent 65e94c6 commit 851db23
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 161 deletions.
3 changes: 0 additions & 3 deletions x-pack/plugins/reporting/jest.integration.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@ module.exports = {
preset: '@kbn/test/jest_integration',
rootDir: '../../..',
roots: ['<rootDir>/x-pack/plugins/reporting'],
collectCoverageFrom: [
'<rootDir>/x-pack/plugins/reporting/server/export_types/printable_pdf_v2/**/*.{js,ts,tsx}',
],
};
16 changes: 13 additions & 3 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { createConfig, ReportingConfigType } from './config';
import { CsvSearchSourceExportType } from './export_types/csv_searchsource';
import { CsvV2ExportType } from './export_types/csv_v2';
import { PdfExportType } from './export_types/printable_pdf_v2';
import { PngExportType } from './export_types/png_v2';
import { checkLicense, ExportTypesRegistry } from './lib';
import { reportingEventLoggerFactory } from './lib/event_logger/logger';
import type { IReport, ReportingStore } from './lib/store';
Expand Down Expand Up @@ -109,6 +110,8 @@ export class ReportingCore {
private csvSearchSourceExport: CsvSearchSourceExportType;
private csvV2ExportType: CsvV2ExportType;
private pdfExport: PdfExportType;
private pngExport: PngExportType;

private exportTypesRegistry = new ExportTypesRegistry();

public getContract: () => ReportingSetup;
Expand Down Expand Up @@ -138,6 +141,9 @@ export class ReportingCore {
this.pdfExport = new PdfExportType(this.core, this.config, this.logger, this.context);
this.exportTypesRegistry.register(this.pdfExport);

this.pngExport = new PngExportType(this.core, this.config, this.logger, this.context);
this.exportTypesRegistry.register(this.pngExport);

this.deprecatedAllowedRoles = config.roles.enabled ? config.roles.allow : false;
this.executeTask = new ExecuteReportTask(this, config, this.logger);
this.monitorTask = new MonitorReportsTask(this, config, this.logger);
Expand Down Expand Up @@ -166,6 +172,7 @@ export class ReportingCore {
this.csvSearchSourceExport.setup(setupDeps);
this.csvV2ExportType.setup(setupDeps);
this.pdfExport.setup(setupDeps);
this.pngExport.setup(setupDeps);

const { executeTask, monitorTask } = this;
setupDeps.taskManager.registerTaskDefinitions({
Expand All @@ -180,10 +187,13 @@ export class ReportingCore {
public async pluginStart(startDeps: ReportingInternalStart) {
this.pluginStart$.next(startDeps); // trigger the observer
this.pluginStartDeps = startDeps; // cache

const reportingStart = this.getContract();
this.csvSearchSourceExport.start({ ...startDeps, reporting: reportingStart });
this.csvV2ExportType.start({ ...startDeps, reporting: reportingStart });
this.pdfExport.start({ ...startDeps, reporting: reportingStart });
const exportTypeStartDeps = { ...startDeps, reporting: reportingStart };
this.csvSearchSourceExport.start(exportTypeStartDeps);
this.csvV2ExportType.start(exportTypeStartDeps);
this.pdfExport.start(exportTypeStartDeps);
this.pngExport.start(exportTypeStartDeps);

await this.assertKibanaIsAvailable();

Expand Down
20 changes: 0 additions & 20 deletions x-pack/plugins/reporting/server/export_types/png_v2/create_job.ts

This file was deleted.

70 changes: 0 additions & 70 deletions x-pack/plugins/reporting/server/export_types/png_v2/execute_job.ts

This file was deleted.

33 changes: 1 addition & 32 deletions x-pack/plugins/reporting/server/export_types/png_v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,4 @@
* 2.0.
*/

import {
LICENSE_TYPE_ENTERPRISE,
LICENSE_TYPE_GOLD,
LICENSE_TYPE_PLATINUM,
LICENSE_TYPE_CLOUD_STANDARD,
LICENSE_TYPE_TRIAL,
PNG_JOB_TYPE_V2 as jobType,
} from '../../../common/constants';
import { CreateJobFn, ExportTypeDefinition, RunTaskFn } from '../../types';
import { createJobFnFactory } from './create_job';
import { runTaskFnFactory } from './execute_job';
import { metadata } from './metadata';
import { JobParamsPNGV2, TaskPayloadPNGV2 } from './types';

export const getExportType = (): ExportTypeDefinition<
CreateJobFn<JobParamsPNGV2>,
RunTaskFn<TaskPayloadPNGV2>
> => ({
...metadata,
jobType,
jobContentEncoding: 'base64',
jobContentExtension: 'PNG',
createJobFnFactory,
runTaskFnFactory,
validLicenses: [
LICENSE_TYPE_TRIAL,
LICENSE_TYPE_CLOUD_STANDARD,
LICENSE_TYPE_GOLD,
LICENSE_TYPE_PLATINUM,
LICENSE_TYPE_ENTERPRISE,
],
});
export { PngExportType } from './png_v2';
13 changes: 0 additions & 13 deletions x-pack/plugins/reporting/server/export_types/png_v2/metadata.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@
*/

import * as Rx from 'rxjs';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { coreMock, elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
import { Writable } from 'stream';
import { ReportingCore } from '../..';
import { CancellationToken } from '@kbn/reporting-common';
import { ScreenshottingStart } from '@kbn/screenshotting-plugin/server';
import { ReportingCore } from '../..';
import { LocatorParams } from '../../../common/types';
import { cryptoFactory } from '../../lib';
import { createMockConfigSchema, createMockReportingCore } from '../../test_helpers';
import { generatePngObservable } from '../common';
import { runTaskFnFactory } from './execute_job';
import { TaskPayloadPNGV2 } from './types';
import { PngExportType } from './png_v2';

jest.mock('../common/generate_png');

let content: string;
let mockReporting: ReportingCore;
let mockReportingCore: ReportingCore;
let mockPngExportType: PngExportType;
let stream: jest.Mocked<Writable>;

const cancellationToken = {
on: jest.fn(),
} as unknown as CancellationToken;

const getMockLogger = () => loggingSystemMock.createLogger();
const mockLogger = loggingSystemMock.createLogger();

const mockEncryptionKey = 'abcabcsecuresecret';
const encryptHeaders = async (headers: Record<string, string>) => {
Expand All @@ -41,15 +43,31 @@ beforeEach(async () => {
content = '';
stream = { write: jest.fn((chunk) => (content += chunk)) } as unknown as typeof stream;

const mockReportingConfig = createMockConfigSchema({
const configType = createMockConfigSchema({
encryptionKey: mockEncryptionKey,
queue: {
indexInterval: 'daily',
timeout: Infinity,
},
});

mockReporting = await createMockReportingCore(mockReportingConfig);
mockReportingCore = await createMockReportingCore(configType);
const context = coreMock.createPluginInitializerContext(configType);

const mockCoreSetup = coreMock.createSetup();
const mockCoreStart = coreMock.createStart();

mockPngExportType = new PngExportType(mockCoreSetup, configType, mockLogger, context);
mockPngExportType.setup({
basePath: { set: jest.fn() },
});
mockPngExportType.start({
savedObjects: mockCoreStart.savedObjects,
uiSettings: mockCoreStart.uiSettings,
screenshotting: {} as unknown as ScreenshottingStart,
esClient: elasticsearchServiceMock.createClusterClient(),
reporting: mockReportingCore.getContract(),
});
});

afterEach(() => (generatePngObservable as jest.Mock).mockReset());
Expand All @@ -58,9 +76,8 @@ test(`passes browserTimezone to generatePng`, async () => {
const encryptedHeaders = await encryptHeaders({});
(generatePngObservable as jest.Mock).mockReturnValue(Rx.of({ buffer: Buffer.from('') }));

const runTask = runTaskFnFactory(mockReporting, getMockLogger());
const browserTimezone = 'UTC';
await runTask(
await mockPngExportType.runTask(
'pngJobId',
getBasePayload({
forceNow: 'test',
Expand Down Expand Up @@ -89,12 +106,11 @@ test(`passes browserTimezone to generatePng`, async () => {
});

test(`returns content_type of application/png`, async () => {
const runTask = runTaskFnFactory(mockReporting, getMockLogger());
const encryptedHeaders = await encryptHeaders({});

(generatePngObservable as jest.Mock).mockReturnValue(Rx.of({ buffer: Buffer.from('foo') }));

const { content_type: contentType } = await runTask(
const { content_type: contentType } = await mockPngExportType.runTask(
'pngJobId',
getBasePayload({
locatorParams: [{ version: 'test', id: 'test' }] as LocatorParams[],
Expand All @@ -110,9 +126,8 @@ test(`returns content of generatePng getBuffer base64 encoded`, async () => {
const testContent = 'raw string from get_screenhots';
(generatePngObservable as jest.Mock).mockReturnValue(Rx.of({ buffer: Buffer.from(testContent) }));

const runTask = runTaskFnFactory(mockReporting, getMockLogger());
const encryptedHeaders = await encryptHeaders({});
await runTask(
await mockPngExportType.runTask(
'pngJobId',
getBasePayload({
locatorParams: [{ version: 'test', id: 'test' }] as LocatorParams[],
Expand Down
Loading

0 comments on commit 851db23

Please sign in to comment.