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] Consolidate Server Type Defs, move some out of Legacy #66144

Merged
merged 3 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion x-pack/legacy/plugins/reporting/common/get_absolute_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
*/

import url from 'url';
import { AbsoluteURLFactoryOptions } from '../types';

interface AbsoluteURLFactoryOptions {
defaultBasePath: string;
protocol: string;
hostname: string;
port: string | number;
}

export const getAbsoluteUrlFactory = ({
protocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export const LayoutTypes = {
PRESERVE_LAYOUT: 'preserve_layout',
PRINT: 'print',
};

export const DEFAULT_PAGELOAD_SELECTOR = '.application';
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { cryptoFactory } from '../../../server/lib/crypto';
import { Logger } from '../../../types';
import { cryptoFactory, LevelLogger } from '../../../server/lib';
import { decryptJobHeaders } from './decrypt_job_headers';

const encryptHeaders = async (encryptionKey: string, headers: Record<string, string>) => {
Expand All @@ -23,7 +22,7 @@ describe('headers', () => {
},
logger: ({
error: jest.fn(),
} as unknown) as Logger,
} as unknown) as LevelLogger,
});
await expect(getDecryptedHeaders()).rejects.toMatchInlineSnapshot(
`[Error: Failed to decrypt report job data. Please ensure that xpack.reporting.encryptionKey is set and re-generate this report. Error: Invalid IV length]`
Expand All @@ -44,7 +43,7 @@ describe('headers', () => {
type: 'csv',
headers: encryptedHeaders,
},
logger: {} as Logger,
logger: {} as LevelLogger,
});
expect(decryptedHeaders).toEqual(headers);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

import { i18n } from '@kbn/i18n';
import { cryptoFactory } from '../../../server/lib/crypto';
import { Logger } from '../../../types';
import { cryptoFactory, LevelLogger } from '../../../server/lib';

