-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
feature_usage_service.ts
68 lines (61 loc) · 1.91 KB
/
feature_usage_service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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,
}),
});
},
};
}
}