-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
663 lines (591 loc) · 21.6 KB
/
index.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Timetrap.js
// class wrapper of Timetrap command line program
"use strict"
// DEBUGGING
// var util = require('util');
// includes
const {spawn, spawnSync} = require('child_process');
const fs = require('fs');
const path = require('path');
// parent
const {EventEmitter} = require('events').EventEmitter;
////////////////////////////////////////////
/////////////// Timetrap Error Class
////////////////////////////////////////////
class Timetrap_Error extends Error {
constructor(message){
message = "Timetrap Error: "+message;
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
////////////////////////////////////////////
/////////////// Timetrap Class
////////////////////////////////////////////
class Timetrap extends EventEmitter {
constructor({
working_directory = '/tmp',
watched_db_file = process.env.HOME+"/.timetrap.db" } ={})
{
super();
this.config = {
working_directory: `${working_directory}`,
db_monitor: {
IN_MODIFY_count: 0, //aggregate file mod counter
watched_db_file: `${watched_db_file}`,
agg_time: 1000,
agg_timer: 0
},
};
this.data = {
};
//order is important
this.registerCommandTypes();
this.registerEmitTypes();
};
}
////////////////////////////////////////////
/////////////// Data Structures
////////////////////////////////////////////
Timetrap.prototype.registerEmitTypes = function(){
this.emit_types = {
command_complete: {
description: "The object emitted after callCommand() completes",
name: "command_complete",
data: Object.assign({},
this.command_types.output), //an command_types.output object
target: '', // target id
owner: '', // owner id
},
db_change: {
description: "The object emitted from monitorDB when the database changes",
name: "db_change",
data: Number(0), // unixtime
target: '', // target id
owner: '', // owner id
},
checkout_all_sheets: {
description: "The object emitted from monitorDB when the database changes",
name: "checkout_all_sheets",
data: Array(), // array of scheets checked out
target: '', // target id
owner: '', // owner id
},
timetrap_internal: {
description: "The object emitted from monitorDB when the database changes",
name: "timetrap_internal",
data: Array(), // array
target: '', // target id
owner: '', // owner id
},
};
}
// timetrap command definitions
Timetrap.prototype.registerCommandTypes = function(){
this.command_types = {
timetrap: {
description: "the main timetrap command",
_command: ["timetrap", "t"],
args: [],
required: [],
allow_sheet: false,
special: true,
override: false,
get command(){return this._command[0]}
},
changeSheet:{
description: "a sheet name",
_command: ["sheet", "s"],
args: [],
required: [],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
checkIn:{
description: "checkin command",
_command: ["in", "i"],
args: [["-a", "--at"]],
required: [],
allow_sheet: false,
special: false,
override: false,
get command(){return this._command[0]}
},
checkOut:{
description: "checkout command",
_command: ["out", "o"],
args: [["-a", "--at"]],
required: [],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
// resume:{
// description: "resume command",
// _command: ["resume", "r"],
// args: [
// ["-a", "--at"],
// ["-i", "--id"]
// ],
// required: [],
// allow_sheet: false,
// special: true,
// override: false,
// get command(){return this._command[0]}
// },
edit:{
description: "edit command",
_command: ["edit", "e"],
args: [
// TODO: test compound statements
["--id", "-i"],
["--start", "-s"], //can not be used with --end
["--end", "-e"], //can not be used with --start
["--append", "-z"],
["--move", "-m"] //implement with EXTREME caution
],
required: [],
allow_sheet: false,
special: false,
override: false,
get command(){return this._command[0]}
},
today:{
description: "today display alias command",
_command: ["today", "t"],
args: [["-v", "--ids"], ["-fjson", "--format json"]],
required: ["--ids", "--format json"],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
yesterday:{
description: "yesterday display alias command",
_command: ["yesterday", "y"],
args: [["-v", "--ids"], ["-fjson", "--format json"]],
required: ['--ids', '-fjson'],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
week:{
description: "week display alias command",
_command: ["week", "w"],
args: [["-v", "--ids"], ["-fjson", "--format json"]],
required: ['--ids', '-fjson'],
//required: ['--ids'],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
month:{
description: "month display alias command",
_command: ["month", "m"],
args: [["-v", "--ids", "-fjson", "--format json"]],
required: ['--ids', '-fjson'],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
display: {
description: "display command",
_command: ["display", "d"],
args: [
["--ids", "-v"],
["--start", "-s"],
["--end", "-e"],
["--grep", "-g"]
],
required: ['--ids', '-fjson'],
allow_sheet: true,
special: false,
override: false,
get command(){return this._command[0]}
},
now:{
description: "now command",
_command: ["now", "n"],
args: [],
required: [],
allow_sheet: false,
special: true,
override: false,
get command(){return this._command[0]}
},
list:{
description: "list command",
_command: ["list", "l"],
args: [],
required: [],
allow_sheet: false,
special: true,
override: false,
get command(){return this._command[0]}
},
ids:{
description: "return a list of all ids",
_command: ["display", "d"],
args: [["-fids"]],
required: ['-fids'],
allow_sheet: true,
special: true,
override: false,
get command(){return this._command[0]}
},
// kill:{
// description: "kill command",
// _command: ["kill", "k"],
// //TODO: handle delicately -deletes either id or timesheet
// args: [["--id", "-i"]],
// required: [],
// allow_sheet: false, //could be true but we'll make a special exception in code
// special: true,
// override: false,
// get command(){return this._command[0]}
// },
custom:{
description: "custom command -not implemented",
_command: [''],
args: [],
required: [],
allow_sheet: false,
special: false,
override: false,
get command(){return this._command[0]}
},
output: {
description: "callCommand output data structure",
_command: [''],
content: '',
args: [],
required: [],
allow_sheet: false,
special: true,
stdoutData: '',
stderrData: '',
code: 0,
signal: '',
sheet: '',
type: '',
override: false,
sync: false,
cmdln: [],
get command(){return this._command[0]}
}
};
}
// dump utility for command output
Timetrap.prototype.dumpOutput = function(output, method) {
switch(method){
case 'console':
console.log("["+(output.sync?'sync':'async')+"]");
for( let key in output){
//let sync =
console.log(key+": "+String(output[key]));
}
break;
default:
throw new Timetrap_Error("[dumpOutput] specified method not supported");
}
}
////////////////////////////////////////////
/////////////// Commands Interface
////////////////////////////////////////////
// call timetrap commands
Timetrap.prototype.callCommand = function({type = '', owner = '',
timetrap_internal = false, sheet = '', content = '', sync = false} ={}) {
// calls the timetrap program with the appropriat command 'type'
// performs actions like `timetrap --ids dispaly sheetName`
// required parameters
if(type === '') {
throw new Timetrap_Error("[CallCommand] no type argument");
}
// manage the target for internal commands
let emit_target = this.emit_types.command_complete.name;
if(timetrap_internal){
// change the target to an internal operation
emit_target = emit_target+"-"+this.emit_types.timetrap_internal.name;
}
// for depth
let _this = this;
// convenience operational data
let data = {
type: type,
sheet: sheet,
content: content,
sync: sync,
owner: owner
};
// init the return object
let emit_obj = Object.assign({}, this.emit_types.command_complete);
// preserve the emit target
emit_obj.target = emit_target
emit_obj.owner = data.owner
let args = [];
// add required arguments from the command_type structure
if(this.command_types[data.type].required.length > 0){
args = [this.command_types[data.type].command,
this.command_types[data.type].required, data.content];
// TODO: derp flatten args -fix inconsistent data schema....
args = [].concat.apply([], args);
}
else {
// no requirements...
args = [this.command_types[data.type].command, data.content];
}
//some commands can be run with a sheet specification
if ( this.command_types[data.type].allow_sheet)
{
//just push the sheet onto the args stack
args.push(data.sheet);
}
if( ! data.sync ){
//run asynchronous
// for commands that don't allow a sheet to be specified, we have to
// switch to the sheet manually. this is done via a promise chain....
// that ... synchronizes the sequence.
// There's probably a better way to do this....
if( ( ! this.command_types[data.type].allow_sheet)
&& ( ! this.command_types[data.type].special) ){
// we have to change the sheet
this.doCallCommandAsync({
command: this.command_types.timetrap.command,
args: ['sheet', data.sheet],
sheet: data.sheet, type: 'changeSheet', content: data.content} )
.then(function(output){
// handle output
emit_obj.data = Object.assign({}, output);
_this.emit(emit_target, emit_obj);
// now call the actual command
_this.doCallCommandAsync({
command: _this.command_types.timetrap.command,
args: args, sheet: data.sheet, type: data.type,
content: data.content} )
.then(function(output){
// handle output
emit_obj.data = Object.assign({}, output);
_this.emit(emit_target, emit_obj);
}, function(err){
// handle error
throw new Timetrap_Error("attempt to "
+data.type+" sheet failed ["+err+"]");
});
}, function(err){
// handle error
throw new Timetrap_Error("attempt to change sheet failed ["
+err+"]");
});
}
else {
// call the command with the sheet (i.e. change sheet, checkout)
this.doCallCommandAsync({
command: this.command_types.timetrap.command,
args: args, sheet: data.sheet, type: data.type,
content: data.content} ).then(function(output){
// handle output
emit_obj.data = Object.assign({}, output);
_this.emit(emit_target, emit_obj);
//_this.dumpOutput(output, 'console');
}, function(err){
// handle error
throw new Timetrap_Error("attempt to "+data.type
+" sheet failed ["+err+"]");
});
}
}
else {
// TODO: handle errors
//run synchronous
//doCallCommandSync
if( ( ! this.command_types[data.type].allow_sheet )
&& ( ! this.command_types[data.type].special) ){
// we have to change the sheet first
let output = this.doCallCommandSync({
command: this.command_types.timetrap.command,
args: ['sheet', data.sheet],
sheet: data.sheet, type: 'changeSheet',
content: data.content} );
// handle output
emit_obj.data = Object.assign({}, output);
_this.emit(emit_target, emit_obj);
}
let output = this.doCallCommandSync({
command: this.command_types.timetrap.command,
args: args, sheet: data.sheet, type: data.type,
content: data.content} );
// handle output
emit_obj.data = Object.assign({}, output);
_this.emit(emit_target, emit_obj);
}
}
Timetrap.prototype.doCallCommandSync = function({
command = this.command_types.timetrap.command,
args = [['display'],['-v']], sheet = 'default',
type = 'display'} ={})
{
let data = {
command: command,
args: args.filter(function(e) { return e === 0 || e;}),
sheet: sheet,
type: type,
}
// seed the output structure
let output = Object.assign({}, this.command_types.output);
// TODO: needs error block
const cmd = spawnSync(this.command_types.timetrap.command, data.args,
{cwd: this.config.working_directory});
output.sheet = data.sheet;
output.type = data.type;
output.stdoutData = String(cmd.stdout);
output.stderrData = String(cmd.stderr);
output.code = cmd.status;
output.signal = cmd.signal;
output.sync = true;
output.cmdln = [this.command_types.timetrap.command, data.args];
return output;
}
Timetrap.prototype.doCallCommandAsync = function({
command = this.command_types.timetrap.command,
args = [['display'],['-v']], sheet = 'default',
type = 'display'} ={})
{
// return a promise of an asynch execution of the command
let _this = this;
let data = {
command: command,
args: args.filter(function(e) { return e === 0 || e;}),
sheet: sheet,
type: type,
}
// seed the output structure
let output = Object.assign({}, this.command_types.output);
output.sheet = data.sheet;
output.type = data.type;
output.sync = false;
output.cmdln = [this.command_types.timetrap.command, data.args];
return new Promise(function(resolve, reject){
// we're using the promise to conditionally serialize a second call to
// the timetrap program for commands that require a sheet change before
// execution
//spawn the process
const cmd = spawn(_this.command_types.timetrap.command, data.args,
{cwd: _this.config.working_directory});
cmd.stdout.on('data', (data) => {
output.stdoutData += data;
});
cmd.stderr.on('data', (data) => {
output.stderrData += data;
});
cmd.once('close', function (){
resolve(output);
});
cmd.on('close', (code, signal) => {
output.code = code;
output.signal = signal;
reject(output);
})
});
}
////////////////////////////////////////////
/////////////// Database Monitoring
////////////////////////////////////////////
Timetrap.prototype.monitorDBStart = function(){
// monitors the database
// TODO: assert if timer is already running
let _this = this;
this.config.db_monitor.IN_MODIFY_count = 0;
//start the watcher
this.config.db_monitor.watcher = fs.watch(
this.config.db_monitor.watched_db_file);
this.config.db_monitor.watcher.on('change', (event, filename) => {
if(filename == path.basename(this.config.db_monitor.watched_db_file) ){
//incriment counter
this.config.db_monitor.IN_MODIFY_count++;
//start timer
if ( this.config.db_monitor.IN_MODIFY_count > 0) {
// The kernel emits multiple IN_MODIFY events via libuv +
// sometimes multiple writes occur for an action via timetrap
// -so we'll only report the composite within a (arbitrary)
// time window of 1 second.
// see: [fs.watch has double change events for file writes · Issue #3042 · nodejs/node]
// (https://github.com/nodejs/node/issues/3042)
this.config.db_monitor.agg_timer = setTimeout(function () {
_this.monitorDBCatchTimer(
_this.config.db_monitor.IN_MODIFY_count);
}, this.config.db_monitor.agg_time);
}
}
});
}
Timetrap.prototype.monitorDBCatchTimer = function() {
//triggers an emit after a db change aggregate occurs (via timer)
if (this.config.db_monitor.IN_MODIFY_count > 0){
//data structure
this.config.db_monitor.IN_MODIFY_count=0;
let obj = Object.assign({}, this.emit_types.db_change);
obj.data = Date.now();
this.emit(this.emit_types.db_change.name, obj);
}
}
Timetrap.prototype.monitorDBStop = function(){
if(this.config.db_monitor.agg_timer){
//TODO: bette way to do this?
delete this.config.db_monitor.watcher;
clearTimeout(this.config.db_monitor.agg_timer);
this.config.db_monitor.agg_timer = 0;
}
}
////////////////////////////////////////////
/////////////// Extended API
////////////////////////////////////////////
//Timetrap.prototype.stopAllTimers = function(data){
Timetrap.prototype.checkoutAllSheets = function({target = {}, data = {}} ={}){
// check out of all running sheets
let _this = this;
//eat our own dogfood
this.once("command_complete-timetrap_internal", (payload) =>{
// receive data
// arr will have arr.length-1 valid lines.
// It ends up with the last element being ' '
// The sheet name will be preceded by 1 char
// that indicates the 'active' state for that sheet.
let arr = payload.data.stdoutData.split("\n");
//build an array of running sheets
let sheets = [];
if(arr.length > 1) {
for ( let i=0; i < arr.length-1; i++ ) {
let chunk = arr[i].slice(1,arr[i].length);
sheets.push(chunk.split(':')[0])
}
}
// checkout of running sheets
for ( let s in sheets ) {
this.once("command_complete-timetrap_internal", (payload) =>{
// TODO: handle errors etc.
});
_this.callCommand({type: 'checkOut', sheet: sheets[s],
timetrap_internal: true, owner: 'timetrap', sync: true})
}
let emit_obj = Object.assign({}, this.emit_types.checkout_all_sheets);
// emit_obj.target = payload.target;
// emit_obj.target = payload.user_target;
emit_obj.owner = payload.owner;
emit_obj.data = sheets
_this.emit(emit_obj.name, emit_obj);
});
// get list of sheets via 'now'
this.callCommand({type: 'now',
timetrap_internal: true,
owner: 'timetrap',
sync: true,
});
}
module.exports = {Timetrap, Timetrap_Error};