improve stats date

This commit is contained in:
Tykayn 2023-04-18 13:24:20 +02:00 committed by tykayn
parent 24f590d7e5
commit aff36ac2da
1 changed files with 68 additions and 61 deletions

View File

@ -97,14 +97,19 @@ let currentTask = Object.create(task);
* add to tasks to export and refresh current task * add to tasks to export and refresh current task
*/ */
function addAndRefreshCurrentTask() { function addAndRefreshCurrentTask() {
makeWordsStatistics(currentTask.header.trim())
makeWordsStatistics(currentTask.corpus.trim())
tasksObjectsForJsonExport.push(currentTask) tasksObjectsForJsonExport.push(currentTask)
// réinitialisation de tâche pour remplir de nouveau // réinitialisation de tâche pour remplir de nouveau
currentTask = Object.create(task); currentTask = Object.create(task);
currentTask.dates = {}; currentTask.dates = {};
currentTask.tags = []; currentTask.tags = [];
}; }
function makeWordsStatistics(sentence) { function makeWordsStatistics(sentence) {
console.log('sentence', sentence)
let split = sentence.split(' '); let split = sentence.split(' ');
if (split && split.length) { if (split && split.length) {
@ -117,6 +122,36 @@ function makeWordsStatistics(sentence) {
} }
} }
const dateStats = {
created: 0,
refiled: 0,
closed: 0,
cancelled: 0,
scheduled: 0,
deadline: 0,
}
function fillPeriodTime(periodStat, keyword){
if (keyword === 'CLOSED') {
periodStat.closed++;
}
if (keyword === 'CREATED') {
periodStat.created++;
}
if (keyword === 'Refiled') {
periodStat.refiled++;
}
if (keyword === 'CANCELLED') {
periodStat.cancelled++;
}
if (keyword === 'SCHEDULED') {
periodStat.scheduled++;
}
if (keyword === 'DEADLINE') {
periodStat.deadline++;
}
}
/** /**
* pour chaque période de temps, compter les tâches créées et fermées * pour chaque période de temps, compter les tâches créées et fermées
@ -139,59 +174,27 @@ function statisticDateFill(keyword, dateFoundElement) {
let dayOfDate = convertedDate.getFullYear() + '-' + convertedMonth + '-' + convertedDay let dayOfDate = convertedDate.getFullYear() + '-' + convertedMonth + '-' + convertedDay
if (!statistics.dates.years[yearOfDate]) { if (!statistics.dates.years[yearOfDate]) {
statistics.dates.years[yearOfDate] = { statistics.dates.years[yearOfDate] = Object.create(dateStats)
created: 0,
closed: 0,
}
}
if (keyword === 'CLOSED') {
statistics.dates.years[yearOfDate].closed++;
}
if (keyword === 'CREATED') {
statistics.dates.years[yearOfDate].created++;
} }
fillPeriodTime(statistics.dates.years[yearOfDate], keyword)
// par année-semaine // par année-semaine
if (!statistics.dates.weeks[weekOfDate]) { if (!statistics.dates.weeks[weekOfDate]) {
statistics.dates.weeks[weekOfDate] = { statistics.dates.weeks[weekOfDate] = Object.create(dateStats)
created: 0,
closed: 0,
}
} }
if (keyword === 'CLOSED') { fillPeriodTime(statistics.dates.weeks[weekOfDate], keyword)
statistics.dates.weeks[weekOfDate].closed++;
}
if (keyword === 'CREATED') {
statistics.dates.weeks[weekOfDate].created++;
}
// décompte par mois // décompte par mois
if (!statistics.dates.months[monthOfDate]) { if (!statistics.dates.months[monthOfDate]) {
statistics.dates.months[monthOfDate] = { statistics.dates.months[monthOfDate] = Object.create(dateStats)
created: 0,
closed: 0,
}
}
if (keyword === 'CLOSED') {
statistics.dates.months[monthOfDate].closed++;
}
if (keyword === 'CREATED') {
statistics.dates.months[monthOfDate].created++;
} }
fillPeriodTime(statistics.dates.months[monthOfDate], keyword)
// décompte par jours // décompte par jours
if (!statistics.dates.days[dayOfDate]) { if (!statistics.dates.days[dayOfDate]) {
statistics.dates.days[dayOfDate] = { statistics.dates.days[dayOfDate] = Object.create(dateStats)
created: 0,
closed: 0,
}
}
if (keyword === 'CLOSED') {
statistics.dates.days[dayOfDate].closed++;
}
if (keyword === 'CREATED') {
statistics.dates.days[dayOfDate].created++;
} }
fillPeriodTime(statistics.dates.days[dayOfDate], keyword)
} }
@ -251,7 +254,6 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
headers.push(cleanHeader(line)) headers.push(cleanHeader(line))
currentTask.header = cleanHeader(line); currentTask.header = cleanHeader(line);
// makeWordsStatistics(cleanHeader(line));
stateKeywordList.forEach(keyword => { stateKeywordList.forEach(keyword => {
let keywordIsFound = lineHasKeyword(line, keyword) let keywordIsFound = lineHasKeyword(line, keyword)
@ -332,20 +334,22 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
line.indexOf(sectionKeywordList) !== -1 line.indexOf(sectionKeywordList) !== -1
) { ) {
// ajouter le corps complet de la section après le header // ajouter le corps complet de la section après le header
if (line.length && !isHeader) { if (line.length && !isHeader) {
let cleanedLine = line.replace(/\s\s/g, ' '); let cleanedLine = line.replace(/\s\s/g, ' ');
cleanedLine = line.replace(/ {2,}/g, ' ') cleanedLine = line.replace(/ {2,}/g, ' ')
cleanedLine = cleanedLine.trim()
currentTask.corpus += `${cleanedLine} currentTask.corpus += `${cleanedLine}
` `
makeWordsStatistics(cleanedLine)
} }
} }
} }
}) })
// ajouter la dernière tâche parsée // ajouter la dernière tâche parsée
addAndRefreshCurrentTask(); addAndRefreshCurrentTask();
@ -361,17 +365,14 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
statistics.dates.months = sortByKey(statistics.dates.months) statistics.dates.months = sortByKey(statistics.dates.months)
statistics.dates.days = sortByKey(statistics.dates.days) statistics.dates.days = sortByKey(statistics.dates.days)
statistics.tags = sortByValue(statistics.tags) statistics = sortByKey(statistics)
statistics.words = sortByValue(statistics.tags)
sorted_stats = sortByKey(statistics)
const jsonContent = { const jsonContent = {
statistics: { statistics: {
lines_count: everyline.length, lines_count: everyline.length,
headers_count: headers.length, headers_count: headers.length,
statistics: sorted_stats statistics
}, },
meta_data: { meta_data: {
author: '@tykayn@mastodon.Cipherbliss.com', author: '@tykayn@mastodon.Cipherbliss.com',
@ -390,16 +391,6 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
}) })
/**
* ranger un objet littéral selon la valeur décroissante de ses paires
* @param literalobject
* @returns {any}
*/
function sortByValue(literalobject) {
return Object.fromEntries(
Object.entries(literalobject).sort(([, a], [, b]) => b - a)
);
}
function lineHasKeyword(line, keyword = 'TODO') { function lineHasKeyword(line, keyword = 'TODO') {
@ -489,10 +480,26 @@ function sortByKey(objectStuff) {
); );
} }
/**
* ranger un objet littéral selon la valeur décroissante de ses paires
* @param literalobject
* @returns {any}
*/
function sortByValue(literalobject) {
let sortable = [];
for (var keyName in literalobject) {
sortable[keyName] = literalobject[keyName];
}
// return literalobject
return sortable.sort(function (a, b) {
return b[1] - a[1];
});
}
export async function writeFileInOuputFolderFromJsonObject(fileName, jsonObjectThing) { export async function writeFileInOuputFolderFromJsonObject(fileName, jsonObjectThing) {
console.log('statistics.words', statistics.words) console.log('statistics.dates', statistics.dates)
return await fs.writeFile( return await fs.writeFile(
`${outputAbsolutePath}${fileName}`, `${outputAbsolutePath}${fileName}`,