Skip to content

Commit

Permalink
[Reporting] Integration polling config with client code (#63754) (#64093
Browse files Browse the repository at this point in the history
)

* source new platform for reporting ui config

* fix test types

* fix type check

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
tsullivan and elasticmachine authored Apr 21, 2020
1 parent 0c4d33c commit 2e97a9b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 71 deletions.
7 changes: 7 additions & 0 deletions x-pack/plugins/reporting/common/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { ConfigType } from '../server/config';
7 changes: 0 additions & 7 deletions x-pack/plugins/reporting/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
export const JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY =
'xpack.reporting.jobCompletionNotifications';

export const JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG = {
jobCompletionNotifier: {
interval: 10000,
intervalErrorMultiplier: 5,
},
};

// Routes
export const API_BASE_URL = '/api/reporting';
export const API_LIST_URL = `${API_BASE_URL}/jobs`;
Expand Down
12 changes: 0 additions & 12 deletions x-pack/plugins/reporting/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import {
CoreSetup,
CoreStart,
HttpSetup,
Plugin,
PluginInitializerContext,
NotificationsStart,
} from '../../../src/core/public';

export type JobId = string;
export type JobStatus =
| 'completed'
Expand All @@ -21,9 +12,6 @@ export type JobStatus =
| 'processing'
| 'failed';

export type HttpService = HttpSetup;
export type NotificationsService = NotificationsStart;

