Skip to content

Commit

Permalink
register graph usage (elastic#72041)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Jul 17, 2020
1 parent 2913fef commit 47a77b4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/graph/server/lib/license_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import Boom from 'boom';
import { map } from 'rxjs/operators';
import { Observable, Subscription } from 'rxjs';
import { LicensingPluginStart } from '../../../licensing/server';
import { ILicense } from '../../../licensing/common/types';
import { checkLicense, GraphLicenseInformation } from '../../common/check_license';

export class LicenseState {
private licenseInformation: GraphLicenseInformation = checkLicense(undefined);
private subscription: Subscription | null = null;
private observable: Observable<GraphLicenseInformation> | null = null;
private _notifyUsage: LicensingPluginStart['featureUsage']['notifyUsage'] | null = null;

private updateInformation(licenseInformation: GraphLicenseInformation) {
this.licenseInformation = licenseInformation;
Expand All @@ -24,6 +26,17 @@ export class LicenseState {
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
}

public setNotifyUsage(notifyUsage: LicensingPluginStart['featureUsage']['notifyUsage']) {
this._notifyUsage = notifyUsage;
}

// 'Graph' is the only allowed feature here at the moment, if this gets extended in the future, add to the union type
public notifyUsage(featureName: 'Graph') {
if (this._notifyUsage) {
this._notifyUsage(featureName);
}
}

public stop() {
if (this.subscription) {
this.subscription.unsubscribe();
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/graph/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import { i18n } from '@kbn/i18n';
import { Plugin, CoreSetup } from 'src/core/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { Plugin, CoreSetup, CoreStart } from 'src/core/server';
import { LicensingPluginSetup, LicensingPluginStart } from '../../licensing/server';
import { LicenseState } from './lib/license_state';
import { registerSearchRoute } from './routes/search';
import { registerExploreRoute } from './routes/explore';
Expand Down Expand Up @@ -34,6 +34,7 @@ export class GraphPlugin implements Plugin {
licenseState.start(licensing.license$);
this.licenseState = licenseState;
core.savedObjects.registerType(graphWorkspace);
licensing.featureUsage.register('Graph', 'platinum');

if (home) {
registerSampleData(home.sampleData, licenseState);
Expand Down Expand Up @@ -79,7 +80,10 @@ export class GraphPlugin implements Plugin {
registerExploreRoute({ licenseState, router });
}

public start() {}
public start(core: CoreStart, { licensing }: { licensing: LicensingPluginStart }) {
this.licenseState!.setNotifyUsage(licensing.featureUsage.notifyUsage);
}

public stop() {
if (this.licenseState) {
this.licenseState.stop();
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/graph/server/routes/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function registerExploreRoute({
response
) => {
verifyApiAccess(licenseState);
licenseState.notifyUsage('Graph');
try {
return response.ok({
body: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/graph/server/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function registerSearchRoute({
response
) => {
verifyApiAccess(licenseState);
licenseState.notifyUsage('Graph');
const includeFrozen = await uiSettings.get<boolean>(UI_SETTINGS.SEARCH_INCLUDE_FROZEN);
try {
return response.ok({
Expand Down

0 comments on commit 47a77b4

Please sign in to comment.