line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
50
|
33
|
1
|
|
640
|
if (@ARGV and @ARGV[0] =~ /^\w/) { |
4
|
0
|
0
|
|
|
|
0
|
@ARGV = grep { (/^-{1,2}h\w{0,3}$/ ? ($ENV{APP_TT_HELP} = $ARGV[0], 0) : (1, 1))[1] } @ARGV; |
|
0
|
|
|
|
|
0
|
|
5
|
|
|
|
|
|
|
} |
6
|
|
|
|
|
|
|
} |
7
|
1
|
|
|
1
|
|
560
|
use Applify; |
|
1
|
|
|
|
|
5992
|
|
|
1
|
|
|
|
|
8
|
|
8
|
1
|
|
|
1
|
|
270
|
use Cwd (); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
18
|
|
9
|
1
|
|
|
1
|
|
5
|
use File::Basename; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
82
|
|
10
|
1
|
|
|
1
|
|
6
|
use File::Find; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
11
|
1
|
|
|
1
|
|
723
|
use File::HomeDir; |
|
1
|
|
|
|
|
5753
|
|
|
1
|
|
|
|
|
54
|
|
12
|
1
|
|
|
1
|
|
7
|
use File::Path; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
72
|
|
13
|
1
|
|
|
1
|
|
8
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
14
|
1
|
|
|
1
|
|
543
|
use IO::Handle; |
|
1
|
|
|
|
|
6654
|
|
|
1
|
|
|
|
|
50
|
|
15
|
1
|
|
|
1
|
|
761
|
use JSON::XS (); |
|
1
|
|
|
|
|
3362
|
|
|
1
|
|
|
|
|
29
|
|
16
|
1
|
|
|
1
|
|
8
|
use List::Util qw(uniq); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
94
|
|
17
|
1
|
|
|
1
|
|
10
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
18
|
1
|
|
|
1
|
|
633
|
use Time::Piece; |
|
1
|
|
|
|
|
10687
|
|
|
1
|
|
|
|
|
19
|
|
19
|
1
|
|
|
1
|
|
89
|
use Time::Seconds; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
80
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
50
|
1
|
|
6
|
use constant DEBUG => $ENV{APP_TT_DEBUG} || 0; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6735
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
option str => project => 'Project name. Normally autodetected', alias => 'p'; |
24
|
|
|
|
|
|
|
option str => tag => 'Tags for an event', alias => 't', n_of => '@'; |
25
|
|
|
|
|
|
|
option str => description => 'Description for an event', alias => 'd'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
option str => group_by => 'Group log output: --group-by day', alias => 'g'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
option str => month => 'Mass edit a month'; |
30
|
|
|
|
|
|
|
option str => year => 'Mass edit a year'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
documentation 'App::tt'; |
33
|
|
|
|
|
|
|
version 'App::tt'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $NOW = localtime; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$SIG{__DIE__} = sub { Carp::confess($_[0]) } |
38
|
|
|
|
|
|
|
if DEBUG; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub command_edit { |
41
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
42
|
0
|
0
|
0
|
|
|
0
|
return $self->_edit_with_editor($_[0]) if @_ and -f $_[0]; # Edit a given file |
43
|
0
|
0
|
0
|
|
|
0
|
return $self->_mass_edit(@_) if $self->year or $self->month or !-t STDIN; |
|
|
|
0
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return $self->_edit_with_editor; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub command_export { |
48
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
49
|
0
|
|
|
|
|
0
|
my $res = $self->_log(@_); |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
my @cols = split /,/, $self->config('export_columns'); |
52
|
0
|
|
|
|
|
0
|
my $format = join ',', map {'"%s"'} @cols; |
|
0
|
|
|
|
|
0
|
|
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
0
|
$res->{rounded} = 0; |
55
|
0
|
|
|
|
|
0
|
$self->_say($format, @cols); |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
0
|
for my $event (sort { $a->{start} <=> $b->{start} } @{$res->{log}}) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
58
|
0
|
|
|
|
|
0
|
$event->{date} = $event->{start}; |
59
|
0
|
|
|
|
|
0
|
$event->{hours} = int($event->{seconds} / 3600); |
60
|
0
|
|
|
|
|
0
|
$event->{seconds} -= $event->{hours} * 3600; |
61
|
0
|
|
|
|
|
0
|
$event->{minutes} = int($event->{seconds} / 60); |
62
|
|
|
|
|
|
|
$event->{rounded} |
63
|
0
|
0
|
|
|
|
0
|
= $event->{hours} + ($event->{minutes} >= $self->config('round_up_at') ? 1 : 0); |
64
|
0
|
|
|
|
|
0
|
$event->{hours} += sprintf '%.1f', $event->{minutes} / 60; |
65
|
0
|
|
|
|
|
0
|
$res->{rounded} += $event->{rounded}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$self->_say( |
68
|
|
|
|
|
|
|
$format, |
69
|
|
|
|
|
|
|
map { |
70
|
0
|
|
0
|
|
|
0
|
my $val = $event->{$_} // ''; |
|
0
|
|
|
|
|
0
|
|
71
|
0
|
0
|
0
|
|
|
0
|
$val = $val->ymd if blessed $val and $val->can('ymd'); |
72
|
0
|
0
|
|
|
|
0
|
$val = join ',', @$val if ref $val eq 'ARRAY'; |
73
|
0
|
|
|
|
|
0
|
$val =~ s!"!""!g; |
74
|
0
|
|
|
|
|
0
|
$val; |
75
|
|
|
|
|
|
|
} @cols |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$self->_diag( |
80
|
|
|
|
|
|
|
"\nExact hours: %s. Rounded hours: %s Events: %s", |
81
|
|
|
|
|
|
|
$self->_hms_duration($res->{seconds}, 'hm'), |
82
|
|
|
|
|
|
|
$res->{rounded}, $res->{events}, |
83
|
0
|
|
|
|
|
0
|
); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
0
|
return 0; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub command_help { |
89
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
90
|
0
|
|
0
|
|
|
0
|
my $for = shift || 'app'; |
91
|
0
|
|
|
|
|
0
|
my $today = $NOW->ymd; |
92
|
0
|
|
|
|
|
0
|
my @help; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
0
|
if ($for eq 'app') { |
95
|
0
|
|
|
|
|
0
|
$self->_script->print_help; |
96
|
0
|
|
|
|
|
0
|
return 0; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
0
|
require App::tt; |
100
|
0
|
0
|
|
|
|
0
|
open my $POD, '<', $INC{'App/tt.pm'} or die "Cannot open App/tt.pm: $!"; |
101
|
0
|
|
|
|
|
0
|
while (<$POD>) { |
102
|
0
|
|
|
|
|
0
|
s/\b2020-01-01(T\d+:)/$today$1/g; # make "register" command easier to copy/paste |
103
|
0
|
0
|
|
|
|
0
|
push @help, $_ if /^=head2 $for/ ... /^=(head|cut)/; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# remove =head and =cut lines |
107
|
0
|
|
|
|
|
0
|
shift @help; |
108
|
0
|
|
|
|
|
0
|
pop @help; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
0
|
die "Could not find help for $for.\n" unless @help; |
111
|
0
|
|
|
|
|
0
|
$self->_say("@help"); |
112
|
0
|
|
|
|
|
0
|
return 0; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub command_log { |
116
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
117
|
0
|
|
|
|
|
0
|
my $res = $self->_log(@_); |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
for my $event (sort { $a->{start} <=> $b->{start} } @{$res->{log}}) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
120
|
0
|
|
|
|
|
0
|
my $start = $event->{start}; |
121
|
|
|
|
|
|
|
$self->_say( |
122
|
|
|
|
|
|
|
"%3s %2s %02s:%02s %5s %-$res->{max_project_chr}s %s", |
123
|
|
|
|
|
|
|
$start->month, |
124
|
|
|
|
|
|
|
$start->mday, |
125
|
|
|
|
|
|
|
$start->hour, |
126
|
|
|
|
|
|
|
$start->minute, |
127
|
|
|
|
|
|
|
$self->_hms_duration($event->{seconds}, 'hm'), |
128
|
|
|
|
|
|
|
$event->{project} || '---', |
129
|
0
|
|
0
|
|
|
0
|
join(',', @{$event->{tags}}), |
|
0
|
|
|
|
|
0
|
|
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$self->_diag( |
134
|
|
|
|
|
|
|
"\nTotal for %s events since %s: %s", |
135
|
|
|
|
|
|
|
$res->{events}, |
136
|
|
|
|
|
|
|
join(' ', $res->{when}->month, $res->{when}->year), |
137
|
0
|
|
|
|
|
0
|
$self->_hms_duration($res->{seconds}, 'hms'), |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
0
|
$self->_log_print_time_left($res); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
return 0; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub command_register { |
146
|
0
|
|
|
0
|
|
0
|
my ($self, $start, $stop) = @_; |
147
|
0
|
0
|
0
|
|
|
0
|
return $self->command_help('register') unless $start and $stop and $self->project; |
|
|
|
0
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
0
|
$start = $self->_time(str => $start); |
150
|
0
|
|
|
|
|
0
|
$stop = $self->_time(str => $stop, ref => $start); |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
my %event = ( |
153
|
|
|
|
|
|
|
__CLASS__ => 'App::TimeTracker::Data::Task', |
154
|
|
|
|
|
|
|
project => $self->project, |
155
|
|
|
|
|
|
|
start => $start, |
156
|
|
|
|
|
|
|
stop => $stop, |
157
|
|
|
|
|
|
|
user => scalar(getpwuid $<), |
158
|
|
|
|
|
|
|
tags => [$self->_tags], |
159
|
|
|
|
|
|
|
description => $self->description, |
160
|
|
|
|
|
|
|
); |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
0
|
my $trc_file = $self->_trc_path($self->project, $start); |
163
|
0
|
0
|
|
|
|
0
|
if (-e $trc_file) { |
164
|
0
|
|
|
|
|
0
|
$self->_diag("$trc_file already exists."); |
165
|
0
|
|
|
|
|
0
|
return 1; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
0
|
$self->_say('Registered "%s" at %s with duration %s', @event{qw(project start duration)}) |
169
|
|
|
|
|
|
|
if $self->_save(\%event); |
170
|
0
|
|
|
|
|
0
|
return $!; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub command_start { |
174
|
0
|
|
|
0
|
|
0
|
my ($self, @args) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
0
|
my $event = {}; |
177
|
0
|
|
|
|
|
0
|
$event->{start} = $self->_time(+(grep {/\d+\:/} @args)[0]); |
|
0
|
|
|
|
|
0
|
|
178
|
|
|
|
|
|
|
|
179
|
0
|
|
0
|
|
|
0
|
$event->{project} ||= $self->project; |
180
|
0
|
|
0
|
|
|
0
|
$event->{project} ||= (grep {/^[A-Za-z0-9-]+$/} @args)[0]; |
|
0
|
|
|
|
|
0
|
|
181
|
0
|
0
|
0
|
|
|
0
|
$event->{project} ||= basename(Cwd::getcwd) if -d '.git'; |
182
|
0
|
|
0
|
|
|
0
|
$event->{project} ||= $self->config('project'); |
183
|
0
|
0
|
|
|
|
0
|
return $self->command_help('start') unless $event->{project}; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
0
|
$self->_stop_previous(@args); |
186
|
0
|
0
|
|
|
|
0
|
return $! unless $self->_save($event); |
187
|
0
|
|
|
|
|
0
|
_spurt($event->{path} => $self->_path('previous')); |
188
|
|
|
|
|
|
|
$self->_say('Started working on project "%s" at %s.', |
189
|
0
|
|
|
|
|
0
|
$event->{project}, $event->{start}->hms(':')); |
190
|
0
|
|
|
|
|
0
|
return 0; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub command_stop { |
194
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
195
|
0
|
|
|
|
|
0
|
my $exit_code = $self->_stop_previous(@_); |
196
|
0
|
0
|
|
|
|
0
|
$self->_diag("No previous event to stop.") if $exit_code == 3; |
197
|
0
|
|
|
|
|
0
|
return $exit_code; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub command_status { |
201
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
202
|
0
|
|
|
|
|
0
|
my $event = $self->_get_previous_event; |
203
|
0
|
|
|
|
|
0
|
warn "[APP_TT] status $event->{path}\n" if DEBUG; |
204
|
|
|
|
|
|
|
|
205
|
0
|
0
|
|
|
|
0
|
if (!$event->{start}) { |
|
|
0
|
|
|
|
|
|
206
|
0
|
|
|
|
|
0
|
$self->_say('No event is being tracked.'); |
207
|
0
|
|
|
|
|
0
|
return 3; # No such process |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
elsif ($event->{stop}) { |
210
|
|
|
|
|
|
|
$self->_say('Stopped working on "%s" at %s after %s', |
211
|
0
|
|
|
|
|
0
|
$event->{project}, $event->{stop}, $self->_hms_duration($event->{seconds})); |
212
|
0
|
|
|
|
|
0
|
return 0; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
else { |
215
|
0
|
|
|
|
|
0
|
my $duration = $NOW - $event->{start} + $NOW->tzoffset; |
216
|
|
|
|
|
|
|
$self->_say('Been working on "%s", for %s', |
217
|
0
|
|
|
|
|
0
|
$event->{project}, $self->_hms_duration($duration, 'hms')); |
218
|
0
|
|
|
|
|
0
|
return 0; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub config { |
223
|
0
|
|
|
0
|
|
0
|
my ($self, $key) = @_; |
224
|
|
|
|
|
|
|
|
225
|
0
|
0
|
|
|
|
0
|
unless ($self->{config}) { |
226
|
0
|
|
|
|
|
0
|
local %ENV = %ENV; |
227
|
0
|
|
|
|
|
0
|
for my $path ($self->_path('config'), '.env') { |
228
|
0
|
0
|
|
|
|
0
|
next unless -r $path; |
229
|
0
|
0
|
|
|
|
0
|
open my $CONFIG, '<', $path or die qq{Can't open file "$path": $!}; |
230
|
0
|
|
|
|
|
0
|
while (my $line = readline $CONFIG) { |
231
|
0
|
0
|
|
|
|
0
|
next unless $line =~ m!^(?:TT_)?(\w+)=(\S+)!; |
232
|
0
|
|
|
|
|
0
|
$ENV{$1} = $2; |
233
|
0
|
|
|
|
|
0
|
$self->{config}{lc($1)} = $2; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
$self->{defaults} ||= { |
238
|
|
|
|
|
|
|
editor => $ENV{EDITOR} || 'nano', |
239
|
|
|
|
|
|
|
export_columns => $ENV{TT_COLUMNS} || 'date,project,hours,rounded,tags,description', |
240
|
|
|
|
|
|
|
hours_per_month => $ENV{TT_HOURS_PER_MONTH} || 0, |
241
|
|
|
|
|
|
|
min_time => $ENV{TT_MIN_TIME} || 300, |
242
|
0
|
|
0
|
|
|
0
|
round_up_at => $ENV{TT_ROUND_UP_AT} || 30, |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
243
|
|
|
|
|
|
|
}; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
0
|
|
|
0
|
return $self->{config}{$key} // $self->{defaults}{$key} // die "Missing option '$key'.\n"; |
|
|
|
0
|
|
|
|
|
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub _edit_with_editor { |
250
|
0
|
|
|
0
|
|
0
|
require File::Temp; |
251
|
0
|
|
|
|
|
0
|
my ($self, $trc_file) = @_; |
252
|
0
|
0
|
|
|
|
0
|
my ($event, $prev) = $trc_file ? ($self->_load($trc_file), 0) : ($self->_get_previous_event, 1); |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
0
|
|
|
0
|
$trc_file = $event->{path} //= 'Not found'; |
255
|
0
|
0
|
|
|
|
0
|
die "Could not find file to edit. ($event->{path})\n" unless $event->{start}; |
256
|
|
|
|
|
|
|
|
257
|
0
|
|
|
|
|
0
|
my $fh = File::Temp->new; |
258
|
0
|
|
|
|
|
0
|
printf $fh "# %s\n", $event->{path}; |
259
|
0
|
|
|
|
|
0
|
for my $k (qw(project tags description start stop user)) { |
260
|
0
|
0
|
|
|
|
0
|
$event->{$k} = join ', ', @{$event->{$k} || []} if $k eq 'tags'; |
|
0
|
0
|
|
|
|
0
|
|
261
|
0
|
0
|
0
|
|
|
0
|
$event->{$k} = $event->{$k}->datetime if $k eq 'start' or $k eq 'stop' and $event->{$k}; |
|
|
|
0
|
|
|
|
|
262
|
0
|
|
0
|
|
|
0
|
printf $fh "%-12s %s\n", "$k:", $event->{$k} // ''; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
0
|
close $fh; |
266
|
0
|
|
|
|
|
0
|
$self->_diag("Edit $event->{path}"); |
267
|
0
|
|
|
|
|
0
|
system $self->config('editor') => "$fh"; |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
0
|
for (split /\n/, _slurp("$fh")) { |
270
|
0
|
0
|
|
|
|
0
|
my ($k, $v) = /^(\w+)\s*:\s*(.+)/ or next; |
271
|
0
|
0
|
|
|
|
0
|
$v = [grep {length} split /[\s,]+/, $v] if $k eq 'tags'; |
|
0
|
|
|
|
|
0
|
|
272
|
0
|
0
|
|
|
|
0
|
$v = $self->_time(str => $v) if $k eq 'start'; |
273
|
0
|
0
|
|
|
|
0
|
$v = $self->_time(str => $v, ref => $event->{start}) if $k eq 'stop'; |
274
|
0
|
|
|
|
|
0
|
$event->{$k} = $v; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
0
|
0
|
|
|
|
0
|
return $! unless $self->_save($event); |
278
|
0
|
0
|
|
|
|
0
|
_spurt($event->{path} => $self->_path('previous')) if $prev; |
279
|
0
|
0
|
0
|
|
|
0
|
unlink $trc_file or die "rm $trc_file: $!" unless $trc_file eq $event->{path}; |
280
|
0
|
|
|
|
|
0
|
return 0; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
sub _diag { |
284
|
|
|
|
|
|
|
my ($self, $format) = (shift, shift); |
285
|
|
|
|
|
|
|
return warn "$format\n" unless @_; |
286
|
|
|
|
|
|
|
return warn sprintf "$format\n", @_; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
sub _say { |
290
|
|
|
|
|
|
|
my ($self, $format) = (shift, shift); |
291
|
|
|
|
|
|
|
print "$format\n" unless @_; |
292
|
|
|
|
|
|
|
print sprintf "$format\n", @_ if @_; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub _fill_log_days { |
296
|
0
|
|
|
0
|
|
0
|
my ($self, $last, $now) = @_; |
297
|
0
|
|
|
|
|
0
|
my $interval = int(($now - $last)->days); |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
map { |
300
|
0
|
|
|
|
|
0
|
my $t = $last + $_ * 86400; |
|
0
|
|
|
|
|
0
|
|
301
|
0
|
|
|
|
|
0
|
+{seconds => 0, start => $t, tags => [$t->day]} |
302
|
|
|
|
|
|
|
} 1 .. $interval; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
sub _get_previous_event { |
306
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
307
|
0
|
|
|
|
|
0
|
my $previous = $self->_path('previous'); |
308
|
0
|
0
|
|
|
|
0
|
return {path => ''} unless -r $previous; |
309
|
0
|
|
|
|
|
0
|
my $trc_file = _slurp($previous); # $ROOT/previous contains path to last .trc file |
310
|
0
|
0
|
|
|
|
0
|
return -r $trc_file ? $self->_load($trc_file) : {path => $trc_file}; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
sub _group_by_day { |
314
|
0
|
|
|
0
|
|
0
|
my ($self, $res) = @_; |
315
|
0
|
|
|
|
|
0
|
my $pl = 0; |
316
|
0
|
|
|
|
|
0
|
my %log; |
317
|
|
|
|
|
|
|
|
318
|
0
|
|
|
|
|
0
|
for my $e (@{$res->{log}}) { |
|
0
|
|
|
|
|
0
|
|
319
|
0
|
|
|
|
|
0
|
my $k = $e->{start}->ymd; |
320
|
0
|
|
0
|
|
|
0
|
$log{$k} ||= {%$e, seconds => 0}; |
321
|
0
|
|
|
|
|
0
|
$log{$k}{seconds} += $e->{seconds}; |
322
|
0
|
|
|
|
|
0
|
$log{$k}{_project}{$e->{project}} = 1; |
323
|
0
|
|
|
|
|
0
|
$log{$k}{_tags}{$_} = 1 for @{$e->{tags}}; |
|
0
|
|
|
|
|
0
|
|
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
$res->{log} = [ |
327
|
|
|
|
|
|
|
map { |
328
|
0
|
|
|
|
|
0
|
my $p = join ', ', keys %{$_->{_project}}; |
|
0
|
|
|
|
|
0
|
|
329
|
0
|
0
|
|
|
|
0
|
$pl = length $p if $pl < length $p; |
330
|
0
|
|
|
|
|
0
|
+{%$_, project => $p, tags => [keys %{$_->{_tags}}]}; |
|
0
|
|
|
|
|
0
|
|
331
|
0
|
|
|
|
|
0
|
} map { $log{$_} } sort keys %log |
|
0
|
|
|
|
|
0
|
|
332
|
|
|
|
|
|
|
]; |
333
|
|
|
|
|
|
|
|
334
|
0
|
|
|
|
|
0
|
$res->{max_project_chr} = $pl; |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
sub _hms_duration { |
338
|
0
|
|
|
0
|
|
0
|
my ($self, $duration, $sep) = @_; |
339
|
0
|
0
|
|
|
|
0
|
my $seconds = int(ref $duration ? $duration->seconds : $duration); |
340
|
0
|
|
|
|
|
0
|
my ($hours, $minutes); |
341
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
0
|
$hours = int($seconds / 3600); |
343
|
0
|
|
|
|
|
0
|
$seconds -= $hours * 3600; |
344
|
0
|
|
|
|
|
0
|
$minutes = int($seconds / 60); |
345
|
0
|
|
|
|
|
0
|
$seconds -= $minutes * 60; |
346
|
|
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
0
|
return sprintf '%s:%02s:%02s', $hours, $minutes, $seconds if !$sep; |
348
|
0
|
0
|
|
|
|
0
|
return sprintf '%2s:%02s', $hours, $minutes if $sep eq 'hm'; |
349
|
0
|
|
|
|
|
0
|
return sprintf '%sh %sm %ss', $hours, $minutes, $seconds; |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub _load { |
353
|
0
|
|
|
0
|
|
0
|
my ($self, $trc_file) = @_; |
354
|
0
|
|
|
|
|
0
|
my $trc = JSON::XS::decode_json(_slurp($trc_file)); |
355
|
0
|
|
|
|
|
0
|
$trc->{path} = $trc_file; |
356
|
0
|
0
|
|
|
|
0
|
$trc->{tags} = [map { split /\s*,\s*/, $_ } @{$trc->{tags} || []}]; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
357
|
0
|
|
|
|
|
0
|
$trc->{$_} = $self->_time($trc->{$_}) for grep { $trc->{$_} } qw(start stop); |
|
0
|
|
|
|
|
0
|
|
358
|
0
|
|
|
|
|
0
|
return $trc; |
359
|
|
|
|
|
|
|
} |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
sub _log { |
362
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
363
|
0
|
|
|
|
|
0
|
my $tags = join ',', $self->_tags; |
364
|
0
|
|
0
|
|
|
0
|
my @project_re = map {qr{^$_\b}} split /,/, $self->project || '.+'; |
|
0
|
|
|
|
|
0
|
|
365
|
|
|
|
|
|
|
|
366
|
0
|
|
|
|
|
0
|
my $res = {events => 0, log => [], max_project_chr => 0, seconds => 0}; |
367
|
|
|
|
|
|
|
|
368
|
0
|
|
|
|
|
0
|
for (@_) { |
369
|
0
|
0
|
0
|
|
|
0
|
/^(-\d+)(m|y|month|year)$/ and ($res->{start_at} = $1 and $res->{interval} = $2); |
370
|
0
|
0
|
0
|
|
|
0
|
/^(-\d+)$/ and $res->{start_at} ||= $1; |
371
|
0
|
0
|
0
|
|
|
0
|
/^(month|year)/ and $res->{interval} ||= $1; |
372
|
0
|
0
|
0
|
|
|
0
|
/^--fill/ and $res->{fill} ||= 1; |
373
|
|
|
|
|
|
|
} |
374
|
|
|
|
|
|
|
|
375
|
0
|
|
0
|
|
|
0
|
$res->{fill} ||= 0; |
376
|
0
|
|
0
|
|
|
0
|
$res->{interval} ||= 'month'; |
377
|
0
|
|
0
|
|
|
0
|
$res->{start_at} ||= 0; |
378
|
|
|
|
|
|
|
|
379
|
0
|
0
|
|
|
|
0
|
if ($res->{interval} =~ m!^y!) { |
380
|
0
|
|
|
|
|
0
|
$res->{when} = $self->_time(Y => $NOW->year + $res->{start_at}, m => 1, d => 1); |
381
|
0
|
|
|
|
|
0
|
$res->{path} = $self->_path($res->{when}->year); |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
else { |
384
|
0
|
|
|
|
|
0
|
$res->{when} = $self->_time(m => $NOW->mon + $res->{start_at}, d => 1); |
385
|
0
|
|
|
|
|
0
|
$res->{path} = $self->_path($res->{when}->year, sprintf '%02s', $res->{when}->mon); |
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
-d $res->{path} and find { |
389
|
|
|
|
|
|
|
no_chdir => 0, |
390
|
|
|
|
|
|
|
wanted => sub { |
391
|
0
|
0
|
|
0
|
|
0
|
my ($date, $hms, $project) = /^(\d+)-(\d+)_(.*)\.trc$/ or return; |
392
|
0
|
|
|
|
|
0
|
my $event = $self->_load($_); |
393
|
0
|
0
|
0
|
|
|
0
|
return if @project_re and !grep { $event->{project} =~ $_ } @project_re; |
|
0
|
|
|
|
|
0
|
|
394
|
0
|
0
|
0
|
|
|
0
|
return if $tags and !grep { $tags =~ /\b$_\b/ } @{$event->{tags}}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
395
|
0
|
|
0
|
|
|
0
|
$event->{stop} ||= $NOW + $NOW->tzoffset; |
396
|
0
|
|
0
|
|
|
0
|
$event->{seconds} ||= $event->{stop} - $event->{start}; |
397
|
0
|
|
|
|
|
0
|
push @{$res->{log}}, |
398
|
0
|
|
|
|
|
0
|
$self->_fill_log_days(@{$res->{log}} ? $res->{log}[-1]{start} : $res->{when}, |
399
|
|
|
|
|
|
|
$event->{start}) |
400
|
0
|
0
|
|
|
|
0
|
if $res->{fill}; |
|
|
0
|
|
|
|
|
|
401
|
0
|
|
|
|
|
0
|
pop @{$res->{log}} |
402
|
0
|
|
|
|
|
0
|
if @{$res->{log}} |
403
|
|
|
|
|
|
|
and !$res->{log}[-1]{project} |
404
|
0
|
0
|
0
|
|
|
0
|
and $res->{log}[-1]{start}->mday == $event->{start}->mday; |
|
|
|
0
|
|
|
|
|
405
|
0
|
|
|
|
|
0
|
push @{$res->{log}}, $event; |
|
0
|
|
|
|
|
0
|
|
406
|
|
|
|
|
|
|
$res->{max_project_chr} = length $event->{project} |
407
|
0
|
0
|
|
|
|
0
|
if $res->{max_project_chr} < length $event->{project}; |
408
|
0
|
|
|
|
|
0
|
$res->{events}++; |
409
|
0
|
|
|
|
|
0
|
$res->{seconds} += $event->{seconds}; |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
}, |
412
|
0
|
0
|
|
|
|
0
|
$res->{path}; |
413
|
|
|
|
|
|
|
|
414
|
0
|
0
|
0
|
|
|
0
|
if (my $method = $self->can(sprintf '_group_by_%s', $self->group_by || 'nothing')) { |
415
|
0
|
|
|
|
|
0
|
$self->$method($res); |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
0
|
|
|
|
|
0
|
return $res; |
419
|
|
|
|
|
|
|
} |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
sub _log_print_time_left { |
422
|
0
|
|
|
0
|
|
0
|
my ($self, $res) = @_; |
423
|
0
|
0
|
0
|
|
|
0
|
return unless $self->config('hours_per_month') and $res->{interval} eq 'month'; |
424
|
|
|
|
|
|
|
|
425
|
0
|
|
|
|
|
0
|
my $start = $self->_time(d => 1, H => 0, M => 0, S => 0); |
426
|
0
|
|
|
|
|
0
|
my $end = $self->_time(d => 1, m => $start->mon + 1, H => 0, M => 0, S => 0); |
427
|
0
|
|
|
|
|
0
|
my $total_days = 0; |
428
|
0
|
|
|
|
|
0
|
my $worked_days = 0; |
429
|
0
|
|
|
|
|
0
|
while ($start < $end) { |
430
|
0
|
0
|
0
|
|
|
0
|
if ($start->day_of_week != 0 and $start->day_of_week != 6) { |
431
|
0
|
0
|
|
|
|
0
|
$worked_days++ if $start < $NOW; |
432
|
0
|
|
|
|
|
0
|
$total_days++; |
433
|
|
|
|
|
|
|
} |
434
|
0
|
|
|
|
|
0
|
$start += ONE_DAY; |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
0
|
0
|
|
|
|
0
|
my $remaining_days = $total_days - $worked_days + ($NOW->hour > 12 ? 0 : 1); |
438
|
0
|
|
|
|
|
0
|
my $total_seconds = $self->config('hours_per_month') * 3600; |
439
|
0
|
|
|
|
|
0
|
my $remaining_seconds = $total_seconds - $res->{seconds}; |
440
|
|
|
|
|
|
|
|
441
|
0
|
0
|
|
|
|
0
|
$self->_diag( |
|
|
0
|
|
|
|
|
|
442
|
|
|
|
|
|
|
'Remaining this month: %sd, %sh/d.', |
443
|
|
|
|
|
|
|
$remaining_days > 0 ? $remaining_days : 0, |
444
|
|
|
|
|
|
|
$self->_hms_duration($remaining_seconds / ($remaining_days <= 0 ? 1 : $remaining_days), 'hm'), |
445
|
|
|
|
|
|
|
); |
446
|
|
|
|
|
|
|
} |
447
|
|
|
|
|
|
|
|
448
|
0
|
|
|
0
|
|
0
|
sub _path { File::Spec->catfile(shift->_root, @_) } |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
sub _root { |
451
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
452
|
0
|
0
|
|
|
|
0
|
return $self->{root} if $self->{root}; |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
my $root |
455
|
|
|
|
|
|
|
= $ENV{TT_HOME} |
456
|
|
|
|
|
|
|
|| $ENV{TIMETRACKER_HOME} |
457
|
0
|
|
0
|
|
|
0
|
|| File::Spec->catdir(File::HomeDir->my_home || File::Spec->curdir, '.TimeTracker'); |
458
|
|
|
|
|
|
|
|
459
|
0
|
|
0
|
|
|
0
|
return $self->{root} = readlink($root) || $root; |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
sub _mass_edit { |
463
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
464
|
|
|
|
|
|
|
|
465
|
0
|
0
|
0
|
|
|
0
|
$self->year($NOW->year) if $self->month and !$self->year; |
466
|
0
|
|
|
|
|
0
|
my $path = $self->_root; |
467
|
0
|
0
|
|
|
|
0
|
$path = File::Spec->catdir($path, $self->year) if $self->year; |
468
|
0
|
0
|
|
|
|
0
|
$path = File::Spec->catdir($path, sprintf '%02s', $self->month) if $self->month; |
469
|
|
|
|
|
|
|
|
470
|
0
|
|
|
|
|
0
|
my $re = ''; |
471
|
0
|
|
0
|
|
|
0
|
$re .= sprintf '(%s)', $self->year || '\d{4}'; # (year) = ($1) |
472
|
0
|
|
0
|
|
|
0
|
$re .= sprintf '(%02s)', $self->month || '\d{2}'; # (month) = ($2) |
473
|
0
|
|
|
|
|
0
|
$re .= '(\d{2})-(\d+)_'; # (day, hms) = ($3, $4) |
474
|
0
|
|
0
|
|
|
0
|
$re .= sprintf '(%s)', $self->project || '[^.]+'; # (project) = ($5) |
475
|
0
|
|
|
|
|
0
|
$re .= '\.trc'; |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
# Edit all files with code from STDIN |
478
|
0
|
|
|
|
|
0
|
my $code = ''; |
479
|
0
|
0
|
0
|
|
|
0
|
if (!-t STDIN or !($self->year or $self->month)) { |
|
|
|
0
|
|
|
|
|
480
|
0
|
|
|
|
|
0
|
$code .= $_ while ; |
481
|
0
|
0
|
|
|
|
0
|
$code = "sub {$code}" unless $code =~ /^\s*sub\b/s; |
482
|
0
|
0
|
|
|
|
0
|
$code = eval "use 5.10.1;use strict;use warnings;$code" |
483
|
|
|
|
|
|
|
or die "Could not compile code from STDIN: $@\n"; |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
0
|
|
|
|
|
0
|
my @found; |
487
|
|
|
|
|
|
|
find( |
488
|
|
|
|
|
|
|
{ |
489
|
|
|
|
|
|
|
no_chdir => 0, |
490
|
|
|
|
|
|
|
wanted => sub { |
491
|
0
|
0
|
|
0
|
|
0
|
return unless $_ =~ m!^$re$!; |
492
|
0
|
|
|
|
|
0
|
my %info = (file => Cwd::abs_path($_)); |
493
|
0
|
|
|
|
|
0
|
@info{qw(year month day date hms project)} = ($1, $2, $3, "$1$2$3", $4, $5); |
494
|
0
|
|
|
|
|
0
|
push @found, \%info; |
495
|
|
|
|
|
|
|
}, |
496
|
|
|
|
|
|
|
}, |
497
|
0
|
|
|
|
|
0
|
$path, |
498
|
|
|
|
|
|
|
); |
499
|
|
|
|
|
|
|
|
500
|
0
|
|
|
|
|
0
|
for my $found (sort { $a->{file} cmp $b->{file} } @found) { |
|
0
|
|
|
|
|
0
|
|
501
|
0
|
0
|
|
|
|
0
|
$self->command_edit($found->{file}), next unless $code; |
502
|
0
|
|
|
|
|
0
|
my $event = $self->_load($found->{file}); |
503
|
0
|
|
|
|
|
0
|
local %_ = %$found; |
504
|
0
|
0
|
0
|
|
|
0
|
$self->_save($event) if $code and $self->$code($event); |
505
|
0
|
0
|
0
|
|
|
0
|
unlink $found->{file} or die "rm $found->{file}: $!" unless $found->{file} eq $event->{path}; |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
|
508
|
0
|
|
|
|
|
0
|
return 0; |
509
|
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub _save { |
512
|
0
|
|
|
0
|
|
0
|
my ($self, $event) = @_; |
513
|
|
|
|
|
|
|
|
514
|
0
|
|
0
|
|
|
0
|
$event->{__CLASS__} ||= 'App::TimeTracker::Data::Task'; |
515
|
0
|
|
0
|
|
|
0
|
$event->{description} ||= $self->description // ''; |
|
|
|
0
|
|
|
|
|
516
|
0
|
|
0
|
|
|
0
|
$event->{project} ||= $self->project; |
517
|
0
|
|
0
|
|
|
0
|
$event->{seconds} ||= 0; |
518
|
0
|
0
|
0
|
|
|
0
|
$event->{tags} ||= [uniq @{$event->{tags} || []}, $self->_tags]; |
|
0
|
|
|
|
|
0
|
|
519
|
0
|
|
0
|
|
|
0
|
$event->{user} ||= scalar(getpwuid $<); |
520
|
|
|
|
|
|
|
|
521
|
0
|
0
|
0
|
|
|
0
|
if (my $duration = $event->{stop} && $event->{start} && $event->{stop} - $event->{start}) { |
522
|
0
|
|
|
|
|
0
|
$event->{seconds} = $duration->seconds; |
523
|
|
|
|
|
|
|
} |
524
|
0
|
0
|
0
|
|
|
0
|
if ($event->{stop} and $event->{seconds} < $self->config('min_time')) { |
525
|
0
|
|
|
|
|
0
|
$self->_diag('Too short duration (%s)', $self->_hms_duration($event->{duration})); |
526
|
0
|
|
|
|
|
0
|
$! = 52; |
527
|
0
|
|
|
|
|
0
|
return 0; |
528
|
|
|
|
|
|
|
} |
529
|
|
|
|
|
|
|
|
530
|
0
|
|
|
|
|
0
|
my %event = %$event; |
531
|
0
|
|
|
|
|
0
|
delete $event{path}; |
532
|
0
|
0
|
|
|
|
0
|
$event{start} = $event->{start}->datetime if $event->{start}; |
533
|
0
|
0
|
|
|
|
0
|
$event{stop} = $event->{stop}->datetime if $event->{stop}; |
534
|
0
|
|
|
|
|
0
|
$event{duration} = $self->_hms_duration($event{seconds}); # Used by App::TimeTracker::Data::Task |
535
|
|
|
|
|
|
|
|
536
|
0
|
|
|
|
|
0
|
$event->{path} = $self->_trc_path($event->{project}, $event->{start}); |
537
|
0
|
0
|
|
|
|
0
|
File::Path::make_path(dirname($event->{path})) unless -d dirname($event->{path}); |
538
|
0
|
|
|
|
|
0
|
_spurt(JSON::XS::encode_json(\%event) => $event->{path}); |
539
|
0
|
|
|
|
|
0
|
return 1; |
540
|
|
|
|
|
|
|
} |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
sub _tags { |
543
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
544
|
0
|
0
|
|
|
|
0
|
return $self->tag(shift) if @_; |
545
|
0
|
0
|
|
|
|
0
|
return uniq map { split /,/, $_ } @{$self->tag || []}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
# From Mojo::Util |
549
|
|
|
|
|
|
|
sub _slurp { |
550
|
0
|
|
|
0
|
|
0
|
my $path = shift; |
551
|
0
|
0
|
|
|
|
0
|
die qq{Can't open file "$path": $!} unless open my $file, '<', $path; |
552
|
0
|
|
|
|
|
0
|
my $content = ''; |
553
|
0
|
|
|
|
|
0
|
while ($file->sysread(my $buffer, 131072, 0)) { $content .= $buffer } |
|
0
|
|
|
|
|
0
|
|
554
|
0
|
|
|
|
|
0
|
$content =~ s!\s*$!!; |
555
|
0
|
|
|
|
|
0
|
return $content; |
556
|
|
|
|
|
|
|
} |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
# From Mojo::Util |
559
|
|
|
|
|
|
|
sub _spurt { |
560
|
0
|
|
|
0
|
|
0
|
my ($content, $path) = @_; |
561
|
0
|
0
|
|
|
|
0
|
die qq{Can't open file "$path": $!} unless open my $file, '>', $path; |
562
|
0
|
0
|
|
|
|
0
|
die qq{Can't write to file "$path": $!} unless defined $file->syswrite($content); |
563
|
0
|
|
|
|
|
0
|
return $content; |
564
|
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
sub _stop_previous { |
567
|
0
|
|
|
0
|
|
0
|
my ($self, @args) = @_; |
568
|
|
|
|
|
|
|
|
569
|
0
|
|
|
|
|
0
|
my $event = $self->_get_previous_event; |
570
|
0
|
0
|
0
|
|
|
0
|
return 3 if !$event->{start} or $event->{stop}; # 3 == No such process |
571
|
|
|
|
|
|
|
|
572
|
0
|
|
|
|
|
0
|
$event->{stop} = $self->_time(+(grep {/\d+\:/} @args)[0]); |
|
0
|
|
|
|
|
0
|
|
573
|
|
|
|
|
|
|
|
574
|
0
|
|
|
|
|
0
|
my $duration = $event->{stop} - $event->{start}; |
575
|
0
|
0
|
|
|
|
0
|
if ($duration->seconds < $self->config('min_time')) { |
576
|
|
|
|
|
|
|
$self->_say( |
577
|
0
|
|
|
|
|
0
|
qq(Dropping log event for "%s" since worked less than @{[$self->config('min_time')]}s.), |
578
|
0
|
|
|
|
|
0
|
$event->{project}); |
579
|
0
|
0
|
|
|
|
0
|
unlink $event->{path} or die "rm $event->{path}: $!"; |
580
|
0
|
|
|
|
|
0
|
return 52; |
581
|
|
|
|
|
|
|
} |
582
|
|
|
|
|
|
|
|
583
|
0
|
0
|
|
|
|
0
|
if ($self->_save($event)) { |
584
|
|
|
|
|
|
|
$self->_say('Stopped working on "%s" after %s', |
585
|
0
|
|
|
|
|
0
|
$event->{project}, $self->_hms_duration($duration, 'hms')); |
586
|
0
|
|
|
|
|
0
|
return 0; |
587
|
|
|
|
|
|
|
} |
588
|
|
|
|
|
|
|
|
589
|
0
|
|
0
|
|
|
0
|
return $! || 1; |
590
|
|
|
|
|
|
|
} |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
sub _time { |
593
|
7
|
|
|
7
|
|
19787
|
my $self = shift; |
594
|
7
|
50
|
|
|
|
27
|
my %t = @_ == 1 ? (str => shift) : (@_); |
595
|
|
|
|
|
|
|
|
596
|
7
|
50
|
|
|
|
19
|
if ($t{str}) { |
597
|
7
|
|
|
|
|
38
|
my ($ymd, $hms) = split /[T\s]/, $t{str}; |
598
|
7
|
100
|
66
|
|
|
29
|
($hms, $ymd) = ($ymd, '') if !$hms and $ymd =~ m!:!; |
599
|
7
|
100
|
|
|
|
28
|
$t{Y} = $1 if $ymd =~ s!(\d{4})!!; |
600
|
7
|
100
|
|
|
|
28
|
$t{m} = $1 if $ymd =~ s!0?(\d{1,2})-(\d+)!$2!; |
601
|
7
|
100
|
|
|
|
31
|
$t{d} = $1 if $ymd =~ s!0?(\d{1,2})!!; |
602
|
7
|
50
|
|
|
|
31
|
$t{H} = $1 if $hms =~ s!0?(\d{1,2})!!; |
603
|
7
|
100
|
|
|
|
24
|
$t{M} = $1 if $hms =~ s!0?(\d{1,2})!!; |
604
|
7
|
100
|
|
|
|
23
|
$t{S} = $1 if $hms =~ s!0?(\d{1,2})!!; |
605
|
|
|
|
|
|
|
} |
606
|
|
|
|
|
|
|
|
607
|
7
|
|
33
|
|
|
89
|
my $ref = $t{ref} || $NOW; |
608
|
7
|
50
|
|
|
|
321
|
$ref = $self->_time($ref) unless ref $ref; |
609
|
7
|
|
66
|
|
|
33
|
$t{Y} ||= $ref->year; |
610
|
7
|
|
66
|
|
|
45
|
$t{m} //= $ref->mon; |
611
|
7
|
|
66
|
|
|
33
|
$t{d} //= $ref->mday; |
612
|
7
|
50
|
33
|
|
|
35
|
$t{S} //= defined $t{H} || defined $t{M} ? 0 : $ref->second; |
|
|
|
66
|
|
|
|
|
613
|
7
|
50
|
66
|
|
|
20
|
$t{M} //= defined $t{H} ? 0 : $ref->min; |
614
|
7
|
|
33
|
|
|
11
|
$t{H} //= $ref->hour; |
615
|
|
|
|
|
|
|
|
616
|
7
|
50
|
|
|
|
14
|
@t{qw(m Y)} = (12 - $t{m}, $t{Y} - 1) if $t{m} <= 0; |
617
|
7
|
50
|
|
|
|
15
|
@t{qw(m Y)} = (1, $t{Y} + 1) if $t{m} > 12; |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
eval { |
620
|
7
|
|
|
|
|
34
|
$t{iso} = sprintf '%s-%02s-%02sT%02s:%02s:%02s', @t{qw(Y m d H M S)}; |
621
|
7
|
|
|
|
|
29
|
$t{tp} = Time::Piece->strptime("$t{iso}+0000", '%Y-%m-%dT%H:%M:%S%z'); |
622
|
7
|
100
|
|
|
|
11
|
} or do { |
623
|
1
|
|
|
|
|
27
|
$@ =~ s!\r?\n$!!; |
624
|
1
|
|
|
|
|
6
|
$@ =~ s!\sat\s\W+.*!! unless DEBUG; |
625
|
1
|
50
|
|
|
|
13
|
die "Invalid time: $t{str} ($t{iso}): $@\n" if $t{str}; |
626
|
0
|
|
|
|
|
0
|
die "Invalid time: $t{iso}: $@\n"; |
627
|
|
|
|
|
|
|
}; |
628
|
|
|
|
|
|
|
|
629
|
6
|
|
|
|
|
565
|
return $t{tp}; |
630
|
|
|
|
|
|
|
} |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
sub _trc_path { |
633
|
0
|
|
|
0
|
|
|
my ($self, $project, $t) = @_; |
634
|
0
|
|
|
|
|
|
my $month = sprintf '%02s', $t->mon; |
635
|
0
|
|
|
|
|
|
my $file; |
636
|
|
|
|
|
|
|
|
637
|
0
|
|
|
|
|
|
$project =~ s!\W!_!g; |
638
|
0
|
|
|
|
|
|
$file = sprintf '%s-%s_%s.trc', $t->ymd(''), $t->hms(''), $project; |
639
|
|
|
|
|
|
|
|
640
|
0
|
|
|
|
|
|
return $self->_path($t->year, $month, $file); |
641
|
|
|
|
|
|
|
} |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
app { |
644
|
|
|
|
|
|
|
my ($self, $command) = (shift, shift); |
645
|
|
|
|
|
|
|
return $self->command_help($ENV{APP_TT_HELP}) if $ENV{APP_TT_HELP}; |
646
|
|
|
|
|
|
|
return $self->command_status(@_) if !$command or $command eq 'status'; |
647
|
|
|
|
|
|
|
my $method = $self->can("command_$command"); |
648
|
|
|
|
|
|
|
return $self->$method(@_) if $method; |
649
|
|
|
|
|
|
|
die qq(Unknown command "$command".\n); |
650
|
|
|
|
|
|
|
}; |