From 9c5f402a677e3a37c366ac94ef676a5805d852b9 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Fri, 22 Sep 2023 09:25:44 -0700 Subject: [PATCH] feat: Add flag version to migration op event. (#281) --- .../__tests__/MigrationOpTracker.test.ts | 78 +++++++++++++++++++ .../shared/sdk-server/src/LDClientImpl.ts | 2 + .../src/MigrationOpEventConversion.ts | 4 + .../sdk-server/src/MigrationOpTracker.ts | 2 + .../src/api/data/LDMigrationOpEvent.ts | 1 + 5 files changed, 87 insertions(+) diff --git a/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts b/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts index 4c045717e..8346aa2b5 100644 --- a/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts +++ b/packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts @@ -58,6 +58,83 @@ it('generates an event if the minimal requirements are met.', () => { }); }); +it('can include the variation in the event', () => { + const tracker = new MigrationOpTracker( + 'flag', + { user: 'bob' }, + LDMigrationStage.Off, + LDMigrationStage.Off, + { + kind: 'FALLTHROUGH', + }, + undefined, + 1, + ); + + tracker.op('write'); + tracker.invoked('old'); + + expect(tracker.createEvent()).toMatchObject({ + contextKeys: { user: 'bob' }, + evaluation: { + default: 'off', + key: 'flag', + reason: { kind: 'FALLTHROUGH' }, + value: 'off', + variation: 1, + }, + kind: 'migration_op', + measurements: [ + { + key: 'invoked', + values: { + old: true, + }, + }, + ], + operation: 'write', + }); +}); + +it('can include the version in the event', () => { + const tracker = new MigrationOpTracker( + 'flag', + { user: 'bob' }, + LDMigrationStage.Off, + LDMigrationStage.Off, + { + kind: 'FALLTHROUGH', + }, + undefined, + undefined, + 2, + ); + + tracker.op('write'); + tracker.invoked('old'); + + expect(tracker.createEvent()).toMatchObject({ + contextKeys: { user: 'bob' }, + evaluation: { + default: 'off', + key: 'flag', + reason: { kind: 'FALLTHROUGH' }, + value: 'off', + version: 2, + }, + kind: 'migration_op', + measurements: [ + { + key: 'invoked', + values: { + old: true, + }, + }, + ], + operation: 'write', + }); +}); + it('includes errors if at least one is set', () => { const tracker = new MigrationOpTracker( 'flag', @@ -250,6 +327,7 @@ it('can handle exceptions thrown in the consistency check method', () => { undefined, undefined, undefined, + undefined, logger, ); tracker.op('read'); diff --git a/packages/shared/sdk-server/src/LDClientImpl.ts b/packages/shared/sdk-server/src/LDClientImpl.ts index 3b8590ad7..80c94f904 100644 --- a/packages/shared/sdk-server/src/LDClientImpl.ts +++ b/packages/shared/sdk-server/src/LDClientImpl.ts @@ -336,6 +336,7 @@ export default class LDClientImpl implements LDClient { reason, checkRatio, undefined, + undefined, samplingRatio, ), }); @@ -352,6 +353,7 @@ export default class LDClientImpl implements LDClient { checkRatio, // Can be null for compatibility reasons. detail.variationIndex === null ? undefined : detail.variationIndex, + flag?.version, samplingRatio, ), }); diff --git a/packages/shared/sdk-server/src/MigrationOpEventConversion.ts b/packages/shared/sdk-server/src/MigrationOpEventConversion.ts index f18348c30..75b6a1530 100644 --- a/packages/shared/sdk-server/src/MigrationOpEventConversion.ts +++ b/packages/shared/sdk-server/src/MigrationOpEventConversion.ts @@ -196,6 +196,10 @@ function validateEvaluation(evaluation: LDMigrationEvaluation): LDMigrationEvalu validated.variation = evaluation.variation; } + if (evaluation.version !== undefined && TypeValidators.Number.is(evaluation.version)) { + validated.version = evaluation.version; + } + return validated; } diff --git a/packages/shared/sdk-server/src/MigrationOpTracker.ts b/packages/shared/sdk-server/src/MigrationOpTracker.ts index eb30be50b..9001e42fd 100644 --- a/packages/shared/sdk-server/src/MigrationOpTracker.ts +++ b/packages/shared/sdk-server/src/MigrationOpTracker.ts @@ -43,6 +43,7 @@ export default class MigrationOpTracker implements LDMigrationTracker { private readonly reason: LDEvaluationReason, private readonly checkRatio?: number, private readonly variation?: number, + private readonly version?: number, private readonly samplingRatio?: number, private readonly logger?: LDLogger, ) {} @@ -123,6 +124,7 @@ export default class MigrationOpTracker implements LDMigrationTracker { default: this.defaultStage, reason: this.reason, variation: this.variation, + version: this.version, }, measurements, samplingRatio: this.samplingRatio ?? 1, diff --git a/packages/shared/sdk-server/src/api/data/LDMigrationOpEvent.ts b/packages/shared/sdk-server/src/api/data/LDMigrationOpEvent.ts index c3e69d702..544a7b34c 100644 --- a/packages/shared/sdk-server/src/api/data/LDMigrationOpEvent.ts +++ b/packages/shared/sdk-server/src/api/data/LDMigrationOpEvent.ts @@ -13,6 +13,7 @@ export interface LDMigrationEvaluation { value: LDMigrationStage; default: LDMigrationStage; variation?: number; + version?: number; reason: LDEvaluationReason; }