-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups.ts
60 lines (54 loc) · 1.61 KB
/
groups.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { type Worker, type WorkerManager } from "@workers/manager";
import { type Conversation } from "@xmtp/node-sdk";
/**
* Creates a group with specified participants and measures performance
*/
export async function createGroupWithBatch(
creator: Worker,
allWorkers: WorkerManager,
batchSize: number,
installationsPerUser: number,
): Promise<{
groupId: string | undefined;
memberCount: number;
totalInstallations: number;
executionTimeMs: number;
}> {
const startTime = performance.now();
const logLabel = `create group with ${batchSize} participants (${batchSize * installationsPerUser} installations)`;
console.time(logLabel);
const group = await creator.client?.conversations.newGroup(
allWorkers
.getWorkers()
.map((w) => w.client.inboxId)
.slice(0, batchSize),
);
const members = await group?.members();
const totalInstallations = (members ?? []).reduce(
(sum, m) => sum + (m?.installationIds.length ?? 0),
0,
);
console.log(
`Group created: ${group?.id} | Members: ${members?.length} | Installations: ${totalInstallations}`,
);
console.timeEnd(logLabel);
return {
groupId: group?.id,
memberCount: members?.length ?? 0,
totalInstallations,
executionTimeMs: performance.now() - startTime,
};
}
/**
* Gets workers that are members of a group
*/
export async function getWorkersFromGroup(
group: Conversation,
workers: WorkerManager,
): Promise<Worker[]> {
await group.sync();
const memberIds = (await group.members()).map((m) => m.inboxId);
return workers
.getWorkers()
.filter((w) => memberIds.includes(w.client.inboxId));
}