interface HasEncryptedHeaders {
headers?: string;
Expand All @@ -23,7 +22,7 @@ export const decryptJobHeaders = async <
}: {
encryptionKey?: string;
job: JobDocPayloadType;
logger: Logger;
logger: LevelLogger;
}): Promise<Record<string, string>> => {
try {
if (typeof job.headers !== 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

import sinon from 'sinon';
import { ReportingConfig, ReportingCore } from '../../../server';
import { JobDocPayload } from '../../../server/types';
import { createMockReportingCore } from '../../../test_helpers';
import { ReportingConfig, ReportingCore } from '../../../server/types';
import { JobDocPayload } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

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 { ReportingConfig } from '../../../server/types';
import { ConditionalHeaders } from '../../../types';
import { ReportingConfig } from '../../../server';
import { ConditionalHeaders } from '../../../server/types';

export const getConditionalHeaders = <JobDocPayloadType>({
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import { UI_SETTINGS_CUSTOM_PDF_LOGO } from '../../../common/constants';
import { ReportingConfig, ReportingCore } from '../../../server/types';
import { ConditionalHeaders } from '../../../types';
import { ReportingConfig, ReportingCore } from '../../../server';
import { ConditionalHeaders } from '../../../server/types';
import { JobDocPayloadPDF } from '../../printable_pdf/types'; // Logo is PDF only

export const getCustomLogo = async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'url';
import { getAbsoluteUrlFactory } from '../../../common/get_absolute_url';
import { validateUrls } from '../../../common/validate_urls';
import { ReportingConfig } from '../../../server/types';
import { ReportingConfig } from '../../../server';
import { JobDocPayloadPNG } from '../../png/types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';

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

import { CaptureConfig } from '../../../server/types';
import { LayoutTypes } from '../constants';
import { Layout, LayoutParams } from './layout';
import { LayoutParams, LayoutTypes } from './';
import { Layout } from './layout';
import { PreserveLayout } from './preserve_layout';
import { PrintLayout } from './print_layout';

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

import { HeadlessChromiumDriver } from '../../../server/browsers';
import { LevelLogger } from '../../../server/lib';
import { Layout } from './layout';

export { createLayout } from './create_layout';
export { PrintLayout } from './print_layout';
export { Layout } from './layout';
export { PreserveLayout } from './preserve_layout';
export { PrintLayout } from './print_layout';

export const LayoutTypes = {
PRESERVE_LAYOUT: 'preserve_layout',
PRINT: 'print',
};

export const getDefaultLayoutSelectors = (): LayoutSelectorDictionary => ({
screenshot: '[data-shared-items-container]',
renderComplete: '[data-shared-item]',
itemsCountAttribute: 'data-shared-items-count',
timefilterDurationAttribute: 'data-shared-timefilter-duration',
});

export interface PageSizeParams {
pageMarginTop: number;
pageMarginBottom: number;
pageMarginWidth: number;
tableBorderWidth: number;
headingHeight: number;
subheadingHeight: number;
}

export interface LayoutSelectorDictionary {
screenshot: string;
renderComplete: string;
itemsCountAttribute: string;
timefilterDurationAttribute: string;
}

export interface PdfImageSize {
width: number;
height?: number;
}

export interface Size {
width: number;
height: number;
}

export interface LayoutParams {
id: string;
dimensions: Size;
}

interface LayoutSelectors {
// Fields that are not part of Layout: the instances
// independently implement these fields on their own
selectors: LayoutSelectorDictionary;
positionElements?: (browser: HeadlessChromiumDriver, logger: LevelLogger) => Promise<void>;
}

export type LayoutInstance = Layout & LayoutSelectors & Size;
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HeadlessChromiumDriver } from '../../../server/browsers/chromium/driver';
import { LevelLogger } from '../../../server/lib';
import { PageSizeParams, PdfImageSize, Size } from './';

export interface ViewZoomWidthHeight {
zoom: number;
width: number;
height: number;
}

export interface PageSizeParams {
pageMarginTop: number;
pageMarginBottom: number;
pageMarginWidth: number;
tableBorderWidth: number;
headingHeight: number;
subheadingHeight: number;
}

export interface LayoutSelectorDictionary {
screenshot: string;
renderComplete: string;
itemsCountAttribute: string;
timefilterDurationAttribute: string;
}

export interface PdfImageSize {
width: number;
height?: number;
}

export const getDefaultLayoutSelectors = (): LayoutSelectorDictionary => ({
screenshot: '[data-shared-items-container]',
renderComplete: '[data-shared-item]',
itemsCountAttribute: 'data-shared-items-count',
timefilterDurationAttribute: 'data-shared-timefilter-duration',
});

export abstract class Layout {
public id: string = '';

Expand All @@ -62,22 +33,3 @@ export abstract class Layout {

public abstract getCssOverridesPath(): string;
}

export interface Size {
width: number;
height: number;
}

export interface LayoutParams {
id: string;
dimensions: Size;
}

interface LayoutSelectors {
// Fields that are not part of Layout: the instances
// independently implement these fields on their own
selectors: LayoutSelectorDictionary;
positionElements?: (browser: HeadlessChromiumDriver, logger: LevelLogger) => Promise<void>;
}

export type LayoutInstance = Layout & LayoutSelectors & Size;
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import path from 'path';
import { LayoutTypes } from '../constants';
import {
getDefaultLayoutSelectors,
Layout,
LayoutSelectorDictionary,
LayoutTypes,
PageSizeParams,
Size,
} from './layout';
} from './';

// We use a zoom of two to bump up the resolution of the screenshot a bit.
const ZOOM: number = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import path from 'path';
import { EvaluateFn, SerializableOrJSHandle } from 'puppeteer';
import { HeadlessChromiumDriver } from '../../../server/browsers';
import { LevelLogger } from '../../../server/lib';
import { HeadlessChromiumDriver } from '../../../server/browsers';
import { CaptureConfig } from '../../../server/types';
import { LayoutTypes } from '../constants';
import { getDefaultLayoutSelectors, Layout, LayoutSelectorDictionary, Size } from './layout';
import { getDefaultLayoutSelectors, LayoutSelectorDictionary, Size, LayoutTypes } from './';
import { Layout } from './layout';

export class PrintLayout extends Layout {
public readonly selectors: LayoutSelectorDictionary = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
*/

import { i18n } from '@kbn/i18n';
import { HeadlessChromiumDriver as HeadlessBrowser } from '../../../../server/browsers';
import { LevelLogger as Logger, startTrace } from '../../../../server/lib';
import { LayoutInstance } from '../../layouts/layout';
import { HeadlessChromiumDriver } from '../../../../server/browsers';
import { LevelLogger, startTrace } from '../../../../server/lib';
import { AttributesMap, ElementsPositionAndAttribute } from '../../../../server/types';
import { LayoutInstance } from '../../layouts';
import { CONTEXT_ELEMENTATTRIBUTES } from './constants';
import { AttributesMap, ElementsPositionAndAttribute } from './types';

export const getElementPositionAndAttributes = async (
browser: HeadlessBrowser,
browser: HeadlessChromiumDriver,
layout: LayoutInstance,
logger: Logger
logger: LevelLogger
): Promise<ElementsPositionAndAttribute[] | null> => {
const endTrace = startTrace('get_element_position_data', 'read');
const { screenshot: screenshotSelector } = layout.selectors; // data-shared-items-container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import { i18n } from '@kbn/i18n';
import { HeadlessChromiumDriver as HeadlessBrowser } from '../../../../server/browsers';
import { LevelLogger, startTrace } from '../../../../server/lib';
import { HeadlessChromiumDriver } from '../../../../server/browsers';
import { CaptureConfig } from '../../../../server/types';
import { LayoutInstance } from '../../layouts/layout';
import { LayoutInstance } from '../../layouts';
import { CONTEXT_GETNUMBEROFITEMS, CONTEXT_READMETADATA } from './constants';

export const getNumberOfItems = async (
captureConfig: CaptureConfig,
browser: HeadlessBrowser,
browser: HeadlessChromiumDriver,
layout: LayoutInstance,
logger: LevelLogger
): Promise<number> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import { i18n } from '@kbn/i18n';
import { HeadlessChromiumDriver as HeadlessBrowser } from '../../../../server/browsers';
import { HeadlessChromiumDriver } from '../../../../server/browsers';
import { LevelLogger, startTrace } from '../../../../server/lib';
import { Screenshot, ElementsPositionAndAttribute } from './types';
import { ElementsPositionAndAttribute, Screenshot } from '../../../../server/types';

export const getScreenshots = async (
browser: HeadlessBrowser,
browser: HeadlessChromiumDriver,
elementsPositionAndAttributes: ElementsPositionAndAttribute[],
logger: LevelLogger
): Promise<Screenshot[]> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HeadlessChromiumDriver as HeadlessBrowser } from '../../../../server/browsers';
import { LevelLogger, startTrace } from '../../../../server/lib';
import { LayoutInstance } from '../../layouts/layout';
import { HeadlessChromiumDriver } from '../../../../server/browsers';
import { LayoutInstance } from '../../layouts';
import { CONTEXT_GETTIMERANGE } from './constants';
import { TimeRange } from './types';

export const getTimeRange = async (
browser: HeadlessBrowser,
browser: HeadlessChromiumDriver,
layout: LayoutInstance,
logger: LevelLogger
): Promise<TimeRange | null> => {
): Promise<string | null> => {
const endTrace = startTrace('get_time_range', 'read');
logger.debug('getting timeRange');

const timeRange: TimeRange | null = await browser.evaluate(
const timeRange = await browser.evaluate(
{
fn: durationAttribute => {
const durationElement = document.querySelector(`[${durationAttribute}]`);
Expand All @@ -32,7 +31,7 @@ export const getTimeRange = async (
return null;
}

return { duration };
return duration; // user-friendly date string
},
args: [layout.selectors.timefilterDurationAttribute],
},
Expand All @@ -41,7 +40,7 @@ export const getTimeRange = async (
);

if (timeRange) {
logger.info(`timeRange: ${timeRange.duration}`);
logger.info(`timeRange: ${timeRange}`);
} else {
logger.debug('no timeRange');
}
Expand Down
Loading
Loading