assignment-user-matcher 1.0.10 → 1.0.11
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/.github/workflows/npm-publish.yml +67 -69
- package/LICENCE +53 -0
- package/README.md +186 -148
- package/docs/README.md +362 -65
- package/docs/classes/default.md +107 -81
- package/docs/interfaces/User.md +16 -5
- package/docs/modules.md +21 -21
- package/package.json +2 -2
- package/src/matcher.class.ts +262 -93
- package/tests/matcher.test.ts +85 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assignment-user-matcher",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"author": "Viljar Võidula",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"rimraf": "4.4.1",
|
|
28
28
|
"sinon": "20.0.0",
|
|
29
29
|
"ts-node": "10.9.2",
|
|
30
|
-
"typedoc": "0.
|
|
30
|
+
"typedoc": "^0.25.0",
|
|
31
31
|
"typedoc-plugin-markdown": "3.17.1",
|
|
32
32
|
"typescript": "4.9.5"
|
|
33
33
|
},
|
package/src/matcher.class.ts
CHANGED
|
@@ -4,6 +4,9 @@ export type RedisClientType = ReturnType<typeof createClient>;
|
|
|
4
4
|
export interface User {
|
|
5
5
|
id: string;
|
|
6
6
|
tags: string[];
|
|
7
|
+
// Optional routing weights per tag; if present, matching will use these weights.
|
|
8
|
+
// Example: { english: 100, dutch: 30, brazil: 100, german: 0 }
|
|
9
|
+
routingWeights?: Record<string, number>;
|
|
7
10
|
[key: string]: any;
|
|
8
11
|
}
|
|
9
12
|
|
|
@@ -60,7 +63,10 @@ export default class AssignmentMatcher {
|
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
private async initRedis() {
|
|
63
|
-
|
|
66
|
+
// Only attempt to connect if the client isn't already connected
|
|
67
|
+
if (!this.redisClient.isOpen) {
|
|
68
|
+
await this.redisClient.connect();
|
|
69
|
+
}
|
|
64
70
|
return this;
|
|
65
71
|
}
|
|
66
72
|
|
|
@@ -89,13 +95,14 @@ export default class AssignmentMatcher {
|
|
|
89
95
|
.multi()
|
|
90
96
|
.hSet(this.assignmentsRefKey, id, JSON.stringify(assignment))
|
|
91
97
|
.hSet(assignmentPriorityKey, 'priority', priority)
|
|
92
|
-
|
|
98
|
+
// Ensure 'default' tag is persisted consistently when enabled
|
|
99
|
+
.hSet(assignmentTagsKey, 'tags', tags.join(',') + (this.enableDefaultMatching ? ',default' : ''))
|
|
93
100
|
.zAdd(this.assignmentsKey, {
|
|
94
101
|
score: priority,
|
|
95
102
|
value: id,
|
|
96
103
|
});
|
|
104
|
+
|
|
97
105
|
if (latitude && longitude) {
|
|
98
|
-
debugger;
|
|
99
106
|
multi.geoAdd(assignmentGeoKey, {
|
|
100
107
|
longitude,
|
|
101
108
|
latitude,
|
|
@@ -103,6 +110,16 @@ export default class AssignmentMatcher {
|
|
|
103
110
|
});
|
|
104
111
|
}
|
|
105
112
|
|
|
113
|
+
// Also index assignment into per-tag sorted sets (by priority)
|
|
114
|
+
// This enables efficient weighted matching via ZUNIONSTORE.
|
|
115
|
+
const indexTags = [...tags, ...(this.enableDefaultMatching ? ['default'] : [])];
|
|
116
|
+
for (const tag of indexTags) {
|
|
117
|
+
multi.zAdd(this.redisPrefix + `tag:${tag}:assignments`, {
|
|
118
|
+
score: Number(priority),
|
|
119
|
+
value: id,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
106
123
|
await multi.exec();
|
|
107
124
|
|
|
108
125
|
return assignment;
|
|
@@ -112,12 +129,22 @@ export default class AssignmentMatcher {
|
|
|
112
129
|
const assignmentPriorityKey = this.redisPrefix + `assignment:${id}:priority`;
|
|
113
130
|
const assignmentTagsKey = this.redisPrefix + `assignment:${id}:tags`;
|
|
114
131
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
132
|
+
// Fetch tags to remove from per-tag indices
|
|
133
|
+
const [tagsCsv] = await this.redisClient.multi().hGet(assignmentTagsKey, 'tags').exec();
|
|
134
|
+
|
|
135
|
+
const tags = (typeof tagsCsv === 'string' && tagsCsv.length > 0 ? tagsCsv.split(',') : []) as string[];
|
|
136
|
+
|
|
137
|
+
const multi = this.redisClient.multi();
|
|
138
|
+
// Remove from reference and per-assignment hashes
|
|
139
|
+
multi.hDel(this.assignmentsRefKey, id);
|
|
140
|
+
multi.del(assignmentPriorityKey);
|
|
141
|
+
multi.del(assignmentTagsKey);
|
|
142
|
+
// Remove from global zset
|
|
143
|
+
multi.zRem(this.assignmentsKey, id.toString());
|
|
144
|
+
// Remove from per-tag zsets
|
|
145
|
+
for (const tag of tags) {
|
|
146
|
+
multi.zRem(this.redisPrefix + `tag:${tag}:assignments`, id.toString());
|
|
147
|
+
}
|
|
121
148
|
|
|
122
149
|
await multi.exec();
|
|
123
150
|
return id;
|
|
@@ -144,13 +171,33 @@ export default class AssignmentMatcher {
|
|
|
144
171
|
assignmentPriority: string | number,
|
|
145
172
|
assignmentId?: string,
|
|
146
173
|
): Promise<[number, number]> {
|
|
174
|
+
// If weighted skills are present, use them for scoring
|
|
175
|
+
if (user.routingWeights && Object.keys(user.routingWeights).length > 0) {
|
|
176
|
+
const aTags = assignmentTags ? assignmentTags.split(',') : [];
|
|
177
|
+
// If any assignment tag has explicit weight 0, treat as a hard exclude
|
|
178
|
+
for (const t of aTags) {
|
|
179
|
+
if (user.routingWeights[t] === 0) {
|
|
180
|
+
return [0, Number(assignmentPriority) || 0];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
let weightSum = 0;
|
|
184
|
+
for (const t of aTags) {
|
|
185
|
+
const w = user.routingWeights[t];
|
|
186
|
+
if (typeof w === 'number' && w > 0) weightSum += w;
|
|
187
|
+
}
|
|
188
|
+
const base = Number(assignmentPriority) || 0;
|
|
189
|
+
// score: total positive weight overlap; priority: base weighted slightly by weight
|
|
190
|
+
return [weightSum, base + weightSum];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Fallback to default tag-intersection scoring
|
|
147
194
|
const userTagSet = new Set(user.tags);
|
|
148
|
-
const assignmentTagSet = new Set(assignmentTags.split(','));
|
|
195
|
+
const assignmentTagSet = new Set((assignmentTags || '').split(',').filter(Boolean));
|
|
149
196
|
const intersection = new Set([...userTagSet].filter((tag) => assignmentTagSet.has(tag)));
|
|
150
197
|
|
|
151
|
-
const score = intersection.size / userTagSet.size;
|
|
152
|
-
|
|
153
|
-
return [score || 0,
|
|
198
|
+
const score = userTagSet.size > 0 ? intersection.size / userTagSet.size : 0;
|
|
199
|
+
const base = Number(assignmentPriority) || 0;
|
|
200
|
+
return [score || 0, base + (score || 0)];
|
|
154
201
|
}
|
|
155
202
|
private async calculatePriority(...args: (Assignment | undefined)[]) {
|
|
156
203
|
const [assignment] = args;
|
|
@@ -186,102 +233,224 @@ export default class AssignmentMatcher {
|
|
|
186
233
|
|
|
187
234
|
async setAssignmentPriority(id: string, priority: number) {
|
|
188
235
|
const assignmentPriorityKey = this.redisPrefix + `assignment:${id}:priority`;
|
|
189
|
-
|
|
236
|
+
// Fetch tags to update per-tag indices
|
|
237
|
+
const assignmentTagsKey = this.redisPrefix + `assignment:${id}:tags`;
|
|
238
|
+
const [existsRef, tagsCsv] = await this.redisClient
|
|
239
|
+
.multi()
|
|
240
|
+
.hExists(this.assignmentsRefKey, id)
|
|
241
|
+
.hGet(assignmentTagsKey, 'tags')
|
|
242
|
+
.exec();
|
|
243
|
+
const tags = (typeof tagsCsv === 'string' && tagsCsv.length > 0 ? tagsCsv.split(',') : []) as string[];
|
|
244
|
+
|
|
245
|
+
const multi = this.redisClient.multi();
|
|
246
|
+
multi.hSet(assignmentPriorityKey, 'priority', priority);
|
|
247
|
+
if (existsRef) {
|
|
248
|
+
// Update global zset score and per-tag zset scores only if assignment still exists
|
|
249
|
+
multi.zAdd(this.assignmentsKey, { score: Number(priority), value: id });
|
|
250
|
+
for (const tag of tags) {
|
|
251
|
+
multi.zAdd(this.redisPrefix + `tag:${tag}:assignments`, { score: Number(priority), value: id });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
await multi.exec();
|
|
190
255
|
|
|
191
256
|
return { id, priority };
|
|
192
257
|
}
|
|
193
258
|
|
|
194
259
|
async setAssignmentPriorityByTags(tags: string[], priority: number) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
260
|
+
// Update priorities for all assignments that include any of the specified tags.
|
|
261
|
+
const assignments = await this.getAllAssignments();
|
|
262
|
+
const relevantAssignments = assignments.filter((assignment) => {
|
|
263
|
+
const assignmentTagSet = new Set(assignment.tags);
|
|
264
|
+
return tags.some((tag) => assignmentTagSet.has(tag));
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
if (relevantAssignments.length > 0) {
|
|
268
|
+
const multi = this.redisClient.multi();
|
|
269
|
+
for (const assignment of relevantAssignments) {
|
|
270
|
+
const id = assignment.id;
|
|
271
|
+
const assignmentPriorityKey = this.redisPrefix + `assignment:${id}:priority`;
|
|
272
|
+
// Update the stored priority
|
|
273
|
+
multi.hSet(assignmentPriorityKey, 'priority', priority);
|
|
274
|
+
// Update global zset score
|
|
275
|
+
multi.zAdd(this.assignmentsKey, { score: Number(priority), value: id });
|
|
276
|
+
// Update per-tag zset scores
|
|
277
|
+
const idxTags = [...assignment.tags, ...(this.enableDefaultMatching ? ['default'] : [])];
|
|
278
|
+
for (const tag of idxTags) {
|
|
279
|
+
multi.zAdd(this.redisPrefix + `tag:${tag}:assignments`, { score: Number(priority), value: id });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
await multi.exec();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return relevantAssignments.map((assignment) => ({ id: assignment.id, priority }));
|
|
286
|
+
}
|
|
211
287
|
|
|
212
288
|
private async getUserRelatedAssignments(user: User) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
289
|
+
// Build candidate assignments using per-tag zset union with user weights for scalability.
|
|
290
|
+
const positiveWeights: Array<{ tag: string; weight: number }> = [];
|
|
291
|
+
const zeroWeightTags: string[] = [];
|
|
292
|
+
|
|
293
|
+
if (user.routingWeights && Object.keys(user.routingWeights).length > 0) {
|
|
294
|
+
for (const [tag, w] of Object.entries(user.routingWeights)) {
|
|
295
|
+
if (typeof w === 'number' && w > 0) positiveWeights.push({ tag, weight: w });
|
|
296
|
+
else if (w === 0) zeroWeightTags.push(tag);
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
// Fallback: use unweighted tags (weight 1)
|
|
300
|
+
for (const tag of user.tags || []) {
|
|
301
|
+
positiveWeights.push({ tag, weight: 1 });
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Ensure default tag is included if enabled and not explicitly weighted to zero
|
|
306
|
+
if (this.enableDefaultMatching) {
|
|
307
|
+
const defaultInPos = positiveWeights.find((t) => t.tag === 'default');
|
|
308
|
+
const defaultZero = zeroWeightTags.includes('default');
|
|
309
|
+
if (!defaultInPos && !defaultZero) positiveWeights.push({ tag: 'default', weight: 1 });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const userAssignments: Array<{ id: string; priority: number }> = [];
|
|
313
|
+
|
|
314
|
+
if (positiveWeights.length === 0) return userAssignments;
|
|
315
|
+
|
|
316
|
+
const tempKey = this.redisPrefix + `tmp:user:${user.id}:candidates`;
|
|
317
|
+
const tempZeroKey = this.redisPrefix + `tmp:user:${user.id}:exclude`;
|
|
318
|
+
const tempFinalKey = this.redisPrefix + `tmp:user:${user.id}:final`;
|
|
319
|
+
|
|
320
|
+
const unionKeys = positiveWeights.map((pw) => this.redisPrefix + `tag:${pw.tag}:assignments`);
|
|
321
|
+
const unionWeights = positiveWeights.map((pw) => pw.weight);
|
|
322
|
+
|
|
323
|
+
const multi = this.redisClient.multi();
|
|
324
|
+
// Weighted union of positive tag assignment zsets
|
|
325
|
+
multi.zUnionStore(tempKey, unionKeys, { WEIGHTS: unionWeights });
|
|
326
|
+
// Optional exclude set for zero-weight tags
|
|
327
|
+
if (zeroWeightTags.length > 0) {
|
|
328
|
+
const zeroKeys = zeroWeightTags.map((t) => this.redisPrefix + `tag:${t}:assignments`);
|
|
329
|
+
multi.zUnionStore(tempZeroKey, zeroKeys);
|
|
330
|
+
// Final = candidates - exclude
|
|
331
|
+
multi.zDiffStore(tempFinalKey, [tempKey, tempZeroKey]);
|
|
332
|
+
// Set short TTLs to clean up
|
|
333
|
+
multi.expire(tempKey, 5);
|
|
334
|
+
multi.expire(tempZeroKey, 5);
|
|
335
|
+
multi.expire(tempFinalKey, 5);
|
|
336
|
+
} else {
|
|
337
|
+
// No exclude; just use tempKey as final (copy into final for uniformity)
|
|
338
|
+
multi.zUnionStore(tempFinalKey, [tempKey]);
|
|
339
|
+
multi.expire(tempKey, 5);
|
|
340
|
+
multi.expire(tempFinalKey, 5);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
await multi.exec();
|
|
344
|
+
|
|
345
|
+
// Pull top-N in ascending score to preserve previous behavior on ties (lex ascending)
|
|
346
|
+
const top = await this.redisClient.zRangeWithScores(tempFinalKey, 0, this.relevantBatchSize - 1);
|
|
347
|
+
|
|
348
|
+
if (top.length === 0) return userAssignments;
|
|
349
|
+
|
|
350
|
+
// Fetch per-assignment stored (base) priority and tags in a single pipeline (fewer round trips)
|
|
351
|
+
const batched = this.redisClient.multi();
|
|
352
|
+
for (const entry of top) {
|
|
353
|
+
const assignmentId = (entry as any).value;
|
|
220
354
|
const assignmentPriorityKey = this.redisPrefix + `assignment:${assignmentId}:priority`;
|
|
221
355
|
const assignmentTagsKey = this.redisPrefix + `assignment:${assignmentId}:tags`;
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
.hGet(assignmentPriorityKey, 'priority')
|
|
225
|
-
.hGet(assignmentTagsKey, 'tags')
|
|
226
|
-
.exec();
|
|
227
|
-
|
|
228
|
-
returnAssignments.push({
|
|
229
|
-
id: assignmentId,
|
|
230
|
-
priority,
|
|
231
|
-
tags,
|
|
232
|
-
});
|
|
356
|
+
batched.hGet(assignmentPriorityKey, 'priority');
|
|
357
|
+
batched.hGet(assignmentTagsKey, 'tags');
|
|
233
358
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
359
|
+
const flat = await batched.exec();
|
|
360
|
+
const details = [] as Array<{ id: string; basePriority: number; tags: string }>;
|
|
361
|
+
for (let i = 0; i < top.length; i++) {
|
|
362
|
+
const assignmentId = (top[i] as any).value;
|
|
363
|
+
const priority = Number(flat[i * 2]) || 0;
|
|
364
|
+
const tags = String(flat[i * 2 + 1] || '');
|
|
365
|
+
details.push({ id: assignmentId, basePriority: priority, tags });
|
|
366
|
+
}
|
|
367
|
+
// Prefer higher base priority first to satisfy tests and practical expectations
|
|
368
|
+
details.sort((a, b) => b.basePriority - a.basePriority);
|
|
369
|
+
|
|
370
|
+
const selected: Array<{ id: string; tags: string }> = [];
|
|
371
|
+
for (const d of details) {
|
|
372
|
+
if (userAssignments.length >= this.maxUserBacklogSize) break;
|
|
373
|
+
// Use custom matchScore (supports weights) for a final check and tie-break
|
|
374
|
+
const [score, combinedPriority] = await this.matchScore(user, d.tags, d.basePriority, d.id);
|
|
375
|
+
if (score && combinedPriority) {
|
|
376
|
+
userAssignments.push({ id: d.id, priority: combinedPriority });
|
|
377
|
+
selected.push({ id: d.id, tags: d.tags });
|
|
238
378
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Batch-remove all selected assignments from indexes and references to reduce round trips
|
|
382
|
+
if (selected.length > 0) {
|
|
383
|
+
const rm = this.redisClient.multi();
|
|
384
|
+
for (const s of selected) {
|
|
385
|
+
const assignmentPriorityKey = this.redisPrefix + `assignment:${s.id}:priority`;
|
|
386
|
+
const assignmentTagsKey = this.redisPrefix + `assignment:${s.id}:tags`;
|
|
387
|
+
// Remove from reference and per-assignment hashes
|
|
388
|
+
rm.hDel(this.assignmentsRefKey, s.id);
|
|
389
|
+
rm.del(assignmentPriorityKey);
|
|
390
|
+
rm.del(assignmentTagsKey);
|
|
391
|
+
// Remove from global zset
|
|
392
|
+
rm.zRem(this.assignmentsKey, s.id);
|
|
393
|
+
// Remove from per-tag zsets
|
|
394
|
+
const tags = (s.tags ? s.tags.split(',').filter(Boolean) : []) as string[];
|
|
395
|
+
for (const tag of tags) {
|
|
396
|
+
rm.zRem(this.redisPrefix + `tag:${tag}:assignments`, s.id);
|
|
397
|
+
}
|
|
253
398
|
}
|
|
399
|
+
await rm.exec();
|
|
254
400
|
}
|
|
401
|
+
|
|
255
402
|
return userAssignments;
|
|
256
403
|
}
|
|
257
404
|
async matchUsersAssignments(userId?: string): Promise<void> {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
405
|
+
// If a specific userId is provided, process only that user for precise per-user matching.
|
|
406
|
+
if (userId) {
|
|
407
|
+
const userJson = await this.redisClient.hGet(this.usersKey, userId);
|
|
408
|
+
if (!userJson) return;
|
|
409
|
+
const user = JSON.parse(userJson);
|
|
410
|
+
const userMatchesCount = await this.redisClient.sCard(this.redisPrefix + `user:${user.id}:assignments`);
|
|
411
|
+
if (userMatchesCount >= this.maxUserBacklogSize) return;
|
|
412
|
+
|
|
413
|
+
const relevantAssignments = await this.getUserRelatedAssignments(user);
|
|
414
|
+
const relevantAssignmentKeys = relevantAssignments
|
|
415
|
+
.sort((a, b) => b.priority - a.priority)
|
|
416
|
+
.map((a) => `assignment:${a.id}`);
|
|
417
|
+
|
|
418
|
+
if (relevantAssignmentKeys.length > 0) {
|
|
419
|
+
await this.redisClient.sAdd(this.redisPrefix + `user:${user.id}:assignments`, relevantAssignmentKeys);
|
|
420
|
+
}
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Otherwise, process all users in parallel as before.
|
|
425
|
+
let users: any[] = [];
|
|
426
|
+
let cursor = 0;
|
|
427
|
+
|
|
428
|
+
// Fetch all users in a single loop
|
|
429
|
+
do {
|
|
430
|
+
const { cursor: nextCursor, tuples } = await this.redisClient.hScan(this.usersKey, cursor);
|
|
431
|
+
users.push(...tuples.map((t: { value: string }) => JSON.parse(t.value)));
|
|
432
|
+
cursor = nextCursor;
|
|
433
|
+
} while (cursor !== 0);
|
|
434
|
+
|
|
435
|
+
// Process users in parallel
|
|
436
|
+
await Promise.all(
|
|
437
|
+
users.map(async (user) => {
|
|
438
|
+
const userMatchesCount = await this.redisClient.sCard(this.redisPrefix + `user:${user.id}:assignments`);
|
|
439
|
+
if (userMatchesCount >= this.maxUserBacklogSize) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const relevantAssignments = await this.getUserRelatedAssignments(user);
|
|
444
|
+
const relevantAssignmentKeys = relevantAssignments
|
|
445
|
+
.sort((a, b) => b.priority - a.priority)
|
|
446
|
+
.map((a) => `assignment:${a.id}`);
|
|
447
|
+
|
|
448
|
+
if (relevantAssignmentKeys.length > 0) {
|
|
449
|
+
await this.redisClient.sAdd(this.redisPrefix + `user:${user.id}:assignments`, relevantAssignmentKeys);
|
|
450
|
+
}
|
|
451
|
+
}),
|
|
452
|
+
);
|
|
453
|
+
}
|
|
285
454
|
|
|
286
455
|
async getCurrentAssignmentsForUser(userId: string): Promise<string[]> {
|
|
287
456
|
const userAssignments = await this.redisClient.sMembers(this.redisPrefix + `user:${userId}:assignments`);
|
package/tests/matcher.test.ts
CHANGED
|
@@ -118,7 +118,7 @@ describe('Matcher base tests', async function () {
|
|
|
118
118
|
tags: ['tag1', 'tag2', 'tag3'],
|
|
119
119
|
priority: 5,
|
|
120
120
|
latitude: 40.7128,
|
|
121
|
-
longitude: -74.
|
|
121
|
+
longitude: -74.006,
|
|
122
122
|
};
|
|
123
123
|
const assignment = await matcher.addAssignment({ ...assignmentData });
|
|
124
124
|
|
|
@@ -260,13 +260,13 @@ describe('Priority management tests', async function () {
|
|
|
260
260
|
tags: ['tagA', 'tagB', 'tagC'],
|
|
261
261
|
priority: 10,
|
|
262
262
|
});
|
|
263
|
-
|
|
263
|
+
|
|
264
264
|
await matcher.addAssignment({
|
|
265
265
|
id: '2',
|
|
266
266
|
tags: ['tagA', 'tagD'],
|
|
267
267
|
priority: 5,
|
|
268
268
|
});
|
|
269
|
-
|
|
269
|
+
|
|
270
270
|
await matcher.addAssignment({
|
|
271
271
|
id: '3',
|
|
272
272
|
tags: ['tagE', 'tagF'],
|
|
@@ -279,18 +279,18 @@ describe('Priority management tests', async function () {
|
|
|
279
279
|
// Verify the updated assignments
|
|
280
280
|
expect(updatedAssignments).to.be.an('array');
|
|
281
281
|
expect(updatedAssignments).to.have.lengthOf(2);
|
|
282
|
-
|
|
282
|
+
|
|
283
283
|
// Verify that the returned values match the expected structure
|
|
284
|
-
expect(updatedAssignments.some(a => a.id === '1' && a.priority === 50)).to.be.true;
|
|
285
|
-
expect(updatedAssignments.some(a => a.id === '2' && a.priority === 50)).to.be.true;
|
|
286
|
-
|
|
284
|
+
expect(updatedAssignments.some((a) => a.id === '1' && a.priority === 50)).to.be.true;
|
|
285
|
+
expect(updatedAssignments.some((a) => a.id === '2' && a.priority === 50)).to.be.true;
|
|
286
|
+
|
|
287
287
|
// Get individual assignments and verify their updated priorities
|
|
288
288
|
await matcher.setAssignmentPriority('1', 50); // Explicitly set to make sure test passes
|
|
289
289
|
await matcher.setAssignmentPriority('2', 50); // Explicitly set to make sure test passes
|
|
290
|
-
|
|
290
|
+
|
|
291
291
|
// Verify the assignment with different tags was not affected
|
|
292
292
|
const allAssignments = await matcher.getAllAssignments();
|
|
293
|
-
const assignment3 = allAssignments.find(a => a.id === '3');
|
|
293
|
+
const assignment3 = allAssignments.find((a) => a.id === '3');
|
|
294
294
|
expect(assignment3).to.not.be.undefined;
|
|
295
295
|
expect(assignment3!.priority).to.equal(15);
|
|
296
296
|
});
|
|
@@ -316,9 +316,9 @@ describe('Parallel matching tests', async function () {
|
|
|
316
316
|
{ id: 'user1', tags: ['tag1', 'tag2'] },
|
|
317
317
|
{ id: 'user2', tags: ['tag2', 'tag3'] },
|
|
318
318
|
{ id: 'user3', tags: ['tag1', 'tag3'] },
|
|
319
|
-
{ id: 'user4', tags: ['tag4', 'tag5'] }
|
|
319
|
+
{ id: 'user4', tags: ['tag4', 'tag5'] },
|
|
320
320
|
];
|
|
321
|
-
|
|
321
|
+
|
|
322
322
|
for (const user of users) {
|
|
323
323
|
await matcher.addUser(user);
|
|
324
324
|
}
|
|
@@ -334,9 +334,9 @@ describe('Parallel matching tests', async function () {
|
|
|
334
334
|
{ id: 'a7', tags: ['tag3'], priority: 70 },
|
|
335
335
|
{ id: 'a8', tags: ['tag4'], priority: 80 },
|
|
336
336
|
{ id: 'a9', tags: ['tag5'], priority: 90 },
|
|
337
|
-
{ id: 'a10', tags: ['tag4', 'tag5'], priority: 100 }
|
|
337
|
+
{ id: 'a10', tags: ['tag4', 'tag5'], priority: 100 },
|
|
338
338
|
];
|
|
339
|
-
|
|
339
|
+
|
|
340
340
|
for (const assignment of assignments) {
|
|
341
341
|
await matcher.addAssignment(assignment);
|
|
342
342
|
}
|
|
@@ -353,13 +353,13 @@ describe('Parallel matching tests', async function () {
|
|
|
353
353
|
// Verify each user got assignments
|
|
354
354
|
expect(user1Assignments).to.be.an('array');
|
|
355
355
|
expect(user1Assignments.length).to.be.greaterThan(0);
|
|
356
|
-
|
|
356
|
+
|
|
357
357
|
expect(user2Assignments).to.be.an('array');
|
|
358
358
|
expect(user2Assignments.length).to.be.greaterThan(0);
|
|
359
|
-
|
|
359
|
+
|
|
360
360
|
expect(user3Assignments).to.be.an('array');
|
|
361
361
|
expect(user3Assignments.length).to.be.greaterThan(0);
|
|
362
|
-
|
|
362
|
+
|
|
363
363
|
expect(user4Assignments).to.be.an('array');
|
|
364
364
|
expect(user4Assignments.length).to.be.greaterThan(0);
|
|
365
365
|
|
|
@@ -370,3 +370,72 @@ describe('Parallel matching tests', async function () {
|
|
|
370
370
|
expect(stats.usersWithoutAssignment).to.be.an('array');
|
|
371
371
|
});
|
|
372
372
|
});
|
|
373
|
+
|
|
374
|
+
describe('Weighted skill matching tests', async function () {
|
|
375
|
+
let matcher: Matcher;
|
|
376
|
+
let redisClient: any;
|
|
377
|
+
before(async function () {
|
|
378
|
+
redisClient = await createClient({});
|
|
379
|
+
await redisClient.connect();
|
|
380
|
+
await redisClient.flushAll();
|
|
381
|
+
matcher = new Matcher(redisClient, {
|
|
382
|
+
maxUserBacklogSize: 10,
|
|
383
|
+
relevantBatchSize: 10,
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('Excludes zero-weight tags (dutch:0)', async function () {
|
|
388
|
+
await matcher.addUser({
|
|
389
|
+
id: 'wu1',
|
|
390
|
+
tags: ['english', 'dutch'],
|
|
391
|
+
// @ts-ignore enrich user data with routing weights
|
|
392
|
+
routingWeights: { english: 100, dutch: 0 },
|
|
393
|
+
} as any);
|
|
394
|
+
|
|
395
|
+
await matcher.addAssignment({ id: 'e1', tags: ['english'], priority: 10 });
|
|
396
|
+
await matcher.addAssignment({ id: 'd1', tags: ['dutch'], priority: 10 });
|
|
397
|
+
|
|
398
|
+
await matcher.matchUsersAssignments('wu1');
|
|
399
|
+
const assignments = await matcher.getCurrentAssignmentsForUser('wu1');
|
|
400
|
+
expect(assignments).to.deep.equal(['e1']);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('Prefers higher weights when priorities are equal (english:100 > dutch:30)', async function () {
|
|
404
|
+
await redisClient.flushAll();
|
|
405
|
+
matcher = new Matcher(redisClient, {
|
|
406
|
+
maxUserBacklogSize: 10,
|
|
407
|
+
relevantBatchSize: 10,
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
await matcher.addUser({
|
|
411
|
+
id: 'wu2',
|
|
412
|
+
tags: ['english', 'dutch'],
|
|
413
|
+
// @ts-ignore enrich user data with routing weights
|
|
414
|
+
routingWeights: { english: 100, dutch: 30 },
|
|
415
|
+
} as any);
|
|
416
|
+
|
|
417
|
+
// Equal base priorities, english should be ranked ahead of dutch due to higher weight
|
|
418
|
+
await matcher.addAssignment({ id: 'e1', tags: ['english'], priority: 10 });
|
|
419
|
+
await matcher.addAssignment({ id: 'd1', tags: ['dutch'], priority: 10 });
|
|
420
|
+
await matcher.addAssignment({ id: 'e2', tags: ['english'], priority: 10 });
|
|
421
|
+
await matcher.addAssignment({ id: 'd2', tags: ['dutch'], priority: 10 });
|
|
422
|
+
|
|
423
|
+
await matcher.matchUsersAssignments('wu2');
|
|
424
|
+
const assignments = await matcher.getCurrentAssignmentsForUser('wu2');
|
|
425
|
+
expect(assignments).to.have.lengthOf(4);
|
|
426
|
+
|
|
427
|
+
const i_e1 = assignments.indexOf('e1');
|
|
428
|
+
const i_e2 = assignments.indexOf('e2');
|
|
429
|
+
const i_d1 = assignments.indexOf('d1');
|
|
430
|
+
const i_d2 = assignments.indexOf('d2');
|
|
431
|
+
expect(i_e1).to.be.greaterThan(-1);
|
|
432
|
+
expect(i_e2).to.be.greaterThan(-1);
|
|
433
|
+
expect(i_d1).to.be.greaterThan(-1);
|
|
434
|
+
expect(i_d2).to.be.greaterThan(-1);
|
|
435
|
+
// English assignments should appear before Dutch assignments
|
|
436
|
+
expect(i_e1).to.be.lessThan(i_d1);
|
|
437
|
+
expect(i_e1).to.be.lessThan(i_d2);
|
|
438
|
+
expect(i_e2).to.be.lessThan(i_d1);
|
|
439
|
+
expect(i_e2).to.be.lessThan(i_d2);
|
|
440
|
+
});
|
|
441
|
+
});
|