-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
abortBar.js
349 lines (303 loc) · 11.8 KB
/
abortBar.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Write a Javascript function `simulateLongJob()` that when you run it, returns the number of seconds elapsed and 1 of 3 states (success, aborted, or stillWaiting). The idea is that this function should simulate a job that would take a long time to run, such as reformatting a hard drive, downloading a huge file, or trying to book some complicated job online. It should be a realisitic simulation. So, for example, after a certain point no one should be in the "stillWaiting" point, but should have aborted or succeeded. Please choose an intelligent set of parameters for simulateLongJob. One of the parameters should be the random seed for reproducible runs. Then, write a function `populationSimulation`, which should takes parameters that let me run say, 10 million runs, or 1,000 runs, and it should return an array of points, sampling 100 points in time X, and having the # of people in the succesful state at that point, and the # of people in the aborted state at that point, and the # of people in the still waiting state at that point. Those 3 counts should always add up to 100% of the population.
*/
class AbortBar {
constructor(historicalStatistics, width = 600, height = 25) {
this.historicalStatistics = historicalStatistics
this.width = width
this.height = height
this.maxSeconds =
this.historicalStatistics[
this.historicalStatistics.length - 1
].timeInSeconds
}
start() {
let startTime = Date.now()
this.interval = setInterval(() => {
const secondsElapsed = (Date.now() - startTime) / 1000
this.updateVerticalLine(secondsElapsed)
}, 10)
return this
}
stop() {
clearInterval(this.interval)
return this
}
colors = {
success: "rgb(101,170,102)",
abort: "rgb(208,85,87)",
border: "rgba(100,100,100,.5)",
background: "rgba(200,200,200,.5)",
}
// Function to update the vertical line
updateVerticalLine(currentTime) {
if (currentTime > this.maxSeconds) {
currentTime = this.maxSeconds
this.stop()
return
}
this.svg.selectAll(".time-line,.time-line-border").remove() // Remove previous line if any
const { colors } = this
// Find the closest data point to the given time
const closestData = this.historicalStatistics.reduce((prev, curr) => {
return Math.abs(curr.timeInSeconds - currentTime) <
Math.abs(prev.timeInSeconds - currentTime)
? curr
: prev
})
const total =
closestData.remainingSuccesses + closestData.remainingAborts
const abortProbability = total ? closestData.remainingAborts / total : 1
// Calculate y-coordinates for splitting the line
const splitY = this.height * abortProbability
// Define the border color and width
const borderColor = colors.border
const borderWidth = 6 // Adjust the width as needed
// Draw the success (green) part of the line with a border
this.svg
.append("line")
.attr("class", "time-line-border")
.attr("x1", this.x(currentTime))
.attr("x2", this.x(currentTime))
.attr("y1", splitY)
.attr("y2", this.height)
.attr("stroke", borderColor)
.attr("stroke-width", borderWidth)
this.svg
.append("line")
.attr("class", "time-line")
.attr("x1", this.x(currentTime))
.attr("x2", this.x(currentTime))
.attr("y1", splitY)
.attr("y2", this.height)
.attr("stroke", colors.success)
.attr("stroke-width", 4)
// Draw the abort (red) part of the line with a border
this.svg
.append("line")
.attr("class", "time-line-border")
.attr("x1", this.x(currentTime))
.attr("x2", this.x(currentTime))
.attr("y1", 0)
.attr("y2", splitY)
.attr("stroke", borderColor)
.attr("stroke-width", borderWidth)
this.svg
.append("line")
.attr("class", "time-line")
.attr("x1", this.x(currentTime))
.attr("x2", this.x(currentTime))
.attr("y1", 0)
.attr("y2", splitY)
.attr("stroke", colors.abort)
.attr("stroke-width", 4)
}
draw() {
// Set the dimensions and margins of the graph
const margin = { top: 0, right: 0, bottom: 0, left: 0 }
const width = this.width - margin.left - margin.right
const height = this.height - margin.top - margin.bottom
// Append the svg object to the body of the page
const svg = d3
.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr(
"style",
`border: 1px solid ${this.colors.border};background: ${this.colors.background};`,
)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`)
this.svg = svg
// Add X axis
const x = d3
.scaleLinear()
.domain(d3.extent(simulationResults, (d) => d.timeInSeconds))
.range([0, width])
this.x = x
// Add Y axis
const maxY = d3.max(
this.historicalStatistics,
(d) => d.successInThisPeriod + d.abortedInThisPeriod,
)
const y = d3.scaleLinear().domain([0, maxY]).range([height, 0])
// Prepare the data for the stacked area chart
const stack = d3
.stack()
.keys(["successInThisPeriod", "abortedInThisPeriod"])
.order(d3.stackOrderNone)
// No offset specified, defaults to d3.stackOffsetNone which stacks the data without normalization
const series = stack(
this.historicalStatistics.map((d) => ({
timeInSeconds: d.timeInSeconds,
successInThisPeriod: d.successInThisPeriod,
abortedInThisPeriod: d.abortedInThisPeriod,
})),
)
// Add the areas
const area = d3
.area()
.x((d) => x(d.data.timeInSeconds))
.y0((d) => y(d[0]))
.y1((d) => y(d[1]))
const { colors } = this
svg.selectAll(".area")
.data(series)
.enter()
.append("path")
.attr(
"style",
(d) =>
`fill:${
colors[d.key.includes("uccess") ? "success" : "abort"]
};`,
)
.attr("d", area)
return this
}
}
class AbortBarSim {
constructor(
seed = Math.random(),
params = {
minSeconds: 0,
maxSeconds: 30,
shape: 1, // Shape parameter for Gamma distribution
scale: 10, // Scale parameter for Gamma distribution,
abortProbabilityMultiplier: 1,
},
) {
this.seed = seed
this.params = params
}
rand() {
// Simple linear congruential generator (LCG) for reproducible results
this.seed = (this.seed * 9301 + 49297) % 233280
return this.seed / 233280
}
gammaRandom(shape, scale) {
if (shape < 1) {
// Use the method for shape < 1 by Marsaglia and Tsang
const u = this.rand()
return this.gammaRandom(1 + shape, scale) * Math.pow(u, 1 / shape)
}
// Use the method for shape >= 1 by Marsaglia and Tsang
const d = shape - 1 / 3
const c = 1 / Math.sqrt(9 * d)
while (true) {
let x, v
do {
x = this.rand()
v = 1 + c * (x - 0.5)
} while (v <= 0)
v = v * v * v
const u = this.rand()
const x2 = x * x
if (
u < 1 - 0.0331 * x2 * x2 ||
Math.log(u) < 0.5 * x2 + d * (1 - v + Math.log(v))
) {
return scale * d * v
}
}
}
simulateLongRunningJob() {
const {
shape,
scale,
maxSeconds,
abortProbabilityMultiplier,
minSeconds,
} = this.params
// Simulating the job duration using gamma distribution
const duration = this.gammaRandom(shape, scale)
// Calculating the probability of abort based on exponential growth over time
const abortProbability = 1 - Math.exp(-duration / maxSeconds)
// Determining if the job results in an abort or success
const isAbort =
this.rand() < abortProbabilityMultiplier * abortProbability
const result = isAbort ? "abort" : "success"
// Clamping the duration to the maximum time if it exceeds
const clampedDuration = Math.max(
minSeconds,
Math.min(duration, maxSeconds),
)
return {
result,
duration: clampedDuration,
}
}
populationSimulation(populationSize = 1000) {
const summary = []
// Generate job results
const jobResults = Array(populationSize)
.fill(null)
.map(() => this.simulateLongRunningJob())
// Determine the maximum duration
let maxTime = Math.max(...jobResults.map((job) => job.duration)) + 1
// always end in a failure
jobResults.push({ result: "abort", duration: maxTime })
// Generate 100 evenly spaced time intervals from 0 to maxTime
const timeIntervals = Array.from(
{ length: 100 },
(_, i) => (i / 99) * maxTime,
)
let totalSuccesses = 0
let totalAborts = 0
jobResults.forEach((run) => {
if (run.result === "success") totalSuccesses++
else totalAborts++
})
for (const timeInSeconds of timeIntervals) {
let successCount = 0
let abortedCount = 0
let stillWaitingCount = 0
jobResults.forEach((job) => {
if (job.duration <= timeInSeconds) {
if (job.result === "success") {
successCount++
} else if (job.result === "abort") {
abortedCount++
}
} else {
stillWaitingCount++
}
})
let successInThisPeriod = 0
let abortedInThisPeriod = 0
jobResults
.filter((job) => !job.accountedFor)
.forEach((job) => {
if (job.duration <= timeInSeconds) {
if (job.result === "success") {
successInThisPeriod++
} else if (job.result === "abort") {
abortedInThisPeriod++
}
job.accountedFor = true
} else {
}
})
summary.push({
timeInSeconds,
successInThisPeriod,
abortedInThisPeriod,
remainingSuccesses: totalSuccesses - successCount,
remainingAborts: totalAborts - abortedCount,
success: (successCount / populationSize) * 100,
aborted: (abortedCount / populationSize) * 100,
stillWaiting: (stillWaitingCount / populationSize) * 100,
})
}
return summary
}
}
const simulationResults = new AbortBarSim().populationSimulation()
console.log(simulationResults)
const abortBar = new AbortBar(
simulationResults,
document.querySelector(".scrollParagraph").offsetWidth,
)
.draw()
.start()