@naturalcycles/abba 2.13.0 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abba.d.ts +4 -4
- package/dist/abba.js +4 -4
- package/dist/dao/experiment.dao.js +1 -0
- package/dist/dao/userAssignment.dao.d.ts +3 -3
- package/dist/util.d.ts +5 -5
- package/package.json +1 -1
- package/src/abba.ts +10 -7
- package/src/dao/experiment.dao.ts +1 -0
- package/src/dao/userAssignment.dao.ts +4 -4
- package/src/util.ts +7 -5
package/dist/abba.d.ts
CHANGED
|
@@ -11,11 +11,11 @@ export declare class Abba {
|
|
|
11
11
|
* Returns all experiments.
|
|
12
12
|
* Cached (see CACHE_TTL)
|
|
13
13
|
*/
|
|
14
|
-
getAllExperimentsWithBuckets(
|
|
14
|
+
getAllExperimentsWithBuckets(opt?: GetAllExperimentsOpts): Promise<ExperimentWithBuckets[]>;
|
|
15
15
|
/**
|
|
16
16
|
* Returns all experiments.
|
|
17
17
|
*/
|
|
18
|
-
getAllExperimentsWithBucketsNoCache(
|
|
18
|
+
getAllExperimentsWithBucketsNoCache(opt?: GetAllExperimentsOpts): Promise<ExperimentWithBuckets[]>;
|
|
19
19
|
getUserExperiments(userId: string): Promise<UserExperiment[]>;
|
|
20
20
|
/**
|
|
21
21
|
* Changes all user assignments from one userId to another, as long as no
|
|
@@ -26,12 +26,12 @@ export declare class Abba {
|
|
|
26
26
|
* Creates a new experiment.
|
|
27
27
|
* Cold method.
|
|
28
28
|
*/
|
|
29
|
-
createExperiment(input: ExperimentInput, buckets: BucketInput[]): Promise<ExperimentWithBuckets>;
|
|
29
|
+
createExperiment(input: ExperimentInput, buckets: readonly BucketInput[]): Promise<ExperimentWithBuckets>;
|
|
30
30
|
/**
|
|
31
31
|
* Update experiment information, will also validate the buckets' ratio if experiment.active is true
|
|
32
32
|
* Cold method.
|
|
33
33
|
*/
|
|
34
|
-
saveExperiment(experiment: Experiment, buckets: (Unsaved<Bucket> | BucketInput)[]): Promise<ExperimentWithBuckets>;
|
|
34
|
+
saveExperiment(experiment: Experiment, buckets: readonly (Unsaved<Bucket> | BucketInput)[]): Promise<ExperimentWithBuckets>;
|
|
35
35
|
/**
|
|
36
36
|
* Ensures that mutual exclusions are maintained
|
|
37
37
|
*/
|
package/dist/abba.js
CHANGED
|
@@ -25,14 +25,14 @@ export class Abba {
|
|
|
25
25
|
* Returns all experiments.
|
|
26
26
|
* Cached (see CACHE_TTL)
|
|
27
27
|
*/
|
|
28
|
-
async getAllExperimentsWithBuckets(
|
|
29
|
-
return await this.getAllExperimentsWithBucketsNoCache(
|
|
28
|
+
async getAllExperimentsWithBuckets(opt) {
|
|
29
|
+
return await this.getAllExperimentsWithBucketsNoCache(opt);
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Returns all experiments.
|
|
33
33
|
*/
|
|
34
|
-
async getAllExperimentsWithBucketsNoCache(
|
|
35
|
-
const experiments = await this.experimentDao.getAllExperiments(
|
|
34
|
+
async getAllExperimentsWithBucketsNoCache(opt) {
|
|
35
|
+
const experiments = await this.experimentDao.getAllExperiments(opt);
|
|
36
36
|
const buckets = await this.bucketDao.getAll();
|
|
37
37
|
return experiments.map(experiment => ({
|
|
38
38
|
...experiment,
|
|
@@ -41,6 +41,7 @@ export function experimentDao(db) {
|
|
|
41
41
|
*/
|
|
42
42
|
function parseMySQLDate(date) {
|
|
43
43
|
// @ts-expect-error MySQL may return Date instead of string
|
|
44
|
+
// oxlint-disable-next-line no-restricted-globals -- the MySQL driver may return a Date
|
|
44
45
|
if (date instanceof Date)
|
|
45
46
|
return localDate(date).toISODate();
|
|
46
47
|
return date;
|
|
@@ -3,16 +3,16 @@ import { CommonDao } from '@naturalcycles/db-lib/dao';
|
|
|
3
3
|
import type { UserAssignment } from '../types.js';
|
|
4
4
|
export declare class UserAssignmentDao extends CommonDao<UserAssignment> {
|
|
5
5
|
getUserAssignmentByExperimentId(userId: string, experimentId: string): Promise<UserAssignment | null>;
|
|
6
|
-
getUserAssigmentsByExperimentIds(userId: string, experimentIds: string[]): Promise<UserAssignment[]>;
|
|
6
|
+
getUserAssigmentsByExperimentIds(userId: string, experimentIds: readonly string[]): Promise<UserAssignment[]>;
|
|
7
7
|
/**
|
|
8
8
|
* Returns every UserAssignment whose `userId` is in `userIds` AND whose `experimentId`
|
|
9
9
|
* is in `experimentIds`. This is the cross-product, not paired lookup: callers must
|
|
10
10
|
* filter the result by the specific (userId, experimentId) pairs they care about.
|
|
11
11
|
*/
|
|
12
|
-
getByUserIdsAndExperimentIds(userIds: string[], experimentIds: string[]): Promise<UserAssignment[]>;
|
|
12
|
+
getByUserIdsAndExperimentIds(userIds: readonly string[], experimentIds: readonly string[]): Promise<UserAssignment[]>;
|
|
13
13
|
deleteByExperimentId(experimentId: string): Promise<void>;
|
|
14
14
|
getCountByExperimentId(experimentId: string): Promise<number>;
|
|
15
15
|
getCountByBucketId(bucketId: string): Promise<number>;
|
|
16
|
-
getUserIdsByBucketIds(bucketIds: string[]): Promise<string[]>;
|
|
16
|
+
getUserIdsByBucketIds(bucketIds: readonly string[]): Promise<string[]>;
|
|
17
17
|
}
|
|
18
18
|
export declare function userAssignmentDao(db: CommonDB): UserAssignmentDao;
|
package/dist/util.d.ts
CHANGED
|
@@ -16,15 +16,15 @@ export declare const randomService: RandomService;
|
|
|
16
16
|
/**
|
|
17
17
|
* Determines a users assignment for this experiment. Returns null if they are not considered to be in the sampling group
|
|
18
18
|
*/
|
|
19
|
-
export declare function determineAssignment(sampling: number, buckets: Bucket[]): Bucket | null;
|
|
19
|
+
export declare function determineAssignment(sampling: number, buckets: readonly Bucket[]): Bucket | null;
|
|
20
20
|
/**
|
|
21
21
|
* Determines which bucket a user assignment will recieve
|
|
22
22
|
*/
|
|
23
|
-
export declare function determineBucket(buckets: Bucket[]): Bucket;
|
|
23
|
+
export declare function determineBucket(buckets: readonly Bucket[]): Bucket;
|
|
24
24
|
/**
|
|
25
25
|
* Validate the total ratio of the buckets equals 100
|
|
26
26
|
*/
|
|
27
|
-
export declare function validateTotalBucketRatio(buckets: (Unsaved<Bucket> | BucketInput)[]): void;
|
|
27
|
+
export declare function validateTotalBucketRatio(buckets: readonly (Unsaved<Bucket> | BucketInput)[]): void;
|
|
28
28
|
/**
|
|
29
29
|
* Validate a users segmentation data against multiple rules. Returns false if any fail
|
|
30
30
|
*
|
|
@@ -32,7 +32,7 @@ export declare function validateTotalBucketRatio(buckets: (Unsaved<Bucket> | Buc
|
|
|
32
32
|
* @param segmentationData
|
|
33
33
|
* @returns
|
|
34
34
|
*/
|
|
35
|
-
export declare function validateSegmentationRules(rules: SegmentationRule[], segmentationData: SegmentationData): boolean;
|
|
35
|
+
export declare function validateSegmentationRules(rules: readonly SegmentationRule[], segmentationData: SegmentationData): boolean;
|
|
36
36
|
/**
|
|
37
37
|
* Map of segmentation rule validators
|
|
38
38
|
*/
|
|
@@ -45,5 +45,5 @@ export declare function canGenerateNewAssignments(experiment: Experiment, exclus
|
|
|
45
45
|
* Returns an object that includes keys of all experimentIds a user should not be assigned to
|
|
46
46
|
* based on a combination of existing assignments and mutual exclusion configuration
|
|
47
47
|
*/
|
|
48
|
-
export declare function getUserExclusionSet(experiments: UserExperiment[]): ExclusionSet;
|
|
48
|
+
export declare function getUserExclusionSet(experiments: readonly UserExperiment[]): ExclusionSet;
|
|
49
49
|
export {};
|
package/package.json
CHANGED
package/src/abba.ts
CHANGED
|
@@ -54,18 +54,18 @@ export class Abba {
|
|
|
54
54
|
*/
|
|
55
55
|
@_Memo({ cacheFactory: () => new LRUMemoCache({ ttl: CACHE_TTL, max: 1 }) })
|
|
56
56
|
async getAllExperimentsWithBuckets(
|
|
57
|
-
|
|
57
|
+
opt?: GetAllExperimentsOpts,
|
|
58
58
|
): Promise<ExperimentWithBuckets[]> {
|
|
59
|
-
return await this.getAllExperimentsWithBucketsNoCache(
|
|
59
|
+
return await this.getAllExperimentsWithBucketsNoCache(opt)
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* Returns all experiments.
|
|
64
64
|
*/
|
|
65
65
|
async getAllExperimentsWithBucketsNoCache(
|
|
66
|
-
|
|
66
|
+
opt?: GetAllExperimentsOpts,
|
|
67
67
|
): Promise<ExperimentWithBuckets[]> {
|
|
68
|
-
const experiments = await this.experimentDao.getAllExperiments(
|
|
68
|
+
const experiments = await this.experimentDao.getAllExperiments(opt)
|
|
69
69
|
const buckets = await this.bucketDao.getAll()
|
|
70
70
|
|
|
71
71
|
return experiments.map(experiment => ({
|
|
@@ -126,7 +126,7 @@ export class Abba {
|
|
|
126
126
|
*/
|
|
127
127
|
async createExperiment(
|
|
128
128
|
input: ExperimentInput,
|
|
129
|
-
buckets: BucketInput[],
|
|
129
|
+
buckets: readonly BucketInput[],
|
|
130
130
|
): Promise<ExperimentWithBuckets> {
|
|
131
131
|
if (input.status === AssignmentStatus.Active) {
|
|
132
132
|
validateTotalBucketRatio(buckets)
|
|
@@ -158,7 +158,7 @@ export class Abba {
|
|
|
158
158
|
*/
|
|
159
159
|
async saveExperiment(
|
|
160
160
|
experiment: Experiment,
|
|
161
|
-
buckets: (Unsaved<Bucket> | BucketInput)[],
|
|
161
|
+
buckets: readonly (Unsaved<Bucket> | BucketInput)[],
|
|
162
162
|
): Promise<ExperimentWithBuckets> {
|
|
163
163
|
if (experiment.status === AssignmentStatus.Active) {
|
|
164
164
|
validateTotalBucketRatio(buckets)
|
|
@@ -183,7 +183,10 @@ export class Abba {
|
|
|
183
183
|
/**
|
|
184
184
|
* Ensures that mutual exclusions are maintained
|
|
185
185
|
*/
|
|
186
|
-
private async updateExclusions(
|
|
186
|
+
private async updateExclusions(
|
|
187
|
+
experimentId: string,
|
|
188
|
+
updatedExclusions: readonly string[],
|
|
189
|
+
): Promise<void> {
|
|
187
190
|
const experiments = await this.experimentDao.getAll()
|
|
188
191
|
|
|
189
192
|
const requiresUpdating: Experiment[] = []
|
|
@@ -49,6 +49,7 @@ export function experimentDao(db: CommonDB): ExperimentDao {
|
|
|
49
49
|
*/
|
|
50
50
|
function parseMySQLDate(date: string): IsoDate {
|
|
51
51
|
// @ts-expect-error MySQL may return Date instead of string
|
|
52
|
+
// oxlint-disable-next-line no-restricted-globals -- the MySQL driver may return a Date
|
|
52
53
|
if (date instanceof Date) return localDate(date).toISODate()
|
|
53
54
|
return date as IsoDate
|
|
54
55
|
}
|
|
@@ -15,7 +15,7 @@ export class UserAssignmentDao extends CommonDao<UserAssignment> {
|
|
|
15
15
|
|
|
16
16
|
async getUserAssigmentsByExperimentIds(
|
|
17
17
|
userId: string,
|
|
18
|
-
experimentIds: string[],
|
|
18
|
+
experimentIds: readonly string[],
|
|
19
19
|
): Promise<UserAssignment[]> {
|
|
20
20
|
const query = this.query().filterEq('userId', userId).filterIn('experimentId', experimentIds)
|
|
21
21
|
return await this.runQuery(query)
|
|
@@ -27,8 +27,8 @@ export class UserAssignmentDao extends CommonDao<UserAssignment> {
|
|
|
27
27
|
* filter the result by the specific (userId, experimentId) pairs they care about.
|
|
28
28
|
*/
|
|
29
29
|
async getByUserIdsAndExperimentIds(
|
|
30
|
-
userIds: string[],
|
|
31
|
-
experimentIds: string[],
|
|
30
|
+
userIds: readonly string[],
|
|
31
|
+
experimentIds: readonly string[],
|
|
32
32
|
): Promise<UserAssignment[]> {
|
|
33
33
|
if (!userIds.length || !experimentIds.length) return []
|
|
34
34
|
const query = this.query().filterIn('userId', userIds).filterIn('experimentId', experimentIds)
|
|
@@ -47,7 +47,7 @@ export class UserAssignmentDao extends CommonDao<UserAssignment> {
|
|
|
47
47
|
return await this.query().filterEq('bucketId', bucketId).runQueryCount()
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
async getUserIdsByBucketIds(bucketIds: string[]): Promise<string[]> {
|
|
50
|
+
async getUserIdsByBucketIds(bucketIds: readonly string[]): Promise<string[]> {
|
|
51
51
|
if (!bucketIds.length) return []
|
|
52
52
|
|
|
53
53
|
const userIds = await this.query()
|
package/src/util.ts
CHANGED
|
@@ -51,7 +51,7 @@ export const randomService = new RandomService()
|
|
|
51
51
|
/**
|
|
52
52
|
* Determines a users assignment for this experiment. Returns null if they are not considered to be in the sampling group
|
|
53
53
|
*/
|
|
54
|
-
export function determineAssignment(sampling: number, buckets: Bucket[]): Bucket | null {
|
|
54
|
+
export function determineAssignment(sampling: number, buckets: readonly Bucket[]): Bucket | null {
|
|
55
55
|
// Should this person be considered for the experiment?
|
|
56
56
|
if (randomService.rollDie() > sampling) {
|
|
57
57
|
return null
|
|
@@ -64,7 +64,7 @@ export function determineAssignment(sampling: number, buckets: Bucket[]): Bucket
|
|
|
64
64
|
/**
|
|
65
65
|
* Determines which bucket a user assignment will recieve
|
|
66
66
|
*/
|
|
67
|
-
export function determineBucket(buckets: Bucket[]): Bucket {
|
|
67
|
+
export function determineBucket(buckets: readonly Bucket[]): Bucket {
|
|
68
68
|
const bucketRoll = randomService.rollDie()
|
|
69
69
|
let range: [number, number] | undefined
|
|
70
70
|
const bucket = buckets.find(b => {
|
|
@@ -89,7 +89,9 @@ export function determineBucket(buckets: Bucket[]): Bucket {
|
|
|
89
89
|
/**
|
|
90
90
|
* Validate the total ratio of the buckets equals 100
|
|
91
91
|
*/
|
|
92
|
-
export function validateTotalBucketRatio(
|
|
92
|
+
export function validateTotalBucketRatio(
|
|
93
|
+
buckets: readonly (Unsaved<Bucket> | BucketInput)[],
|
|
94
|
+
): void {
|
|
93
95
|
const bucketSum = buckets.reduce((sum, current) => sum + current.ratio, 0)
|
|
94
96
|
_assert(
|
|
95
97
|
bucketSum === 100,
|
|
@@ -106,7 +108,7 @@ export function validateTotalBucketRatio(buckets: (Unsaved<Bucket> | BucketInput
|
|
|
106
108
|
* @returns
|
|
107
109
|
*/
|
|
108
110
|
export function validateSegmentationRules(
|
|
109
|
-
rules: SegmentationRule[],
|
|
111
|
+
rules: readonly SegmentationRule[],
|
|
110
112
|
segmentationData: SegmentationData,
|
|
111
113
|
): boolean {
|
|
112
114
|
for (const rule of rules) {
|
|
@@ -175,7 +177,7 @@ export function canGenerateNewAssignments(
|
|
|
175
177
|
* Returns an object that includes keys of all experimentIds a user should not be assigned to
|
|
176
178
|
* based on a combination of existing assignments and mutual exclusion configuration
|
|
177
179
|
*/
|
|
178
|
-
export function getUserExclusionSet(experiments: UserExperiment[]): ExclusionSet {
|
|
180
|
+
export function getUserExclusionSet(experiments: readonly UserExperiment[]): ExclusionSet {
|
|
179
181
|
const exclusionSet: ExclusionSet = new Set()
|
|
180
182
|
experiments.forEach(experiment => {
|
|
181
183
|
const { userAssignment } = experiment
|