diff --git a/app.js b/app.js index 3bcb09b..6caed63 100644 --- a/app.js +++ b/app.js @@ -1,15 +1,18 @@ -const fs = require('node-fs') +const fs = require('node-fs'); const sourceFilePath = './sources/tasks.json'; const stats = { - countAllTasks: 0, - tasksByDay: {} -} + countAllTasks: 0, + tasksByDay : [], + tasksByMonth : [], + tasksByYear : [], + +}; // const enableFilterByTag = true; const enableFilterByTag = false; -const filterByTag = "work"; +const filterByTag = 'work'; // const outputFileName = "work_report"; -const outputFileName = "all_tasks_report"; +const outputFileName = 'all_tasks_report'; let countExcluded = 0; let countIncluded = 0; @@ -17,237 +20,291 @@ let countIncluded = 0; console.log(` ### lecture de ${sourceFilePath}`); fs.stat(sourceFilePath, function (err, stat) { - if (err == null) { - console.log(`File ${sourceFilePath} exists`); - sortTasksFromJson(stat) - } else if (err.code === 'ENOENT') { - // file does not exist - console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err) - } else { - console.log('Some other error: ', err.code); - } + if (err == null) { + console.log(`File ${sourceFilePath} exists`); + sortTasksFromJson(stat); + } else if (err.code === 'ENOENT') { + // file does not exist + console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err); + } else { + console.log('Some other error: ', err.code); + } }); // parcourir les tâches function sortTasksFromJson(statObject) { - console.log('sortTasksFromJson') - fs.readFile(sourceFilePath, 'utf8', function (err, data) { - if (err) { - return console.log(err); - } - let dataTransformed = JSON.parse(data); - // console.log('data keys ', Object.keys(dataTransformed)) - if (dataTransformed["contents"]) { + console.log('sortTasksFromJson'); + fs.readFile(sourceFilePath, 'utf8', function (err, data) { + if (err) { + return console.log(err); + } + let dataTransformed = JSON.parse(data); + // console.log('data keys ', Object.keys(dataTransformed)) + if (dataTransformed['contents']) { + countTasks = dataTransformed['contents'].length; + stats.countAllTasks = countTasks; - countTasks = dataTransformed["contents"].length - stats.countAllTasks = countTasks; + console.log('yes data ! tasks:', countTasks); - console.log('yes data ! tasks:', countTasks) + dataTransformed['contents'].forEach((elem, index) => { - dataTransformed["contents"].forEach((elem, index) => { - - // console.log('tâche ', index) - // évacuer les sous tâches - // if (elem['contents']) { - // console.log('content ception, on évacue') - // countExcluded++; - // return; - // } - // filtrer par tag les tâches - if (enableFilterByTag) { - console.log('filtre activé', filterByTag) - if (!elem['drawer'] || !elem['properties']['tags']) { - - - console.log('on vire cette tâche n\'ayant pas de drawer :', elem) - countExcluded++; - return; - } else if ( - elem['properties'] && - typeof elem['properties']['tags'] !== "undefined" && - elem['properties']['tags'].indexOf(filterByTag) === -1 - ) { - console.log('on vire cette tâche ayant les itags :', elem['drawer']['ARCHIVE_ITAGS']) - countExcluded++; - return; - } - } else { - console.log(' - filtre désactivé') - } - // après le filtre, on range les tâches - - if (elem['properties']) { - console.log(' - ref ', elem['ref']) - let title = elem['properties']['raw-value']; - let tags = elem['tags'] ? elem['tags'] : []; - let closeDate = 'Indéfini'; - let todoKeyword = 'DONE'; - if (elem['properties']['closed']) { - closeDate = elem['properties']['closed']['end']; - - } else if (elem['drawer'] && elem['drawer']['ARCHIVE_TIME']) { - closeDate = elem['drawer']['ARCHIVE_TIME']; - } - if (elem['drawer'] && elem['drawer']['ARCHIVE_TODO']) { - - todoKeyword = elem['drawer']['ARCHIVE_TODO']; - } - if (!tags.length && elem['drawer'] && elem['drawer']['ARCHIVE_ITAGS']) { - - tags += elem['drawer']['ARCHIVE_ITAGS']; - } - - // jour, 11 premiers caractères - - let day = closeDate.substring(0, 10); - if (!stats.tasksByDay[day]) { - stats.tasksByDay[day] = [] - } - stats.tasksByDay[day].push({ - todoKeyword, - title, - day, - closeDate, - tags, - }) - // console.log(' ' + title) - countIncluded++; - } else { - console.log('no', elem['properties']['raw-value']) - } - }) + // console.log('tâche ', index) + // évacuer les sous tâches + // if (elem['contents']) { + // console.log('content ception, on évacue') + // countExcluded++; + // return; + // } + // filtrer par tag les tâches + if (enableFilterByTag) { + console.log('filtre activé', filterByTag); + if (!elem['drawer'] || !elem['properties']['tags']) { + console.log('on vire cette tâche n\'ayant pas de drawer :', elem); + countExcluded++; + return; + } else if ( + elem['properties'] && + typeof elem['properties']['tags'] !== 'undefined' && + elem['properties']['tags'].indexOf(filterByTag) === -1 + ) { + console.log('on vire cette tâche ayant les itags :', elem['drawer']['ARCHIVE_ITAGS']); + countExcluded++; + return; + } } else { - console.log('no content') + console.log(' - filtre désactivé'); } - // console.log(data); - console.log('tâches inclues:', countIncluded) - writeHtmlOutput() - }); + // après le filtre, on range les tâches + if (elem['properties']) { + console.log(' - ref ', elem['ref']); + let title = elem['properties']['raw-value']; + let tags = elem['tags'] ? elem['tags'] : []; + let closeDate = 'Indéfini'; + let todoKeyword = 'DONE'; + if (elem['properties']['closed']) { + closeDate = elem['properties']['closed']['end']; - // les répartir dans des tableaux selon les périodes de temps + } else if (elem['drawer'] && elem['drawer']['ARCHIVE_TIME']) { + closeDate = elem['drawer']['ARCHIVE_TIME']; + } + if (elem['drawer'] && elem['drawer']['ARCHIVE_TODO']) { + + todoKeyword = elem['drawer']['ARCHIVE_TODO']; + } + if (!tags.length && elem['drawer'] && elem['drawer']['ARCHIVE_ITAGS']) { + + tags += elem['drawer']['ARCHIVE_ITAGS']; + } + + // jour, 11 premiers caractères + + let day = closeDate.substring(0, 10); + let month = closeDate.substring(0, 7); + console.log('month', month) + let year = closeDate.substring(0, 4); + let taskDescription = { + todoKeyword, + title, + day, + closeDate, + tags, + }; + + if (!stats.tasksByDay[day]) { + stats.tasksByDay[day] = []; + } + if (!stats.tasksByMonth[month]) { + stats.tasksByMonth[month] = { + period: month, + tasks : [], + }; + } + if (!stats.tasksByYear[year]) { + stats.tasksByYear[year] = { + period: year, + tasks : [], + }; + } + + stats.tasksByDay[day].push(taskDescription); + stats.tasksByMonth[month].tasks.push(taskDescription); + stats.tasksByYear[year].tasks.push(taskDescription); + // console.log(' ' + title) + countIncluded++; + } else { + console.log('no', elem['properties']['raw-value']); + } + }); + + } else { + console.log('no content'); + } + // console.log(data); + console.log('tâches inclues:', countIncluded); + writeHtmlOutput(); + }); + + // les répartir dans des tableaux selon les périodes de temps } +/** + * Retourne un html présentant le nombre de tâches réalisées pour chaque mois + * @return {string} + */ +function RenderStatsMonth() { + + let months = Object.keys(stats.tasksByMonth); + console.log('months.length', months.length); + + let monthHTML = ''; + + + months.forEach((elem) => { + + monthData = stats.tasksByMonth[elem] + console.log('monthData.tasks.length', monthData.tasks.length) + + monthHTML += ` +
Contenu filtré sur le tag: ${filterByTag}. ${countExcluded} tâches exclues, ${countIncluded} tâches inclues sur le total de ${countExcluded + countIncluded}
` - } + let daysListRef = Object.keys(stats.tasksByDay).sort(); + let filterInfo = ``; + if (enableFilterByTag) { + filterInfo = `Contenu filtré sur le tag: ${filterByTag}. ${countExcluded} tâches exclues, ${countIncluded} tâches inclues sur le total de ${countExcluded + countIncluded}
`; + } - let htmlOut = ` + let htmlOut = `par Tykayn
+par Tykayn
${filterInfo} + ${RenderStatsMonth()} - ` - daysListRef.reverse().forEach((dayRefString) => { + `; + daysListRef.reverse().forEach((dayRefString) => { - let tasksOfTheDay = ''; + let tasksOfTheDay = ''; - stats.tasksByDay[dayRefString].forEach((dayObj) => { - let graphicKeyword = '✅' - if ('DONE' !== dayObj.todoKeyword) { - graphicKeyword = dayObj.todoKeyword; - } + stats.tasksByDay[dayRefString].forEach((dayObj) => { + let graphicKeyword = '✅'; + if ('DONE' !== dayObj.todoKeyword) { + graphicKeyword = dayObj.todoKeyword; + } - let tagDisplay = ''; - // console.log('dayObj.tags', dayObj.tags) - if (dayObj.tags && dayObj.tags.length ) { - tagDisplay = ` - ` - } + `; + } - tasksOfTheDay += `
-Source: org-report-stats
+Source: org-report-stats
-Contactez-moi: par un de ces moyens
+Contactez-moi: par un de ces moyens