Skip to content

Commit b3d1a33

Browse files
[Reporting] Integration polling config with client code (elastic#63754)
* source new platform for reporting ui config * fix test types * fix type check Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 7ae74e2 commit b3d1a33

File tree

8 files changed

+67
-71
lines changed

8 files changed

+67
-71
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export { ConfigType } from '../server/config';

x-pack/plugins/reporting/constants.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77
export const JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY =
88
'xpack.reporting.jobCompletionNotifications';
99

10-
export const JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG = {
11-
jobCompletionNotifier: {
12-
interval: 10000,
13-
intervalErrorMultiplier: 5,
14-
},
15-
};
16-
1710
// Routes
1811
export const API_BASE_URL = '/api/reporting';
1912
export const API_LIST_URL = `${API_BASE_URL}/jobs`;

x-pack/plugins/reporting/index.d.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import {
8-
CoreSetup,
9-
CoreStart,
10-
HttpSetup,
11-
Plugin,
12-
PluginInitializerContext,
13-
NotificationsStart,
14-
} from '../../../src/core/public';
15-
167
export type JobId = string;
178
export type JobStatus =
189
| 'completed'
@@ -21,9 +12,6 @@ export type JobStatus =
2112
| 'processing'
2213
| 'failed';
2314

24-
export type HttpService = HttpSetup;
25-
export type NotificationsService = NotificationsStart;
26-
2715
export interface SourceJob {
2816
_id: JobId;
2917
_source: {

x-pack/plugins/reporting/public/components/report_listing.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,24 @@ const toasts = {
4747
addDanger: jest.fn(),
4848
} as any;
4949

50+
const mockPollConfig = {
51+
jobCompletionNotifier: {
52+
interval: 5000,
53+
intervalErrorMultiplier: 3,
54+
},
55+
jobsRefresh: {
56+
interval: 5000,
57+
intervalErrorMultiplier: 3,
58+
},
59+
};
60+
5061
describe('ReportListing', () => {
5162
it('Report job listing with some items', () => {
5263
const wrapper = mountWithIntl(
5364
<ReportListing
5465
apiClient={reportingAPIClient as ReportingAPIClient}
5566
license$={license$}
67+
pollConfig={mockPollConfig}
5668
redirect={jest.fn()}
5769
toasts={toasts}
5870
/>
@@ -74,6 +86,7 @@ describe('ReportListing', () => {
7486
<ReportListing
7587
apiClient={reportingAPIClient as ReportingAPIClient}
7688
license$={subMock as Observable<ILicense>}
89+
pollConfig={mockPollConfig}
7790
redirect={jest.fn()}
7891
toasts={toasts}
7992
/>

x-pack/plugins/reporting/public/components/report_listing.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import { i18n } from '@kbn/i18n';
1616
import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react';
1717
import { get } from 'lodash';
1818
import moment from 'moment';
19-
import { Component, Fragment, default as React } from 'react';
19+
import { Component, default as React, Fragment } from 'react';
2020
import { Subscription } from 'rxjs';
2121
import { ApplicationStart, ToastsSetup } from 'src/core/public';
2222
import { ILicense, LicensingPluginSetup } from '../../../licensing/public';
2323
import { Poller } from '../../common/poller';
24-
import { JobStatuses, JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG } from '../../constants';
24+
import { JobStatuses } from '../../constants';
2525
import { checkLicense } from '../lib/license_check';
2626
import { JobQueueEntry, ReportingAPIClient } from '../lib/reporting_api_client';
27+
import { ClientConfigType } from '../plugin';
2728
import {
2829
ReportDeleteButton,
2930
ReportDownloadButton,
@@ -53,6 +54,7 @@ export interface Props {
5354
intl: InjectedIntl;
5455
apiClient: ReportingAPIClient;
5556
license$: LicensingPluginSetup['license$'];
57+
pollConfig: ClientConfigType['poll'];
5658
redirect: ApplicationStart['navigateToApp'];
5759
toasts: ToastsSetup;
5860
}
@@ -167,12 +169,10 @@ class ReportListingUi extends Component<Props, State> {
167169
functionToPoll: () => {
168170
return this.fetchJobs();
169171
},
170-
pollFrequencyInMillis:
171-
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG.jobCompletionNotifier.interval,
172+
pollFrequencyInMillis: this.props.pollConfig.jobsRefresh.interval,
172173
trailing: false,
173174
continuePollingOnError: true,
174-
pollFrequencyErrorMultiplier:
175-
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG.jobCompletionNotifier.intervalErrorMultiplier,
175+
pollFrequencyErrorMultiplier: this.props.pollConfig.jobsRefresh.intervalErrorMultiplier,
176176
});
177177
this.poller.start();
178178
this.licenseSubscription = this.props.license$.subscribe(this.licenseHandler);

x-pack/plugins/reporting/public/lib/stream_handler.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { i18n } from '@kbn/i18n';
78
import * as Rx from 'rxjs';
89
import { catchError, map } from 'rxjs/operators';
9-
import { i18n } from '@kbn/i18n';
10+
import { NotificationsSetup } from 'src/core/public';
1011
import {
1112
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY,
1213
JOB_STATUS_COMPLETED,
1314
JOB_STATUS_FAILED,
1415
JOB_STATUS_WARNINGS,
1516
} from '../../constants';
16-
17-
import {
18-
JobId,
19-
JobSummary,
20-
JobStatusBuckets,
21-
NotificationsService,
22-
SourceJob,
23-
} from '../../index.d';
24-
17+
import { JobId, JobStatusBuckets, JobSummary, SourceJob } from '../../index.d';
2518
import {
26-
getSuccessToast,
2719
getFailureToast,
20+
getGeneralErrorToast,
21+
getSuccessToast,
2822
getWarningFormulasToast,
2923
getWarningMaxSizeToast,
30-
getGeneralErrorToast,
3124
} from '../components';
3225
import { ReportingAPIClient } from './reporting_api_client';
3326

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

4942
export class ReportingNotifierStreamHandler {
50-
constructor(private notifications: NotificationsService, private apiClient: ReportingAPIClient) {}
43+
constructor(private notifications: NotificationsSetup, private apiClient: ReportingAPIClient) {}
5144

5245
/*
5346
* Use Kibana Toast API to show our messages

x-pack/plugins/reporting/public/plugin.tsx

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,50 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import * as Rx from 'rxjs';
7+
import { i18n } from '@kbn/i18n';
8+
import { I18nProvider } from '@kbn/i18n/react';
89
import React from 'react';
910
import ReactDOM from 'react-dom';
11+
import * as Rx from 'rxjs';
1012
import { catchError, filter, map, mergeMap, takeUntil } from 'rxjs/operators';
11-
import { i18n } from '@kbn/i18n';
13+
import {
14+
CoreSetup,
15+
CoreStart,
16+
NotificationsSetup,
17+
Plugin,
18+
PluginInitializerContext,
19+
} from 'src/core/public';
1220
import { ManagementSetup } from 'src/plugins/management/public';
13-
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';
14-
import { I18nProvider } from '@kbn/i18n/react';
1521
import { UiActionsSetup } from 'src/plugins/ui_actions/public';
16-
17-
import { ReportListing } from './components/report_listing';
18-
import { getGeneralErrorToast } from './components';
19-
20-
import { ReportingNotifierStreamHandler as StreamHandler } from './lib/stream_handler';
21-
import { ReportingAPIClient } from './lib/reporting_api_client';
22-
import { GetCsvReportPanelAction } from './panel_actions/get_csv_panel_action';
23-
import { csvReportingProvider } from './share_context_menu/register_csv_reporting';
24-
import { reportingPDFPNGProvider } from './share_context_menu/register_pdf_png_reporting';
25-
26-
import { LicensingPluginSetup } from '../../licensing/public';
22+
import { JobId, JobStatusBuckets } from '../';
2723
import { CONTEXT_MENU_TRIGGER } from '../../../../src/plugins/embeddable/public';
28-
import { SharePluginSetup } from '../../../../src/plugins/share/public';
29-
3024
import {
3125
FeatureCatalogueCategory,
3226
HomePublicPluginSetup,
3327
} from '../../../../src/plugins/home/public';
28+
import { SharePluginSetup } from '../../../../src/plugins/share/public';
29+
import { LicensingPluginSetup } from '../../licensing/public';
30+
import { ConfigType } from '../common/types';
31+
import { JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY } from '../constants';
32+
import { getGeneralErrorToast } from './components';
33+
import { ReportListing } from './components/report_listing';
34+
import { ReportingAPIClient } from './lib/reporting_api_client';
35+
import { ReportingNotifierStreamHandler as StreamHandler } from './lib/stream_handler';
36+
import { GetCsvReportPanelAction } from './panel_actions/get_csv_panel_action';
37+
import { csvReportingProvider } from './share_context_menu/register_csv_reporting';
38+
import { reportingPDFPNGProvider } from './share_context_menu/register_pdf_png_reporting';
3439

35-
import {
36-
JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG,
37-
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY,
38-
} from '../constants';
39-
40-
import { JobId, JobStatusBuckets, NotificationsService } from '..';
41-
42-
const {
43-
jobCompletionNotifier: { interval: JOBS_REFRESH_INTERVAL },
44-
} = JOB_COMPLETION_NOTIFICATIONS_POLLER_CONFIG;
40+
export interface ClientConfigType {
41+
poll: ConfigType['poll'];
42+
}
4543

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

5149
function handleError(
52-
notifications: NotificationsService,
50+
notifications: NotificationsSetup,
5351
err: Error
5452
): Rx.Observable<JobStatusBuckets> {
5553
notifications.toasts.addDanger(
@@ -64,18 +62,19 @@ function handleError(
6462
return Rx.of({ completed: [], failed: [] });
6563
}
6664

67-
export class ReportingPublicPlugin implements Plugin<any, any> {
65+
export class ReportingPublicPlugin implements Plugin<void, void> {
66+
private config: ClientConfigType;
6867
private readonly stop$ = new Rx.ReplaySubject(1);
69-
7068
private readonly title = i18n.translate('xpack.reporting.management.reportingTitle', {
7169
defaultMessage: 'Reporting',
7270
});
73-
7471
private readonly breadcrumbText = i18n.translate('xpack.reporting.breadcrumb', {
7572
defaultMessage: 'Reporting',
7673
});
7774

78-
constructor(initializerContext: PluginInitializerContext) {}
75+
constructor(initializerContext: PluginInitializerContext) {
76+
this.config = initializerContext.config.get<ClientConfigType>();
77+
}
7978

8079
public setup(
8180
core: CoreSetup,
@@ -130,6 +129,7 @@ export class ReportingPublicPlugin implements Plugin<any, any> {
130129
<ReportListing
131130
toasts={toasts}
132131
license$={license$}
132+
pollConfig={this.config.poll}
133133
redirect={start.application.navigateToApp}
134134
apiClient={apiClient}
135135
/>
@@ -163,8 +163,9 @@ export class ReportingPublicPlugin implements Plugin<any, any> {
163163
const { http, notifications } = core;
164164
const apiClient = new ReportingAPIClient(http);
165165
const streamHandler = new StreamHandler(notifications, apiClient);
166+
const { interval } = this.config.poll.jobsRefresh;
166167

167-
Rx.timer(0, JOBS_REFRESH_INTERVAL)
168+
Rx.timer(0, interval)
168169
.pipe(
169170
takeUntil(this.stop$), // stop the interval when stop method is called
170171
map(() => getStored()), // read all pending job IDs from session storage

x-pack/plugins/reporting/server/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ConfigSchema, ConfigType } from './schema';
1010
export { createConfig$ } from './create_config';
1111

1212
export const config: PluginConfigDescriptor<ConfigType> = {
13+
exposeToBrowser: { poll: true },
1314
schema: ConfigSchema,
1415
deprecations: ({ unused }) => [
1516
unused('capture.browser.chromium.maxScreenshotDimension'),

0 commit comments

Comments
 (0)