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

feat: Add JS experiment client #26

Merged
merged 1 commit into from
May 6, 2024
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
54 changes: 53 additions & 1 deletion clients/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as jsonLogic from 'json-logic-js';
import { deepMerge } from './utils/deepMerge';
import { compareSemanticIsGreater } from './utils/operations'
import { IObject, Dimension } from './types'
import { IObject, Dimension, Experiments, Variant, VariantType, Variants } from './types'

type DataFromCacApi = {
contexts: Array<Dimension>;
Expand Down Expand Up @@ -39,3 +39,55 @@ export class CacReader {
return deepMerge(targetConfig, ...requiredOverrides);
}
}

export class ExperimentReader {
experiments: Experiments;

constructor(experiments: Experiments) {
this.experiments = experiments;
}

public getApplicableVariant(data: IObject, toss: number): Array<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

the type number allows floating point values too, can we add an assertion that toss is always a positive integer?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add the toss isInteger check here?

if (!Number.isInteger(toss)) {
throw new Error("Invalid toss, valid range: -1 to 100");
}
const experiments = this.getSatisfiedExperiments(data);
const variants = [];
for (const exp of experiments) {
const v = this.decideVariant(exp.traffic_percentage, exp.variants, toss);
if (v) {
variants.push(v.id)
}
}
return variants;
};

getSatisfiedExperiments(data: IObject): Experiments {
return this.experiments.filter(exp => jsonLogic.apply(exp.context, data));
};

// decide which variant to return among all applicable experiments
decideVariant(
traffic: number,
Datron marked this conversation as resolved.
Show resolved Hide resolved
applicable_variants: Variants,
toss: number,
): Variant | undefined {
if (!Number.isInteger(traffic) || !Number.isInteger(toss)) {
return undefined;
}
if (toss < 0) {
Datron marked this conversation as resolved.
Show resolved Hide resolved
for (const variant of applicable_variants) {
if (variant.variant_type == VariantType.EXPERIMENTAL) {
return variant;
}
}
}
const variant_count = applicable_variants.length;
const range = traffic * variant_count;
if (toss >= range) {
return undefined;
}
const index = Math.floor(toss / traffic);
return applicable_variants[index];
}
}
32 changes: 31 additions & 1 deletion clients/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,34 @@ export type DimensionConfig = {

export type IIsObject = {
(item: any): boolean;
}
}

export enum VariantType {
CONTROL = "CONTROL",
EXPERIMENTAL = "EXPERIMENTAL",
}

export type Variant = {
id: String,
overrides: Object,
variant_type: VariantType,
}

export type Variants = Array<Variant>;

export enum ExperimentStatusType {
CREATED = "CREATED",
INPROGRESS = "INPROGRESS",
CONCLUDED = "CONCLUDED",
}

export type Experiment = {
variants: Variants,
name: String,
id: String,
traffic_percentage: number,
Datron marked this conversation as resolved.
Show resolved Hide resolved
context: Object,
status: ExperimentStatusType,
}

export type Experiments = Array<Experiment>;
Loading