From 9f03a7237470708df72204b7f0607bc6ff036372 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 24 Jun 2018 22:49:46 +0200 Subject: [PATCH] Add statistics function with average closed time Add conditional check if post.md already exists --- main.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 99b66bc..e568dd7 100644 --- a/main.js +++ b/main.js @@ -41,7 +41,10 @@ for (const arg of process.argv) { } if (!args.includes('--no-download')) download() -else createTable() +else { + createTable() + createStatistics() +} function download() { api.Projects @@ -62,6 +65,7 @@ function download() { } }) .then(createTable) + .then(createStatistics) }) } @@ -143,6 +147,42 @@ function createTable() { table += ` ${closedAt} |\n` } - fs.copyFileSync(path.join(__dirname, 'template.md'), path.join(__dirname, year, month, 'post.md')) + if (!fs.existsSync(path.join(__dirname, year, month, 'post.md'))) + fs.copyFileSync(path.join(__dirname, 'template.md'), path.join(__dirname, year, month, 'post.md')) fs.writeFileSync(path.join(__dirname, year, month, 'table.md'), table, {encoding: 'UTF-8'}) } + +function createStatistics() { + let opened = klaw(openedPath) + let closed = klaw(closedPath) + + let statistics = '## Statistics\n\n' + + statistics += `In the month of ${month} ` + statistics += `${opened.length} issues were opened and ` + statistics += `${closed.length} issues were closed.\n\n` + + let averageClosedDays + let averageClosedHours + for (const file of closed) { + const issue = require(file.path) + const openedAt = new Date(issue.created_at) + const closedAt = new Date(issue.closed_at) + const diffDays = Math.floor((closedAt - openedAt) / (1000 * 60 * 60 * 24)) + const diffHours = Math.floor((closedAt - openedAt) / (1000 * 60 * 60)) + + averageClosedDays = (typeof averageClosedDays === 'undefined') + ? averageClosedDays = diffDays + : averageClosedDays += diffDays + averageClosedHours = (typeof averageClosedHours === 'undefined') + ? averageClosedHours = diffHours + : averageClosedHours += diffHours + } + averageClosedDays = Math.floor(averageClosedDays / closed.length) + averageClosedHours = Math.floor(averageClosedHours / closed.length) + + statistics += `The average time to close issues was ${averageClosedDays} days ` + statistics += `or ${averageClosedHours} hours.\n\n` + + fs.writeFileSync(path.join(__dirname, year, month, 'statistics.md'), statistics, {encoding: 'UTF-8'}) +}