forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
executable file
·169 lines (142 loc) · 4.1 KB
/
base.js
File metadata and controls
executable file
·169 lines (142 loc) · 4.1 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
const _ = require('underscore');
const fs = require('fs');
const moment = require('moment');
const defaultFormats = {
headline: h => `${h}\n`,
warning: w => w,
write: w => `\n${w}`
};
/**
* Base output
*/
class base {
/**
* constructor
* @param config
* @param report
*/
constructor(config, report) {
this.config = config;
this.report = report;
this.out = '';
this.formats = defaultFormats;
this.calculate();
}
set format(value) {
this.formats = Object.assign(this.formats, value);
}
/**
* print a headline
* @param string
*/
headline(string) {
if (this.config.get('noHeadlines')) return;
this.write(this.formats.headline(string));
}
/**
* print a warning
* @param string
*/
warning(string) {
if (this.config.get('noWarnings')) return;
this.write(this.formats.warning(string));
}
/**
* add the given string
* @param string
* @returns {base}
*/
write(string) {
this.out += this.formats.write(string);
return this;
}
/**
* make
*/
make() {
if (this.config.get('report').includes('stats')) {
this.makeStats();
}
if (this.config.get('report').includes('issues')) {
this.makeIssues();
}
if (this.config.get('report').includes('merge_requests')) {
this.makeMergeRequests();
}
if (this.config.get('report').includes('records')) {
this.makeRecords();
}
}
/**
* render to stdout
*/
toStdOut() {
console.log(this.out);
}
/**
* render to file
*/
toFile(file, resolve) {
fs.writeFileSync(file, this.out);
if (resolve) resolve();
}
/**
* calculate stats
*/
calculate() {
let totalEstimate = 0;
let totalSpent = 0;
let spent = 0;
let users = {};
let projects = {};
let times = [];
['issues', 'mergeRequests'].forEach(type => {
this.report[type].forEach(issue => {
issue.times.forEach(time => {
if (!users[time.user]) users[time.user] = 0;
if (!projects[time.project_namespace]) projects[time.project_namespace] = 0;
users[time.user] += time.seconds;
projects[time.project_namespace] += time.seconds;
spent += time.seconds;
times.push(time);
});
totalEstimate += parseInt(issue.stats.time_estimate);
totalSpent += parseInt(issue.stats.total_time_spent);
});
this.report[type].sort((a, b) => {
if (a.iid === b.iid) return 0;
return (a.iid - b.iid) < 0 ? 1 : -1;
});
});
this.times = times;
this.times.sort((a, b) => {
if (a.date.isSame(b.date)) return 0;
return a.date.isBefore(b.date) ? 1 : -1;
});
this.users = _.mapObject(users, user => this.config.toHumanReadable(user, 'stats'));
this.projects = _.mapObject(projects, project => this.config.toHumanReadable(project, 'stats'));
this.stats = {
'total estimate': this.config.toHumanReadable(totalEstimate, 'stats'),
'total spent': this.config.toHumanReadable(totalSpent, 'stats'),
'spent': this.config.toHumanReadable(spent, 'stats')
};
}
/**
* prepare the given object by only returning
* the given columns/properties and formatting
* special properties like moment instances
* @param obj
* @param columns
* @returns {Array}
*/
prepare(obj = {}, columns = []) {
return columns.map(column => {
if (moment.isMoment(obj[column]))
return obj[column].format(this.config.get('dateFormat'));
if (obj[column] === undefined || obj[column] === null)
return '';
return obj[column];
});
}
}
module.exports = base;