assignment-user-matcher 1.1.2 → 1.1.6
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 +45 -0
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +230 -11
- package/package.json +6 -2
package/dist/matcher.class.d.ts
CHANGED
|
@@ -42,13 +42,58 @@ export default class AssignmentMatcher {
|
|
|
42
42
|
pendingAssignmentsExpiryKey: string;
|
|
43
43
|
assignmentOwnerKey: string;
|
|
44
44
|
allTagsKey: string;
|
|
45
|
+
acceptedAssignmentsKey: string;
|
|
45
46
|
constructor(redisClient: RedisClientType, options?: options);
|
|
46
47
|
private initRedis;
|
|
47
48
|
addUser(user: User): Promise<User>;
|
|
48
49
|
addAssignment(assignment: Assignment): Promise<Assignment>;
|
|
49
50
|
removeAssignment(id: string): Promise<string>;
|
|
50
51
|
removeUser(userId: string): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Get all assignments (queued, pending, and accepted).
|
|
54
|
+
* For large datasets, prefer using getAssignmentsPaginated() instead.
|
|
55
|
+
*/
|
|
51
56
|
getAllAssignments(): Promise<Assignment[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Get assignments with pagination support for efficient querying of large datasets.
|
|
59
|
+
* Uses cursor-based pagination across statuses.
|
|
60
|
+
*
|
|
61
|
+
* @param options.cursor - Cursor for pagination (null/undefined for first page)
|
|
62
|
+
* @param options.limit - Maximum number of assignments to return per page (default: 100)
|
|
63
|
+
* @param options.status - Filter by status: 'queued' | 'pending' | 'accepted' | 'all' (default: 'all')
|
|
64
|
+
* @param options.includeTotal - Whether to include total count (slower for large datasets)
|
|
65
|
+
*/
|
|
66
|
+
getAssignmentsPaginated(options?: {
|
|
67
|
+
cursor?: string | null;
|
|
68
|
+
limit?: number;
|
|
69
|
+
status?: 'queued' | 'pending' | 'accepted' | 'all';
|
|
70
|
+
includeTotal?: boolean;
|
|
71
|
+
}): Promise<{
|
|
72
|
+
assignments: Assignment[];
|
|
73
|
+
nextCursor: string | null;
|
|
74
|
+
hasMore: boolean;
|
|
75
|
+
total?: number;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Get assignment counts by status without fetching the data.
|
|
79
|
+
* Efficient for dashboards and monitoring.
|
|
80
|
+
*/
|
|
81
|
+
getAssignmentCounts(): Promise<{
|
|
82
|
+
queued: number;
|
|
83
|
+
pending: number;
|
|
84
|
+
accepted: number;
|
|
85
|
+
total: number;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Get a single assignment by ID from any status.
|
|
89
|
+
*/
|
|
90
|
+
getAssignment(id: string): Promise<(Assignment & {
|
|
91
|
+
_status?: string;
|
|
92
|
+
}) | null>;
|
|
93
|
+
/**
|
|
94
|
+
* Get multiple assignments by IDs efficiently.
|
|
95
|
+
*/
|
|
96
|
+
getAssignmentsByIds(ids: string[]): Promise<Assignment[]>;
|
|
52
97
|
/**
|
|
53
98
|
* Parse an IPv4 address into a BigInt representation
|
|
54
99
|
*/
|
|
@@ -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;IAGxC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,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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CAAC,UAAU,GAAE,MAAc;IAOnD,uBAAuB;YAOT,kBAAkB;CAwBnC"}
|
package/dist/matcher.class.js
CHANGED
|
@@ -16,6 +16,7 @@ class AssignmentMatcher {
|
|
|
16
16
|
this.pendingAssignmentsExpiryKey = this.redisPrefix + 'assignments:pending:expiry';
|
|
17
17
|
this.assignmentOwnerKey = this.redisPrefix + 'assignments:pending:owner';
|
|
18
18
|
this.allTagsKey = this.redisPrefix + 'all:tags';
|
|
19
|
+
this.acceptedAssignmentsKey = this.redisPrefix + 'assignments:accepted';
|
|
19
20
|
this.calculatePriority = options?.prioritizationFunction ?? this.calculatePriority;
|
|
20
21
|
this.matchScore = options?.matchingFunction ?? this.matchScore;
|
|
21
22
|
this.initRedis();
|
|
@@ -77,7 +78,7 @@ class AssignmentMatcher {
|
|
|
77
78
|
// Fetch tags to remove from per-tag indices
|
|
78
79
|
const [tagsCsv] = await this.redisClient.multi().hGet(assignmentTagsKey, 'tags').exec();
|
|
79
80
|
let tags = [];
|
|
80
|
-
if (
|
|
81
|
+
if (tagsCsv && tagsCsv.length > 0) {
|
|
81
82
|
tags = tagsCsv.split(',');
|
|
82
83
|
}
|
|
83
84
|
const multi = this.redisClient.multi();
|
|
@@ -103,9 +104,217 @@ class AssignmentMatcher {
|
|
|
103
104
|
await multi.exec();
|
|
104
105
|
return userId;
|
|
105
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Get all assignments (queued, pending, and accepted).
|
|
109
|
+
* For large datasets, prefer using getAssignmentsPaginated() instead.
|
|
110
|
+
*/
|
|
106
111
|
async getAllAssignments() {
|
|
107
|
-
const
|
|
108
|
-
|
|
112
|
+
const [queuedAssignments, pendingAssignments, acceptedAssignments] = await Promise.all([
|
|
113
|
+
this.redisClient.hGetAll(this.assignmentsRefKey),
|
|
114
|
+
this.redisClient.hGetAll(this.pendingAssignmentsKey),
|
|
115
|
+
this.redisClient.hGetAll(this.acceptedAssignmentsKey),
|
|
116
|
+
]);
|
|
117
|
+
const allAssignments = { ...queuedAssignments, ...pendingAssignments, ...acceptedAssignments };
|
|
118
|
+
return Object.values(allAssignments).map((assignment) => JSON.parse(assignment));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get assignments with pagination support for efficient querying of large datasets.
|
|
122
|
+
* Uses cursor-based pagination across statuses.
|
|
123
|
+
*
|
|
124
|
+
* @param options.cursor - Cursor for pagination (null/undefined for first page)
|
|
125
|
+
* @param options.limit - Maximum number of assignments to return per page (default: 100)
|
|
126
|
+
* @param options.status - Filter by status: 'queued' | 'pending' | 'accepted' | 'all' (default: 'all')
|
|
127
|
+
* @param options.includeTotal - Whether to include total count (slower for large datasets)
|
|
128
|
+
*/
|
|
129
|
+
async getAssignmentsPaginated(options) {
|
|
130
|
+
const limit = options?.limit ?? 100;
|
|
131
|
+
const status = options?.status ?? 'all';
|
|
132
|
+
const includeTotal = options?.includeTotal ?? false;
|
|
133
|
+
const getKeyForStatus = (s) => {
|
|
134
|
+
switch (s) {
|
|
135
|
+
case 'queued': return this.assignmentsRefKey;
|
|
136
|
+
case 'pending': return this.pendingAssignmentsKey;
|
|
137
|
+
case 'accepted': return this.acceptedAssignmentsKey;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const statusOrder = status === 'all' ? ['queued', 'pending', 'accepted'] : [status];
|
|
141
|
+
// Parse cursor: "statusIndex:offset" where offset is how many we've seen in current status
|
|
142
|
+
let statusIndex = 0;
|
|
143
|
+
let offset = 0;
|
|
144
|
+
if (options?.cursor) {
|
|
145
|
+
const parts = options.cursor.split(':');
|
|
146
|
+
statusIndex = parseInt(parts[0], 10) || 0;
|
|
147
|
+
offset = parseInt(parts[1], 10) || 0;
|
|
148
|
+
}
|
|
149
|
+
const assignments = [];
|
|
150
|
+
let nextCursor = null;
|
|
151
|
+
let hasMore = false;
|
|
152
|
+
while (assignments.length < limit && statusIndex < statusOrder.length) {
|
|
153
|
+
const scanStatus = statusOrder[statusIndex];
|
|
154
|
+
const key = getKeyForStatus(scanStatus);
|
|
155
|
+
// Get count for this status
|
|
156
|
+
const totalInStatus = await this.redisClient.hLen(key);
|
|
157
|
+
const remainingInStatus = totalInStatus - offset;
|
|
158
|
+
if (remainingInStatus <= 0) {
|
|
159
|
+
// Move to next status
|
|
160
|
+
statusIndex++;
|
|
161
|
+
offset = 0;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
// Scan to get entries - we need to scan through and skip offset entries
|
|
165
|
+
const needed = limit - assignments.length;
|
|
166
|
+
let scanned = 0;
|
|
167
|
+
let cursor = '0';
|
|
168
|
+
const seenIds = new Set();
|
|
169
|
+
do {
|
|
170
|
+
const { cursor: newCursor, entries } = await this.redisClient.hScan(key, cursor, {
|
|
171
|
+
COUNT: Math.max(needed + offset - scanned, 100),
|
|
172
|
+
});
|
|
173
|
+
for (const entry of entries) {
|
|
174
|
+
if (seenIds.has(entry.field))
|
|
175
|
+
continue; // Skip duplicates from HSCAN
|
|
176
|
+
seenIds.add(entry.field);
|
|
177
|
+
if (scanned < offset) {
|
|
178
|
+
scanned++;
|
|
179
|
+
continue; // Skip already-seen entries
|
|
180
|
+
}
|
|
181
|
+
if (assignments.length >= limit) {
|
|
182
|
+
// We have enough, set cursor for next page
|
|
183
|
+
nextCursor = `${statusIndex}:${offset + (assignments.length - (assignments.filter(a => a._status === scanStatus).length - 1))}`;
|
|
184
|
+
hasMore = true;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const assignment = JSON.parse(entry.value);
|
|
189
|
+
assignment._status = scanStatus;
|
|
190
|
+
assignments.push(assignment);
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
// Skip invalid JSON
|
|
194
|
+
}
|
|
195
|
+
scanned++;
|
|
196
|
+
}
|
|
197
|
+
cursor = newCursor;
|
|
198
|
+
} while (cursor !== '0' && assignments.length < limit);
|
|
199
|
+
if (assignments.length >= limit) {
|
|
200
|
+
// Calculate correct offset for resume
|
|
201
|
+
const countFromCurrentStatus = assignments.filter(a => a._status === scanStatus).length;
|
|
202
|
+
nextCursor = `${statusIndex}:${offset + countFromCurrentStatus}`;
|
|
203
|
+
hasMore = true;
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
// Move to next status
|
|
207
|
+
statusIndex++;
|
|
208
|
+
offset = 0;
|
|
209
|
+
}
|
|
210
|
+
// Check if there's more data in remaining statuses
|
|
211
|
+
if (!hasMore && statusIndex < statusOrder.length) {
|
|
212
|
+
for (let i = statusIndex; i < statusOrder.length; i++) {
|
|
213
|
+
const count = await this.redisClient.hLen(getKeyForStatus(statusOrder[i]));
|
|
214
|
+
const processedInThisStatus = i === statusIndex ? offset : 0;
|
|
215
|
+
if (count > processedInThisStatus) {
|
|
216
|
+
nextCursor = `${i}:${processedInThisStatus}`;
|
|
217
|
+
hasMore = true;
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const result = {
|
|
223
|
+
assignments,
|
|
224
|
+
nextCursor: hasMore ? nextCursor : null,
|
|
225
|
+
hasMore,
|
|
226
|
+
};
|
|
227
|
+
// Optionally include total count
|
|
228
|
+
if (includeTotal) {
|
|
229
|
+
if (status === 'all') {
|
|
230
|
+
const [queuedCount, pendingCount, acceptedCount] = await Promise.all([
|
|
231
|
+
this.redisClient.hLen(this.assignmentsRefKey),
|
|
232
|
+
this.redisClient.hLen(this.pendingAssignmentsKey),
|
|
233
|
+
this.redisClient.hLen(this.acceptedAssignmentsKey),
|
|
234
|
+
]);
|
|
235
|
+
result.total = queuedCount + pendingCount + acceptedCount;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
result.total = await this.redisClient.hLen(getKeyForStatus(status));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Get assignment counts by status without fetching the data.
|
|
245
|
+
* Efficient for dashboards and monitoring.
|
|
246
|
+
*/
|
|
247
|
+
async getAssignmentCounts() {
|
|
248
|
+
const [queued, pending, accepted] = await Promise.all([
|
|
249
|
+
this.redisClient.hLen(this.assignmentsRefKey),
|
|
250
|
+
this.redisClient.hLen(this.pendingAssignmentsKey),
|
|
251
|
+
this.redisClient.hLen(this.acceptedAssignmentsKey),
|
|
252
|
+
]);
|
|
253
|
+
return {
|
|
254
|
+
queued,
|
|
255
|
+
pending,
|
|
256
|
+
accepted,
|
|
257
|
+
total: queued + pending + accepted,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Get a single assignment by ID from any status.
|
|
262
|
+
*/
|
|
263
|
+
async getAssignment(id) {
|
|
264
|
+
// Check all three stores in parallel
|
|
265
|
+
const [queued, pending, accepted] = await Promise.all([
|
|
266
|
+
this.redisClient.hGet(this.assignmentsRefKey, id),
|
|
267
|
+
this.redisClient.hGet(this.pendingAssignmentsKey, id),
|
|
268
|
+
this.redisClient.hGet(this.acceptedAssignmentsKey, id),
|
|
269
|
+
]);
|
|
270
|
+
if (queued) {
|
|
271
|
+
const assignment = JSON.parse(queued);
|
|
272
|
+
assignment._status = 'queued';
|
|
273
|
+
return assignment;
|
|
274
|
+
}
|
|
275
|
+
if (pending) {
|
|
276
|
+
const assignment = JSON.parse(pending);
|
|
277
|
+
assignment._status = 'pending';
|
|
278
|
+
return assignment;
|
|
279
|
+
}
|
|
280
|
+
if (accepted) {
|
|
281
|
+
const assignment = JSON.parse(accepted);
|
|
282
|
+
assignment._status = 'accepted';
|
|
283
|
+
return assignment;
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Get multiple assignments by IDs efficiently.
|
|
289
|
+
*/
|
|
290
|
+
async getAssignmentsByIds(ids) {
|
|
291
|
+
if (ids.length === 0)
|
|
292
|
+
return [];
|
|
293
|
+
const multi = this.redisClient.multi();
|
|
294
|
+
for (const id of ids) {
|
|
295
|
+
multi.hGet(this.assignmentsRefKey, id);
|
|
296
|
+
multi.hGet(this.pendingAssignmentsKey, id);
|
|
297
|
+
multi.hGet(this.acceptedAssignmentsKey, id);
|
|
298
|
+
}
|
|
299
|
+
const results = await multi.exec();
|
|
300
|
+
const assignments = [];
|
|
301
|
+
for (let i = 0; i < ids.length; i++) {
|
|
302
|
+
const queued = results[i * 3];
|
|
303
|
+
const pending = results[i * 3 + 1];
|
|
304
|
+
const accepted = results[i * 3 + 2];
|
|
305
|
+
const json = queued || pending || accepted;
|
|
306
|
+
if (json) {
|
|
307
|
+
try {
|
|
308
|
+
const assignment = JSON.parse(json);
|
|
309
|
+
assignment._status = queued ? 'queued' : pending ? 'pending' : 'accepted';
|
|
310
|
+
assignments.push(assignment);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// Skip invalid JSON
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return assignments;
|
|
109
318
|
}
|
|
110
319
|
// ==================== CIDR/IP Matching Utilities ====================
|
|
111
320
|
/**
|
|
@@ -346,7 +555,7 @@ class AssignmentMatcher {
|
|
|
346
555
|
.hExists(this.assignmentsRefKey, id)
|
|
347
556
|
.hGet(assignmentTagsKey, 'tags')
|
|
348
557
|
.exec();
|
|
349
|
-
const tags = (
|
|
558
|
+
const tags = (tagsCsv && tagsCsv.length > 0 ? tagsCsv.split(',') : []);
|
|
350
559
|
const multi = this.redisClient.multi();
|
|
351
560
|
multi.hSet(assignmentPriorityKey, 'priority', priority);
|
|
352
561
|
if (existsRef) {
|
|
@@ -423,11 +632,15 @@ class AssignmentMatcher {
|
|
|
423
632
|
const tempZeroKey = this.redisPrefix + `tmp:user:${user.id}:exclude`;
|
|
424
633
|
const tempFinalKey = this.redisPrefix + `tmp:user:${user.id}:final`;
|
|
425
634
|
const rejectedKey = this.redisPrefix + `user:${user.id}:rejected`;
|
|
426
|
-
const
|
|
427
|
-
|
|
635
|
+
const unionKeysWithWeights = expandedPositive.map((pw) => ({
|
|
636
|
+
key: this.redisPrefix + `tag:${pw.tag}:assignments`,
|
|
637
|
+
weight: pw.weight
|
|
638
|
+
}));
|
|
428
639
|
const multi = this.redisClient.multi();
|
|
429
640
|
// Weighted union of positive tag assignment zsets
|
|
430
|
-
|
|
641
|
+
if (unionKeysWithWeights.length > 0) {
|
|
642
|
+
multi.zUnionStore(tempKey, unionKeysWithWeights);
|
|
643
|
+
}
|
|
431
644
|
const excludeKeys = [];
|
|
432
645
|
// Optional exclude set for zero-weight tags
|
|
433
646
|
if (finalZeroTags.length > 0) {
|
|
@@ -550,13 +763,13 @@ class AssignmentMatcher {
|
|
|
550
763
|
}
|
|
551
764
|
// Otherwise, process all users in parallel as before.
|
|
552
765
|
let users = [];
|
|
553
|
-
let cursor = 0;
|
|
766
|
+
let cursor = '0';
|
|
554
767
|
// Fetch all users in a single loop
|
|
555
768
|
do {
|
|
556
|
-
const { cursor: nextCursor,
|
|
557
|
-
users.push(...
|
|
769
|
+
const { cursor: nextCursor, entries } = await this.redisClient.hScan(this.usersKey, cursor);
|
|
770
|
+
users.push(...entries.map((t) => JSON.parse(t.value)));
|
|
558
771
|
cursor = nextCursor;
|
|
559
|
-
} while (cursor !== 0);
|
|
772
|
+
} while (cursor !== '0');
|
|
560
773
|
// Process users in parallel
|
|
561
774
|
await Promise.all(users.map(async (user) => {
|
|
562
775
|
const userMatchesCount = await this.redisClient.sCard(this.redisPrefix + `user:${user.id}:assignments`);
|
|
@@ -580,11 +793,17 @@ class AssignmentMatcher {
|
|
|
580
793
|
const isAssigned = await this.redisClient.sIsMember(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
581
794
|
if (!isAssigned)
|
|
582
795
|
throw new Error('Assignment not found for user');
|
|
796
|
+
// Get the assignment JSON before removing from pending
|
|
797
|
+
const json = await this.redisClient.hGet(this.pendingAssignmentsKey, assignmentId);
|
|
583
798
|
const multi = this.redisClient.multi();
|
|
584
799
|
multi.sRem(this.redisPrefix + `user:${userId}:assignments`, `assignment:${assignmentId}`);
|
|
585
800
|
multi.hDel(this.pendingAssignmentsKey, assignmentId);
|
|
586
801
|
multi.zRem(this.pendingAssignmentsExpiryKey, assignmentId);
|
|
587
802
|
multi.hDel(this.assignmentOwnerKey, assignmentId);
|
|
803
|
+
// Store in accepted assignments
|
|
804
|
+
if (json) {
|
|
805
|
+
multi.hSet(this.acceptedAssignmentsKey, assignmentId, json);
|
|
806
|
+
}
|
|
588
807
|
await multi.exec();
|
|
589
808
|
return true;
|
|
590
809
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assignment-user-matcher",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/ViljarVoidula/assignment-user-matcher"
|
|
7
|
+
},
|
|
4
8
|
"main": "dist/index.js",
|
|
5
9
|
"module": "dist/index.js",
|
|
6
10
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +23,7 @@
|
|
|
19
23
|
"license": "MIT",
|
|
20
24
|
"private": false,
|
|
21
25
|
"dependencies": {
|
|
22
|
-
"redis": "
|
|
26
|
+
"redis": "5.10.0"
|
|
23
27
|
},
|
|
24
28
|
"prettier": {
|
|
25
29
|
"semi": true,
|