Skip to content

Commit

Permalink
simplify reporting usage collector setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Feb 12, 2020
1 parent e8aa02d commit 25dc761
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
7 changes: 6 additions & 1 deletion x-pack/legacy/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ export class ReportingPlugin
// END LEGACY

// Register a function with server to manage the collection of usage stats
registerReportingUsageCollector(__LEGACY, usageCollection, this.exportTypesRegistry);
registerReportingUsageCollector(
__LEGACY,
usageCollection,
this.pluginStart$,
this.exportTypesRegistry
);

this.pluginSetup$.next({ browserDriverFactory });
return { browserDriverFactory };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ describe('license checks', () => {
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const usageCollection = getMockUsageCollection();
const { fetch: getReportingUsage } = getReportingUsageCollector(
usageCollection,
serverWithBasicLicenseMock,
() => {},
usageCollection,
exportTypesRegistry
);
usageStats = await getReportingUsage(callClusterMock, exportTypesRegistry);
Expand Down Expand Up @@ -101,9 +100,8 @@ describe('license checks', () => {
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const usageCollection = getMockUsageCollection();
const { fetch: getReportingUsage } = getReportingUsageCollector(
usageCollection,
serverWithNoLicenseMock,
() => {},
usageCollection,
exportTypesRegistry
);
usageStats = await getReportingUsage(callClusterMock, exportTypesRegistry);
Expand Down Expand Up @@ -132,9 +130,8 @@ describe('license checks', () => {
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const usageCollection = getMockUsageCollection();
const { fetch: getReportingUsage } = getReportingUsageCollector(
usageCollection,
serverWithPlatinumLicenseMock,
() => {},
usageCollection,
exportTypesRegistry
);
usageStats = await getReportingUsage(callClusterMock, exportTypesRegistry);
Expand Down Expand Up @@ -163,9 +160,8 @@ describe('license checks', () => {
const callClusterMock = jest.fn(() => Promise.resolve({}));
const usageCollection = getMockUsageCollection();
const { fetch: getReportingUsage } = getReportingUsageCollector(
usageCollection,
serverWithBasicLicenseMock,
() => {},
usageCollection,
exportTypesRegistry
);
usageStats = await getReportingUsage(callClusterMock, exportTypesRegistry);
Expand All @@ -190,9 +186,8 @@ describe('data modeling', () => {
.stub()
.returns('platinum');
({ fetch: getReportingUsage } = getReportingUsageCollector(
usageCollection,
serverWithPlatinumLicenseMock,
() => {},
usageCollection,
exportTypesRegistry
));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import * as Rx from 'rxjs';
import { first, mapTo } from 'rxjs/operators';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { ServerFacade, ExportTypesRegistry, ESCallCluster } from '../../types';
import { KIBANA_REPORTING_TYPE } from '../../common/constants';
import { ReportingStart } from '../../server/plugin';
import { ESCallCluster, ExportTypesRegistry, ServerFacade } from '../../types';
import { getReportingUsage } from './get_reporting_usage';
import { RangeStats } from './types';

Expand All @@ -18,16 +21,16 @@ const METATYPE = 'kibana_stats';
* @return {Object} kibana usage stats type collection object
*/
export function getReportingUsageCollector(
usageCollection: UsageCollectionSetup,
server: ServerFacade,
isReady: () => boolean,
exportTypesRegistry: ExportTypesRegistry
usageCollection: UsageCollectionSetup,
exportTypesRegistry: ExportTypesRegistry,
isReady: () => Promise<boolean>
) {
return usageCollection.makeUsageCollector({
type: KIBANA_REPORTING_TYPE,
isReady,
fetch: (callCluster: ESCallCluster) =>
getReportingUsage(server, callCluster, exportTypesRegistry),
isReady,

/*
* Format the response data into a model for internal upload
Expand All @@ -50,16 +53,17 @@ export function getReportingUsageCollector(
}

export function registerReportingUsageCollector(
usageCollection: UsageCollectionSetup,
server: ServerFacade,
isReady: () => boolean,
usageCollection: UsageCollectionSetup,
pluginStart$: Rx.Observable<ReportingStart>,
exportTypesRegistry: ExportTypesRegistry
) {
const collectionIsReady = () => pluginStart$.pipe(first(), mapTo(true)).toPromise();
const collector = getReportingUsageCollector(
usageCollection,
server,
isReady,
exportTypesRegistry
usageCollection,
exportTypesRegistry,
collectionIsReady
);
usageCollection.registerCollector(collector);
}

0 comments on commit 25dc761

Please sign in to comment.