-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
170 lines (135 loc) · 4.93 KB
/
populate.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
const _ = require('lodash');
const co = require('co');
const URI = require('urijs');
const debug = require('debug');
const pgtools = require('pgtools');
const moment = require('moment');
const { Pool } = require('pg');
const { Readable } = require('stream');
const { from: copyFrom } = require('pg-copy-streams');
const { postgres } = require('./config.json');
const { queries, counters, hosts, schemas, timeRange, browsers } = require('./populate.json');
const { runInPromise, randomInt, randomStr, getFromSet } = require('./helpers');
const pool = new Pool(postgres);
(function main() {
const log = debug('vtask::main');
co(function* () {
yield createDb(postgres);
log(`Db ${postgres.database} created`);
const client = yield pool.connect();
log('Client connected');
yield createTables(client);
log('Tables created');
console.time('Time spent on filling tables');
yield fillTables(client);
log('Tables filled');
console.timeEnd('Time spent on filling tables');
client.release();
process.exit(0);
}).catch((err) => {
log(`Error happend: ${err.code || err.status || err.statusCode || ''} ${err.message || err.nativeMessage || ''}`);
log(`Stack: ${err.stack || ''}`);
process.exit(1);
});
}());
function* createDb({ user, host, database }) {
const log = debug('vtask::createDb');
try {
yield runInPromise(pgtools.dropdb, [{ user, host }, database]);
} catch (err) {
log(`Skip dropping db ${database} by reason:
${err.code || err.status || err.statusCode || ''} ${err.message || err.nativeMessage || ''}`);
}
return yield runInPromise(pgtools.createdb, [{ user, host }, database]);
}
function* createTables(client) {
return yield [queries.createA, queries.createB, queries.index1, queries.index2]
.map((q) => client.query(q));
}
function fillTables(client) {
const log = debug('vtask::fillTables');
const [firstClick, otherClicks, unclicked] = [[], [], []];
const rowA = () => {
const [productSet, visitorSet] = [new Set(), new Set()];
let impressionsWithClicksCounter = 0;
return function A() {
const timestamp = randomInt(timeRange.start, timeRange.stop);
const click = { impression_id: A.counter, timestamp };
if (impressionsWithClicksCounter < counters.impressions && Math.random() >= 0.5) {
impressionsWithClicksCounter++;
firstClick.push(click);
otherClicks.push(..._.times(randomInt(0, 5), () => (click)));
} else {
unclicked.push(click);
}
const time = moment(timestamp).format('YYYY-MM-DDTHH:mm:ss');
const product_id = productSet.size < counters.product
? randomInt(1, 99999999)
: getFromSet(productSet, 'product');
productSet.add(product_id);
const visitor_id = visitorSet.size < counters.visitor
? randomInt(1, 99999999)
: getFromSet(visitorSet, 'visitor');
visitorSet.add(visitor_id);
const browser = _.sample(browsers);
const urlParams = {
protocol: _.sample(schemas),
hostname: _.sample(hosts),
path: _.times(randomInt(0, 5), () => `/${randomStr(5)}`).join('') || undefined,
query: _.times(randomInt(0, 3), () => `${randomStr(5)}=${randomStr(5)}`).join('&') || undefined,
};
const url = new URI(urlParams).toString();
return `${time}\t${product_id}\t${visitor_id}\t${browser}\t${url}\n`;
};
};
const rowB = () => {
return function B() {
let imp;
if (firstClick.length > 0) {
imp = firstClick.pop();
} else {
imp = otherClicks.pop() || unclicked.pop();
}
const { impression_id, timestamp } = imp;
const local_time = moment(timestamp + randomInt(250, 250000)).format('YYYY-MM-DDTHH:mm:ss');
const click_id = randomInt(0, 99999999);
return `${impression_id}\t${click_id}\t${local_time}\n`;
};
};
const [generateA, generateB] = [new Readable(), new Readable()];
const [streamA, streamB] = [queries.copyA, queries.copyB].map((q) => client.query(copyFrom(q)));
log('COPY tables started.');
log('Process can take few minutes. Please wait awhile )');
generateA._read = makeReadFn(generateA, 'A', rowA());
generateB._read = makeReadFn(generateB, 'B', rowB());
return new Promise((resolve, reject) => {
const done = () => {
log('All streams closed.');
resolve();
};
streamA.on('error', reject);
streamA.on('end', () => {
log('Stream for Table A closed');
generateB.pipe(streamB);
});
streamB.on('error', reject);
streamB.on('end', () => {
log('Stream for Table B closed');
done();
});
generateA.on('error', reject);
generateB.on('error', reject);
generateA.pipe(streamA);
});
}
function makeReadFn(stream, name, rowFn) {
rowFn.counter = 0;
return () => {
if (++rowFn.counter === counters.rows + 1) {
stream.push(null);
} else {
stream.push(rowFn());
// log('Read %s %d', name, rowFn.counter);
}
};
}