-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourses_progess.js
More file actions
56 lines (48 loc) · 1.73 KB
/
courses_progess.js
File metadata and controls
56 lines (48 loc) · 1.73 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
const request = require('request');
const user = process.argv[2];
const pass = process.argv[3];
const domain = process.argv[4] || 'localhost:5984';
const protocol = process.argv[5] || 'http';
const uri = protocol + '://' + user + ':' + pass + '@' + domain + '/courses_progress/_all_docs?include_docs=true';
const postUri = protocol + '://' + user + ':' + pass + '@' + domain + '/courses_progress';
const batchSize = 500;
const getCourseProgress = (callback) => {
request.get({ uri, json: true }, callback);
}
const postCallback = (progresses, index) => (err, response) => {
if (err) {
console.log(err);
}
postInBatches(progresses, index + batchSize);
}
const updateProgresses = (err, response) => {
const progresses = response.body.rows.map(({ doc }) => {
if (doc._id.indexOf('_design/') > -1) {
return;
}
if (!doc.updatedDate || !doc.createdDate) {
if (isNaN(doc.createdOn)) {
return { updatedDate: 0, createdDate: 0, ...doc };
}
return { updatedDate: doc.createdOn, createdDate: doc.createdOn, ...doc };
}
if(isNaN(doc.updatedDate) || isNaN(doc.createdDate)) {
return { ...doc , updatedDate: 0, createdDate: 0 };
}
return;
}).filter(progress => progress !== undefined);
postInBatches(progresses, 0);
}
const postInBatches = (progresses, startIndex) => {
if (startIndex > progresses.length) {
return;
}
console.log('Updating courses_progress ' + startIndex + ' through ' + (startIndex + batchSize - 1));
const progressBatch = progresses.slice(startIndex, startIndex + batchSize);
request.post({
uri: postUri + '/_bulk_docs',
body: { docs: progressBatch },
json: true
}, postCallback(progresses, startIndex));
}
getCourseProgress(updateProgresses);