export interface SourceJob {
_id: JobId;
_source: {
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/reporting/public/components/report_listing.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ const toasts = {
addDanger: jest.fn(),
} as any;

const mockPollConfig = {
jobCompletionNotifier: {
interval: 5000,
intervalErrorMultiplier: 3,
},
jobsRefresh: {
interval: 5000,
intervalErrorMultiplier: 3,
},
};

describe('ReportListing', () => {
it('Report job listing with some items', () => {
const wrapper = mountWithIntl(
<ReportListing
apiClient={reportingAPIClient as ReportingAPIClient}
license$={license$}
pollConfig={mockPollConfig}
redirect={jest.fn()}
toasts={toasts}
/>
Expand All @@ -74,6 +86,7 @@ describe('ReportListing', () => {
<ReportListing
apiClient={reportingAPIClient as ReportingAPIClient}
license$={subMock as Observable<ILicense>}
pollConfig={mockPollConfig}
redirect={jest.fn()}
toasts={toasts}
/>
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/reporting/public/components/report_listing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react';
import { get } from 'lodash';
import moment from 'moment';
import { Component, Fragment, default as React } from 'react';
import { Component, default as React, Fragment } from 'react';
import { Subscription } from 'rxjs';
import { ApplicationStart, ToastsSetup } from 'src/core/public';
import { ILicense, LicensingPluginSetup } from '../../../licensing/public';
import { Poller } from '../../common/poller';
import { JobStatuses, JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG } from '../../constants';
import { JobStatuses } from '../../constants';
import { checkLicense } from '../lib/license_check';
import { JobQueueEntry, ReportingAPIClient } from '../lib/reporting_api_client';
import { ClientConfigType } from '../plugin';
import {
ReportDeleteButton,
ReportDownloadButton,
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface Props {
intl: InjectedIntl;
apiClient: ReportingAPIClient;
license$: LicensingPluginSetup['license$'];
pollConfig: ClientConfigType['poll'];
redirect: ApplicationStart['navigateToApp'];
toasts: ToastsSetup;
}
Expand Down Expand Up @@ -167,12 +169,10 @@ class ReportListingUi extends Component<Props, State> {
functionToPoll: () => {
return this.fetchJobs();
},
pollFrequencyInMillis:
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG.jobCompletionNotifier.interval,
pollFrequencyInMillis: this.props.pollConfig.jobsRefresh.interval,
trailing: false,
continuePollingOnError: true,
pollFrequencyErrorMultiplier:
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG.jobCompletionNotifier.intervalErrorMultiplier,
pollFrequencyErrorMultiplier: this.props.pollConfig.jobsRefresh.intervalErrorMultiplier,
});
this.poller.start();
this.licenseSubscription = this.props.license$.subscribe(this.licenseHandler);
Expand Down
19 changes: 6 additions & 13 deletions x-pack/plugins/reporting/public/lib/stream_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import * as Rx from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { NotificationsSetup } from 'src/core/public';
import {
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY,
JOB_STATUS_COMPLETED,
JOB_STATUS_FAILED,
JOB_STATUS_WARNINGS,
} from '../../constants';

import {
JobId,
JobSummary,
JobStatusBuckets,
NotificationsService,
SourceJob,
} from '../../index.d';

import { JobId, JobStatusBuckets, JobSummary, SourceJob } from '../../index.d';
import {
getSuccessToast,
getFailureToast,
getGeneralErrorToast,
getSuccessToast,
getWarningFormulasToast,
getWarningMaxSizeToast,
getGeneralErrorToast,
} from '../components';
import { ReportingAPIClient } from './reporting_api_client';

Expand All @@ -47,7 +40,7 @@ function summarizeJob(src: SourceJob): JobSummary {
}

export class ReportingNotifierStreamHandler {
constructor(private notifications: NotificationsService, private apiClient: ReportingAPIClient) {}
constructor(private notifications: NotificationsSetup, private apiClient: ReportingAPIClient) {}

/*
* Use Kibana Toast API to show our messages
Expand Down
67 changes: 34 additions & 33 deletions x-pack/plugins/reporting/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,50 @@
* you may not use this file except in compliance with the Elastic License.
*/

import * as Rx from 'rxjs';
import { i18n } from '@kbn/i18n';
import { I18nProvider } from '@kbn/i18n/react';
import React from 'react';
import ReactDOM from 'react-dom';
import * as Rx from 'rxjs';
import { catchError, filter, map, mergeMap, takeUntil } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import {
CoreSetup,
CoreStart,
NotificationsSetup,
Plugin,
PluginInitializerContext,
} from 'src/core/public';
import { ManagementSetup } from 'src/plugins/management/public';
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';
import { I18nProvider } from '@kbn/i18n/react';
import { UiActionsSetup } from 'src/plugins/ui_actions/public';

import { ReportListing } from './components/report_listing';
import { getGeneralErrorToast } from './components';

import { ReportingNotifierStreamHandler as StreamHandler } from './lib/stream_handler';
import { ReportingAPIClient } from './lib/reporting_api_client';
import { GetCsvReportPanelAction } from './panel_actions/get_csv_panel_action';
import { csvReportingProvider } from './share_context_menu/register_csv_reporting';
import { reportingPDFPNGProvider } from './share_context_menu/register_pdf_png_reporting';

import { LicensingPluginSetup } from '../../licensing/public';
import { JobId, JobStatusBuckets } from '../';
import { CONTEXT_MENU_TRIGGER } from '../../../../src/plugins/embeddable/public';
import { SharePluginSetup } from '../../../../src/plugins/share/public';

import {
FeatureCatalogueCategory,
HomePublicPluginSetup,
} from '../../../../src/plugins/home/public';
import { SharePluginSetup } from '../../../../src/plugins/share/public';
import { LicensingPluginSetup } from '../../licensing/public';
import { ConfigType } from '../common/types';
import { JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY } from '../constants';
import { getGeneralErrorToast } from './components';
import { ReportListing } from './components/report_listing';
import { ReportingAPIClient } from './lib/reporting_api_client';
import { ReportingNotifierStreamHandler as StreamHandler } from './lib/stream_handler';
import { GetCsvReportPanelAction } from './panel_actions/get_csv_panel_action';
import { csvReportingProvider } from './share_context_menu/register_csv_reporting';
import { reportingPDFPNGProvider } from './share_context_menu/register_pdf_png_reporting';

import {
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG,
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY,
} from '../constants';

import { JobId, JobStatusBuckets, NotificationsService } from '..';

const {
jobCompletionNotifier: { interval: JOBS_REFRESH_INTERVAL },
} = JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG;
export interface ClientConfigType {
poll: ConfigType['poll'];
}

function getStored(): JobId[] {
const sessionValue = sessionStorage.getItem(JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY);
return sessionValue ? JSON.parse(sessionValue) : [];
}

function handleError(
notifications: NotificationsService,
notifications: NotificationsSetup,
err: Error
): Rx.Observable<JobStatusBuckets> {
notifications.toasts.addDanger(
Expand All @@ -64,18 +62,19 @@ function handleError(
return Rx.of({ completed: [], failed: [] });
}

export class ReportingPublicPlugin implements Plugin<any, any> {
export class ReportingPublicPlugin implements Plugin<void, void> {
private config: ClientConfigType;
private readonly stop$ = new Rx.ReplaySubject(1);

private readonly title = i18n.translate('xpack.reporting.management.reportingTitle', {
defaultMessage: 'Reporting',
});

private readonly breadcrumbText = i18n.translate('xpack.reporting.breadcrumb', {
defaultMessage: 'Reporting',
});

constructor(initializerContext: PluginInitializerContext) {}
constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.get<ClientConfigType>();
}

public setup(
core: CoreSetup,
Expand Down Expand Up @@ -130,6 +129,7 @@ export class ReportingPublicPlugin implements Plugin<any, any> {
<ReportListing
toasts={toasts}
license$={license$}
pollConfig={this.config.poll}
redirect={start.application.navigateToApp}
apiClient={apiClient}
/>
Expand Down Expand Up @@ -163,8 +163,9 @@ export class ReportingPublicPlugin implements Plugin<any, any> {
const { http, notifications } = core;
const apiClient = new ReportingAPIClient(http);
const streamHandler = new StreamHandler(notifications, apiClient);
const { interval } = this.config.poll.jobsRefresh;

Rx.timer(0, JOBS_REFRESH_INTERVAL)
Rx.timer(0, interval)
.pipe(
takeUntil(this.stop$), // stop the interval when stop method is called
map(() => getStored()), // read all pending job IDs from session storage
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/reporting/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ConfigSchema, ConfigType } from './schema';
export { createConfig$ } from './create_config';

export const config: PluginConfigDescriptor<ConfigType> = {
exposeToBrowser: { poll: true },
schema: ConfigSchema,
deprecations: ({ unused }) => [
unused('capture.browser.chromium.maxScreenshotDimension'),
Expand Down

0 comments on commit 2e97a9b

Please sign in to comment.