Skip to content

Commit 7855f6b

Browse files
committed
Add timeformat for different report parts
1 parent 565ffc6 commit 7855f6b

File tree

9 files changed

+79
-29
lines changed

9 files changed

+79
-29
lines changed

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,16 @@ dateFormat: DD.MM.YYYY HH:mm:ss
576576
# defaults to "[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]"
577577
timeFormat: "[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]"
578578

579+
# Time format for different parts of the report
580+
# Instead of specifying one global time format you can specify one for every
581+
# part of the report and the log command
582+
timeFormat:
583+
log: "[%sign][%hours_overall]"
584+
stats: "[%sign][%days_overall]"
585+
issues: "[%sign][%hours_overall]"
586+
merge_requests: "[%sign][%hours_overall]"
587+
records: "[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]"
588+
579589
# Output type
580590
# Available: csv, table, markdown, pdf
581591
# defaults to table

spec/include/config.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,33 @@ describe('The config class', () => {
4242
});
4343
});
4444

45+
it('returns values from objects and falls back to the default', () => {
46+
let Config = new config(),
47+
objectsWithDefaults = ['timeFormat'],
48+
defaults = Object.assign({}, Config.data),
49+
stringData = 'booze',
50+
objectData = {
51+
foo: 'bar',
52+
baz: 'bar'
53+
};
54+
55+
objectsWithDefaults.forEach(key => {
56+
Config.set(key, objectData);
57+
58+
expect(Config.get(key, 'foo')).to.equal('bar');
59+
expect(Config.get(key, 'baz')).to.equal('bar');
60+
expect(Config.get(key, 'not_a_real_key')).to.equal(defaults[key]);
61+
62+
Config.set(key, stringData);
63+
64+
expect(Config.get(key, 'foo')).to.equal('booze');
65+
expect(Config.get(key, 'baz')).to.equal('booze');
66+
expect(Config.get(key, 'not_a_real_key')).to.equal('booze');
67+
});
68+
});
69+
4570
it('makes durations human readable', () => {
46-
let Config = new config,
71+
let Config = new config(),
4772
humanReadable = "1d 4h 30m 10s",
4873
seconds = 45010;
4974

src/gtt-log.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ const Tasks = require('./include/tasks');
1111
program
1212
.option('--verbose', 'show verbose output')
1313
.option('--hours_per_day <hours>', 'hours per day for human readable time formats')
14+
.option('--time_format <time_format>', 'time format')
1415
.parse(process.argv);
1516

1617
Cli.verbose = program.verbose;
1718

18-
let config = new Config(__dirname)
19-
.set('hoursPerDay', program.hours_per_day),
20-
tasks = new Tasks(config);
19+
let config = new Config(__dirname).set('hoursPerDay', program.hours_per_day),
20+
tasks = new Tasks(config),
21+
timeFormat = config.set('timeFormat', program.time_format).get('timeFormat');
22+
23+
timeFormat = _.isObject(timeFormat) && timeFormat['log'] ? timeFormat['log'] : timeFormat;
2124

2225
function toHumanReadable(input) {
23-
return Time.toHumanReadable(Math.ceil(input), config.get('hoursPerDay'), config.get('timeFormat'));
26+
return Time.toHumanReadable(Math.ceil(input), config.get('hoursPerDay'), timeFormat);
2427
}
2528

2629
tasks.log()

src/include/config.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const moment = require('moment');
2+
const _ = require('underscore');
23

34
const Time = require('./../models/time');
45

56
const dates = ['from', 'to'];
7+
const objectsWithDefaults = ['timeFormat', 'columns'];
68
const defaults = {
79
type: 'project',
810
subgroups: false,
@@ -47,7 +49,7 @@ class config {
4749
* construct
4850
*/
4951
constructor() {
50-
this.data = defaults;
52+
this.data = _.extend({}, defaults);
5153
}
5254

5355
/**
@@ -70,21 +72,27 @@ class config {
7072
/**
7173
* get a value by the given key
7274
* @param key
75+
* @param subKey
7376
* @returns {*}
7477
*/
75-
get(key) {
76-
if (!dates.includes(key)) return this.data[key];
78+
get(key, subKey = false) {
79+
if (dates.includes(key))
80+
return moment(this.data[key]);
7781

78-
return moment(this.data[key]);
82+
if (objectsWithDefaults.includes(key) && _.isObject(this.data[key]))
83+
return subKey && this.data[key][subKey] ? this.data[key][subKey] : defaults[key];
84+
85+
return this.data[key];
7986
}
8087

8188
/**
8289
* get a human readable version of the given time
8390
* @param input
91+
* @param timeFormat
8492
* @returns {string}
8593
*/
86-
toHumanReadable(input) {
87-
return Time.toHumanReadable(input, this.get('hoursPerDay'), this.get('timeFormat'));
94+
toHumanReadable(input, timeFormat = false) {
95+
return Time.toHumanReadable(input, this.get('hoursPerDay'), this.get('timeFormat', timeFormat));
8896
}
8997
}
9098

src/models/hasTimes.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class hasTimes extends Base {
1515
constructor(config) {
1616
super(config);
1717
this.times = [];
18+
this.name = '';
1819
}
1920

2021
/**
@@ -55,9 +56,10 @@ class hasTimes extends Base {
5556
* @returns {Promise}
5657
*/
5758
getTimes() {
58-
let times = [];
59-
let timeSpent = 0;
60-
let timeUsers = {};
59+
let times = [],
60+
timeSpent = 0,
61+
timeUsers = {},
62+
timeFormat = this.config.get('timeFormat', this.name);
6163

6264
// sort by created at
6365
this.notes.sort((a, b) => {
@@ -69,7 +71,7 @@ class hasTimes extends Base {
6971
let created = moment(note.created_at), match, subMatch, removeMatch;
7072

7173
if (
72-
// filter out user notes
74+
// filter out user notes
7375
!note.system ||
7476
// only include times by the configured user
7577
(this.config.get('user') && this.config.get('user') !== note.author.username) ||
@@ -94,7 +96,7 @@ class hasTimes extends Base {
9496
});
9597

9698
promise.then(() => {
97-
_.each(timeUsers, (time, name) => this[`time_${name}`] = this.config.toHumanReadable(time));
99+
_.each(timeUsers, (time, name) => this[`time_${name}`] = Time.toHumanReadable(time, this.config.get('hoursPerDay'), timeFormat));
98100
this.timeSpent = timeSpent;
99101
this.times = times
100102
});

src/models/issue.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class issue extends hasTimes {
1010
constructor(config, data = {}) {
1111
super(config);
1212
this.data = data;
13+
this.name= 'issues';
1314
}
1415

1516
make(project, id, create = false) {
@@ -87,15 +88,15 @@ class issue extends hasTimes {
8788
}
8889

8990
get spent() {
90-
return this.config.toHumanReadable(this.timeSpent);
91+
return this.config.toHumanReadable(this.timeSpent, this.name);
9192
}
9293

9394
get total_spent() {
94-
return this.stats ? this.config.toHumanReadable(this.stats.total_time_spent) : null;
95+
return this.stats ? this.config.toHumanReadable(this.stats.total_time_spent, this.name) : null;
9596
}
9697

9798
get total_estimate() {
98-
return this.stats ? this.config.toHumanReadable(this.stats.time_estimate) : null;
99+
return this.stats ? this.config.toHumanReadable(this.stats.time_estimate, this.name) : null;
99100
}
100101

101102
get _type() {

src/models/mergeRequest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class mergeRequest extends hasTimes {
77
constructor(config, data = {}) {
88
super(config);
99
this.data = data;
10+
this.name = 'merge_requests';
1011
}
1112

1213
make(project, id, create = false) {
@@ -80,15 +81,15 @@ class mergeRequest extends hasTimes {
8081
}
8182

8283
get spent() {
83-
return this.config.toHumanReadable(this.timeSpent);
84+
return this.config.toHumanReadable(this.timeSpent, this.name);
8485
}
8586

8687
get total_spent() {
87-
return this.stats ? this.config.toHumanReadable(this.stats.total_time_spent) : null;
88+
return this.stats ? this.config.toHumanReadable(this.stats.total_time_spent, this.name) : null;
8889
}
8990

9091
get total_estimate() {
91-
return this.stats ? this.config.toHumanReadable(this.stats.time_estimate) : null;
92+
return this.stats ? this.config.toHumanReadable(this.stats.time_estimate, this.name) : null;
9293
}
9394

9495
get _type() {

src/models/time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class time {
6161
}
6262

6363
get _timeFormat() {
64-
return this.config && this.config.get('timeFormat') ? this.config.get('timeFormat') : '';
64+
return this.config && this.config.get('timeFormat', 'records') ? this.config.get('timeFormat', 'records') : '';
6565
}
6666

6767
get _hoursPerDay() {

src/output/base.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class base {
9090
*/
9191
toFile(file, resolve) {
9292
fs.writeFileSync(file, this.out);
93-
if(resolve) resolve();
93+
if (resolve) resolve();
9494
}
9595

9696
/**
@@ -123,12 +123,12 @@ class base {
123123
});
124124

125125
this.times = times;
126-
this.users = _.mapObject(users, user => this.config.toHumanReadable(user));
127-
this.projects = _.mapObject(projects, project => this.config.toHumanReadable(project));
126+
this.users = _.mapObject(users, user => this.config.toHumanReadable(user, 'stats'));
127+
this.projects = _.mapObject(projects, project => this.config.toHumanReadable(project, 'stats'));
128128
this.stats = {
129-
'total estimate': this.config.toHumanReadable(totalEstimate),
130-
'total spent': this.config.toHumanReadable(totalSpent),
131-
'spent': this.config.toHumanReadable(spent)
129+
'total estimate': this.config.toHumanReadable(totalEstimate, 'stats'),
130+
'total spent': this.config.toHumanReadable(totalSpent, 'stats'),
131+
'spent': this.config.toHumanReadable(spent, 'stats')
132132
};
133133
}
134134

0 commit comments

Comments
 (0)