-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add client-side feature usage API #75486
Changes from all commits
a264a99
e041704
1a7f106
ac70c20
49e01e1
8e43d60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}; |
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(), | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pgayvallet, should we catch and swallow errors here? I guess consumer shouldn't care about handling errors from this (nor probably wait for a success) and unhandled promise rejection from this call would be a bummer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idk. Realistically, the only reason this would fails (appart from network failures) would be if the consumer tries to notify usage of an unregistered feature, which it a consumer cause (and should be detected in development). Also, unhandled rejections on the client-side don't really do much (even if of course they should be avoided) But that's not a strong opinion, and if really you think this is preferable, I can change that. |
||
body: JSON.stringify({ | ||
featureName, | ||
lastUsed, | ||
}), | ||
}); | ||
}, | ||
}; | ||
} | ||
} |
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'; |
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'; |
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, | ||
}, | ||
}); | ||
} | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in theory, we can overload the server-side if
notifyUsage
is called frequently. But it might be premature optimization.