From 11f77a21deba4154ab72a2ffc778cde1fe426cbb Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 17 Jul 2023 10:10:15 +0200 Subject: [PATCH] #4205: activate event emission (#4240) This PR activates the event emission that was prepared for in https://github.com/Unleash/unleash/pull/4239. It emits events (behind a flag) when something is marked as potentially stale or the opposite. It takes the features returned from the store and creates events out of them. The events only contain data, no preData. This is because the preData can easily be inferred and because it gives a nicer event in the event log. Here is an image of the difference. The top event uses only data, so it shows the name of the feature and the new potentiallyStale status. The bottom event uses both preData and data, so it only shows the new potentiallyStale status and not the feature name (unless you show the raw event): ![image](https://github.com/Unleash/unleash/assets/17786332/5ec0fbef-f4cf-4dc6-9af6-9203fca30e5d) Should not be merged before #4239. Merge that and then rebase this off main or cherry the commit. ## Discussion ### `preData` Should we also use preData or is it enough to use only data? It seems unnecessary in this event, but I'm open to hearing your thoughts. ### event author: `createdBy` I've set `unleash-system` as the `createdBy` property on these events because they are generated by the system. I found the same string used some other places. However, it may be that there we want to use a different author. --- .../feature-service-potentially-stale.test.ts | 52 +++++++++++++++++++ src/lib/services/feature-toggle-service.ts | 20 ++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/lib/services/feature-service-potentially-stale.test.ts diff --git a/src/lib/services/feature-service-potentially-stale.test.ts b/src/lib/services/feature-service-potentially-stale.test.ts new file mode 100644 index 0000000000..8009f5e295 --- /dev/null +++ b/src/lib/services/feature-service-potentially-stale.test.ts @@ -0,0 +1,52 @@ +import { IUnleashConfig, IUnleashStores } from '../types'; +import { createTestConfig } from '../../test/config/test-config'; +import FeatureToggleService from './feature-toggle-service'; +import { AccessService } from './access-service'; +import { IChangeRequestAccessReadModel } from 'lib/features/change-request-access-service/change-request-access-read-model'; +import { ISegmentService } from 'lib/segments/segment-service-interface'; + +test('Should store events', async () => { + expect.assertions(4); + const featureUpdates = [ + { name: 'feature1', potentiallyStale: true }, + { name: 'feature2', potentiallyStale: false }, + ]; + + const config = createTestConfig(); + const featureToggleService = new FeatureToggleService( + { + featureToggleStore: { + updatePotentiallyStaleFeatures: () => featureUpdates, + }, + eventStore: { + batchStore: ([event1, event2]) => { + // expect 'feature1' + expect(event1.data).toMatchObject({ + name: 'feature1', + potentiallyStale: true, + }); + expect(event1.preData).toBeUndefined(); + // expect 'feature1' + expect(event2.data).toMatchObject({ + name: 'feature2', + potentiallyStale: false, + }); + expect(event2.preData).toBeUndefined(); + }, + }, + } as unknown as IUnleashStores, + { + ...config, + flagResolver: { isEnabled: () => true }, + experimental: { + ...(config.experimental ?? {}), + emitPotentiallyStaleEvents: true, + }, + } as unknown as IUnleashConfig, + {} as ISegmentService, + {} as AccessService, + {} as IChangeRequestAccessReadModel, + ); + + await featureToggleService.updatePotentiallyStaleFeatures(); +}); diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index f369397d18..925023e453 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -39,6 +39,7 @@ import { SKIP_CHANGE_REQUEST, Unsaved, WeightType, + FEATURE_POTENTIALLY_STALE_UPDATED, } from '../types'; import { Logger } from '../logger'; import BadDataError from '../error/bad-data-error'; @@ -1984,7 +1985,24 @@ class FeatureToggleService { } async updatePotentiallyStaleFeatures(): Promise { - await this.featureToggleStore.updatePotentiallyStaleFeatures(); + const potentiallyStaleFeatures = + await this.featureToggleStore.updatePotentiallyStaleFeatures(); + if (this.flagResolver.isEnabled('emitPotentiallyStaleEvents')) { + if (potentiallyStaleFeatures.length > 0) { + return this.eventStore.batchStore( + potentiallyStaleFeatures.map( + ({ name, potentiallyStale }) => ({ + type: FEATURE_POTENTIALLY_STALE_UPDATED, + createdBy: 'unleash-system', + data: { + name, + potentiallyStale, + }, + }), + ), + ); + } + } } }