@naturalcycles/abba 2.13.0 → 2.14.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 CHANGED
@@ -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
  */
@@ -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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/abba",
3
3
  "type": "module",
4
- "version": "2.13.0",
4
+ "version": "2.14.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/db-lib": "^10",
7
7
  "@naturalcycles/js-lib": "^15",
package/src/abba.ts CHANGED
@@ -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(experimentId: string, updatedExclusions: string[]): Promise<void> {
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[] = []
@@ -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(buckets: (Unsaved<Bucket> | BucketInput)[]): void {
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