-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
65 lines (56 loc) · 1.44 KB
/
index.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
'use strict';
/**
* Notify user of updates
*/
var client = require('./_client');
var randomEvent = require('./_randomEvent');
var eventBuffer = require('./eventBuffer');
var createIndex = require('./_createIndex');
var argv = require('./argv');
var total = argv.total;
var startingMoment = argv.start;
var endingMoment = argv.end;
var indexPrefix = argv.indexPrefix;
client.usable
.then(function () {
console.log('Generating', total, 'events from', startingMoment.format(), 'to', endingMoment.format());
})
.then(function () {
if (argv.dry) return;
return createIndex();
})
.then(function () {
argv.log('creating', total, 'events');
var i = total;
return (function crunch() {
argv.log('creating no more than', i, 'events');
for (; i >= 0; i--) {
var event = randomEvent(indexPrefix);
if (argv.dry) {
console.log('\n\n', event);
continue;
}
var header = { _index: event.index };
if (argv.types) {
header._type = '_doc';
}
// eventBuffer.push might return a promise,
var delay = eventBuffer.push({
header,
body: event
});
if (delay) {
argv.log('waiting for bulk to complete');
// stop the loop and restart once complete
return Promise.resolve(delay).then(crunch);
}
}
}());
})
.then(function () {
if (argv.dry) return;
eventBuffer.push(false);
})
.catch(function (err) {
console.error(err.stack);
});