Skip to content

Commit

Permalink
schema utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Sep 16, 2020
1 parent 5319844 commit 2fdaf88
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 41 deletions.
36 changes: 36 additions & 0 deletions x-pack/plugins/reporting/common/schema_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ByteSizeValue } from '@kbn/config-schema';
import moment from 'moment';

/*
* For cleaner code: use these functions when a config schema value could be
* one type or another. This allows you to treat the value as one type.
*/

export const durationToNumber = (value: number | moment.Duration): number => {
if (typeof value === 'number') {
return value;
}
return value.asMilliseconds();
};

export const numberToByteSizeValue = (value: number | ByteSizeValue) => {
if (typeof value === 'number') {
return new ByteSizeValue(value);
}

return value;
};

export const byteSizeValueToNumber = (value: number | ByteSizeValue) => {
if (typeof value === 'number') {
return value;
}

return value.getValueInBytes();
};
8 changes: 3 additions & 5 deletions x-pack/plugins/reporting/public/components/report_listing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import {
EuiBasicTable,
EuiFlexItem,
EuiFlexGroup,
EuiFlexItem,
EuiPageContent,
EuiSpacer,
EuiText,
Expand All @@ -23,6 +23,7 @@ import { Subscription } from 'rxjs';
import { ApplicationStart, ToastsSetup } from 'src/core/public';
import { ILicense, LicensingPluginSetup } from '../../../licensing/public';
import { Poller } from '../../common/poller';
import { durationToNumber } from '../../common/schema_utils';
import { JobStatuses } from '../../constants';
import { checkLicense } from '../lib/license_check';
import { JobQueueEntry, ReportingAPIClient } from '../lib/reporting_api_client';
Expand Down Expand Up @@ -185,10 +186,7 @@ class ReportListingUi extends Component<Props, State> {
this.mounted = true;
const { pollConfig, license$ } = this.props;

const pollFrequencyInMillis =
typeof pollConfig.jobsRefresh.interval === 'number'
? pollConfig.jobsRefresh.interval
: pollConfig.jobsRefresh.interval.asMilliseconds();
const pollFrequencyInMillis = durationToNumber(pollConfig.jobsRefresh.interval);
this.poller = new Poller({
functionToPoll: () => {
return this.fetchJobs();
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { ManagementSetup } from '../../../../src/plugins/management/public';
import { SharePluginSetup } from '../../../../src/plugins/share/public';
import { LicensingPluginSetup } from '../../licensing/public';
import { durationToNumber } from '../common/schema_utils';
import { JobId, JobStatusBuckets, ReportingConfigType } from '../common/types';
import { JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY } from '../constants';
import { getGeneralErrorToast } from './components';
Expand Down Expand Up @@ -158,8 +159,7 @@ export class ReportingPublicPlugin implements Plugin<void, void> {
const { http, notifications } = core;
const apiClient = new ReportingAPIClient(http);
const streamHandler = new StreamHandler(notifications, apiClient);
const { interval: intervalRaw } = this.config.poll.jobsRefresh;
const interval = typeof intervalRaw === 'number' ? intervalRaw : intervalRaw.asMilliseconds();
const interval = durationToNumber(this.config.poll.jobsRefresh.interval);
Rx.timer(0, interval)
.pipe(
takeUntil(this.stop$), // stop the interval when stop method is called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { InnerSubscriber } from 'rxjs/internal/InnerSubscriber';
import { ignoreElements, map, mergeMap, tap } from 'rxjs/operators';
import { getChromiumDisconnectedError } from '../';
import { BROWSER_TYPE } from '../../../../common/constants';
import { durationToNumber } from '../../../../common/schema_utils';
import { CaptureConfig } from '../../../../server/types';
import { LevelLogger } from '../../../lib';
import { safeChildProcess } from '../../safe_child_process';
Expand Down Expand Up @@ -90,9 +91,7 @@ export class HeadlessChromiumDriverFactory {

// Set the default timeout for all navigation methods to the openUrl timeout (30 seconds)
// All waitFor methods have their own timeout config passed in to them
const timeoutRaw = this.captureConfig.timeouts.openUrl;
const timeout = typeof timeoutRaw === 'number' ? timeoutRaw : timeoutRaw.asMilliseconds();
page.setDefaultTimeout(timeout);
page.setDefaultTimeout(durationToNumber(this.captureConfig.timeouts.openUrl));

logger.debug(`Browser page driver created`);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ByteSizeValue } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { IUiSettingsClient } from 'src/core/server';
import { ReportingConfig } from '../../../';
import { CancellationToken } from '../../../../../../plugins/reporting/common';
import { CSV_BOM_CHARS } from '../../../../common/constants';
import { byteSizeValueToNumber } from '../../../../common/schema_utils';
import { LevelLogger } from '../../../lib';
import { getFieldFormats } from '../../../services';
import { IndexPatternSavedObject, SavedSearchGeneratorResult } from '../types';
Expand Down Expand Up @@ -47,13 +47,6 @@ export interface GenerateCsvParams {
conflictedTypesFields: string[];
}

const getBytes = (sizeBytes: number | ByteSizeValue): number => {
if (typeof sizeBytes === 'number') {
return sizeBytes;
}
return sizeBytes.getValueInBytes();
};

export function createGenerateCsv(logger: LevelLogger) {
const hitIterator = createHitIterator(logger);

Expand All @@ -72,7 +65,7 @@ export function createGenerateCsv(logger: LevelLogger) {
);
const escapeValue = createEscapeValue(settings.quoteValues, settings.escapeFormulaValues);
const bom = config.get('csv', 'useByteOrderMarkEncoding') ? CSV_BOM_CHARS : '';
const builder = new MaxSizeStringBuilder(getBytes(settings.maxSizeBytes), bom);
const builder = new MaxSizeStringBuilder(byteSizeValueToNumber(settings.maxSizeBytes), bom);

const { fields, metaFields, conflictedTypesFields } = job;
const header = `${fields.map(escapeValue).join(settings.separator)}\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

import { i18n } from '@kbn/i18n';
import { durationToNumber } from '../../../common/schema_utils';
import { LevelLogger, startTrace } from '../';
import { HeadlessChromiumDriver } from '../../browsers';
import { CaptureConfig } from '../../types';
import { LevelLogger, startTrace } from '../';
import { LayoutInstance } from '../layouts';
import { CONTEXT_GETNUMBEROFITEMS, CONTEXT_READMETADATA } from './constants';

Expand All @@ -31,8 +32,7 @@ export const getNumberOfItems = async (
// the dashboard is using the `itemsCountAttribute` attribute to let us
// know how many items to expect since gridster incrementally adds panels
// we have to use this hint to wait for all of them
const timeoutRaw = captureConfig.timeouts.waitForElements;
const timeout = typeof timeoutRaw === 'number' ? timeoutRaw : timeoutRaw.asMilliseconds();
const timeout = durationToNumber(captureConfig.timeouts.waitForElements);
await browser.waitForSelector(
`${renderCompleteSelector},[${itemsCountAttribute}]`,
{ timeout },
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/reporting/server/lib/screenshots/open_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

import { i18n } from '@kbn/i18n';
import { durationToNumber } from '../../../common/schema_utils';
import { LevelLogger, startTrace } from '../';
import { HeadlessChromiumDriver } from '../../browsers';
import { CaptureConfig, ConditionalHeaders } from '../../types';
import { LevelLogger, startTrace } from '../';

export const openUrl = async (
captureConfig: CaptureConfig,
Expand All @@ -19,8 +20,7 @@ export const openUrl = async (
): Promise<void> => {
const endTrace = startTrace('open_url', 'wait');
try {
const timeoutRaw = captureConfig.timeouts.openUrl;
const timeout = typeof timeoutRaw === 'number' ? timeoutRaw : timeoutRaw.asMilliseconds();
const timeout = durationToNumber(captureConfig.timeouts.openUrl);
await browser.open(
url,
{ conditionalHeaders, waitForSelector: pageLoadSelector, timeout },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
*/

import { i18n } from '@kbn/i18n';
import { Duration } from 'moment';
import { durationToNumber } from '../../../common/schema_utils';
import { HeadlessChromiumDriver } from '../../browsers';
import { CaptureConfig } from '../../types';
import { LevelLogger, startTrace } from '../';
import { LayoutInstance } from '../layouts';
import { CONTEXT_WAITFORRENDER } from './constants';

const toMilliseconds = (rawValue: number | Duration) => {
return typeof rawValue === 'number' ? rawValue : rawValue.asMilliseconds();
};

export const waitForRenderComplete = async (
captureConfig: CaptureConfig,
browser: HeadlessChromiumDriver,
Expand Down Expand Up @@ -72,7 +68,7 @@ export const waitForRenderComplete = async (

return Promise.all(renderedTasks).then(hackyWaitForVisualizations);
},
args: [layout.selectors.renderComplete, toMilliseconds(captureConfig.loadDelay)],
args: [layout.selectors.renderComplete, durationToNumber(captureConfig.loadDelay)],
},
{ context: CONTEXT_WAITFORRENDER },
logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/

import { i18n } from '@kbn/i18n';
import { HeadlessChromiumDriver } from '../../browsers';
import { durationToNumber } from '../../../common/schema_utils';
import { LevelLogger, startTrace } from '../';
import { HeadlessChromiumDriver } from '../../browsers';
import { CaptureConfig } from '../../types';
import { LayoutInstance } from '../layouts';
import { CONTEXT_WAITFORELEMENTSTOBEINDOM } from './constants';
Expand Down Expand Up @@ -40,8 +41,7 @@ export const waitForVisualizations = async (
);

try {
const timeoutRaw = captureConfig.timeouts.renderComplete;
const timeout = typeof timeoutRaw === 'number' ? timeoutRaw : timeoutRaw.asMilliseconds();
const timeout = durationToNumber(captureConfig.timeouts.renderComplete);
await browser.waitFor(
{ fn: getCompletedItemsCount, args: [{ renderCompleteSelector }], toEqual, timeout },
{ context: CONTEXT_WAITFORELEMENTSTOBEINDOM },
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/server/lib/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { ElasticsearchServiceSetup } from 'src/core/server';
import { durationToNumber } from '../../../common/schema_utils';
import { LevelLogger, statuses } from '../';
import { ReportingCore } from '../../';
import { BaseParams, BaseParamsEncryptedFields, ReportingUser } from '../../types';
Expand Down Expand Up @@ -47,9 +48,8 @@ export class ReportingStore {
this.indexPrefix = config.get('index');
this.indexInterval = config.get('queue', 'indexInterval');

const timeoutRaw = config.get('queue', 'timeout');
this.jobSettings = {
timeout: typeof timeoutRaw === 'number' ? timeoutRaw : timeoutRaw.asMilliseconds(),
timeout: durationToNumber(config.get('queue', 'timeout')),
browser_type: config.get('capture', 'browser', 'type'),
max_attempts: config.get('capture', 'maxAttempts'),
priority: 10, // unused
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/reporting/server/routes/diagnostic/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n';
import { defaults, get } from 'lodash';
import { ReportingCore } from '../..';
import { API_DIAGNOSE_URL } from '../../../common/constants';
import { numberToByteSizeValue } from '../../../common/schema_utils';
import { LevelLogger as Logger } from '../../lib';
import { DiagnosticResponse } from '../../types';
import { authorizedUserPreRoutingFactory } from '../lib/authorized_user_pre_routing';
Expand Down Expand Up @@ -43,11 +44,7 @@ export const registerDiagnoseConfig = (reporting: ReportingCore, logger: Logger)
'100mb'
);
const elasticSearchMaxContentBytes = ByteSizeValue.parse(elasticSearchMaxContent);
const kibanaMaxContentBytesRaw = config.get('csv', 'maxSizeBytes');
const kibanaMaxContentBytes =
typeof kibanaMaxContentBytesRaw === 'number'
? new ByteSizeValue(kibanaMaxContentBytesRaw)
: kibanaMaxContentBytesRaw;
const kibanaMaxContentBytes = numberToByteSizeValue(config.get('csv', 'maxSizeBytes'));

if (kibanaMaxContentBytes.isGreaterThan(elasticSearchMaxContentBytes)) {
const maxContentSizeWarning = i18n.translate(
Expand Down

0 comments on commit 2fdaf88

Please sign in to comment.