org-report-stats/app.js

249 lines
7.6 KiB
JavaScript
Raw Normal View History

2022-07-20 16:03:28 +02:00
const fs = require('node-fs')
const sourceFilePath = 'source/tasks.json';
2022-07-20 16:56:17 +02:00
const stats = {
countAllTasks: 0,
tasksByDay: {}
2022-07-20 16:03:28 +02:00
}
2022-07-20 18:45:29 +02:00
// const enableFilterByTag = true;
const enableFilterByTag = false;
const filterByTag = "work";
// const outputFileName = "work_report";
const outputFileName = "all_tasks_report";
let countExcluded = 0;
let countIncluded = 0;
2022-07-20 16:03:28 +02:00
// prendre le json source représentant les tâches DONE
console.log(' ### lecture de source/emacs_json.json');
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.`)
} else {
console.log('Some other error: ', err.code);
}
});
// parcourir les tâches
2022-07-20 16:56:17 +02:00
function sortTasksFromJson(statObject) {
2022-07-20 16:03:28 +02:00
console.log('sortTasksFromJson')
2022-07-20 16:56:17 +02:00
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
2022-07-20 16:03:28 +02:00
if (err) {
return console.log(err);
}
let dataTransformed = JSON.parse(data);
2022-07-20 18:45:29 +02:00
// console.log('data keys ', Object.keys(dataTransformed))
2022-07-20 16:56:17 +02:00
if (dataTransformed["contents"]) {
2022-07-20 16:03:28 +02:00
2022-07-20 16:56:17 +02:00
countTasks = dataTransformed["contents"].length
stats.countAllTasks = countTasks;
2022-07-20 18:45:29 +02:00
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) {
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é')
2022-07-20 16:56:17 +02:00
}
2022-07-20 18:45:29 +02:00
// après le filtre, on range les tâches
if (elem['properties']) {
console.log(' - ref ', elem['ref'])
2022-07-20 16:56:17 +02:00
let title = elem['properties']['raw-value'];
let tags = elem['tags'] ? elem['tags'] : [];
2022-07-20 18:45:29 +02:00
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'];
}
2022-07-20 16:56:17 +02:00
// jour, 11 premiers caractères
let day = closeDate.substring(0, 10);
if (!stats.tasksByDay[day]) {
stats.tasksByDay[day] = []
}
stats.tasksByDay[day].push({
2022-07-20 18:45:29 +02:00
todoKeyword,
2022-07-20 16:56:17 +02:00
title,
2022-07-20 18:45:29 +02:00
day,
2022-07-20 16:56:17 +02:00
closeDate,
tags,
})
// console.log(' ' + title)
2022-07-20 18:45:29 +02:00
countIncluded++;
2022-07-20 16:56:17 +02:00
} else {
2022-07-20 17:01:53 +02:00
console.log('no', elem['properties']['raw-value'])
2022-07-20 16:56:17 +02:00
}
})
} else {
console.log('no content')
2022-07-20 16:03:28 +02:00
}
// console.log(data);
2022-07-20 18:45:29 +02:00
console.log('tâches inclues:', countIncluded)
2022-07-20 16:56:17 +02:00
writeHtmlOutput()
2022-07-20 16:03:28 +02:00
});
// les répartir dans des tableaux selon les périodes de temps
}
// 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() {
console.log('writeHtmlOutput', stats.countAllTasks)
let daysListRef = Object.keys(stats.tasksByDay).sort()
2022-07-20 18:45:29 +02:00
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 16:56:17 +02:00
let htmlOut = `
<html>
<head>
<title>
Rapport d'activité
</title>
<meta charset="UTF-8" />
<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-20 16:56:17 +02:00
<div id="report_days" class="container content">
2022-07-20 18:45:29 +02:00
<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>
${filterInfo}
2022-07-20 16:56:17 +02:00
`
2022-07-20 18:45:29 +02:00
daysListRef.reverse().forEach((dayRefString) => {
2022-07-20 16:56:17 +02:00
let tasksOfTheDay = '';
stats.tasksByDay[dayRefString].forEach((dayObj) => {
2022-07-20 17:01:53 +02:00
let graphicKeyword = '✅'
if ('DONE' !== dayObj.todoKeyword) {
graphicKeyword = dayObj.todoKeyword;
}
2022-07-20 18:45:29 +02:00
let tagDisplay = '';
if (dayObj.tags.length) {
tagDisplay = ` <p class="archived-tags tag is-light is-primary">
${dayObj.tags.join(' ')}
</p>
`
}
2022-07-20 16:56:17 +02:00
tasksOfTheDay += ` <li>
2022-07-20 18:45:29 +02:00
<h2>
2022-07-20 17:01:53 +02:00
<span class="keyword"> ${graphicKeyword}</span>
2022-07-20 16:56:17 +02:00
<span class="title"> ${dayObj.title} </span>
</h2>
2022-07-20 18:45:29 +02:00
${tagDisplay}
2022-07-20 16:56:17 +02:00
</li>`
})
htmlOut += `
2022-07-20 17:01:53 +02:00
<div class="box">
2022-07-20 16:56:17 +02:00
<h1 class="title is-1">
2022-07-20 17:01:53 +02:00
<span class="day-ref">
${dayRefString}
</span>
2022-07-20 18:45:29 +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-20 16:56:17 +02:00
<div class="day-detail">
2022-07-20 17:01:53 +02:00
<ul>
${tasksOfTheDay}
</ul>
</div>
</div> `
2022-07-20 16:56:17 +02:00
})
htmlOut += `
2022-07-20 18:45:29 +02:00
<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>
Source: <a href="https://forge.chapril.org/tykayn/org-report-stats">org-report-stats</a>
<br>
Contactez-moi: <a href="https://www.cipherbliss.com/contact">par un de ces moyens</a>
</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>
`
fs.writeFile('output/output.json', JSON.stringify(stats), function (err, data) {
if (err) {
return console.log(err);
}
console.log('wrote output json', data)
})
2022-07-20 18:45:29 +02:00
fs.writeFile('output/output_' + outputFileName + '.html', htmlOut, function (err, data) {
2022-07-20 16:56:17 +02:00
if (err) {
return console.log(err);
}
2022-07-20 18:45:29 +02:00
console.log('wrote output html ' + outputFileName, data)
2022-07-20 16:56:17 +02:00
})
2022-07-20 18:45:29 +02:00
if (enableFilterByTag) {
console.log('excluded tasks by tag filter ', countExcluded)
}
2022-07-20 16:56:17 +02:00
}
function sortObj(obj) {
return Object.keys(obj).sort().reduce(function (result, key) {
result[key] = obj[key];
return result;
}, {});
}