-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMockData.js
205 lines (190 loc) · 6.63 KB
/
generateMockData.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// https://twitchtracker.com/channels/ranking/french
// Array.from(document.querySelectorAll('td:nth-child(odd) a')).map(el => el.getAttribute('href').slice(1)).join('\n')
import { readFile } from "fs/promises"
import { v4 as genId } from "uuid"
import { Language } from "voynich-ipsum"
import { PLATFORMS } from "~constants"
import { DAY_IN_MS } from "./constants"
const lang = new Language({ seed: 1 })
const MIN_NUMBER_OF_SESSIONS = 1
const MAX_NUMBER_OF_SESSIONS = 12
const tossCoin = (prob) => {
return Math.random() <= prob
}
const buildMockData = ({ streamersList, numberOfPastDays }) => {
let events = []
let today = new Date()
today.setHours(0)
today.setMinutes(0)
today = today.getTime()
today = today - (today % DAY_IN_MS)
for (let i = 0; i < numberOfPastDays; i++) {
const day = today - DAY_IN_MS * i
// decide if it was an active day
const wasActive = tossCoin(0.6)
if (wasActive) {
// set number of sessions
const numberOfSessions = Math.round(
Math.random() * (MAX_NUMBER_OF_SESSIONS - MIN_NUMBER_OF_SESSIONS) +
MIN_NUMBER_OF_SESSIONS
)
// for each session create fake activity
let time = day + Math.random() * DAY_IN_MS
let sessionIndex = 0
while (time < day + DAY_IN_MS && sessionIndex < numberOfSessions) {
const sessionDuration = Math.floor(Math.random() * 2 * 3600 * 1000)
const sessionStart = time + Math.floor(Math.random() * 3 * 3600 * 1000)
const sessionId = genId()
time = sessionStart
if (time < day + DAY_IN_MS) {
const sessionEnd = sessionStart + sessionDuration
// choose platform
const platform =
PLATFORMS[Math.floor(Math.random() * (PLATFORMS.length - 1))]
console.log("====")
console.log("day: ", new Date(day))
console.log("session start: ", new Date(sessionStart))
console.log("session end: ", new Date(sessionEnd))
console.log("platform", platform)
// set user actions sequence: ['watches', 'engages', 'blurs', 'changes']
const ACTION_TYPES = ["moves", "blurs", "changes"]
const numberOfActions = Math.round(Math.random() * 20)
let actionsSequence = [
{
type: "changes",
absStart: 0
}
]
let lastWasBlurred = false
let absStart = 0
for (let i = 0; i < numberOfActions; i++) {
let actionType =
ACTION_TYPES[Math.floor(Math.random() * ACTION_TYPES.length)]
if (actionType === "blurs") {
if (lastWasBlurred) {
actionType = "focuses"
lastWasBlurred = false
} else {
lastWasBlurred = true
}
} else if (lastWasBlurred) {
actionsSequence.push({
type: "focuses",
absStart
})
lastWasBlurred = false
}
absStart += Math.random()
actionsSequence.push({
type: actionType,
absStart
})
}
const maxAbsStart =
actionsSequence[actionsSequence.length - 1].absStart
const absStartToRelStart = (ab) => {
return ab === 0
? sessionStart
: Math.floor(sessionStart + sessionDuration / ab)
}
actionsSequence = actionsSequence.map((a) => ({
...a,
date: absStartToRelStart(a.absStart)
}))
actionsSequence.forEach(({ type, date }) => {
const typeMap = {
changes: "BROWSE_VIEW",
focuses: "FOCUS_TAB",
blurs: "BLUR_TAB",
moves: "POINTER_ACTIVITY_RECORD"
}
let payload = {}
let event = {
id: genId(),
type: typeMap[type],
date: new Date(date),
platform,
injectionId: sessionId,
...payload
}
switch (type) {
case "changes":
if (platform === "youtube") {
const channelName =
youtubersList[
Math.floor(Math.random() * youtubersList.length)
]
payload = {
metadata: {
description: "mock",
keywords: "mock",
interactionCount: "mock",
datePublished: "mock",
uploadDate: "mock",
genre: "mock",
url: "mock url",
viewType: "video",
title: lang.exclamation(id),
shortlinkUrl: "random",
videoimageSrc:
"https://i.ytimg.com/vi/14NGU7KpFRI/maxresdefault.jpg",
channelName,
channelId: channelName,
ownerSubcount: Math.round(Math.random() * 10000000000),
likesCount: Math.round(Math.random() * 10000000000),
channelImageSrc:
"https://yt3.ggpht.com/rTCMq4spCIuBBSRMamqqPZnksnMCc8KA7575JX1_ogBUe1kTAe1aRINxgVOiZj7chSh6KoQACA=s68-c-k-c0x00ffffff-no-rj"
}
// metadata
}
} else if (platform === "twitch") {
}
break
case "moves":
payload = {
timeSpan: 5000,
activityScore: 1
}
event = {
...event,
...payload
}
events.push(event)
break
case "focus":
case "blurs":
default:
event = {
...event
}
events.push(event)
break
}
})
}
sessionIndex++
}
}
}
console.log(events)
return events
}
let streamersList
let youtubersList
readFile("mockGenerationResources/top-youtubers.txt", "utf8")
.then((str) => {
youtubersList = str.split("\n")
return readFile("mockGenerationResources/top-streamers.txt", "utf8")
})
.then((streamers) => {
streamersList = streamers.split("\n")
// [10,100,1000]
;[10].forEach((numberOfPastDays) => {
const mockHistory = buildMockData({
streamersList,
youtubersList,
numberOfPastDays
})
// writeFileSync(`mockData/mock-history-${numberOfPastDays}-days.json`, mockHistory, 'utf8')
})
})