diff --git a/x-pack/plugins/licensing/server/services/feature_usage_service.test.ts b/x-pack/plugins/licensing/server/services/feature_usage_service.test.ts index e31e7abff509d8..f0ef0dbec0b220 100644 --- a/x-pack/plugins/licensing/server/services/feature_usage_service.test.ts +++ b/x-pack/plugins/licensing/server/services/feature_usage_service.test.ts @@ -43,6 +43,16 @@ describe('FeatureUsageService', () => { expect(start.getLastUsages().get('feature')).toBe(127001); }); + it('can receive a Date object', () => { + const setup = service.setup(); + setup.register('feature'); + const start = service.start(); + + const usageTime = new Date(2015, 9, 21, 17, 54, 12); + start.notifyUsage('feature', usageTime); + expect(start.getLastUsages().get('feature')).toBe(usageTime.getTime()); + }); + it('uses the current time when `usedAt` is unspecified', () => { jest.spyOn(Date, 'now').mockReturnValue(42); diff --git a/x-pack/plugins/licensing/server/services/feature_usage_service.ts b/x-pack/plugins/licensing/server/services/feature_usage_service.ts index ff9cf7eb28149f..47ffe3a3d9f54f 100644 --- a/x-pack/plugins/licensing/server/services/feature_usage_service.ts +++ b/x-pack/plugins/licensing/server/services/feature_usage_service.ts @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ +import { isDate } from 'lodash'; + /** @public */ export interface FeatureUsageServiceSetup { /** @@ -16,9 +18,11 @@ export interface FeatureUsageServiceSetup { export interface FeatureUsageServiceStart { /** * Notify of a registered feature usage at given time. - * If `usedAt` is not specified, it will use the current time instead. + * + * @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?: number): void; + notifyUsage(featureName: string, usedAt?: Date | number): void; /** * Return a map containing last usage timestamp for all features. * Features that were not used yet do not appear in the map. @@ -47,6 +51,9 @@ export class FeatureUsageService { if (!this.features.includes(featureName)) { throw new Error(`Feature '${featureName}' is not registered.`); } + if (isDate(usedAt)) { + usedAt = usedAt.getTime(); + } const currentValue = this.lastUsages.get(featureName) ?? 0; this.lastUsages.set(featureName, Math.max(usedAt, currentValue)); },