Skip to content

Commit

Permalink
usageCollection is an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Apr 3, 2020
1 parent 8d95558 commit 67acc88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/reporting/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ReportingConfig, ReportingConfigType } from './core';
export interface ReportingSetupDeps {
elasticsearch: ElasticsearchServiceSetup;
security: SecurityPluginSetup;
usageCollection: UsageCollectionSetup;
usageCollection?: UsageCollectionSetup;
__LEGACY: LegacySetup;
}

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

import { get } from 'lodash';
import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main';
import { ESCallCluster, ExportTypesRegistry } from '../../types';
import { ReportingConfig, ReportingSetupDeps } from '../types';
import { ReportingConfig } from '../types';
import { decorateRangeStats } from './decorate_range_stats';
import { getExportTypesHandler } from './get_export_type_handler';
import {
Expand All @@ -19,6 +20,8 @@ import {
RangeStats,
} from './types';

type XPackInfo = XPackMainPlugin['info'];

const JOB_TYPES_KEY = 'jobTypes';
const JOB_TYPES_FIELD = 'jobtype';
const LAYOUT_TYPES_KEY = 'layoutTypes';
Expand Down Expand Up @@ -100,7 +103,7 @@ async function handleResponse(response: AggregationResults): Promise<RangeStatSe

export async function getReportingUsage(
config: ReportingConfig,
plugins: ReportingSetupDeps,
xpackMainInfo: XPackInfo,
callCluster: ESCallCluster,
exportTypesRegistry: ExportTypesRegistry
) {
Expand Down Expand Up @@ -137,7 +140,6 @@ export async function getReportingUsage(
},
};

const { info: xpackMainInfo } = plugins.__LEGACY.plugins.xpack_main;
return callCluster('search', params)
.then((response: AggregationResults) => handleResponse(response))
.then((usage: RangeStatSets) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ describe('license checks', () => {
beforeAll(async () => {
const plugins = getPluginsMock({ license: 'basic' });
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const { fetch } = getReportingUsageCollector(
mockConfig,
plugins.usageCollection,
plugins.__LEGACY.plugins.xpack_main.info,
exportTypesRegistry
);
usageStats = await fetch(callClusterMock, exportTypesRegistry);
});

Expand All @@ -93,7 +98,12 @@ describe('license checks', () => {
beforeAll(async () => {
const plugins = getPluginsMock({ license: 'none' });
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const { fetch } = getReportingUsageCollector(
mockConfig,
plugins.usageCollection,
plugins.__LEGACY.plugins.xpack_main.info,
exportTypesRegistry
);
usageStats = await fetch(callClusterMock, exportTypesRegistry);
});

Expand All @@ -115,7 +125,12 @@ describe('license checks', () => {
beforeAll(async () => {
const plugins = getPluginsMock({ license: 'platinum' });
const callClusterMock = jest.fn(() => Promise.resolve(getResponseMock()));
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const { fetch } = getReportingUsageCollector(
mockConfig,
plugins.usageCollection,
plugins.__LEGACY.plugins.xpack_main.info,
exportTypesRegistry
);
usageStats = await fetch(callClusterMock, exportTypesRegistry);
});

Expand All @@ -137,7 +152,12 @@ describe('license checks', () => {
beforeAll(async () => {
const plugins = getPluginsMock({ license: 'basic' });
const callClusterMock = jest.fn(() => Promise.resolve({}));
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const { fetch } = getReportingUsageCollector(
mockConfig,
plugins.usageCollection,
plugins.__LEGACY.plugins.xpack_main.info,
exportTypesRegistry
);
usageStats = await fetch(callClusterMock, exportTypesRegistry);
});

Expand All @@ -155,7 +175,12 @@ describe('data modeling', () => {
test('with normal looking usage data', async () => {
const mockConfig = getMockReportingConfig();
const plugins = getPluginsMock();
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const { fetch } = getReportingUsageCollector(
mockConfig,
plugins.usageCollection,
plugins.__LEGACY.plugins.xpack_main.info,
exportTypesRegistry
);
const callClusterMock = jest.fn(() =>
Promise.resolve(
getResponseMock({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main';
import { KIBANA_REPORTING_TYPE } from '../../common/constants';
import { ReportingConfig, ReportingCore, ReportingSetupDeps } from '../../server/types';
import { ESCallCluster, ExportTypesRegistry } from '../../types';
import { getReportingUsage } from './get_reporting_usage';
import { RangeStats } from './types';

type XPackInfo = XPackMainPlugin['info'];

// places the reporting data as kibana stats
const METATYPE = 'kibana_stats';

Expand All @@ -18,15 +22,15 @@ const METATYPE = 'kibana_stats';
*/
export function getReportingUsageCollector(
config: ReportingConfig,
plugins: ReportingSetupDeps,
usageCollection: UsageCollectionSetup,
xpackMainInfo: XPackInfo,
exportTypesRegistry: ExportTypesRegistry,
isReady: () => Promise<boolean>
) {
const { usageCollection } = plugins;
return usageCollection.makeUsageCollector({
type: KIBANA_REPORTING_TYPE,
fetch: (callCluster: ESCallCluster) =>
getReportingUsage(config, plugins, callCluster, exportTypesRegistry),
getReportingUsage(config, xpackMainInfo, callCluster, exportTypesRegistry),
isReady,

/*
Expand All @@ -53,13 +57,19 @@ export function registerReportingUsageCollector(
reporting: ReportingCore,
plugins: ReportingSetupDeps
) {
if (!plugins.usageCollection) {
return;
}
const xpackMainInfo = plugins.__LEGACY.plugins.xpack_main.info;

const exportTypesRegistry = reporting.getExportTypesRegistry();
const collectionIsReady = reporting.pluginHasStarted.bind(reporting);
const config = reporting.getConfig();

const collector = getReportingUsageCollector(
config,
plugins,
plugins.usageCollection,
xpackMainInfo,
exportTypesRegistry,
collectionIsReady
);
Expand Down

0 comments on commit 67acc88

Please sign in to comment.