2021-02-27 10:26:37 +01:00
var express = require ( 'express' ) ;
2021-03-01 18:02:04 +01:00
var fs = require ( 'fs' ) ;
2021-03-02 11:57:31 +01:00
var _ = require ( 'lodash' ) ;
2021-03-01 18:02:04 +01:00
var parseString = require ( "xml2js" ) . parseString ;
2021-02-27 10:26:37 +01:00
var router = express . Router ( ) ;
2021-03-03 16:55:41 +01:00
let searchTagsForHebdoReport = /\b(dev|sysadmin|écriture|edu|fediverse|april|chapril)\b/g ;
2021-03-02 12:26:19 +01:00
let convertFiles = true ;
// let convertFiles = false;
2021-03-02 10:12:15 +01:00
let computeDataOnExtract = true ;
2021-03-03 16:34:29 +01:00
// let computeDataOnExtract = false;
2021-03-01 18:14:47 +01:00
2021-03-02 09:23:33 +01:00
let jsonAllData = {
2021-03-02 10:57:26 +01:00
tasks : { } ,
stats : { } ,
projects : { } ,
tags : { } ,
2021-03-01 18:02:04 +01:00
}
2021-02-27 10:26:37 +01:00
/* GET home page. */
2021-03-01 18:02:04 +01:00
/ * *
* obtenir la liste de tags que l ' on a entré
* /
2021-03-03 16:34:29 +01:00
router . get ( '/xml' , async function ( req , res , next ) {
fs . stat ( 'sources/gtg_tasks.xml' , function ( err , stat ) {
if ( err == null ) {
console . log ( 'File sources/gtg_tasks.xml exists' ) ;
} else if ( err . code === 'ENOENT' ) {
// file does not exist
res . send ( 'le fichier sources/gtg_tasks.xml est introuvable. Impossible d en extraire des infos. Importez d abord les fichiers de données xml de Getting Things Gnome dans le dossier sources' )
} else {
console . log ( 'Some other error: ' , err . code ) ;
}
} ) ;
2021-03-01 18:14:47 +01:00
2021-03-03 16:34:29 +01:00
convertOneXmlToJson ( 'gtg_tasks' , res )
convertOneXmlToJson ( 'tags' , res )
convertOneXmlToJson ( 'projects' , res )
console . log ( 'success conversion xml' )
res . redirect ( '/' )
} )
router . get ( '/' , async function ( req , res , next ) {
2021-03-01 18:02:04 +01:00
// récupérer le contenu des json
2021-03-02 09:23:33 +01:00
jsonAllData . tags = require ( '../sources/tags_gtg.json' )
jsonAllData . tasks = require ( '../sources/gtg_tasks_gtg.json' )
2021-03-03 16:34:29 +01:00
if ( ! jsonAllData . tasks ) {
res . redirect ( '/xml' )
}
2021-03-02 09:23:33 +01:00
2021-03-02 10:57:26 +01:00
if ( computeDataOnExtract ) {
2021-03-02 09:23:33 +01:00
2021-03-02 10:57:26 +01:00
jsonAllData = computeData ( jsonAllData )
computeDataOnExtract = false ;
} else {
2021-03-02 09:23:33 +01:00
console . log ( '================== calcul de stats désactivé dans index.js ================== ' )
}
2021-03-02 11:10:53 +01:00
exportToMarkdown ( res )
2021-03-02 10:57:26 +01:00
console . log ( 'json files read' )
res . render ( 'index' , {
title : "Conversion de GTG tâches" ,
json : jsonAllData ,
2021-03-03 16:19:11 +01:00
markdown : markdownExportData ,
2021-03-03 16:34:29 +01:00
weeklyDevReport ,
2021-03-02 10:57:26 +01:00
getPercent ,
computeBgColorOnProportionOfOpenTasks ,
getPercentOfOpenTasks
} )
2021-03-02 09:23:33 +01:00
} ) ;
2021-03-02 10:57:26 +01:00
async function readfileJson ( fileToOpen ) {
2021-03-02 09:23:33 +01:00
fs . readFile ( ` sources/ ${ fileToOpen } ` , "utf-8" , function ( err , data ) {
2021-03-01 18:02:04 +01:00
if ( err ) console . log ( err ) ;
2021-03-02 10:57:26 +01:00
console . log ( 'ok ' , fileToOpen )
2021-03-02 09:23:33 +01:00
return JSON . parse ( data )
2021-03-01 18:02:04 +01:00
} ) ;
2021-03-02 09:23:33 +01:00
}
2021-03-01 18:02:04 +01:00
2021-03-02 10:57:26 +01:00
function computeData ( jsonAllData ) {
2021-03-01 18:14:47 +01:00
2021-03-02 09:23:33 +01:00
// trouver le nombre de tâches ayant un tag en particulier
// et rajouter ce nombre à chaque tag, dans countTasks : N
// let tagCountTasks = Object.create(jsonAllData.tags);
2021-03-01 18:02:04 +01:00
2021-03-02 09:53:16 +01:00
jsonAllData . stats = {
2021-03-02 11:57:31 +01:00
tasksCount : jsonAllData . tasks . project . task . length ,
tasksClosed : 0 ,
tasksOpen : 0 ,
maxTasksCountPerTag : 0 ,
listOpen : [ ] ,
listOpenWeekly : [ ] ,
2021-03-02 12:09:05 +01:00
listOpenMonthly : [ ] ,
2021-03-02 11:57:31 +01:00
listClosed : [ ] ,
listClosedWeekly : [ ] ,
2021-03-02 12:26:19 +01:00
listDevWeeklyClosed : [ ] ,
2021-03-03 16:55:41 +01:00
listDevWeeklyOpen : [ ] ,
2021-03-02 12:09:05 +01:00
listClosedMonthly : [ ] ,
2021-03-02 09:53:16 +01:00
}
2021-03-02 12:09:05 +01:00
let now = new Date ( ) ;
2021-03-02 09:53:16 +01:00
2021-03-02 10:57:26 +01:00
for ( let t of jsonAllData . tasks . project . task ) {
2021-03-02 09:53:16 +01:00
// console.log('t', t)
let tags = t [ "$" ] . tags . split ( ',' )
2021-03-02 11:57:31 +01:00
// count open and closed tasks
2021-03-02 12:09:05 +01:00
let dateOfTask = new Date ( )
2021-03-02 11:57:31 +01:00
2021-03-02 12:09:05 +01:00
if ( t [ '$' ] . status == 'Active' ) {
jsonAllData . stats . listOpen . push ( t )
let addeddate = new Date ( t . addeddate )
let daysDiff = getNumberOfDaysDiffTwoDates ( addeddate , now ) ;
if ( daysDiff <= 7 ) {
jsonAllData . stats . listOpenWeekly . push ( t )
2021-03-03 16:55:41 +01:00
// listDevWeeklyOpen
// tâches uniquement de dev
if ( ( searchTagsForHebdoReport ) . test ( t [ '$' ] . tags )
) {
jsonAllData . stats . listDevWeeklyOpen . push ( t )
}
2021-03-02 12:09:05 +01:00
} else if ( daysDiff >= 7 & daysDiff <= 31 ) {
jsonAllData . stats . listOpenMonthly . push ( t )
}
} else if ( t [ '$' ] . status == 'Done' ) {
jsonAllData . stats . listClosed . push ( t )
let donedate = new Date ( t . donedate )
let daysDiff = getNumberOfDaysDiffTwoDates ( donedate , now ) ;
if ( daysDiff <= 7 ) {
jsonAllData . stats . listClosedWeekly . push ( t )
2021-03-02 12:26:19 +01:00
// tâches uniquement de dev
2021-03-03 16:55:41 +01:00
if ( ( searchTagsForHebdoReport ) . test ( t [ '$' ] . tags )
2021-03-02 12:26:19 +01:00
) {
jsonAllData . stats . listDevWeeklyClosed . push ( t )
}
2021-03-02 12:09:05 +01:00
} else if ( daysDiff >= 7 & daysDiff <= 31 ) {
jsonAllData . stats . listClosedMonthly . push ( t )
}
2021-03-02 11:57:31 +01:00
}
2021-03-02 10:57:26 +01:00
for ( let tag of tags ) {
2021-03-02 09:53:16 +01:00
// console.log('tag', tag)
2021-03-02 10:57:26 +01:00
jsonAllData . tags . tagstore . tag . map ( elem => {
2021-03-02 09:53:16 +01:00
// console.log('elem', elem)
2021-03-02 10:57:26 +01:00
if ( elem [ '$' ] . name == tag ) {
if ( ! elem . tasks ) {
2021-03-02 09:53:16 +01:00
elem . tasks = 0
}
2021-03-02 10:57:26 +01:00
if ( ! elem . tasksListById ) {
2021-03-02 09:53:16 +01:00
elem . tasksListById = [ ]
}
2021-03-02 10:57:26 +01:00
if ( ! elem . tasksListByTitle ) {
2021-03-02 09:53:16 +01:00
elem . tasksListByTitle = [ ]
}
2021-03-02 10:57:26 +01:00
if ( ! elem . tasksListIsActiveByTitle ) {
2021-03-02 09:53:16 +01:00
elem . tasksListIsActiveByTitle = [ ]
}
2021-03-02 10:57:26 +01:00
if ( ! elem . tasksListIsDoneByTitle ) {
2021-03-02 09:53:16 +01:00
elem . tasksListIsDoneByTitle = [ ]
}
elem . tasks ++
elem . tasksListById . push ( t [ '$' ] . id )
elem . tasksListByTitle . push ( t . title )
2021-03-02 10:57:26 +01:00
if ( t [ '$' ] . status == "Active" ) {
elem . tasksListIsActiveByTitle . push ( t . title )
2021-03-02 11:57:31 +01:00
2021-03-02 09:53:16 +01:00
}
2021-03-02 10:57:26 +01:00
if ( t [ '$' ] . status == "Done" ) {
elem . tasksListIsDoneByTitle . push ( t . title )
2021-03-02 09:53:16 +01:00
}
}
2021-03-02 11:57:31 +01:00
if ( elem . tasks > jsonAllData . stats . maxTasksCountPerTag ) {
2021-03-02 11:10:53 +01:00
jsonAllData . stats . maxTasksCountPerTag = elem . tasks
}
2021-03-02 09:53:16 +01:00
} )
2021-03-02 11:10:53 +01:00
2021-03-02 09:53:16 +01:00
}
2021-03-02 09:23:33 +01:00
}
2021-03-02 11:57:31 +01:00
// sort tags by name
2021-03-02 09:23:33 +01:00
return jsonAllData
2021-03-01 18:02:04 +01:00
2021-03-02 09:23:33 +01:00
}
2021-03-02 10:57:26 +01:00
2021-03-02 12:09:05 +01:00
const oneDay = 24 * 60 * 60 * 1000 ; // hours*minutes*seconds*milliseconds
function getNumberOfDaysDiffTwoDates ( firstDate , secondDate ) {
return Math . round ( Math . abs ( ( firstDate - secondDate ) / oneDay ) ) ;
}
2021-03-02 10:57:26 +01:00
function convertOneXmlToJson ( fileToOpen , res ) {
2021-03-01 18:02:04 +01:00
fs . readFile ( ` sources/ ${ fileToOpen } .xml ` , "utf-8" , function ( err , data ) {
if ( err ) console . log ( err ) ;
const results = transformToJson ( data , fileToOpen , res )
console . log ( 'fichier converti en json' , fileToOpen )
} ) ;
}
2021-03-02 10:57:26 +01:00
function getPercentOfOpenTasks ( someCount ) {
return Math . round ( 100 * someCount / jsonAllData . stats . tasksOpen )
}
function getPercent ( someCount , total ) {
return Math . round ( 100 * someCount / total )
}
function computeBgColorOnProportionOfOpenTasks ( someProportionNumber ) {
let result = '' ;
2021-03-02 11:57:31 +01:00
if ( someProportionNumber >= 95 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-95'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 90 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-90'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 80 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-80'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 65 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-65'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 50 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-50'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 40 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-40'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 25 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-25'
2021-03-02 11:57:31 +01:00
} else if ( someProportionNumber >= 15 ) {
2021-03-02 10:57:26 +01:00
result = 'proportion-over-15'
}
return result ;
}
2021-03-02 12:26:19 +01:00
let datenow = new Date ( ) ;
2021-03-03 16:19:11 +01:00
let markdownExportData = ''
2021-03-03 16:34:29 +01:00
let weeklyDevReport = ''
2021-03-02 11:10:53 +01:00
function exportToMarkdown ( res ) {
2021-03-02 12:26:19 +01:00
2021-03-03 16:19:11 +01:00
markdownExportData = ` # Export de Tâches GTG \n date: ` + datenow . toLocaleString ( )
2021-03-02 11:57:31 +01:00
// fichier de rapport des tâches fermées durant les 7 derniers jours
2021-03-03 16:55:41 +01:00
weeklyDevReport = ` \n # Rapport hebdomadaire de dev ` ;
weeklyDevReport += ` \n ## 1/ Actions passées ou en cours
\ n `
weeklyDevReport += ` \r \n ## ${ jsonAllData . stats . listDevWeeklyClosed . length } ont été fermées
\ n _ _ _ ` ;
2021-03-02 12:26:19 +01:00
jsonAllData . stats . listDevWeeklyClosed . map ( elem => {
2021-03-03 16:55:41 +01:00
weeklyDevReport += ` \n | ${ elem [ '$' ] . tags } | ${ elem . title } | ` ;
2021-03-02 12:26:19 +01:00
} )
2021-03-03 16:55:41 +01:00
weeklyDevReport += `
\ n _ _ _
\ n
\ n # # 2 / Actions à venir
\ n `
weeklyDevReport += ` \r \n ## ${ jsonAllData . stats . listDevWeeklyOpen . length } ont été ouvertes
\ n _ _ _ ` ;
jsonAllData . stats . listDevWeeklyOpen . map ( elem => {
// weeklyDevReport += `\n| ${elem['$'].tags} | ${elem.title}|`;
weeklyDevReport += ` \n | ${ elem . title } | ` ;
} )
weeklyDevReport += `
\ n _ _ _
\ n
\ n # # 3 / Points de blocage / points en retard corrigés cette semaine
\ n
\ n # # 4 / Points de blocage existants / points en retard à traiter
\ n
\ n # # 5 / Points forts de la semaine
\ n
\ n # # 6 / Points de vigilance de la semaine
\ n
\ n # # 7 / Points forts de la réunion
\ n
\ n # # 8 / Points de vigilance de la réunion ` ;
2021-03-03 16:34:29 +01:00
markdownExportData += ` \n ${ weeklyDevReport } ` ;
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n --- ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n # Rapport des tâches durant les 7 derniers jours ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n ## ${ jsonAllData . stats . listClosedWeekly . length } ont été fermées ` ;
markdownExportData += ` \n ` ;
2021-03-02 12:26:19 +01:00
jsonAllData . stats . listClosedWeekly . map ( elem => {
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n * ${ elem . title } ` ;
2021-03-02 12:26:19 +01:00
} )
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n ## ${ jsonAllData . stats . listOpenWeekly . length } ont été ouvertes ` ;
markdownExportData += ` \n ` ;
2021-03-02 12:26:19 +01:00
jsonAllData . stats . listOpenWeekly . map ( elem => {
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n * ${ elem . title } ` ;
2021-03-02 12:26:19 +01:00
} )
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n --- ` ;
2021-03-02 11:57:31 +01:00
// fichier de rapport des tâches fermées durant les 31 derniers jours
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n # Rapport des autres tâches durant le mois ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n ## ${ jsonAllData . stats . listClosedMonthly . length } ont été fermées ` ;
markdownExportData += ` \n ` ;
jsonAllData . stats . listClosedMonthly . map ( elem => {
markdownExportData += ` \n * ${ elem . title } ` ;
} )
markdownExportData += ` \n ## ${ jsonAllData . stats . listOpenMonthly . length } ont été ouvertes ` ;
markdownExportData += ` \n ` ;
jsonAllData . stats . listOpenMonthly . map ( elem => {
markdownExportData += ` \n * ${ elem . title } ` ;
} )
markdownExportData += ` \n --- ` ;
2021-03-02 11:57:31 +01:00
// stats de fermeture des tâches
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n # Stats de fermeture des tâches ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n --- ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n # Export de toutes les ${ jsonAllData . stats . listOpen . length } tâches ouvertes GTG \n ` ;
markdownExportData += ` \n ` ;
2021-03-02 11:57:31 +01:00
// jsonAllData.stats.listOpen.map(elem => {
// dataTagsMd += `\n## ${elem.title} `;
// dataTagsMd += `\n statut : ${elem['$'].status} `;
// dataTagsMd += `\n créé le : ${elem.addeddate} `;
// dataTagsMd += `\n modifié le : ${elem.modified} `;
// dataTagsMd += `\n prévu pour : ${elem.duedate | ''} `;
// dataTagsMd += `\n ${elem.content} `;
// })
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n --- ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n # Export de toutes les ${ jsonAllData . stats . listClosed . length } tâches fermées GTG \n ` ;
markdownExportData += ` \n ` ;
2021-03-02 11:57:31 +01:00
//
// jsonAllData.stats.listClosed.map(elem=>{
// dataTagsMd += `\n * ${elem.title} `;
// })
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n ` ;
markdownExportData += ` \n # Export de Tags GTG \n ` ;
markdownExportData += ` \n ` ;
markdownExportData += ` \n ` ;
2021-03-02 11:57:31 +01:00
let sortedTagsByTitle = _ . sortBy ( jsonAllData . tags . tagstore . tag , [ ( o ) => {
o [ "$" ] . name
} ] )
sortedTagsByTitle . map ( elem => {
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n * ${ elem [ '$' ] . name } , ${ elem . tasks | 0 } ` ;
2021-03-02 11:57:31 +01:00
} )
2021-03-03 16:19:11 +01:00
markdownExportData += ` \n ` ;
markdownExportData += ` \n rapport généré avec gtg2nodejs par tykayn - contact@cipherbliss.com ` ;
2021-03-02 11:10:53 +01:00
// write file to disk
2021-03-03 16:19:11 +01:00
fs . writeFile ( ` output/export_hebdo.md ` , markdownExportData , ` utf8 ` , ( err ) => {
2021-03-02 11:10:53 +01:00
if ( err ) {
console . log ( ` Error writing file: ${ err } ` ) ;
res . send ( err )
} else {
console . log ( ` File \` output/export_tags.md \` is written successfully! ` ) ;
}
} ) ;
2021-03-02 10:57:26 +01:00
2021-03-02 10:12:15 +01:00
}
2021-03-02 10:57:26 +01:00
function transformToJson ( xml , fileToOpen , res ) {
2021-03-01 18:02:04 +01:00
return parseString ( xml , function ( err , result ) {
if ( err ) console . log ( err ) ;
// console.dir(JSON.stringify(result));
console . log ( 'Done' ) ;
// write file to disk
fs . writeFile ( ` sources/ ${ fileToOpen } _gtg.json ` , JSON . stringify ( result , null , 4 ) , 'utf8' , ( err ) => {
if ( err ) {
console . log ( ` Error writing file: ${ err } ` ) ;
// return JSON.stringify(result)
res . send ( err )
} else {
console . log ( ` File \` sources/ ${ fileToOpen } _gtg.json \` is written successfully! ` ) ;
}
} ) ;
} ) ;
}
2021-02-27 10:26:37 +01:00
module . exports = router ;