-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_data.js
executable file
·270 lines (227 loc) · 10.1 KB
/
migrate_data.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
#!/usr/local/bin/node
var range = require('range');
var exec = require('child_process').exec;
var fs = require('fs');
var btoa = require('btoa');
var print = require('util').print;
var utils = require('./utils');
var mpInfo = require('./mixpanel-info');
var debug = false;
function printUsage() {
console.log('\033[0;36m', 'Usage: migrate_data.js [year [month [day]]]\n' , '\033[0m' );
console.log('\033[0;36m', ' /? : prints help' , '\033[0m' );
console.log('\033[0;36m', ' [year] : migrates all events for that year, per day' , '\033[0m' );
console.log('\033[0;36m', ' [year month] : migrates all events for that year/month, per day' , '\033[0m' );
console.log('\033[0;36m', '[year month day] : migrates all events for the given day\n' , '\033[0m' );
console.log('\033[0;31m', 'No parameters migrates all events between 1/1/2013 to 12/31/2017, per day (1825 days)' , '\033[0m' );
console.log('\033[0m', ' (only do this when you are ready to migrate ALL DATA and you know everything works)\n' , '\033[0m' );
}
var years = range.range(2014, 2018);
var months = range.range(1, 13);
var runSingleDay = null;
if (process.argv.length === 2) {
printUsage();
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
console.log('\033[0;33m', '>>>> Do you really want to migrate all data (this may take a really long time)? [y|n] ' , '\033[0m' );
// rl.setPrompt('Do you really want to migrate all data (this may take a long time)? [y|n] \n');
rl.prompt();
rl.on('line', function(line) {
if (line.toLowerCase() !== "y") rl.close();
migrateData();
}).on('close',function(){
process.exit(0);
});
}
if (process.argv.length > 2) {
if (process.argv.length === 3) {
years = [process.argv[2]];
if (years.toString() === '/?') {
printUsage();
process.exit(0);
}
if (years.toString() === 'all') {
years = range.range(2014, 2018);
}
}
if (process.argv.length === 4) {
years = [process.argv[2]];
months = [process.argv[3]];
}
if (process.argv.length === 5) {
years = [process.argv[2]];
months = [process.argv[3]];
runSingleDay = process.argv[4];
}
migrateData();
}
function migrateData() {
var processedFile = 'done/_processed.txt';
return fs.readFileAsync(processedFile)
.catch(function() {
return [];
})
.then(function(processed) {
var processes = [];
years.reverse().forEach(function(year) {
months.reverse().forEach(function(month) {
var startDay = 1;
var numDays = utils.getDaysInMonth(month, year);
if (runSingleDay) {
startDay = numDays = runSingleDay;
}
if (debug) console.log('\033[0;32m', 'days: ' , startDay, numDays , '\033[0m' );
for (var day = startDay; day <= numDays; day++) {
var migrateDataForDay = function(month, day, year) {
return function() {
// console.log('\033[0;32m', utils.getTimestamp(), 'Processing: ' , utils.formatDate(month, day, year) , '\033[0m' );
process.stdout.write('\n\033[0;32m');
process.stdout.write(utils.getTimestamp() + ' Processing: ' + utils.formatDate(month, day, year) + ' : exporting...');
process.stdout.write('\033[0m' );
var file = 'out/' + utils.pad(year) + utils.pad(month) + utils.pad(day) + '.json';
var doneFile = file.replace('.json', '.log').replace('out/', 'done/');
if (~processed.indexOf(file)) {
console.log('\033[0;33m', '>>>> Already processed:' , utils.formatDate(month, day, year), 'skipping...' , '\033[0m' );
return;
}
var exportCmd = 'curl https://data.mixpanel.com/api/2.0/export/ -u "' + mpInfo.alpineMobileApi + '" -d from_date="' + utils.formatDate(month, day, year) + '" -d to_date="' + utils.formatDate(month, day, year) + '" > ' + file;
var cleanupCmd = 'rm -f ' + file;
// if (debug) console.log(' >>>> exportCmd ', exportCmd);
return utils.execAsync(exportCmd)
.then(function(res) {
fs.appendFileSync(doneFile, 'Export Stats:\n' + res + '\n');
})
.then(function() {
return importEvents(file);
})
.then(function(err) {
// if (!err)
fs.appendFileSync(processedFile, file + '\n');
})
.then(function() {
return utils.execAsync(cleanupCmd)
});
};
}
processes.push(migrateDataForDay(month, day, year));
}
})
});
return processes.reduce(function(current, next) {
return current.then(function() {
return next();
});
}, Promise.resolve())
.then(function() {
console.log('\033[0;32m', '\n' + utils.getTimestamp(), '>>>> DONE ' , '\033[0m' );
});
});
}
function importEvents(file) {
var errorFile = file.replace('.json', '.log').replace('out/', 'err/');
var doneFile = file.replace('.json', '.log').replace('out/', 'done/');
var errorInfo = '';
return fs.readFileAsync(file)
.then(function(data) {
if (!data) {
console.log('\033[0;33m', '>>>> data file was empty ', file , '\033[0m' );
fs.writeFileSync(errorFile.replace('err/', 'empty/'), 'data file was empty');
return utils.ERRORS.EMPTY;
}
var events = data.split(/\r?\n/);
var numEvents = events.length;
if (events[0] === 'Date range exceeds 1825 days into the past' || events[0] === '') {
console.log('\033[0;33m', '>>>> NO DATA or data too old ', file , '\033[0m' );
fs.writeFileSync(errorFile.replace('err/', 'empty/'), 'NO DATA or data too old: ' + events[0]);
return utils.ERRORS.TOO_OLD;
}
if (debug) console.log('\033[0;36m', '>>>> events: ', file, events.length, (events.length/50) , '\033[0m' );
// var first = true;
var batches = [];
while (events.length) {
batches.push(events.splice(0, 50)
.map(function(item) {
try {
var event = JSON.parse(item);
// {
// "event": "Sync Duration",
// "properties": {
// "time": 1385972209,
// "company": null,
// "duration": 13479,
// "mp_lib": "node",
// "username": "Bob Field"
// }
// }
// "time" is in project timezone, convert back to UTC (+6 hours)
// https://mixpanel.com/help/reference/exporting-raw-data
event.properties.time = event.properties.time + (6 * 60 * 60);
// https://mixpanel.com/help/reference/importing-old-events
event.properties.token = mpInfo.backOfficeToken;
// if (first && event.event === 'TicketAddedToJob') {
// console.log("event: ", event);
// console.log("event formatted: ", btoa(JSON.stringify(event)));
// first = false;
// // process.exit(0);
// }
return event;
}
catch(err) {
console.log('\033[0;36m', '>>>> Error parsing JSON: ' , '\033[0m' );
console.log(item);
errorInfo = item;
throw err;
}
}));
}
if (debug) console.log('\033[0;36m', '>>>> batches: ', file, batches.length , '\033[0m' );
if (debug) console.log('\033[0;36m', '>>>> first batch: ' , '\033[0m' );
if (debug) console.log(batches[0][0]);
if (debug) console.log('\033[0;36m', '>>>> last batch: ' , '\033[0m' );
if (debug) console.log(batches[batches.length - 1][batches[batches.length - 1].length-1]);
var importBatches = batches.map(function(batchedData, idx) {
var json = JSON.stringify(batchedData);
var data = btoa(json);
var importCmd = 'curl https://api.mixpanel.com/import/ -u "' + mpInfo.backOfficeApi + '" -d data=' + data + ' -d verbose=1';
// !!! testing - no import
// importCmd = 'echo ' + importCmd;
return function() {
return utils.execAsync(importCmd)
.then(function(res) {
fs.appendFileSync(doneFile, 'Import Stats [idx: ' + idx + ' | events:' + batchedData.length + ']\n' + res);
fs.appendFileSync(doneFile, '--------------------------------------------------------------------------------\n');
// for(var x=0; x >= 0 && x < (idx-1).toString().length; x++) {
// process.stdout.write("\b");
// }
// process.stdout.write((idx).toString());
utils.showProgress(idx);
})
.catch(function(err) {
console.log('\033[0;31m', '>>>> ERROR: ', file, idx, err, '\033[0m' );
var batchFile = errorFile + '.' + idx;
fs.writeFileSync(batchFile, 'ERROR:' + JSON.stringify(err) + '\n');
fs.appendFileSync(batchFile, 'CMD:' + '\n' + importCmd + '\n');
fs.appendFileSync(batchFile, 'DATA:' + '\n' + data + '\n');
fs.appendFileSync(batchFile, 'JSON:' + '\n' + json + '\n');
});
}
});
fs.appendFileSync(doneFile, '================================================================================\n');
fs.appendFileSync(doneFile, 'Importing: ' + numEvents + ' events / ' + batches.length + ' batches\n');
fs.appendFileSync(doneFile, '================================================================================\n');
process.stdout.write('\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b'); // clear "exporting..." text
process.stdout.write(' ' + batches.length + '/');
// return Promise.all(importBatches);
return importBatches.reduce(function(current, next) {
return current.then(function() {
return next();
});
}, Promise.resolve());
})
.catch(function(err) {
console.log(' >>>> ERROR ', file, err);
fs.writeFileSync(errorFile, err);
if (errorInfo) fs.appendFileSync(errorFile, errorInfo);
return utils.ERRORS.UNKNOWN;
});
};