forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.js
More file actions
214 lines (182 loc) · 6.33 KB
/
tasks.js
File metadata and controls
214 lines (182 loc) · 6.33 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const _ = require('underscore');
const moment = require('moment');
const Config = require('./config');
const Fs = require('./filesystem');
const Frame = require('./../models/frame');
const Issue = require('./../models/issue');
const MergeRequest = require('./../models/mergeRequest');
const FrameCollection = require('./../models/frameCollection');
const classes = {
issue: Issue,
merge_request: MergeRequest
};
class tasks {
constructor(config) {
this.config = config;
this.sync = {};
}
/**
* Filter frames that need an update and resolve merge_requests and issues
* respectively.
* @returns {Promise}
*/
syncResolve() {
this.sync.frames = new FrameCollection(this.config);
this.sync.resources = {
issue: {},
merge_request: {}
};
// filter out frames, that don't need an update
this.sync.frames.filter(frame => !(Math.ceil(frame.duration) === _.reduce(frame.notes, (n, m) => (n + m.time), 0)));
// resolve issues and merge requests
return this.sync.frames.forEach((frame, done) => {
let project = frame.project,
type = frame.resource.type,
id = frame.resource.id,
resource = this.sync.resources[type][id];
if (resource !== undefined && resource.data.project_id)
return done();
this.sync.resources[type][id] = new classes[type](this.config, {});
this.sync.resources[type][id]
.make(project, id)
.then(() => done())
.catch(error => done(`Could not resolve ${type} ${id} on "${project}"`));
})
}
/**
* Get notes for all frames.
*/
syncNotes() {
return this.sync.frames.forEach((frame, done) => {
let project = frame.project,
type = frame.resource.type,
id = frame.resource.id,
notes;
if ((notes = this.sync.resources[type][id].notes) && notes.length > 0) return;
this.sync.resources[type][id]
.getNotes()
.then(() => done())
.catch(error => done(`Could not get notes from ${type} ${id} on "${project}"`));
});
}
syncUpdate(callback) {
return this.sync.frames.forEach((frame, done) => {
let time = frame.duration,
project = frame.project,
type = frame.resource.type,
id = frame.resource.id;
if (frame.notes.length > 0)
time = Math.ceil(frame.duration) - parseInt(_.reduce(frame.notes, (n, m) => (n + m.time), 0));
this._addTime(frame, time)
.then(() => {
if (callback) callback();
done();
})
.catch(error => done(`Could not update ${type} ${id} on ${project}`))
});
}
_addTime(frame, time) {
return new Promise((resolve, reject) => {
let resource = this.sync.resources[frame.resource.type][frame.resource.id];
resource
.createTime(Math.ceil(time))
.then(() => resource.getNotes())
.then(() => {
frame.notes.push({
id: resource.notes[0].id,
time: Math.ceil(time)
});
frame.write();
resolve();
})
.catch(error => reject(error))
});
}
/**
*
* @returns {Promise}
*/
status() {
return new Promise((resolve, reject) => {
Fs.find(`"stop": false`, this.config.frameDir)
.then(frames => resolve(frames.map(file => Frame.fromFile(this.config, file))))
.catch(error => reject(error));
});
}
/**
*
* @returns {Promise}
*/
log() {
return new Promise((resolve, reject) => {
let frames = {},
times = {};
Fs.readDir(this.config.frameDir).forEach(file => {
let frame = Frame.fromFile(this.config, Fs.join(this.config.frameDir, file));
if (frame.stop === false) return;
let date = moment(frame.start).format('YYYY-MM-DD');
if (!frames[date]) frames[date] = [];
if (!times[date]) times[date] = 0;
frames[date].push(frame);
times[date] += Math.ceil(frame.duration);
});
resolve({frames, times})
});
}
/**
*
* @param project
* @param type
* @param id
* @returns {Promise}
*/
start(project, type, id) {
this.config.set('project', project);
return new Promise((resolve, reject) => {
Fs.find(`"stop": false`, this.config.frameDir)
.then(frames => {
if (frames.length > 0)
return reject("Already running. Please stop it first with 'gtt stop'.");
resolve(new Frame(this.config, id, type).startMe());
})
.catch(error => reject(error));
})
}
/**
*
* @returns {Promise}
*/
stop() {
return new Promise((resolve, reject) => {
Fs.find(`"stop": false`, this.config.frameDir)
.then(frames => {
if (frames.length === 0)
return reject('No projects started.');
resolve(frames.map(file => {
return Frame.fromFile(this.config, file).stopMe();
}));
})
.catch(error => reject(error));
});
}
/**
*
* @returns {Promise}
*/
cancel() {
return new Promise((resolve, reject) => {
Fs.find(`"stop": false`, this.config.frameDir)
.then(frames => {
if (frames.length === 0)
return reject('No projects started.');
resolve(frames.map(file => {
let frame = Frame.fromFile(this.config, file);
Fs.remove(file);
return frame;
}));
})
.catch(error => reject(error));
});
}
}
module.exports = tasks;