-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.ts
76 lines (62 loc) · 2.04 KB
/
seed.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { PrismaClient, Prisma, User } from '../node_modules/.prisma/client'
const NUM_USERS = 1000
const COUNT_BLUE = 300
enum ExperimentVariant {
BlueBuyButton = "BlueBuyButton",
GreenBuyButton = "GreenBuyButton",
}
enum EventType {
PageOpened = "PageOpened",
ProductPutInShoppingCart = "ProductPutInShoppingCart",
AddressFilled = "AddressFilled",
PaymentInfoFilled = "PaymentInfoFilled",
CheckedOut = "CheckedOut",
}
const prisma = new PrismaClient()
async function main() {
const usersInput: Prisma.UserCreateInput[] = []
for (let i = 0; i < NUM_USERS; i++) {
usersInput.push({
email: `user${i}@example.com`,
})
}
const users = await prisma.user.createManyAndReturn({ data: usersInput })
await createFunnel(users.slice(0, COUNT_BLUE), ExperimentVariant.BlueBuyButton)
await createFunnel(users.slice(COUNT_BLUE), ExperimentVariant.GreenBuyButton)
}
async function createFunnel(users: User[], variant: ExperimentVariant) {
for (const event of [
EventType.PageOpened,
EventType.ProductPutInShoppingCart,
EventType.AddressFilled,
EventType.AddressFilled,
EventType.CheckedOut,
]) {
await createEvents(users, variant, event)
users = pickRandomSubset(users)
}
}
async function createEvents(users: User[], variant: ExperimentVariant, event: EventType) {
const eventsData = users.map((user) => (
{
variant,
type: event,
userId: user.id,
} satisfies Prisma.TrackingEventCreateManyInput
),
)
await prisma.trackingEvent.createMany({ data: eventsData })
}
function pickRandomSubset(users: User[]): User[] {
let amount = 1 + Math.floor(Math.random() * (users.length - 1))
const result: User[] = []
while (0 < amount) {
const idx = Math.floor(Math.random() * users.length)
const user = users[idx]
if (result.includes(user)) continue;
result.push(user)
amount--;
}
return result
}
main()