Skip to content

support feature variant #13

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

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
extract common code as getVariantAssignment
  • Loading branch information
Eskibear committed Aug 8, 2024
commit 63cdd43fe7ecca650e39248e2f8d080c6b7a939d
52 changes: 27 additions & 25 deletions src/featureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,12 @@ export class FeatureManager implements IFeatureManager {
return result.variant;
}

async #assignVariant(featureFlag: FeatureFlag, context: ITargetingContext): Promise<{
variant: VariantDefinition | undefined;
reason: VariantAssignmentReason;
}> {
async #assignVariant(featureFlag: FeatureFlag, context: ITargetingContext): Promise<VariantAssignment> {
// user allocation
if (featureFlag.allocation?.user !== undefined) {
for (const userAllocation of featureFlag.allocation.user) {
if (isTargetedUser(context.userId, userAllocation.users)) {
const variant = featureFlag.variants?.find(v => v.name == userAllocation.variant);
if (variant !== undefined) {
return { variant, reason: VariantAssignmentReason.User };
} else {
console.warn(`Variant ${userAllocation.variant} not found for feature ${featureFlag.id}.`);
return { variant: undefined, reason: VariantAssignmentReason.None };
}
return getVariantAssignment(featureFlag, userAllocation.variant, VariantAssignmentReason.User);
}
}
}
Expand All @@ -66,13 +57,7 @@ export class FeatureManager implements IFeatureManager {
if (featureFlag.allocation?.group !== undefined) {
for (const groupAllocation of featureFlag.allocation.group) {
if (isTargetedGroup(context.groups, groupAllocation.groups)) {
const variant = featureFlag.variants?.find(v => v.name == groupAllocation.variant);
if (variant !== undefined) {
return { variant, reason: VariantAssignmentReason.Group };
} else {
console.warn(`Variant ${groupAllocation.variant} not found for feature ${featureFlag.id}.`);
return { variant: undefined, reason: VariantAssignmentReason.None };
}
return getVariantAssignment(featureFlag, groupAllocation.variant, VariantAssignmentReason.Group);
}
}
}
Expand All @@ -82,13 +67,7 @@ export class FeatureManager implements IFeatureManager {
for (const percentileAllocation of featureFlag.allocation.percentile) {
const hint = featureFlag.allocation.seed ?? `allocation\n${featureFlag.id}`;
if (isTargetedPercentile(context.userId, hint, percentileAllocation.from, percentileAllocation.to)) {
const variant = featureFlag.variants?.find(v => v.name == percentileAllocation.variant);
if (variant !== undefined) {
return { variant, reason: VariantAssignmentReason.Percentile };
} else {
console.warn(`Variant ${percentileAllocation.variant} not found for feature ${featureFlag.id}.`);
return { variant: undefined, reason: VariantAssignmentReason.None };
}
return getVariantAssignment(featureFlag, percentileAllocation.variant, VariantAssignmentReason.Percentile);
}
}
}
Expand Down Expand Up @@ -225,6 +204,29 @@ function validateFeatureFlagFormat(featureFlag: any): void {
// TODO: should be moved to the feature flag provider.
}

/**
* Try to get the variant assignment for the given variant name. If the variant is not found, override the reason with VariantAssignmentReason.None.
*
* @param featureFlag feature flag definition
* @param variantName variant name
* @param reason variant assignment reason
* @returns variant assignment containing the variant definition and the reason
*/
function getVariantAssignment(featureFlag: FeatureFlag, variantName: string, reason: VariantAssignmentReason): VariantAssignment {
const variant = featureFlag.variants?.find(v => v.name == variantName);
if (variant !== undefined) {
return { variant, reason };
} else {
console.warn(`Variant ${variantName} not found for feature ${featureFlag.id}.`);
return { variant: undefined, reason: VariantAssignmentReason.None };
}
}

type VariantAssignment = {
variant: VariantDefinition | undefined;
reason: VariantAssignmentReason;
};

enum VariantAssignmentReason {
/**
* Variant allocation did not happen. No variant is assigned.
Expand Down