-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMockDataOld.js
125 lines (113 loc) · 4.28 KB
/
generateMockDataOld.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
// 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 { writeFile, writeFileSync } from "fs"
import { readFile } from "fs/promises"
import { csvFormat } from "d3-dsv"
import { v4 as genId } from "uuid"
import { Language } from "voynich-ipsum"
import { DAY_IN_MS } from "./constants"
const lang = new Language({ seed: 1 })
const PLATFORMS = ["twitch", "youtube"]
const buildMockData = ({ streamersList, startOfUse, endOfUse }) => {
// build list of watched streamers
const numberOfWatchedStreamers = Math.round(Math.random() * 9) + 1
const watchedStreamers = []
const toPick = [...streamersList]
for (let i = 0; i < numberOfWatchedStreamers; i++) {
const index = parseInt(Math.random() * toPick.length)
watchedStreamers.push(toPick[index])
toPick.splice(index, 1)
}
// compute period of logging
const endOfUseReal = endOfUse || new Date().getTime()
const startOfUseReal = startOfUse || endOfUseReal - 3 * 30 * 24 * 3600 * 1000 // 3 months;
const slices = []
for (let i = startOfUseReal; i < endOfUseReal + DAY_IN_MS; i += DAY_IN_MS) {
const numberOfSlices = parseInt(Math.random() * 5)
let sliceStartRel = parseInt(Math.random() * (DAY_IN_MS * 0.2))
let currentSliceIndex = 0
while (currentSliceIndex < numberOfSlices && sliceStartRel < DAY_IN_MS) {
const remainingTime = DAY_IN_MS - sliceStartRel
const duration = parseInt(Math.random() * remainingTime * 0.8)
const sliceStart = i + sliceStartRel
const sliceEnd = i + sliceStartRel + duration
const slicePlatform =
PLATFORMS[parseInt(Math.random() * PLATFORMS.length)]
const isReplay =
slicePlatform === "twitch" ? Math.random() * 0.2 > 0.5 : true
const slice = {
startTime: sliceStart,
endTime: sliceEnd,
startTimeStr: new Date(sliceStart),
endTimeStr: new Date(sliceEnd),
contentChannel:
streamersList[parseInt(Math.random() * streamersList.length)],
contentTitle: lang.assertion("pwet"),
contentPlatform: slicePlatform,
isReplay,
id: genId()
}
slices.push(slice)
sliceStartRel += duration + parseInt(Math.random() * remainingTime * 0.8)
currentSliceIndex++
}
}
const minutes = []
for (let i = 0; i < slices.length; i++) {
const slice = slices[i]
let startMinute = slice.startTime - (slice.startTime % 60000)
const avgMessagesPerMinutes = parseInt(Math.random() * 1000)
const messagesVariation = avgMessagesPerMinutes * Math.random() * 0.1
const avgNumberOfViewers = parseInt(
avgMessagesPerMinutes * 10 * Math.random()
)
const viewersVariation = avgNumberOfViewers * Math.random() * 0.1
const isFocused = Math.random() > 0.5
const isActive = isFocused ? Math.random() * 0.5 : false
let userChatMessages
if (!slice.isReplay) {
const hasMessaged = Math.round(Math.random() * 0.6)
if (hasMessaged) {
const numberOfMessages = parseInt(Math.random() * 3)
userChatMessages = []
for (let j = 0; j < numberOfMessages; j++) {
userChatMessages.push(lang.exclamation("pfew"))
}
userChatMessages = userChatMessages.join("|")
}
}
while (startMinute < slice.endTime + 60000) {
const minute = {
sliceId: slice.id,
startTime: startMinute,
startTimeStr: new Date(startMinute),
nbMessages: parseInt(
avgMessagesPerMinutes -
messagesVariation +
Math.random() * messagesVariation * 2
),
nbViewers: parseInt(
avgNumberOfViewers -
viewersVariation +
Math.random() * viewersVariation * 2
),
userIsFocused: isFocused,
userIsActive: isActive,
userChatMessages
}
minutes.push(minute)
startMinute += 60000
}
}
// console.log(minutes)
return {
slices,
minutes
}
}
readFile("top-streamers.txt", "utf8").then((streamers) => {
const streamersList = streamers.split("\n")
const { slices, minutes } = buildMockData({ streamersList })
writeFileSync("mockData/mock-slices.csv", csvFormat(slices), "utf8")
writeFileSync("mockData/mock-minutes.csv", csvFormat(minutes), "utf8")
})