From 6cd791b654d5edf7bdc417c8ef16cd81fe9b8388 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 18:28:02 +0800 Subject: [PATCH 01/11] add basic functionality to add items --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 3907d3b5..15ec090a 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,7 @@ -console.log("works!!", process.argv[2]); - const pg = require('pg'); const configs = { - user: 'akira', + user: 'chelseaee', host: '127.0.0.1', database: 'todo', port: 5432, @@ -26,11 +24,13 @@ let clientConnectionCallback = (err) => { console.log( "error", err.message ); } - let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id"; + const inputs = process.argv.slice(2).join(" "); + + let text = `INSERT INTO items (name) VALUES ('${inputs}') RETURNING id, name`; + + client.query(text, queryDoneCallback); - const values = ["hello"]; - client.query(text, values, queryDoneCallback); }; client.connect(clientConnectionCallback); From eac6f0ba7aa0698df749aec29b5b92b7764ec464 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 19:31:40 +0800 Subject: [PATCH 02/11] add function to complete items --- index.js | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 15ec090a..4eb8a6bb 100644 --- a/index.js +++ b/index.js @@ -9,28 +9,62 @@ const configs = { const client = new pg.Client(configs); +const d = new Date(); +const today = `${d.getDate()}/${(d.getMonth()+1)}/${d.getFullYear()}` +const now = `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}` + +const command = process.argv[2] + let queryDoneCallback = (err, result) => { if (err) { - console.log("query error", err.message); + console.log("query error", err.message); } else { - console.log("result", result.rows ); + showResults(result); } client.end(); }; + +const showResults = res => { + res.rows.forEach(item => { + let box = `[ ]` + item.completion ? box = `[X]` : box + console.log(`${item.id}. ${box} - ${item.name}`); + }); +}; + + let clientConnectionCallback = (err) => { - if( err ){ - console.log( "error", err.message ); - } + if (err) { + console.log("error", err.message); + } + + if (command === 'add') { + const inputs = process.argv.slice(3).join(" "); - const inputs = process.argv.slice(2).join(" "); + let text = `INSERT INTO items (name) VALUES ('${inputs}') RETURNING id, name`; - let text = `INSERT INTO items (name) VALUES ('${inputs}') RETURNING id, name`; + client.query(text, queryDoneCallback); + + } else if (command === 'done') { - client.query(text, queryDoneCallback); + const targetItem = process.argv[3]; + const script = `UPDATE items SET completion = TRUE WHERE id=${targetItem}` + client.query(script, queryDoneCallback); + } else if (command === 'show') { + + const script = `SELECT * FROM items` + displayList(); + + } }; -client.connect(clientConnectionCallback); + +const displayList = ()=> { + client.query(`SELECT * FROM items`, queryDoneCallback); +} + +client.connect(clientConnectionCallback); \ No newline at end of file From f3c3cd73a2a90e9681c81fbe1b9d10f7cfed9248 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 19:50:46 +0800 Subject: [PATCH 03/11] add ability to see created_at, updated_at, as well as archive and unarchive items --- index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 4eb8a6bb..8573cd9e 100644 --- a/index.js +++ b/index.js @@ -27,10 +27,21 @@ let queryDoneCallback = (err, result) => { const showResults = res => { res.rows.forEach(item => { + + if (item.archived) { + return; + } + let box = `[ ]` - item.completion ? box = `[X]` : box - console.log(`${item.id}. ${box} - ${item.name}`); - }); + if (item.completion) { + box = `[X]` + let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n Completed: ${item.completed_at} \n =======================`; + colorLog('green', output); + } else { + let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n =======================`; + colorLog('red', output) + } + }); }; @@ -50,7 +61,7 @@ let clientConnectionCallback = (err) => { } else if (command === 'done') { const targetItem = process.argv[3]; - const script = `UPDATE items SET completion = TRUE WHERE id=${targetItem}` + const script = `UPDATE items SET completion = TRUE, completed_at = CURRENT_TIMESTAMP WHERE id=${targetItem}` client.query(script, queryDoneCallback); } else if (command === 'show') { @@ -58,6 +69,15 @@ let clientConnectionCallback = (err) => { const script = `SELECT * FROM items` displayList(); + } else if (command === 'archive') { + const targetItem = process.argv[3]; + const script = `UPDATE items SET archived = TRUE WHERE id=${targetItem}` + client.query(script, queryDoneCallback); + + } else if (command==='unarchive') { + const targetItem = process.argv[3]; + const script = `UPDATE items SET archived = FALSE WHERE id=${targetItem}` + client.query(script, queryDoneCallback); } }; @@ -67,4 +87,28 @@ const displayList = ()=> { client.query(`SELECT * FROM items`, queryDoneCallback); } -client.connect(clientConnectionCallback); \ No newline at end of file +client.connect(clientConnectionCallback); + + + +//Function to format colors of console logs: lavender, green or red. +const colorLog = (col, output) => { + let r; + let g; + let b + if (col === 'lavender') { + r = 181; + g = 126; + b = 220 + } else if (col === 'red') { + r = 230; + g = 0; + b = 0; + } else if (col === 'green') { + r = 0; + g = 230; + b = 0 + } + + console.log(`\x1b[38;2;${r};${g};${b}m%s\x1b[0m`, output); +} \ No newline at end of file From 0ee4f2fe7b8546127ca2d95f732f4e04dfe70b66 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 21:48:02 +0800 Subject: [PATCH 04/11] add function to get average task completion time --- index.js | 107 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 8573cd9e..f687065b 100644 --- a/index.js +++ b/index.js @@ -15,23 +15,36 @@ const now = `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}` const command = process.argv[2] -let queryDoneCallback = (err, result) => { +let queryDoneCallback = (err, res) => { if (err) { console.log("query error", err.message); } else { - showResults(result); + formatDisplay(res); } client.end(); }; +const getAvgTime = (err, res) => { + if (err) { + console.log("query error", err.message); + } else { + const array = res.rows + const theNums = array.map( item => item.mins_diff); + const theSum = theNums.reduce( (a,b) => a + b ); + const averageMins = (theSum / theNums.length).toFixed(1); + colorLog('lavender', `Average minutes to complete a task: ${averageMins}`) -const showResults = res => { - res.rows.forEach(item => { + + } + client.end(); +}; + +const formatDisplay = res => { + res.rows.forEach(item => { if (item.archived) { return; } - let box = `[ ]` if (item.completion) { box = `[X]` @@ -41,7 +54,7 @@ const showResults = res => { let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n =======================`; colorLog('red', output) } - }); + }); }; @@ -51,43 +64,58 @@ let clientConnectionCallback = (err) => { console.log("error", err.message); } + //ADD ITEMS INTO TO-DO LIST. if (command === 'add') { const inputs = process.argv.slice(3).join(" "); - let text = `INSERT INTO items (name) VALUES ('${inputs}') RETURNING id, name`; - client.query(text, queryDoneCallback); - - } else if (command === 'done') { + //COMPLETE ITEMS INTO TO-DO LIST. + } else if (command === 'done') { const targetItem = process.argv[3]; const script = `UPDATE items SET completion = TRUE, completed_at = CURRENT_TIMESTAMP WHERE id=${targetItem}` client.query(script, queryDoneCallback); + //DISPLAY ITEMS INTO TO-DO LIST. } else if (command === 'show') { - - const script = `SELECT * FROM items` - displayList(); - + client.query(`SELECT * FROM items`, queryDoneCallback); + + // ARCHIVE AN ITEM IN THE TO-DO LIST. } else if (command === 'archive') { const targetItem = process.argv[3]; const script = `UPDATE items SET archived = TRUE WHERE id=${targetItem}` client.query(script, queryDoneCallback); - - } else if (command==='unarchive') { + + // UN-ARCHIVE AN ITEM IN THE TO-DO LIST. + } else if (command === 'unarchive') { const targetItem = process.argv[3]; const script = `UPDATE items SET archived = FALSE WHERE id=${targetItem}` client.query(script, queryDoneCallback); + } else if (command==='stats') { + + const statQuery = process.argv[3] + + switch (statQuery) { + case 'complete-time': + + const script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` + client.query(script, getAvgTime); + break; + + default: + console.log(`Error.`) + break; + } + + } }; +client.connect(clientConnectionCallback); -const displayList = ()=> { - client.query(`SELECT * FROM items`, queryDoneCallback); -} -client.connect(clientConnectionCallback); +// ======= HELPER FUNCTIONS ============ @@ -96,18 +124,33 @@ const colorLog = (col, output) => { let r; let g; let b - if (col === 'lavender') { - r = 181; - g = 126; - b = 220 - } else if (col === 'red') { - r = 230; - g = 0; - b = 0; - } else if (col === 'green') { - r = 0; - g = 230; - b = 0 + + switch (col) { + case 'lavender': + r = 181; + g = 126; + b = 220 + break; + case 'red': + r = 230; + g = 0; + b = 0; + break; + case 'green': + r = 0; + g = 230; + b = 0 + break; + case 'white': + r = 255; + g = 255; + b = 255; + break; + default: + r = 255; + g = 255; + b = 255; + break; } console.log(`\x1b[38;2;${r};${g};${b}m%s\x1b[0m`, output); From 9b0deb6dc8f9dc8ebf715f7265c5b50903309a46 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 21:59:02 +0800 Subject: [PATCH 05/11] add function for best to worst item --- index.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f687065b..766390a5 100644 --- a/index.js +++ b/index.js @@ -33,12 +33,25 @@ const getAvgTime = (err, res) => { const theSum = theNums.reduce( (a,b) => a + b ); const averageMins = (theSum / theNums.length).toFixed(1); colorLog('lavender', `Average minutes to complete a task: ${averageMins}`) - - } client.end(); }; +const bestWorst = (err, res) => { + + err && console.log(`Query error`, err.message) + + const array = res.rows; + const best = array.shift(); + const worst = array.pop(); + let itemDisplay = item => `${item.id}. ${item.name} | Completed in ${item.mins_diff} mins` + let output = `Fastest item: \n ${itemDisplay(best)} \n\n Slowest item: \n ${itemDisplay(worst)}` + colorLog('lavender', output); + + client.end(); + +} + const formatDisplay = res => { res.rows.forEach(item => { @@ -94,14 +107,18 @@ let clientConnectionCallback = (err) => { } else if (command==='stats') { const statQuery = process.argv[3] + let script; + switch (statQuery) { case 'complete-time': - - const script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` - client.query(script, getAvgTime); + script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` + client.query(script, getAvgTime); + break; + case 'best-worst': + script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` + client.query(script, bestWorst) break; - default: console.log(`Error.`) break; From 76486f2cfc96295810928305e78d7a55b9ea0e23 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Wed, 15 Apr 2020 22:41:32 +0800 Subject: [PATCH 06/11] add functionality for stats add-time --- index.js | 138 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index 766390a5..098ff771 100644 --- a/index.js +++ b/index.js @@ -24,51 +24,6 @@ let queryDoneCallback = (err, res) => { client.end(); }; -const getAvgTime = (err, res) => { - if (err) { - console.log("query error", err.message); - } else { - const array = res.rows - const theNums = array.map( item => item.mins_diff); - const theSum = theNums.reduce( (a,b) => a + b ); - const averageMins = (theSum / theNums.length).toFixed(1); - colorLog('lavender', `Average minutes to complete a task: ${averageMins}`) - } - client.end(); -}; - -const bestWorst = (err, res) => { - - err && console.log(`Query error`, err.message) - - const array = res.rows; - const best = array.shift(); - const worst = array.pop(); - let itemDisplay = item => `${item.id}. ${item.name} | Completed in ${item.mins_diff} mins` - let output = `Fastest item: \n ${itemDisplay(best)} \n\n Slowest item: \n ${itemDisplay(worst)}` - colorLog('lavender', output); - - client.end(); - -} - - -const formatDisplay = res => { - res.rows.forEach(item => { - if (item.archived) { - return; - } - let box = `[ ]` - if (item.completion) { - box = `[X]` - let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n Completed: ${item.completed_at} \n =======================`; - colorLog('green', output); - } else { - let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n =======================`; - colorLog('red', output) - } - }); -}; let clientConnectionCallback = (err) => { @@ -83,42 +38,46 @@ let clientConnectionCallback = (err) => { let text = `INSERT INTO items (name) VALUES ('${inputs}') RETURNING id, name`; client.query(text, queryDoneCallback); - //COMPLETE ITEMS INTO TO-DO LIST. + //COMPLETE ITEMS INTO TO-DO LIST. } else if (command === 'done') { const targetItem = process.argv[3]; const script = `UPDATE items SET completion = TRUE, completed_at = CURRENT_TIMESTAMP WHERE id=${targetItem}` client.query(script, queryDoneCallback); - //DISPLAY ITEMS INTO TO-DO LIST. + //DISPLAY ITEMS INTO TO-DO LIST. } else if (command === 'show') { client.query(`SELECT * FROM items`, queryDoneCallback); - - // ARCHIVE AN ITEM IN THE TO-DO LIST. + + // ARCHIVE AN ITEM IN THE TO-DO LIST. } else if (command === 'archive') { const targetItem = process.argv[3]; const script = `UPDATE items SET archived = TRUE WHERE id=${targetItem}` client.query(script, queryDoneCallback); - - // UN-ARCHIVE AN ITEM IN THE TO-DO LIST. + + // UN-ARCHIVE AN ITEM IN THE TO-DO LIST. } else if (command === 'unarchive') { const targetItem = process.argv[3]; const script = `UPDATE items SET archived = FALSE WHERE id=${targetItem}` client.query(script, queryDoneCallback); - } else if (command==='stats') { + } else if (command === 'stats') { const statQuery = process.argv[3] let script; switch (statQuery) { - case 'complete-time': - script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` + case 'complete-time': + script = `SELECT *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` client.query(script, getAvgTime); break; case 'best-worst': - script = `select *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` + script = `SELECT *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` client.query(script, bestWorst) break; + case 'add-time': + script = `SELECT name, date(created_at) AS MYDATE FROM items;` + client.query(script, avgItemsPerDay) + break; default: console.log(`Error.`) break; @@ -163,7 +122,7 @@ const colorLog = (col, output) => { g = 255; b = 255; break; - default: + default: r = 255; g = 255; b = 255; @@ -171,4 +130,71 @@ const colorLog = (col, output) => { } console.log(`\x1b[38;2;${r};${g};${b}m%s\x1b[0m`, output); +} + + +const formatDisplay = res => { + res.rows.forEach(item => { + if (item.archived) { + return; + } + let box = `[ ]` + if (item.completion) { + box = `[X]` + let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n Completed: ${item.completed_at} \n =======================`; + colorLog('green', output); + } else { + let output = `${item.id}. ${box} - ${item.name} \n Created: ${item.created_at} \n =======================`; + colorLog('red', output) + } + }); +}; + + +const getAvgTime = (err, res) => { + if (err) { + console.log("query error", err.message); + } else { + const array = res.rows + const theNums = array.map(item => item.mins_diff); + const theSum = theNums.reduce((a, b) => a + b); + const averageMins = (theSum / theNums.length).toFixed(1); + colorLog('lavender', `Average minutes to complete a task: ${averageMins}`) + } + client.end(); +}; + +const bestWorst = (err, res) => { + + err && console.log(`Query error`, err.message) + + const array = res.rows; + const best = array.shift(); + const worst = array.pop(); + let itemDisplay = item => `${item.id}. ${item.name} | Completed in ${item.mins_diff} mins` + let output = `Fastest item: \n ${itemDisplay(best)} \n\n Slowest item: \n ${itemDisplay(worst)}` + colorLog('lavender', output); + client.end(); +} + +const avgItemsPerDay = (err, res) => { + err && console.log(`Query error`, err.message) + const array = res.rows; + let noOfDays = 0; + + array.forEach((item, index) => { + //If this is not the last item in the array, + if (array[index + 1]) { + //If the next item does not have a similar date, add to the number of days. + if (item.date !== array[index + 1].date) { + noOfDays++; + } + //If this is the last item in the array, save the latest count into the array. + } else { + noOfDays++; + } + }) + const avgNoPerDay = array.length / noOfDays; + colorLog('lavender', `Average number of items created per day = ${avgNoPerDay}`); + client.end(); } \ No newline at end of file From 492faf01072347153c7cb7f4010797154ae1bb78 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Thu, 16 Apr 2020 00:02:58 +0800 Subject: [PATCH 07/11] add function to get items between two dates --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 098ff771..867967f1 100644 --- a/index.js +++ b/index.js @@ -78,8 +78,14 @@ let clientConnectionCallback = (err) => { script = `SELECT name, date(created_at) AS MYDATE FROM items;` client.query(script, avgItemsPerDay) break; + case 'between': + const date1 = process.argv[4]; + const date2 = process.argv[5]; + script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')`; + client.query(script, queryDoneCallback); + break; default: - console.log(`Error.`) + return console.log(`Error. Stat query not found.`) break; } @@ -197,4 +203,4 @@ const avgItemsPerDay = (err, res) => { const avgNoPerDay = array.length / noOfDays; colorLog('lavender', `Average number of items created per day = ${avgNoPerDay}`); client.end(); -} \ No newline at end of file +} From 1bc9dc2de65871d52e94bd7c35ef04fd685ddbab Mon Sep 17 00:00:00 2001 From: chelsejw Date: Thu, 16 Apr 2020 00:10:41 +0800 Subject: [PATCH 08/11] add completed-between function --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 867967f1..f72ae4fa 100644 --- a/index.js +++ b/index.js @@ -64,7 +64,6 @@ let clientConnectionCallback = (err) => { const statQuery = process.argv[3] let script; - switch (statQuery) { case 'complete-time': script = `SELECT *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` @@ -79,11 +78,17 @@ let clientConnectionCallback = (err) => { client.query(script, avgItemsPerDay) break; case 'between': - const date1 = process.argv[4]; - const date2 = process.argv[5]; + var date1 = process.argv[4]; + var date2 = process.argv[5]; script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')`; client.query(script, queryDoneCallback); break; + case 'completed-between': + var date1 = process.argv[4]; + var date2 = process.argv[5]; + script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY') AND completion = true`; + client.query(script, queryDoneCallback); + break; default: return console.log(`Error. Stat query not found.`) break; From 7a9a320a9ef99d84de86fe26986cce460ee549da Mon Sep 17 00:00:00 2001 From: chelsejw Date: Thu, 16 Apr 2020 00:16:46 +0800 Subject: [PATCH 09/11] add complete-time order function --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f72ae4fa..e7a161d4 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,14 @@ let clientConnectionCallback = (err) => { case 'between': var date1 = process.argv[4]; var date2 = process.argv[5]; - script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')`; + if (process.argv[6]==='complete-time') { + let order = (process.argv[7]).toLowerCase(); + script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY') ORDER BY EXTRACT(EPOCH FROM (completed_at - created_at)/60) ${order}`; + + } else { + script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')`; + } + client.query(script, queryDoneCallback); break; case 'completed-between': From 28a0f01d80488ef9d631e083463332ce696831b8 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Thu, 16 Apr 2020 00:53:29 +0800 Subject: [PATCH 10/11] fix add-time function. add comments --- index.js | 127 +++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 64 deletions(-) diff --git a/index.js b/index.js index e7a161d4..9c5a43ab 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,14 @@ const pg = require('pg'); - const configs = { user: 'chelseaee', host: '127.0.0.1', database: 'todo', port: 5432, }; - const client = new pg.Client(configs); - const d = new Date(); const today = `${d.getDate()}/${(d.getMonth()+1)}/${d.getFullYear()}` const now = `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}` - const command = process.argv[2] let queryDoneCallback = (err, res) => { @@ -24,8 +20,6 @@ let queryDoneCallback = (err, res) => { client.end(); }; - - let clientConnectionCallback = (err) => { if (err) { @@ -59,41 +53,46 @@ let clientConnectionCallback = (err) => { const targetItem = process.argv[3]; const script = `UPDATE items SET archived = FALSE WHERE id=${targetItem}` client.query(script, queryDoneCallback); - } else if (command === 'stats') { + + // USE ONE OF THE STATS COMMANDS. + } else if (command === 'stats') { const statQuery = process.argv[3] let script; - switch (statQuery) { + + // GET THE AVERAGE COMPLETION TIME OF EACH TASK. case 'complete-time': - script = `SELECT *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` + script = `SELECT *, EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items` client.query(script, getAvgTime); break; + // GET THE TASKS WITH THE LONGEST / SHORTEST COMPLETION TIME. case 'best-worst': - script = `SELECT *,EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` + script = `SELECT *, EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` client.query(script, bestWorst) break; + // GET THE AVERAGE NUMBER OF ITEMS ADDED PER DAY. case 'add-time': - script = `SELECT name, date(created_at) AS MYDATE FROM items;` + script = `SELECT name, date(created_at) AS date FROM items;` client.query(script, avgItemsPerDay) break; + //GET THE ITEMS CREATED BETWEEN TWO DATES. case 'between': var date1 = process.argv[4]; var date2 = process.argv[5]; - if (process.argv[6]==='complete-time') { + if (process.argv[6] === 'complete-time') { let order = (process.argv[7]).toLowerCase(); script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY') ORDER BY EXTRACT(EPOCH FROM (completed_at - created_at)/60) ${order}`; - } else { script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')`; } - client.query(script, queryDoneCallback); break; - case 'completed-between': + //GET THE ITEMS THAT WERE COMPLETED BETWEEN TWO DATES. + case 'completed-between': var date1 = process.argv[4]; var date2 = process.argv[5]; - script = `SELECT * FROM items WHERE date(created_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY') AND completion = true`; + script = `SELECT * FROM items WHERE date(completed_at) BETWEEN to_date('${date1}', 'DD/MM/YY') AND to_date('${date2}', 'DD/MM/YY')` client.query(script, queryDoneCallback); break; default: @@ -111,46 +110,6 @@ client.connect(clientConnectionCallback); // ======= HELPER FUNCTIONS ============ - - -//Function to format colors of console logs: lavender, green or red. -const colorLog = (col, output) => { - let r; - let g; - let b - - switch (col) { - case 'lavender': - r = 181; - g = 126; - b = 220 - break; - case 'red': - r = 230; - g = 0; - b = 0; - break; - case 'green': - r = 0; - g = 230; - b = 0 - break; - case 'white': - r = 255; - g = 255; - b = 255; - break; - default: - r = 255; - g = 255; - b = 255; - break; - } - - console.log(`\x1b[38;2;${r};${g};${b}m%s\x1b[0m`, output); -} - - const formatDisplay = res => { res.rows.forEach(item => { if (item.archived) { @@ -198,21 +157,61 @@ const bestWorst = (err, res) => { const avgItemsPerDay = (err, res) => { err && console.log(`Query error`, err.message) const array = res.rows; - let noOfDays = 0; + let noOfDays = 1; array.forEach((item, index) => { - //If this is not the last item in the array, - if (array[index + 1]) { - //If the next item does not have a similar date, add to the number of days. - if (item.date !== array[index + 1].date) { - noOfDays++; + let nextItem = array[index+1] + let thisDate = item.date.toString(); + + //If this is not the last item in the array + if (nextItem) { + let nextDate = nextItem.date.toString(); + + //And the next date is not the same, add to noOf Days. + if (thisDate!==nextDate) { + noOfDays++; } //If this is the last item in the array, save the latest count into the array. - } else { - noOfDays++; } }) + const avgNoPerDay = array.length / noOfDays; colorLog('lavender', `Average number of items created per day = ${avgNoPerDay}`); client.end(); } + + +//Function to format colors of console logs: lavender, green or red. +const colorLog = (col, output) => { + let r; + let g; + let b + switch (col) { + case 'lavender': + r = 181; + g = 126; + b = 220 + break; + case 'red': + r = 230; + g = 0; + b = 0; + break; + case 'green': + r = 0; + g = 230; + b = 0 + break; + case 'white': + r = 255; + g = 255; + b = 255; + break; + default: + r = 255; + g = 255; + b = 255; + break; + } + console.log(`\x1b[38;2;${r};${g};${b}m%s\x1b[0m`, output); +} \ No newline at end of file From 40f1d29522426eb4d164a0cb79d28e7394fbecae Mon Sep 17 00:00:00 2001 From: chelsejw Date: Thu, 16 Apr 2020 00:58:35 +0800 Subject: [PATCH 11/11] fix bugs for complete-time and best-worst where items that are not completed are included in the calculation --- index.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 9c5a43ab..f7455cbb 100644 --- a/index.js +++ b/index.js @@ -54,7 +54,6 @@ let clientConnectionCallback = (err) => { const script = `UPDATE items SET archived = FALSE WHERE id=${targetItem}` client.query(script, queryDoneCallback); - // USE ONE OF THE STATS COMMANDS. } else if (command === 'stats') { const statQuery = process.argv[3] @@ -68,12 +67,12 @@ let clientConnectionCallback = (err) => { break; // GET THE TASKS WITH THE LONGEST / SHORTEST COMPLETION TIME. case 'best-worst': - script = `SELECT *, EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items ORDER BY mins_diff ASC` + script = `SELECT *, EXTRACT(EPOCH FROM (completed_at - created_at)/60) as mins_diff from items WHERE completion = true ORDER BY mins_diff ASC` client.query(script, bestWorst) break; // GET THE AVERAGE NUMBER OF ITEMS ADDED PER DAY. case 'add-time': - script = `SELECT name, date(created_at) AS date FROM items;` + script = `SELECT name, date(created_at) AS date FROM items WHERE completion = true;` client.query(script, avgItemsPerDay) break; //GET THE ITEMS CREATED BETWEEN TWO DATES. @@ -99,17 +98,13 @@ let clientConnectionCallback = (err) => { return console.log(`Error. Stat query not found.`) break; } - - } - }; client.connect(clientConnectionCallback); // ======= HELPER FUNCTIONS ============ - const formatDisplay = res => { res.rows.forEach(item => { if (item.archived) { @@ -127,6 +122,7 @@ const formatDisplay = res => { }); }; +//Get the average time of completion for all the items. const getAvgTime = (err, res) => { if (err) { @@ -141,6 +137,7 @@ const getAvgTime = (err, res) => { client.end(); }; +//Get the items with the longest (worst) and shortest (best) completion times. const bestWorst = (err, res) => { err && console.log(`Query error`, err.message) @@ -174,7 +171,6 @@ const avgItemsPerDay = (err, res) => { //If this is the last item in the array, save the latest count into the array. } }) - const avgNoPerDay = array.length / noOfDays; colorLog('lavender', `Average number of items created per day = ${avgNoPerDay}`); client.end();