forked from luksa/kubernetes-in-action-2nd-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.generate-responses.yaml
83 lines (73 loc) · 2.53 KB
/
job.generate-responses.yaml
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
apiVersion: batch/v1
kind: Job
metadata:
name: generate-responses
labels:
app: quiz
spec:
completions: 5
parallelism: 2
template:
metadata:
labels:
app: quiz
spec:
restartPolicy: OnFailure
containers:
- name: mongo
image: mongo:5
command:
- sh
- -c
- |
mongosh mongodb+srv://quiz-pods.kiada.svc.cluster.local/kiada?tls=false --quiet --file /dev/stdin <<EOF
const dateFrom = new Date("2020-01-01Z");
const dateTo = new Date("2023-01-01Z");
const maxTimeBetweenResponsesMillis = 4 * 3600 * 1000;
const failProbability = 0.25;
const batchSize = 10;
print("Generating dummy quiz responses");
print("===============================");
print();
if (Math.random() < failProbability) {
print("Simulating failure. Exiting.");
quit(1);
}
var cursor = db.questions.find();
var questions = cursor.toArray();
cursor.close();
print("Found " + questions.length + " questions.");
print();
print("Date range:");
print(" From: " + dateFrom);
print(" To: " + dateTo);
print();
print("Generated responses:")
var count=0;
var responses = [];
var date = dateFrom;
do {
date.setTime(date.getTime() + Math.floor(Math.random() * maxTimeBetweenResponsesMillis));
var question = questions[Math.floor(Math.random() * questions.length)];
var answerIndex = Math.floor(Math.random() * question.answers.length);
var response = {
timestamp: date,
questionId: question.id,
answerIndex: answerIndex,
correctAnswerIndex: question.correctAnswerIndex,
correct: answerIndex == question.correctAnswerIndex
};
print(JSON.stringify(response));
responses.push(response);
count++;
if (count % batchSize == 0) {
db.responses.insertMany(responses);
responses = [];
}
} while (date.getTime() < dateTo)
db.responses.insertMany(responses); // insert any remaining responses
print();
print("Generated a total of " + count + " responses.");
print();
print("Done.");
EOF