Skip to content
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

decode to number, tests, nit #203

Merged
merged 1 commit into from
Jan 17, 2025
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
7 changes: 4 additions & 3 deletions src/client/eppo-precomputed-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,17 @@ export default class EppoPrecomputedClient {
}

private getPrecomputedBandit(banditKey: string): IPrecomputedBandit | null {
return this.getObfuscatedPrecomputedBandit(banditKey);
const obfuscatedBandit = this.getObfuscatedPrecomputedBandit(banditKey);
return obfuscatedBandit ? decodePrecomputedBandit(obfuscatedBandit) : null;
}

private getObfuscatedPrecomputedBandit(banditKey: string): IPrecomputedBandit | null {
private getObfuscatedPrecomputedBandit(banditKey: string): IObfuscatedPrecomputedBandit | null {
const salt = this.precomputedBanditStore?.salt;
const saltedAndHashedBanditKey = getMD5Hash(banditKey, salt);
const precomputedBandit: IObfuscatedPrecomputedBandit | null = this.precomputedBanditStore?.get(
saltedAndHashedBanditKey,
) as IObfuscatedPrecomputedBandit;
return precomputedBandit ? decodePrecomputedBandit(precomputedBandit) : null;
return precomputedBandit ?? null;
Comment on lines 362 to +373
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense - yes the naming/purpose of these functions was a bit confusing for me. sometimes you WANT to get the obfuscated configuration.

}

public isInitialized() {
Expand Down
49 changes: 47 additions & 2 deletions src/decoding.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { decodeAllocation, decodeSplit, decodeValue, decodeVariations } from './decoding';
import { VariationType, ObfuscatedVariation, Variation } from './interfaces';
import {
decodeAllocation,
decodePrecomputedBandit,
decodeSplit,
decodeValue,
decodeVariations,
} from './decoding';
import {
VariationType,
ObfuscatedVariation,
Variation,
IObfuscatedPrecomputedBandit,
} from './interfaces';

describe('decoding', () => {
describe('decodeVariations', () => {
Expand Down Expand Up @@ -175,4 +186,38 @@ describe('decoding', () => {
expect(decodeAllocation(obfuscatedAllocation)).toEqual(expectedAllocation);
});
});

describe('decode bandit', () => {
it('should correctly decode bandit', () => {
const encodedBandit = {
action: 'Z3JlZW5CYWNrZ3JvdW5k',
actionCategoricalAttributes: {
'Y29sb3I=': 'Z3JlZW4=',
'dHlwZQ==': 'YmFja2dyb3VuZA==',
},
actionNumericAttributes: { Zm9udEhlaWdodEVt: 'MTA=' },
actionProbability: 0.95,
banditKey: 'bGF1bmNoLWJ1dHRvbi10cmVhdG1lbnQ=',
modelVersion: 'MzI0OQ==',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding these

optimalityGap: 0,
} as IObfuscatedPrecomputedBandit;

const decodedBandit = decodePrecomputedBandit(encodedBandit);

expect(decodedBandit).toEqual({
action: 'greenBackground',
actionCategoricalAttributes: {
color: 'green',
type: 'background',
},
actionNumericAttributes: {
fontHeightEm: 10,
},
actionProbability: 0.95,
banditKey: 'launch-button-treatment',
modelVersion: '3249',
optimalityGap: 0,
});
});
});
});
13 changes: 11 additions & 2 deletions src/decoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ export function decodeShard(shard: Shard): Shard {
}

export function decodeObject(obj: Record<string, string>): Record<string, string> {
return decodeObjectTo(obj, (v: string) => v);
}
export function decodeObjectTo<T>(
obj: Record<string, string>,
transform: (v: string) => T,
): Record<string, T> {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [decodeBase64(key), decodeBase64(value)]),
Object.entries(obj).map(([key, value]) => [decodeBase64(key), transform(decodeBase64(value))]),
);
}

Expand All @@ -99,7 +105,10 @@ export function decodePrecomputedBandit(
banditKey: decodeBase64(precomputedBandit.banditKey),
action: decodeBase64(precomputedBandit.action),
modelVersion: decodeBase64(precomputedBandit.modelVersion),
actionNumericAttributes: decodeObject(precomputedBandit.actionNumericAttributes ?? {}),
actionNumericAttributes: decodeObjectTo<number>(
precomputedBandit.actionNumericAttributes ?? {},
(v) => +v, // Convert to a number
),
actionCategoricalAttributes: decodeObject(precomputedBandit.actionCategoricalAttributes ?? {}),
};
}
41 changes: 40 additions & 1 deletion src/obfuscation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { decodeBase64, encodeBase64 } from './obfuscation';
import { IPrecomputedBandit } from './interfaces';
import { decodeBase64, encodeBase64, obfuscatePrecomputedBanditMap } from './obfuscation';

describe('obfuscation', () => {
it('encodes strings to base64', () => {
Expand Down Expand Up @@ -27,4 +28,42 @@ describe('obfuscation', () => {

expect(decodeBase64('a8O8bW1lcnQ=')).toEqual('kümmert');
});

describe('bandit obfuscation', () => {
it('obfuscates precomputed bandits', () => {
const bandit: IPrecomputedBandit = {
action: 'greenBackground',
actionCategoricalAttributes: {
color: 'green',
type: 'background',
},
actionNumericAttributes: {
fontHeightEm: 10,
},
actionProbability: 0.95,
banditKey: 'launch-button-treatment',
modelVersion: '3249',
optimalityGap: 0,
};

const encodedBandit = obfuscatePrecomputedBanditMap('', {
'launch-button-treatment': bandit,
});

expect(encodedBandit).toEqual({
'0ae2ece7bf09e40dd6b28a02574a4826': {
action: 'Z3JlZW5CYWNrZ3JvdW5k',
actionCategoricalAttributes: {
'Y29sb3I=': 'Z3JlZW4=',
'dHlwZQ==': 'YmFja2dyb3VuZA==',
},
actionNumericAttributes: { Zm9udEhlaWdodEVt: 'MTA=' },
actionProbability: 0.95,
banditKey: 'bGF1bmNoLWJ1dHRvbi10cmVhdG1lbnQ=',
modelVersion: 'MzI0OQ==',
optimalityGap: 0,
},
});
});
});
});
Loading