forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add client-side feature usage API (elastic#75486)
* add client-side feature_usage API * use route context for notify feature usage route
- Loading branch information
1 parent
ee7c071
commit 0a8dca9
Showing
14 changed files
with
384 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/licensing/public/services/feature_usage_service.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { | ||
FeatureUsageService, | ||
FeatureUsageServiceSetup, | ||
FeatureUsageServiceStart, | ||
} from './feature_usage_service'; | ||
|
||
const createSetupMock = (): jest.Mocked<FeatureUsageServiceSetup> => { | ||
const mock = { | ||
register: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
const createStartMock = (): jest.Mocked<FeatureUsageServiceStart> => { | ||
const mock = { | ||
notifyUsage: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
const createServiceMock = (): jest.Mocked<PublicMethodsOf<FeatureUsageService>> => { | ||
const mock = { | ||
setup: jest.fn(), | ||
start: jest.fn(), | ||
}; | ||
|
||
mock.setup.mockImplementation(() => createSetupMock()); | ||
mock.start.mockImplementation(() => createStartMock()); | ||
|
||
return mock; | ||
}; | ||
|
||
export const featureUsageMock = { | ||
create: createServiceMock, | ||
createSetup: createSetupMock, | ||
createStart: createStartMock, | ||
}; |
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/licensing/public/services/feature_usage_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { httpServiceMock } from '../../../../../src/core/public/mocks'; | ||
import { FeatureUsageService } from './feature_usage_service'; | ||
|
||
describe('FeatureUsageService', () => { | ||
let http: ReturnType<typeof httpServiceMock.createSetupContract>; | ||
let service: FeatureUsageService; | ||
|
||
beforeEach(() => { | ||
http = httpServiceMock.createSetupContract(); | ||
service = new FeatureUsageService(); | ||
}); | ||
|
||
describe('#setup', () => { | ||
describe('#register', () => { | ||
it('calls the endpoint with the correct parameters', async () => { | ||
const setup = service.setup({ http }); | ||
await setup.register('my-feature', 'platinum'); | ||
expect(http.post).toHaveBeenCalledTimes(1); | ||
expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/register', { | ||
body: JSON.stringify({ | ||
featureName: 'my-feature', | ||
licenseType: 'platinum', | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#start', () => { | ||
describe('#notifyUsage', () => { | ||
it('calls the endpoint with the correct parameters', async () => { | ||
service.setup({ http }); | ||
const start = service.start({ http }); | ||
await start.notifyUsage('my-feature', 42); | ||
|
||
expect(http.post).toHaveBeenCalledTimes(1); | ||
expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/notify', { | ||
body: JSON.stringify({ | ||
featureName: 'my-feature', | ||
lastUsed: 42, | ||
}), | ||
}); | ||
}); | ||
|
||
it('correctly convert dates', async () => { | ||
service.setup({ http }); | ||
const start = service.start({ http }); | ||
|
||
const now = new Date(); | ||
|
||
await start.notifyUsage('my-feature', now); | ||
|
||
expect(http.post).toHaveBeenCalledTimes(1); | ||
expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/notify', { | ||
body: JSON.stringify({ | ||
featureName: 'my-feature', | ||
lastUsed: now.getTime(), | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
x-pack/plugins/licensing/public/services/feature_usage_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import isDate from 'lodash/isDate'; | ||
import type { HttpSetup, HttpStart } from 'src/core/public'; | ||
import { LicenseType } from '../../common/types'; | ||
|
||
/** @public */ | ||
export interface FeatureUsageServiceSetup { | ||
/** | ||
* Register a feature to be able to notify of it's usages using the {@link FeatureUsageServiceStart | service start contract}. | ||
*/ | ||
register(featureName: string, licenseType: LicenseType): Promise<void>; | ||
} | ||
|
||
/** @public */ | ||
export interface FeatureUsageServiceStart { | ||
/** | ||
* Notify of a registered feature usage at given time. | ||
* | ||
* @param featureName - the name of the feature to notify usage of | ||
* @param usedAt - Either a `Date` or an unix timestamp with ms. If not specified, it will be set to the current time. | ||
*/ | ||
notifyUsage(featureName: string, usedAt?: Date | number): Promise<void>; | ||
} | ||
|
||
interface SetupDeps { | ||
http: HttpSetup; | ||
} | ||
|
||
interface StartDeps { | ||
http: HttpStart; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class FeatureUsageService { | ||
public setup({ http }: SetupDeps): FeatureUsageServiceSetup { | ||
return { | ||
register: async (featureName, licenseType) => { | ||
await http.post('/internal/licensing/feature_usage/register', { | ||
body: JSON.stringify({ | ||
featureName, | ||
licenseType, | ||
}), | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
public start({ http }: StartDeps): FeatureUsageServiceStart { | ||
return { | ||
notifyUsage: async (featureName, usedAt = Date.now()) => { | ||
const lastUsed = isDate(usedAt) ? usedAt.getTime() : usedAt; | ||
await http.post('/internal/licensing/feature_usage/notify', { | ||
body: JSON.stringify({ | ||
featureName, | ||
lastUsed, | ||
}), | ||
}); | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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 { | ||
FeatureUsageService, | ||
FeatureUsageServiceSetup, | ||
FeatureUsageServiceStart, | ||
} from './feature_usage_service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* 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 { registerNotifyFeatureUsageRoute } from './notify_feature_usage'; | ||
export { registerRegisterFeatureRoute } from './register_feature'; |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/licensing/server/routes/internal/notify_feature_usage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { schema } from '@kbn/config-schema'; | ||
import { IRouter } from 'src/core/server'; | ||
|
||
export function registerNotifyFeatureUsageRoute(router: IRouter) { | ||
router.post( | ||
{ | ||
path: '/internal/licensing/feature_usage/notify', | ||
validate: { | ||
body: schema.object({ | ||
featureName: schema.string(), | ||
lastUsed: schema.number(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const { featureName, lastUsed } = request.body; | ||
|
||
context.licensing.featureUsage.notifyUsage(featureName, lastUsed); | ||
|
||
return response.ok({ | ||
body: { | ||
success: true, | ||
}, | ||
}); | ||
} | ||
); | ||
} |
Oops, something went wrong.