org-report-stats/app.js

327 lines
8.9 KiB
JavaScript
Raw Normal View History

2022-07-21 16:06:44 +02:00
const fs = require('node-fs');
2022-07-21 15:20:57 +02:00
const sourceFilePath = './sources/tasks.json';
2022-07-20 16:56:17 +02:00
const stats = {
2022-07-21 16:06:44 +02:00
countAllTasks: 0,
tasksByDay : [],
tasksByMonth : [],
tasksByYear : [],
};
const myArgs = process.argv;
console.log('command arguments: ', myArgs);
2022-07-20 18:45:29 +02:00
2022-10-02 21:59:31 +02:00
// const enableFilterByTag = true;
const enableFilterByTag = false;
let outputFileName = 'all_tasks_report';
2022-10-02 21:59:31 +02:00
const filterByTag = 'lemonway';
if(enableFilterByTag){
outputFileName = `${filterByTag}_filtered_tasks_report`;
}
2022-07-20 18:45:29 +02:00
let countExcluded = 0;
let countIncluded = 0;
2022-07-20 16:03:28 +02:00
// prendre le json source représentant les tâches DONE
2022-07-21 15:20:57 +02:00
console.log(` ### lecture de ${sourceFilePath}`);
2022-07-20 16:03:28 +02:00
fs.stat(sourceFilePath, function (err, stat) {
2022-07-21 16:06:44 +02:00
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);
}
2022-07-20 16:03:28 +02:00
});
// parcourir les tâches
2022-07-20 16:56:17 +02:00
function sortTasksFromJson(statObject) {
2022-07-21 16:06:44 +02:00
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;
console.log('yes data ! tasks:', countTasks);
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) {
2022-08-03 12:57:32 +02:00
let tags_list = ' ';
if(elem['properties']['tags']){
tags_list += elem['properties']['tags']
}
if(elem['drawer'] && elem['drawer']['ARCHIVE_ITAGS']){
tags_list += elem['drawer']['ARCHIVE_ITAGS']
}
2022-07-21 16:06:44 +02:00
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' &&
2022-08-03 12:57:32 +02:00
tags_list.indexOf(filterByTag) === -1
2022-07-21 16:06:44 +02:00
) {
console.log('on vire cette tâche ayant les itags :', elem['drawer']['ARCHIVE_ITAGS']);
countExcluded++;
return;
2022-08-03 12:57:32 +02:00
}else{
console.log("on ajoute cette tâche aux tags : ", tags_list)
2022-07-21 16:06:44 +02:00
}
} else {
console.log(' - filtre désactivé');
2022-07-20 16:03:28 +02:00
}
2022-07-21 16:06:44 +02:00
// 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);
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++;
2022-07-20 16:56:17 +02:00
} else {
2022-07-21 16:06:44 +02:00
console.log('no', elem['properties']['raw-value']);
2022-07-20 16:03:28 +02:00
}
2022-07-21 16:06:44 +02:00
});
2022-07-20 16:03:28 +02:00
2022-07-21 16:06:44 +02:00
} else {
console.log('no content');
}
// console.log(data);
console.log('tâches inclues:', countIncluded);
writeHtmlOutput();
});
2022-07-20 16:03:28 +02:00
2022-07-21 16:06:44 +02:00
// les répartir dans des tableaux selon les périodes de temps
2022-07-20 16:03:28 +02:00
}
2022-07-21 16:06:44 +02:00
/**
* 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 += `
<div class='columns'>
<div class='column'>
<h2 class='title is-2'>
${monthData.period}
</h2>
</div>
<div class='column'>
${monthData.tasks.length}
</div>
</div>
`;
});
return `
<div class='box'>
<h1 class='title is-1'>Stats mensuelles</h1>
${monthHTML}
</div>
`;
}
2022-07-20 16:03:28 +02:00
// sortir un html présentant les périodes de temps et les tâches réalisées
2022-07-20 16:56:17 +02:00
function writeHtmlOutput() {
2022-07-21 16:06:44 +02:00
console.log('writeHtmlOutput', stats.countAllTasks);
2022-07-20 16:56:17 +02:00
2022-07-21 16:06:44 +02:00
let daysListRef = Object.keys(stats.tasksByDay).sort();
let filterInfo = ``;
if (enableFilterByTag) {
filterInfo = `<p class='is-info'>Contenu filtré sur le tag: ${filterByTag}. ${countExcluded} tâches exclues, ${countIncluded} tâches inclues sur le total de ${countExcluded + countIncluded}</p>`;
}
2022-07-20 18:45:29 +02:00
2022-07-21 16:06:44 +02:00
let htmlOut = `
2022-07-20 16:56:17 +02:00
<html>
<head>
<title>
Rapport d'activité
</title>
2022-07-21 16:06:44 +02:00
<meta charset='UTF-8' />
2022-07-20 16:56:17 +02:00
<style>
@import "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css";
</style>
</head>
<body>
2022-07-20 18:45:29 +02:00
2022-07-21 16:06:44 +02:00
<div id='report_days' class='container content'>
<h1 class='title is-1'>Feuille de rapport d'activité</h1>
<p class='subtitle is-1'>par <a href='https://www.cipherbliss.com'>Tykayn</a> </p>
2022-07-20 18:45:29 +02:00
${filterInfo}
2022-07-21 16:06:44 +02:00
${RenderStatsMonth()}
2022-07-20 18:45:29 +02:00
2022-07-21 16:06:44 +02:00
`;
daysListRef.reverse().forEach((dayRefString) => {
2022-07-20 16:56:17 +02:00
2022-07-21 16:06:44 +02:00
let tasksOfTheDay = '';
2022-07-20 16:56:17 +02:00
2022-07-21 16:06:44 +02:00
stats.tasksByDay[dayRefString].forEach((dayObj) => {
let graphicKeyword = '✅';
if ('DONE' !== dayObj.todoKeyword) {
graphicKeyword = dayObj.todoKeyword;
}
2022-07-20 18:45:29 +02:00
2022-07-21 16:06:44 +02:00
let tagDisplay = '';
// console.log('dayObj.tags', dayObj.tags)
if (dayObj.tags && dayObj.tags.length) {
tagDisplay = ` <p class='archived-tags tag is-light is-primary'>
2022-07-21 15:20:57 +02:00
${dayObj.tags}
2022-07-20 18:45:29 +02:00
</p>
2022-07-21 16:06:44 +02:00
`;
}
2022-07-20 18:45:29 +02:00
2022-07-21 16:06:44 +02:00
tasksOfTheDay += ` <li>
2022-07-20 18:45:29 +02:00
<h2>
2022-07-21 16:06:44 +02:00
<span class='keyword'> ${graphicKeyword}</span>
<span class='title'> ${dayObj.title} </span>
2022-07-20 16:56:17 +02:00
</h2>
2022-07-20 18:45:29 +02:00
${tagDisplay}
2022-07-21 16:06:44 +02:00
</li>`;
});
htmlOut += `
<div class='box'>
2022-07-20 17:01:53 +02:00
2022-07-21 16:06:44 +02:00
<h1 class='title is-1'>
<span class='day-ref'>
2022-07-20 17:01:53 +02:00
${dayRefString}
</span>
2022-07-21 16:06:44 +02:00
<span class='counter pull-right tag is-success is-light'>
2022-07-20 17:01:53 +02:00
${stats.tasksByDay[dayRefString].length}
</span>
2022-07-20 16:56:17 +02:00
</h1>
2022-07-20 17:01:53 +02:00
2022-07-21 16:06:44 +02:00
<div class='day-detail'>
2022-07-20 17:01:53 +02:00
<ul>
${tasksOfTheDay}
</ul>
</div>
2022-07-21 16:06:44 +02:00
</div> `;
});
htmlOut += `
<div class='box'>
<h1 class='title is-1'>
2022-07-20 17:01:53 +02:00
2022-07-20 18:45:29 +02:00
Tadam, c'est tout!
</h1>
<p>
2022-07-21 16:06:44 +02:00
Source: <a href='https://forge.chapril.org/tykayn/org-report-stats'>org-report-stats</a>
2022-07-20 18:45:29 +02:00
<br>
2022-07-21 16:06:44 +02:00
Contactez-moi: <a href='https://www.cipherbliss.com/contact'>par un de ces moyens</a>
2022-07-20 18:45:29 +02:00
</p>
</div>
2022-07-20 16:56:17 +02:00
</div>
2022-07-20 18:45:29 +02:00
2022-07-20 16:56:17 +02:00
</body>
</html>
2022-07-21 16:06:44 +02:00
`;
2022-07-20 16:56:17 +02:00
2022-07-21 16:06:44 +02:00
fs.writeFile('output/output.json', JSON.stringify(stats), function (err, data) {
if (err) {
return console.log(err);
2022-07-20 18:45:29 +02:00
}
2022-07-21 16:06:44 +02:00
console.log('wrote output json', data);
});
fs.writeFile('output/output_' + outputFileName + '.html', htmlOut, function (err, data) {
if (err) {
return console.log(err);
}
console.log('wrote output html ' + outputFileName, data);
2022-07-20 16:56:17 +02:00
2022-07-21 16:06:44 +02:00
});
if (enableFilterByTag) {
console.log('excluded tasks by tag filter ', countExcluded);
}
}
2022-07-20 16:56:17 +02:00
function sortObj(obj) {
2022-07-21 16:06:44 +02:00
return Object.keys(obj).sort().reduce(function (result, key) {
result[key] = obj[key];
return result;
}, {});
2022-07-20 16:56:17 +02:00
}