Skip to content

Commit

Permalink
fix: last seen environment remove duplicate entries (#4663)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Sep 12, 2023
1 parent cd123ae commit cad4547
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
61 changes: 61 additions & 0 deletions src/lib/services/client-metrics/last-seen-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import createStores from '../../../test/fixtures/store';
import EventEmitter from 'events';
import getLogger from '../../../test/fixtures/no-logger';
import { IUnleashConfig } from '../../types';
import { LastSeenService } from './last-seen-service';

function initLastSeenService(flagEnabled = true) {
const stores = createStores();

const eventBus = new EventEmitter();
eventBus.emit = jest.fn();

const config = {
eventBus,
getLogger,
flagResolver: {
isEnabled: () => {
return flagEnabled;
},
},
} as unknown as IUnleashConfig;

const lastSeenService = new LastSeenService(stores, config);

return { lastSeenService, featureToggleStore: stores.featureToggleStore };
}

test('should not add duplicates per feature/environment', async () => {
const { lastSeenService, featureToggleStore } = initLastSeenService();

lastSeenService.updateLastSeen([
{
featureName: 'myFeature',
environment: 'development',
yes: 1,
no: 0,
appName: 'test',
timestamp: new Date(),
},
]);

lastSeenService.updateLastSeen([
{
featureName: 'myFeature',
environment: 'development',
yes: 1,
no: 0,
appName: 'test',
timestamp: new Date(),
},
]);
featureToggleStore.setLastSeen = jest.fn();
await lastSeenService.store();

expect(featureToggleStore.setLastSeen).toHaveBeenCalledWith([
{
environment: 'development',
featureName: 'myFeature',
},
]);
});
21 changes: 14 additions & 7 deletions src/lib/services/client-metrics/last-seen-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type LastSeenInput = {
export class LastSeenService {
private timers: NodeJS.Timeout[] = [];

private lastSeenToggles: Set<LastSeenInput> = new Set();
private lastSeenToggles: Map<String, LastSeenInput> = new Map();

private logger: Logger;

Expand All @@ -37,8 +37,8 @@ export class LastSeenService {
async store(): Promise<number> {
const count = this.lastSeenToggles.size;
if (count > 0) {
const lastSeenToggles = [...this.lastSeenToggles];
this.lastSeenToggles = new Set();
const lastSeenToggles = Array.from(this.lastSeenToggles.values());
this.lastSeenToggles = new Map<String, LastSeenInput>();
this.logger.debug(
`Updating last seen for ${lastSeenToggles.length} toggles`,
);
Expand All @@ -49,15 +49,22 @@ export class LastSeenService {

updateLastSeen(clientMetrics: IClientMetricsEnv[]): void {
clientMetrics
.filter(
(clientMetric) =>
!this.lastSeenToggles.has(
`${clientMetric.featureName}:${clientMetric.environment}`,
),
)
.filter(
(clientMetric) => clientMetric.yes > 0 || clientMetric.no > 0,
)
.forEach((clientMetric) =>
this.lastSeenToggles.add({
.forEach((clientMetric) => {
const key = `${clientMetric.featureName}:${clientMetric.environment}`;
this.lastSeenToggles.set(key, {
featureName: clientMetric.featureName,
environment: clientMetric.environment,
}),
);
});
});
}

destroy(): void {
Expand Down

0 comments on commit cad4547

Please sign in to comment.