-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimesheet.js
More file actions
184 lines (163 loc) · 5.48 KB
/
timesheet.js
File metadata and controls
184 lines (163 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var help = `
Timesheet summary view
see https://github.com/lawrencewalters/timesheets/ for full docs
Command line parameters:
datafile optional full path to file with your entries. defaults to data.txt in current directory
-h, --help show this help menu
Environment variables this script respects:
LOG_LEVEL - debug,info,warn,error
default execution
$ node timesheet.js
specify a timesheet
$ node timesheet.js /path/to/my/my-timesheet.txt
custom logging, with custom data file
$ LOG_LEVEL=warn node timesheet.js my-timesheet.txt
`
var fs = require('fs');
var winston = require('winston');
var colors = require('colors');
const logger = winston.createLogger({
format: winston.format.json(),
transports: [
new winston.transports.Console({ format: winston.format.simple() })
]
});
if (process.env.hasOwnProperty('LOG_LEVEL')) {
logger.level = process.env.LOG_LEVEL;
} else {
logger.level = 'error';
}
var colorMap = new Map();
var datafile = 'data.txt';
process.argv.forEach(function (val, index, array) {
if (index > 1) {
if (val === '-h' || val === '--help') {
console.log(help);
process.exit();
}
datafile = val;
}
});
processFile(datafile);
fs.watchFile(datafile, function (curr, prev) {
processFile(datafile);
});
function processFile(filename) {
var summary = {};
var current = '';
var daytotal = 0;
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(filename)
});
lineReader.on('line', function (input) { parse(input, summary) });
lineReader.on('close', function () {
totals = {};
for (var day in summary) {
if (summary.hasOwnProperty(day)) {
process.stdout.write(day.toString().bgWhite.blue + ' ' + toHours(summary[day].daytotal).white.bgGreen + ' ');
display(summary[day], totals);
}
}
displayTotals(totals);
displayGrid(summary, totals);
});
}
function display(day, totals) {
var space = '';
for (var key in day) {
if (day.hasOwnProperty(key)) {
if (key != 'daytotal') {
totals[key] = (totals[key] || 0) + day[key].minutes;
console.log(colorByKey(key, space + key + ' : ' + toHours(day[key].minutes) + " : " + day[key].comments));
space = ' ';
}
}
}
}
function displayGrid(summary, totals) {
var projectHoursByDay = {};
console.log(' ' + Object.keys(summary).join(' '));
for (var day in summary) {
for (var projectKey in summary[day]) {
if (projectKey != 'daytotal') {
if (!projectHoursByDay.hasOwnProperty(projectKey)) {
projectHoursByDay[projectKey] = {};
}
projectHoursByDay[projectKey][day] = summary[day][projectKey].minutes;
}
}
}
for (var projectKey in projectHoursByDay) {
console.log(colorByKey(projectKey, (' ' + projectKey + ': ').slice(-6) +
Object.keys(summary).map(function (day) {
if (projectHoursByDay[projectKey][day]) {
return toHours(projectHoursByDay[projectKey][day]);
} else {
return ' ';
}
}).join(' ')));
}
}
function colorByKey(key, text) {
if (!colorMap.has(key)) {
colorMap.set(key, colorMap.size + 1);
}
switch (colorMap.get(key)) {
case 1:
return text.green;
case 2:
return text.yellow;
case 3:
return text.blue;
case 4:
return text.magenta;
case 5:
return text.cyan;
case 6:
return text.gray;
default:
return text;
}
}
function displayTotals(totals) {
total = 0;
msg = '';
for (var key in totals) {
total += totals[key];
msg = msg + key + ':' + toHours(totals[key]).magenta + ' | ';
}
console.log(toHours(total).magenta + ': ' + msg);
}
function toHours(minutes) {
return (Math.floor(minutes / 60)) + ':' + ((minutes % 60) < 9 ? ('0' + (minutes % 60)) : (minutes % 60));
}
function parse(line, summary) {
var parsedId, parsedComment, parsedMinutes;
logger.log('info', line);
if (line[0] == '#') // ignore comment lines
return;
if (line.match(/^\d+\/\d+(\/\d+)?$/)) { // dates - m/d or m/d/y "1/2" "1/2/2020" formats for dates
// match = line.match(/^\d+\/\d+(\/\d+)?$/);
// if (line.lastIndexOf('/')>0) {
current = line;
summary[current] = {
'daytotal': 0
};
} else if (line.match(/^([^,]+),(.+),\s?\b(\d+)$/)) { // entries - task,notes about it,240
match = line.match(/^([^,]+),(.+),\s?\b(\d+)$/);
parsedId = match[1];
parsedComment = match[2];
parsedMinutes = Number(match[3]);
logger.log('info', ' id:', parsedId, '\n comments:', parsedComment, '\n minutes:', parsedMinutes);
if (!(parsedId in summary[current])) {
summary[current][parsedId] = {
'comments': parsedComment + ' (' + parsedMinutes.toString() + ')',
'minutes': parsedMinutes
};
} else {
summary[current][parsedId].comments += parsedComment + ' (' + parsedMinutes.toString() + ')';
summary[current][parsedId].minutes += parsedMinutes;
}
summary[current].daytotal += parsedMinutes;
}
}