From 0552bb3b740debd1101163635b31e944a8101956 Mon Sep 17 00:00:00 2001 From: Laxman Maharjan Date: Tue, 25 Aug 2020 12:45:32 +0545 Subject: [PATCH 1/4] [#14] Courses Progress updatedDate fix --- courses_progess.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 courses_progess.js diff --git a/courses_progess.js b/courses_progess.js new file mode 100644 index 0000000..2b1faaf --- /dev/null +++ b/courses_progess.js @@ -0,0 +1,50 @@ +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) { + return; + } + return { ...doc, updatedDate: doc.createdOn, createdDate: doc.createdOn }; + }).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); \ No newline at end of file From 2610cc99c0e21cf65c4921f8e6974603cc1a3d91 Mon Sep 17 00:00:00 2001 From: Laxman Maharjan Date: Tue, 25 Aug 2020 13:05:41 +0545 Subject: [PATCH 2/4] update script --- courses_progess.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/courses_progess.js b/courses_progess.js index 2b1faaf..b747fac 100644 --- a/courses_progess.js +++ b/courses_progess.js @@ -26,10 +26,16 @@ const updateProgresses = (err, response) => { if (doc._id.indexOf('_design/') > -1) { return; } - if (doc.updatedDate) { + if (isNaN(doc.updatedDate)) { + console.log("Update " + doc.createdOn); + } + if (doc.updatedDate && doc.createdDate) { return; } - return { ...doc, updatedDate: doc.createdOn, createdDate: doc.createdOn }; + if (isNaN(doc.createdOn)) { + return { updatedDate: 0, createdDate: 0, ...doc }; + } + return { updatedDate: doc.createdOn, createdDate: doc.createdOn, ...doc }; }).filter(progress => progress !== undefined); postInBatches(progresses, 0); } From 7079a25591b78c0efd9f63dd432366c101d12a96 Mon Sep 17 00:00:00 2001 From: Laxman Maharjan Date: Tue, 25 Aug 2020 13:12:13 +0545 Subject: [PATCH 3/4] non numeric time --- courses_progess.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/courses_progess.js b/courses_progess.js index b747fac..3c75719 100644 --- a/courses_progess.js +++ b/courses_progess.js @@ -26,16 +26,16 @@ const updateProgresses = (err, response) => { if (doc._id.indexOf('_design/') > -1) { return; } - if (isNaN(doc.updatedDate)) { - console.log("Update " + doc.createdOn); + if (doc.updatedDate || doc.createdDate) { + if (isNaN(doc.createdOn)) { + return { updatedDate: 0, createdDate: 0, ...doc }; + } + return { updatedDate: doc.createdOn, createdDate: doc.createdOn, ...doc }; } - if (doc.updatedDate && doc.createdDate) { - return; - } - if (isNaN(doc.createdOn)) { - return { updatedDate: 0, createdDate: 0, ...doc }; + if(isNaN(doc.updatedDate) || isNaN(doc.createdDate)) { + return { ...doc, updatedDate: 0, createdDate: 0 }; } - return { updatedDate: doc.createdOn, createdDate: doc.createdOn, ...doc }; + return; }).filter(progress => progress !== undefined); postInBatches(progresses, 0); } From 5052bd0cff8061eef227493002cca1820bba4d6c Mon Sep 17 00:00:00 2001 From: Laxman Maharjan Date: Tue, 25 Aug 2020 13:49:05 +0545 Subject: [PATCH 4/4] created date not numeric --- courses_progess.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/courses_progess.js b/courses_progess.js index 3c75719..b238f0e 100644 --- a/courses_progess.js +++ b/courses_progess.js @@ -26,14 +26,14 @@ const updateProgresses = (err, response) => { 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 (!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 { ...doc , updatedDate: 0, createdDate: 0 }; } return; }).filter(progress => progress !== undefined);