-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteStory.js
More file actions
95 lines (84 loc) · 2.91 KB
/
writeStory.js
File metadata and controls
95 lines (84 loc) · 2.91 KB
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
// writeStory.js
// Conor Slack
// Logz.io Hackathon 2020 - Log Shipper UI
// Load Test Data
// const {program, story} = require('./testData');
const randomUseragent = require('random-useragent');
// Get random item from array
const randomItem = (items) => {
return items[Math.floor(Math.random()*items.length)];
}
// Generate random IP address
const randomIp = () => {
const arr = new Array(4).fill(0);
return arr.map(() => Math.floor(Math.random()*255)).join('.')
}
// Standard Normal variate using Box-Muller transform.
const randn_bm = () => {
var u = 0, v = 0;
while(u === 0) u = Math.random();
while(v === 0) v = Math.random();
return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}
// Generate timestamps
const timestampList = (from_time, to_time, n) => {
from_time = new Date(from_time);
to_time = new Date(to_time);
const int = Math.floor((from_time - to_time)/n);
let timestamps = new Array(+n).fill(+from_time);
return timestamps.map((t,i )=> {
let unix = t + i*int + randn_bm()*(int/2); // even intervals plus noise
let timestamp = new Date(unix);
return timestamp.toISOString();
});
}
// Generate random object from template
const randomValues = (fields) => {
let obj = {};
fields.forEach(field => {
if (field.type === 'key') {
let values = [];
if (field.probability && field.probability.length === field.values.length) {
field.values.forEach((v, i) => {
let arr = new Array(Math.floor(field.probability[i]*100)).fill(v)
values = [...values, ...arr]
})
} else {
values = field.values
}
obj[field.field_name] = randomItem(values)
} else if (field.type === 'ip') {
obj[field.field_name] = randomIp()
} else if (field.type === 'ua') {
obj[field.field_name] = randomUseragent.getRandom()
} else if (field.type === 'normal') {
obj[field.field_name] = Math.floor(field.mean + randn_bm()*field.sd)
}
})
return obj
}
// Write an array of logs from a program (one chapter of story)
const generateLogs = (program) => {
const {log_type, from_time, to_time, n, fields} = program;
const timestamps = timestampList(from_time, to_time, n);
let logs = [];
timestamps.forEach(t => {
let logLine = randomValues(fields);
logLine['@timestamp'] = t;
logLine.type = log_type;
logs.push(logLine);
})
return logs;
}
// Write a "story" array from array of "chapters"
const writeStory = (story) => {
return new Promise((resolve, reject) => {
let storyLogs = [];
story.forEach(chapter => {
storyLogs = [...storyLogs, ...generateLogs(chapter)]
})
resolve(storyLogs)
})
};
module.exports = writeStory
// writeStory(story).then(console.log)