assignment-user-matcher 1.0.14 → 1.1.1
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/matcher.class.d.ts +13 -0
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +178 -32
- package/package.json +1 -1
package/dist/matcher.class.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export type options = {
|
|
|
22
22
|
redisPrefix?: string;
|
|
23
23
|
maxUserBacklogSize?: number;
|
|
24
24
|
enableDefaultMatching?: boolean;
|
|
25
|
+
matchExpirationMs?: number;
|
|
25
26
|
prioritizationFunction?: (...args: (Assignment | undefined)[]) => Promise<number>;
|
|
26
27
|
matchingFunction?: (user: User, assignmentTags: string, assignmentPriority: number | string, assignmentId?: string) => Promise<[number, number]>;
|
|
27
28
|
};
|
|
@@ -34,6 +35,11 @@ export default class AssignmentMatcher {
|
|
|
34
35
|
usersKey: string;
|
|
35
36
|
maxUserBacklogSize: number;
|
|
36
37
|
enableDefaultMatching: boolean;
|
|
38
|
+
matchExpirationMs: number;
|
|
39
|
+
pendingAssignmentsKey: string;
|
|
40
|
+
pendingAssignmentsExpiryKey: string;
|
|
41
|
+
assignmentOwnerKey: string;
|
|
42
|
+
allTagsKey: string;
|
|
37
43
|
constructor(redisClient: RedisClientType, options?: options);
|
|
38
44
|
private initRedis;
|
|
39
45
|
addUser(user: User): Promise<User>;
|
|
@@ -56,5 +62,12 @@ export default class AssignmentMatcher {
|
|
|
56
62
|
private getUserRelatedAssignments;
|
|
57
63
|
matchUsersAssignments(userId?: string): Promise<void>;
|
|
58
64
|
getCurrentAssignmentsForUser(userId: string): Promise<string[]>;
|
|
65
|
+
acceptAssignment(userId: string, assignmentId: string): Promise<boolean>;
|
|
66
|
+
rejectAssignment(userId: string, assignmentId: string): Promise<boolean>;
|
|
67
|
+
processExpiredMatches(): Promise<number>;
|
|
68
|
+
private autoReleaseInterval;
|
|
69
|
+
startAutoReleaseInterval(intervalMs?: number): void;
|
|
70
|
+
stopAutoReleaseInterval(): void;
|
|
71
|
+
private expandTagWildcards;
|
|
59
72
|
}
|
|
60
73
|
//# sourceMappingURL=matcher.class.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matcher.class.d.ts","sourceRoot":"","sources":["../src/matcher.class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AACrC,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AAE9D,MAAM,WAAW,IAAI;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,EAAE,CAAC;IAGf,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED,MAAM,MAAM,UAAU,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC;IAClC,oBAAoB,CAAC,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IAClB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,sBAAsB,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,UAAU,GAAG,SAAS,CAAC,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAClF,gBAAgB,CAAC,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AAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;;;;IAyBlD,2BAA2B,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM;;;;YA6BpD,yBAAyB;IA8IjC,qBAAqB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmDrD,4BAA4B,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAM/D,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBxE,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA2BxE,qBAAqB,IAAI,OAAO,CAAC,MAAM,CAAC;IAwB9C,OAAO,CAAC,mBAAmB,CAA+B;IAE1D,wBAAwB,CAAC,UAAU,GAAE,MAAc;IAOnD,uBAAuB;YAOT,kBAAkB;CAwBnC"}
|
package/dist/matcher.class.js
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
class AssignmentMatcher {
|
|
4
4
|
constructor(redisClient, options) {
|
|
5
|
-
var _a, _b, _c, _d, _e, _f;
|
|
5
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
6
6
|
this.redisClient = redisClient;
|
|
7
|
+
this.autoReleaseInterval = null;
|
|
7
8
|
this.relevantBatchSize = (_a = options === null || options === void 0 ? void 0 : options.relevantBatchSize) !== null && _a !== void 0 ? _a : 50;
|
|
8
9
|
this.enableDefaultMatching = (_b = options === null || options === void 0 ? void 0 : options.enableDefaultMatching) !== null && _b !== void 0 ? _b : true;
|
|
9
10
|
this.redisPrefix = (_c = options === null || options === void 0 ? void 0 : options.redisPrefix) !== null && _c !== void 0 ? _c : '';
|
|
@@ -11,8 +12,13 @@ class AssignmentMatcher {
|
|
|
11
12
|
this.assignmentsKey = this.redisPrefix + 'assignments';
|
|
12
13
|
this.assignmentsRefKey = this.redisPrefix + 'assignments:ref';
|
|
13
14
|
this.maxUserBacklogSize = (_d = options === null || options === void 0 ? void 0 : options.maxUserBacklogSize) !== null && _d !== void 0 ? _d : 9;
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
15
|
+
this.matchExpirationMs = (_e = options === null || options === void 0 ? void 0 : options.matchExpirationMs) !== null && _e !== void 0 ? _e : 60000;
|
|
16
|
+
this.pendingAssignmentsKey = this.redisPrefix + 'assignments:pending:data';
|
|
17
|
+
this.pendingAssignmentsExpiryKey = this.redisPrefix + 'assignments:pending:expiry';
|
|
18
|
+
this.assignmentOwnerKey = this.redisPrefix + 'assignments:pending:owner';
|
|
19
|
+
this.allTagsKey = this.redisPrefix + 'all:tags';
|
|
20
|
+
this.calculatePriority = (_f = options === null || options === void 0 ? void 0 : options.prioritizationFunction) !== null && _f !== void 0 ? _f : this.calculatePriority;
|
|
21
|
+
this.matchScore = (_g = options === null || options === void 0 ? void 0 : options.matchingFunction) !== null && _g !== void 0 ? _g : this.matchScore;
|
|
16
22
|
this.initRedis();
|
|
17
23
|
}
|
|
18
24
|
async initRedis() {
|
|
@@ -61,6 +67,7 @@ class AssignmentMatcher {
|
|
|
61
67
|
score: Number(priority),
|
|
62
68
|
value: id,
|
|
63
69
|
});
|
|
70
|
+
multi.zAdd(this.allTagsKey, { score: 0, value: tag });
|
|
64
71
|
}
|
|
65
72
|
await multi.exec();
|
|
66
73
|
return assignment;
|
|
@@ -70,7 +77,10 @@ class AssignmentMatcher {
|
|
|
70
77
|
const assignmentTagsKey = this.redisPrefix + `assignment:${id}:tags`;
|
|
71
78
|
// Fetch tags to remove from per-tag indices
|
|
72
79
|
const [tagsCsv] = await this.redisClient.multi().hGet(assignmentTagsKey, 'tags').exec();
|
|
73
|
-
|
|
80
|
+
let tags = [];
|
|
81
|
+
if (typeof tagsCsv === 'string' && tagsCsv.length > 0) {
|
|
82
|
+
tags = tagsCsv.split(',');
|
|
83
|
+
}
|
|
74
84
|
const multi = this.redisClient.multi();
|
|
75
85
|
// Remove from reference and per-assignment hashes
|
|
76
86
|
multi.hDel(this.assignmentsRefKey, id);
|
|
@@ -89,6 +99,7 @@ class AssignmentMatcher {
|
|
|
89
99
|
const multi = this.redisClient
|
|
90
100
|
.multi()
|
|
91
101
|
.del(this.redisPrefix + `user:${userId}:assignments`)
|
|
102
|
+
.del(this.redisPrefix + `user:${userId}:rejected`)
|
|
92
103
|
.hDel(this.usersKey, userId);
|
|
93
104
|
await multi.exec();
|
|
94
105
|
return userId;
|
|
@@ -98,30 +109,64 @@ class AssignmentMatcher {
|
|
|
98
109
|
return Object.values(assignments).map((assignment) => JSON.parse(assignment));
|
|
99
110
|
}
|
|
100
111
|
async matchScore(user, assignmentTags, assignmentPriority, assignmentId) {
|
|
112
|
+
const aTags = assignmentTags ? assignmentTags.split(',') : [];
|
|
113
|
+
const matchesPattern = (pattern, tag) => {
|
|
114
|
+
if (pattern === tag)
|
|
115
|
+
return true;
|
|
116
|
+
if (pattern.endsWith('*')) {
|
|
117
|
+
const prefix = pattern.slice(0, -1);
|
|
118
|
+
return tag.startsWith(prefix);
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
101
122
|
// If weighted skills are present, use them for scoring
|
|
102
123
|
if (user.routingWeights && Object.keys(user.routingWeights).length > 0) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
124
|
+
// Check for exclusions (weight 0)
|
|
125
|
+
for (const [pattern, weight] of Object.entries(user.routingWeights)) {
|
|
126
|
+
if (weight === 0) {
|
|
127
|
+
for (const t of aTags) {
|
|
128
|
+
if (matchesPattern(pattern, t)) {
|
|
129
|
+
return [0, Number(assignmentPriority) || 0];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
108
132
|
}
|
|
109
133
|
}
|
|
110
134
|
let weightSum = 0;
|
|
111
135
|
for (const t of aTags) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
136
|
+
let matchedByRouting = false;
|
|
137
|
+
for (const [pattern, weight] of Object.entries(user.routingWeights)) {
|
|
138
|
+
if (typeof weight === 'number' && weight > 0) {
|
|
139
|
+
if (matchesPattern(pattern, t)) {
|
|
140
|
+
weightSum += weight;
|
|
141
|
+
matchedByRouting = true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// If enableDefaultMatching is true, and the tag is 'default', and it wasn't matched by any routing weight,
|
|
146
|
+
// we give it default weight 1.
|
|
147
|
+
if (this.enableDefaultMatching && t === 'default' && !matchedByRouting) {
|
|
148
|
+
weightSum += 1;
|
|
149
|
+
}
|
|
115
150
|
}
|
|
116
151
|
const base = Number(assignmentPriority) || 0;
|
|
117
|
-
// score: total positive weight overlap; priority: base weighted slightly by weight
|
|
118
152
|
return [weightSum, base + weightSum];
|
|
119
153
|
}
|
|
120
154
|
// Fallback to default tag-intersection scoring
|
|
121
155
|
const userTagSet = new Set(user.tags);
|
|
122
|
-
const assignmentTagSet = new Set(
|
|
123
|
-
|
|
124
|
-
const
|
|
156
|
+
const assignmentTagSet = new Set(aTags);
|
|
157
|
+
let intersectionSize = 0;
|
|
158
|
+
for (const userTag of userTagSet) {
|
|
159
|
+
let matched = false;
|
|
160
|
+
for (const assignmentTag of assignmentTagSet) {
|
|
161
|
+
if (matchesPattern(userTag, assignmentTag)) {
|
|
162
|
+
matched = true;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (matched)
|
|
167
|
+
intersectionSize++;
|
|
168
|
+
}
|
|
169
|
+
const score = userTagSet.size > 0 ? intersectionSize / userTagSet.size : 0;
|
|
125
170
|
const base = Number(assignmentPriority) || 0;
|
|
126
171
|
return [score || 0, base + (score || 0)];
|
|
127
172
|
}
|
|
@@ -230,31 +275,36 @@ class AssignmentMatcher {
|
|
|
230
275
|
const userAssignments = [];
|
|
231
276
|
if (positiveWeights.length === 0)
|
|
232
277
|
return userAssignments;
|
|
278
|
+
// Expand wildcards
|
|
279
|
+
const expandedPositive = await this.expandTagWildcards(positiveWeights);
|
|
280
|
+
const expandedZero = await this.expandTagWildcards(zeroWeightTags.map((t) => ({ tag: t, weight: 0 })));
|
|
281
|
+
const finalZeroTags = expandedZero.map((t) => t.tag);
|
|
282
|
+
if (expandedPositive.length === 0)
|
|
283
|
+
return userAssignments;
|
|
233
284
|
const tempKey = this.redisPrefix + `tmp:user:${user.id}:candidates`;
|
|
234
285
|
const tempZeroKey = this.redisPrefix + `tmp:user:${user.id}:exclude`;
|
|
235
286
|
const tempFinalKey = this.redisPrefix + `tmp:user:${user.id}:final`;
|
|
236
|
-
const
|
|
237
|
-
const
|
|
287
|
+
const rejectedKey = this.redisPrefix + `user:${user.id}:rejected`;
|
|
288
|
+
const unionKeys = expandedPositive.map((pw) => this.redisPrefix + `tag:${pw.tag}:assignments`);
|
|
289
|
+
const unionWeights = expandedPositive.map((pw) => pw.weight);
|
|
238
290
|
const multi = this.redisClient.multi();
|
|
239
291
|
// Weighted union of positive tag assignment zsets
|
|
240
292
|
multi.zUnionStore(tempKey, unionKeys, { WEIGHTS: unionWeights });
|
|
293
|
+
const excludeKeys = [];
|
|
241
294
|
// Optional exclude set for zero-weight tags
|
|
242
|
-
if (
|
|
243
|
-
const zeroKeys =
|
|
295
|
+
if (finalZeroTags.length > 0) {
|
|
296
|
+
const zeroKeys = finalZeroTags.map((t) => this.redisPrefix + `tag:${t}:assignments`);
|
|
244
297
|
multi.zUnionStore(tempZeroKey, zeroKeys);
|
|
245
|
-
// Final = candidates - exclude
|
|
246
|
-
multi.zDiffStore(tempFinalKey, [tempKey, tempZeroKey]);
|
|
247
|
-
// Set short TTLs to clean up
|
|
248
|
-
multi.expire(tempKey, 5);
|
|
249
298
|
multi.expire(tempZeroKey, 5);
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
// No exclude; just use tempKey as final (copy into final for uniformity)
|
|
254
|
-
multi.zUnionStore(tempFinalKey, [tempKey]);
|
|
255
|
-
multi.expire(tempKey, 5);
|
|
256
|
-
multi.expire(tempFinalKey, 5);
|
|
299
|
+
excludeKeys.push(tempZeroKey);
|
|
257
300
|
}
|
|
301
|
+
// Always exclude rejected assignments
|
|
302
|
+
excludeKeys.push(rejectedKey);
|
|
303
|
+
// Final = candidates - exclude (zero weights + rejected)
|
|
304
|
+
multi.zDiffStore(tempFinalKey, [tempKey, ...excludeKeys]);
|
|
305
|
+
// Set short TTLs to clean up
|
|
306
|
+
multi.expire(tempKey, 5);
|
|
307
|
+
multi.expire(tempFinalKey, 5);
|
|
258
308
|
await multi.exec();
|
|
259
309
|
// Pull top-N in ascending score to preserve previous behavior on ties (lex ascending)
|
|
260
310
|
const top = await this.redisClient.zRangeWithScores(tempFinalKey, 0, this.relevantBatchSize - 1);
|
|
@@ -292,8 +342,14 @@ class AssignmentMatcher {
|
|
|
292
342
|
}
|
|
293
343
|
// Batch-remove all selected assignments from indexes and references to reduce round trips
|
|
294
344
|
if (selected.length > 0) {
|
|
345
|
+
// Fetch JSONs first to store in pending
|
|
346
|
+
const jsonPromises = selected.map((s) => this.redisClient.hGet(this.assignmentsRefKey, s.id));
|
|
347
|
+
const jsons = await Promise.all(jsonPromises);
|
|
348
|
+
const now = Date.now();
|
|
295
349
|
const rm = this.redisClient.multi();
|
|
296
|
-
for (
|
|
350
|
+
for (let i = 0; i < selected.length; i++) {
|
|
351
|
+
const s = selected[i];
|
|
352
|
+
const json = jsons[i];
|
|
297
353
|
const assignmentPriorityKey = this.redisPrefix + `assignment:${s.id}:priority`;
|
|
298
354
|
const assignmentTagsKey = this.redisPrefix + `assignment:${s.id}:tags`;
|
|
299
355
|
// Remove from reference and per-assignment hashes
|
|
@@ -307,6 +363,12 @@ class AssignmentMatcher {
|
|
|
307
363
|
for (const tag of tags) {
|
|
308
364
|
rm.zRem(this.redisPrefix + `tag:${tag}:assignments`, s.id);
|
|
309
365
|
}
|
|
366
|
+
// Add to pending structures
|
|
367
|
+
if (json) {
|
|
368
|
+
rm.hSet(this.pendingAssignmentsKey, s.id, json);
|
|
369
|
+
rm.zAdd(this.pendingAssignmentsExpiryKey, { score: now + this.matchExpirationMs, value: s.id });
|
|
370
|
+
rm.hSet(this.assignmentOwnerKey, s.id, user.id);
|
|
371
|
+
}
|
|
310
372
|
}
|
|
311
373
|
await rm.exec();
|
|
312
374
|
}
|
|
@@ -359,5 +421,89 @@ class AssignmentMatcher {
|
|
|
359
421
|
const userAssignments = await this.redisClient.sMembers(this.redisPrefix + `user:${userId}:assignments`);
|
|
360
422
|
return userAssignments.map((el) => el.split(':')[1]);
|
|
361
423
|
}
|
|
424
|
+
async acceptAssignment(userId, assignmentId) {
|
|
425
|
+
const isAssigned = await this.redisClient.sIsMember(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
426
|
+
if (!isAssigned)
|
|
427
|
+
throw new Error('Assignment not found for user');
|
|
428
|
+
const multi = this.redisClient.multi();
|
|
429
|
+
multi.sRem(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
430
|
+
multi.hDel(this.pendingAssignmentsKey, assignmentId);
|
|
431
|
+
multi.zRem(this.pendingAssignmentsExpiryKey, assignmentId);
|
|
432
|
+
multi.hDel(this.assignmentOwnerKey, assignmentId);
|
|
433
|
+
await multi.exec();
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
async rejectAssignment(userId, assignmentId) {
|
|
437
|
+
const isAssigned = await this.redisClient.sIsMember(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
438
|
+
if (!isAssigned)
|
|
439
|
+
throw new Error('Assignment not found for user');
|
|
440
|
+
const json = await this.redisClient.hGet(this.pendingAssignmentsKey, assignmentId);
|
|
441
|
+
const multi = this.redisClient.multi();
|
|
442
|
+
multi.sRem(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
443
|
+
multi.hDel(this.pendingAssignmentsKey, assignmentId);
|
|
444
|
+
multi.zRem(this.pendingAssignmentsExpiryKey, assignmentId);
|
|
445
|
+
multi.hDel(this.assignmentOwnerKey, assignmentId);
|
|
446
|
+
// Add to rejected list to prevent immediate re-assignment
|
|
447
|
+
multi.sAdd(this.redisPrefix + `user:${userId}:rejected`, assignmentId);
|
|
448
|
+
await multi.exec();
|
|
449
|
+
if (json) {
|
|
450
|
+
await this.addAssignment(JSON.parse(json));
|
|
451
|
+
}
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
async processExpiredMatches() {
|
|
455
|
+
const now = Date.now();
|
|
456
|
+
const expiredIds = await this.redisClient.zRangeByScore(this.pendingAssignmentsExpiryKey, '-inf', now);
|
|
457
|
+
for (const id of expiredIds) {
|
|
458
|
+
const owner = await this.redisClient.hGet(this.assignmentOwnerKey, id);
|
|
459
|
+
const json = await this.redisClient.hGet(this.pendingAssignmentsKey, id);
|
|
460
|
+
const multi = this.redisClient.multi();
|
|
461
|
+
if (owner) {
|
|
462
|
+
multi.sRem(this.redisPrefix + `user:${owner}:assignments`, `assignment:${id}`);
|
|
463
|
+
}
|
|
464
|
+
multi.hDel(this.pendingAssignmentsKey, id);
|
|
465
|
+
multi.zRem(this.pendingAssignmentsExpiryKey, id);
|
|
466
|
+
multi.hDel(this.assignmentOwnerKey, id);
|
|
467
|
+
await multi.exec();
|
|
468
|
+
if (json) {
|
|
469
|
+
await this.addAssignment(JSON.parse(json));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return expiredIds.length;
|
|
473
|
+
}
|
|
474
|
+
startAutoReleaseInterval(intervalMs = 10000) {
|
|
475
|
+
if (this.autoReleaseInterval)
|
|
476
|
+
return;
|
|
477
|
+
this.autoReleaseInterval = setInterval(() => {
|
|
478
|
+
this.processExpiredMatches().catch(console.error);
|
|
479
|
+
}, intervalMs);
|
|
480
|
+
}
|
|
481
|
+
stopAutoReleaseInterval() {
|
|
482
|
+
if (this.autoReleaseInterval) {
|
|
483
|
+
clearInterval(this.autoReleaseInterval);
|
|
484
|
+
this.autoReleaseInterval = null;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
async expandTagWildcards(tags) {
|
|
488
|
+
const expanded = [];
|
|
489
|
+
const wildcards = tags.filter((t) => t.tag.endsWith('*'));
|
|
490
|
+
const literals = tags.filter((t) => !t.tag.endsWith('*'));
|
|
491
|
+
expanded.push(...literals);
|
|
492
|
+
if (wildcards.length > 0) {
|
|
493
|
+
const promises = wildcards.map((w) => {
|
|
494
|
+
const prefix = w.tag.slice(0, -1);
|
|
495
|
+
return this.redisClient.zRangeByLex(this.allTagsKey, `[${prefix}`, `[${prefix}\xff`);
|
|
496
|
+
});
|
|
497
|
+
const results = await Promise.all(promises);
|
|
498
|
+
for (let i = 0; i < wildcards.length; i++) {
|
|
499
|
+
const w = wildcards[i];
|
|
500
|
+
const matchedTags = results[i];
|
|
501
|
+
for (const tag of matchedTags) {
|
|
502
|
+
expanded.push({ tag, weight: w.weight });
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return expanded;
|
|
507
|
+
}
|
|
362
508
|
}
|
|
363
509
|
exports.default = AssignmentMatcher;
|