Skip to content

Commit

Permalink
accept Date for notifyUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Apr 20, 2020
1 parent eb7fa18 commit 6349736
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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.
Expand Down Expand Up @@ -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));
},
Expand Down

0 comments on commit 6349736

Please sign in to comment.