Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: last seen environment remove duplicate entries #4663

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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