mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
fix: last seen environment remove duplicate entries (#4663)
This commit is contained in:
parent
9114969869
commit
0cd0d2f153
61
src/lib/services/client-metrics/last-seen-service.test.ts
Normal file
61
src/lib/services/client-metrics/last-seen-service.test.ts
Normal file
@ -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',
|
||||
},
|
||||
]);
|
||||
});
|
@ -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;
|
||||
|
||||
@ -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`,
|
||||
);
|
||||
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user