Skip to content

[FFM-10041] - Fall back to identifier if bucketBy attr is not found #99

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 6 commits into from
Nov 24, 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
2 changes: 1 addition & 1 deletion examples/getting_started/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harnessio/ff-nodejs-server-sdk",
"version": "1.3.7",
"version": "1.4.0",
"description": "Feature flags SDK for NodeJS environments",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
Expand Down
80 changes: 80 additions & 0 deletions src/__tests__/evaluator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Evaluator } from '../evaluator';
import { Logger } from '../log';

describe('Evaluator', () => {
it('should fallback to identifier if bucketby attribute does not exist', async () => {
const logger: Logger = {
trace: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

const mock_query = {
getFlag: jest.fn(),
getSegment: jest.fn(),
findFlagsBySegment: jest.fn(),
};

const feature_config = {
feature: 'flag',
kind: 'string',
state: 'on',
variationToTargetMap: [],
variations: [
{ identifier: 'variation1', value: 'default_on' },
{ identifier: 'variation2', value: 'wanted_value' },
{ identifier: 'variation3', value: 'default_off' },
],
defaultServe: {
distribution: null,
variation: 'default_on',
},
offVariation: 'variation3',
rules: [
{
ruleId: 'rule1',
clauses: [
{
op: 'equal',
attribute: 'identifier',
values: ['test'],
},
],
serve: {
distribution: {
bucketBy: 'i_do_not_exist',
variations: [
{ variation: 'variation1', weight: 56 },
{ variation: 'variation2', weight: 1 }, // bucket 57
{ variation: 'variation3', weight: 43 },
],
},
},
},
],
};

mock_query.getFlag.mockReturnValue(feature_config);

const evaluator = new Evaluator(mock_query, logger);

const target = {
identifier: 'test', // Test will fall back to bucketing on this (bucket 57)
name: 'test name',
attributes: {
location: 'emea',
},
};

const actual_variation = await evaluator.stringVariation(
'flag',
target,
'fallback_value',
null,
);

expect(actual_variation).toEqual('wanted_value');
});
});
1 change: 1 addition & 0 deletions src/__tests__/sdk_codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('SDK Codes', () => {
'warnDefaultVariationServed',
['flag', { name: 'dummy', identifier: 'dummy' }, 'default value', logger],
],
['warnBucketByAttributeNotFound', ['dummy1', 'dummy2', logger]],
])('it should not throw when %s is called', (fn, args) => {
expect(() => sdkCodes[fn](...args)).not.toThrow();
});
Expand Down
26 changes: 19 additions & 7 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
} from './openapi';
import murmurhash from 'murmurhash';
import { ConsoleLog } from './log';
import { debugEvalSuccess, warnDefaultVariationServed } from './sdk_codes';
import {
debugEvalSuccess,
warnBucketByAttributeNotFound,
warnDefaultVariationServed,
} from './sdk_codes';

type Callback = (
fc: FeatureConfig,
Expand Down Expand Up @@ -54,19 +58,19 @@ export class Evaluator {
}

private getNormalizedNumberWithNormalizer(
property: Type,
bucketBy: string,
property: Type,
normalizer: number,
): number {
const value = [bucketBy, property].join(':');
const hash = parseInt(murmurhash(value).toString());
return (hash % normalizer) + 1;
}

private getNormalizedNumber(property: Type, bucketBy: string): number {
private getNormalizedNumber(bucketBy: string, property: Type): number {
return this.getNormalizedNumberWithNormalizer(
property,
bucketBy,
property,
ONE_HUNDRED,
);
}
Expand All @@ -76,11 +80,19 @@ export class Evaluator {
bucketBy: string,
percentage: number,
): boolean {
const property = this.getAttrValue(target, bucketBy);
let bb = bucketBy;
let property = this.getAttrValue(target, bb);
if (!property) {
return false;
bb = 'identifier';
property = this.getAttrValue(target, bb);

if (!property) {
return false;
}

warnBucketByAttributeNotFound(bucketBy, property?.toString(), this.log);
}
const bucketId = this.getNormalizedNumber(property, bucketBy);
const bucketId = this.getNormalizedNumber(bb, property);
return percentage > 0 && bucketId <= percentage;
}

Expand Down
11 changes: 11 additions & 0 deletions src/sdk_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const sdkCodes: Record<number, string> = {
// SDK_EVAL_6xxx -
6000: 'Evaluation successful: ',
6001: 'Evaluation Failed, returning default variation: ',
6002: "BucketBy attribute not found in target attributes, falling back to 'identifier':",
// SDK_METRICS_7xxx
7000: 'Metrics thread started with request interval:',
7001: 'Metrics stopped',
Expand Down Expand Up @@ -173,3 +174,13 @@ export function warnDefaultVariationServed(
),
);
}

export function warnBucketByAttributeNotFound(
bucketBy: string,
usingValue: string,
logger: Logger,
): void {
logger.warn(
getSdkErrMsg(6002, `missing=${bucketBy}, using value=${usingValue}`),
);
}
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '1.3.7';
export const VERSION = '1.4.0';