Skip to content

Instantly share code, notes, and snippets.

@queses
Created May 5, 2020 11:14
Show Gist options
  • Save queses/add0cbb389cbc45c776f796a66476aff to your computer and use it in GitHub Desktop.
Save queses/add0cbb389cbc45c776f796a66476aff to your computer and use it in GitHub Desktop.
Calculate worker hours in MeisterTask's CSV export file
const fs = require('fs')
const printHelp = () => {
console.log(
'Console tool to extract and sum worker hours from MeisterTask\'s CSV export file\n' +
'Usage: node meistertask-calculate-hours.js FILEPATH [WORKER NAME] [DATE FROM] [DATE TO]\n' +
'Example: node meistertask-calculate-hours.js /home/user/meister.csv Maxim 2020.04.01 2020.04.30'
)
}
const run = () => {
const [ fileName, workerName, dateFromStr, dateToStr ] = process.argv.slice(2)
const lines = fs.readFileSync(fileName, 'utf8').split('\n').slice(1)
lines.pop()
const dateFrom = (dateFromStr && dateFromStr.length >= 10) ? new Date(dateFromStr.substr(0, 10)).getTime() : undefined
const dateTo = (dateToStr && dateToStr.length >= 10) ? new Date(dateToStr.substr(0, 10)).getTime() + 1000 * 60 * 60 * 24 : undefined
let hoursSum = 0
for (const line of lines) {
if (dateFrom || dateTo) {
const date = new Date(line.substr(6, 4) + '.' + line.substr(0, 2) + '.' + line.substr(3, 2)).getTime()
if (dateFrom && date < dateFrom) {
continue
} else if (dateTo && date > dateTo) {
continue
}
}
const parsed = parseCsvLine(line)
const parsedName = parsed[5] + ' ' + parsed[6]
if (workerName && !parsedName.includes(workerName)) {
continue
}
const hours = parseFloat(parsed[4])
hoursSum += hours
console.log(parsedName, '|', Math.round(hours), '|', parsed[0], '|', parsed[3])
}
console.log('Sum:', hoursSum.toFixed(2))
}
const parseCsvLine = (line) => {
const data = []
let commaIndex = -1
while (commaIndex < line.length) {
let nextPartIndex = commaIndex + 1
if (line[nextPartIndex] === '"') {
partEndIndex = line.indexOf('",', nextPartIndex)
if (partEndIndex < 0) {
throw new Error('Parsing error')
}
commaIndex = partEndIndex + 1
data.push(line.substring(nextPartIndex + 1, partEndIndex))
} else {
commaIndex = line.indexOf(',', nextPartIndex)
if (commaIndex < 0) {
commaIndex = line.length
}
data.push(line.substring(nextPartIndex, commaIndex))
}
}
return data
}
if (process.argv[2] === '-h' || process.argv[2] == '--help') {
printHelp()
} else {
run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment