Skip to content

Commit

Permalink
feat: Add flag version to migration op event. (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Sep 22, 2023
1 parent c120948 commit 9c5f402
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
78 changes: 78 additions & 0 deletions packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -250,6 +327,7 @@ it('can handle exceptions thrown in the consistency check method', () => {
undefined,
undefined,
undefined,
undefined,
logger,
);
tracker.op('read');
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/sdk-server/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export default class LDClientImpl implements LDClient {
reason,
checkRatio,
undefined,
undefined,
samplingRatio,
),
});
Expand All @@ -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,
),
});
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/sdk-server/src/MigrationOpEventConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/shared/sdk-server/src/MigrationOpTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface LDMigrationEvaluation {
value: LDMigrationStage;
default: LDMigrationStage;
variation?: number;
version?: number;
reason: LDEvaluationReason;
}

Expand Down

0 comments on commit 9c5f402

Please sign in to comment.