Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#14] Courses Progress updatedDate fix #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions courses_progess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);