File Coverage

blib/lib/App/bif/wlog.pm
Criterion Covered Total %
statement 22 68 32.3
branch 1 26 3.8
condition n/a
subroutine 7 9 77.7
pod 1 3 33.3
total 31 106 29.2


line stmt bran cond sub pod time code
1             package App::bif::wlog;
2 1     1   1637 use strict;
  1         2  
  1         39  
3 1     1   4 use warnings;
  1         1  
  1         36  
4 1     1   3 use utf8;
  1         1  
  1         6  
5 1     1   21 use Bif::Mo;
  1         1  
  1         6  
6 1     1   624 use DBIx::ThinSQL qw/case coalesce sq qv/;
  1         21489  
  1         7  
7 1     1   568 use Text::FormatTable;
  1         2404  
  1         849  
8              
9             our $VERSION = '0.1.5_5';
10             extends 'App::bif';
11              
12             sub run {
13 1     1 1 2 my $self = shift;
14              
15 1         1 my $id;
16 1 50       3 if ( my $str = $self->opts->{identity} ) {
17 0 0       0 unless ( $str eq '-' ) {
18 0         0 my $iinfo = $self->get_node( $str, 'identity' );
19 0         0 $id = $iinfo->{id};
20             }
21             }
22             else {
23 1         7 $id = $self->db->xval(
24             select => 'b.identity_id',
25             from => 'bifkv b',
26             where => { key => 'self' },
27             );
28             }
29              
30 0 0         return $self->work_log_long($id) if $self->opts->{long};
31 0           return $self->work_log_short($id);
32             }
33              
34             sub work_log_long {
35 0     0 0   my $self = shift;
36 0           my $identity_id = shift;
37 0           my $db = $self->db;
38              
39 0           my $sth = $db->xprepare(
40             with => 'src',
41             as => sq(
42             select => [ 'wb.id AS id', 'wb.stop - wb.start AS delta', ],
43             from => 'work_buffers wb',
44             ),
45             select => [
46             'wb.node_id',
47             q{strftime('%Y-%m-%d %H:%M:%S', }
48             . q{ wb.gtime_start, 'unixepoch') AS start},
49             q{wb.start_comment AS start_comment},
50             q{strftime('%Y-%m-%d %H:%M:%S', }
51             . q{ wb.gtime_stop, 'unixepoch') AS stop},
52             q{wb.stop_comment AS stop_comment},
53             q{ printf('%+0.2d:%0.2d:%0.2d', }
54             . q{ src.delta / 3600, }
55             . q{ (src.delta - 3600 * (src.delta / 3600)) / 60, }
56             . q{ src.delta % 60 ) AS delta},
57             q{COALESCE(printf('%s %s [%s]', n.kind, n.id, n.path),'') AS path},
58             ],
59             from => 'src',
60             inner_join => 'work_buffers wb',
61             on => 'wb.id = src.id',
62             left_join => 'nodes n',
63             on => 'n.id = wb.node_id',
64             order_by => 'wb.id DESC',
65             );
66 0           $sth->execute;
67              
68 0           my ( $yellow, $bold, $reset ) = $self->colours( 'yellow', 'bold', 'reset' );
69              
70 0           $self->start_pager;
71              
72 0           while ( my $ref = $sth->hashref ) {
73 0           my $table = Text::FormatTable->new(' l l');
74              
75 0           $table->row( $yellow . 'Work:', $ref->{path} . $reset );
76 0           $table->row( 'Start:', $ref->{start} );
77 0           $table->row( 'Stop:', $ref->{stop} );
78 0           $table->row( 'Delta:', $ref->{delta} );
79             $table->row( 'StartComment:', $ref->{start_comment} )
80 0 0         if $ref->{start_comment};
81             $table->row( 'StopComment:', $ref->{stop_comment} )
82 0 0         if $ref->{stop_comment};
83              
84 0           print $table->render, "\n";
85             }
86              
87 0           return $self->ok('WorkLogLong');
88             }
89              
90             sub work_log_short {
91 0     0 0   my $self = shift;
92 0           my $identity_id = shift;
93 0           my $db = $self->db;
94 0           my ( $yellow, $bold, $reset ) = $self->colours( 'yellow', 'bold', 'reset' );
95              
96 0 0         my $sth = $db->xprepare(
    0          
97             with => 'worked',
98             as => sq(
99              
100             # Grab stuff from the work_buffers table
101             select => [
102             'wb.gtime_start AS start',
103             q{strftime('%Y-%m-%d', }
104             . q{ wb.gtime_start, 'unixepoch') AS start_ymd},
105             'wb.gtime_stop AS stop',
106             'wb.stop - wb.start AS delta',
107             'n.id AS id',
108             'i.shortname AS shortname',
109             'COALESCE(n.path,"-") AS path',
110             q{COALESCE(wb.start_comment || ', ' || wb.stop_comment,}
111             . q{wb.start_comment, wb.stop_comment, '-') AS comment},
112             'wb.billable AS billable',
113             case (
114             when => 'wb.billable',
115             then => qv("(${bold}unrec$reset)"),
116             else => "'-'",
117             )->as('change_id'),
118             ],
119             from => 'work_buffers wb',
120             left_join => 'nodes n',
121             on => 'n.id = wb.node_id',
122             $identity_id
123             ? (
124             inner_join => 'identities i',
125             on => { 'i.id' => $identity_id },
126             )
127             : (
128             inner_join => 'bifkv b',
129             on => { 'b.key' => 'self', },
130             inner_join => 'identities i',
131             on => 'i.id = b.identity_id',
132             ),
133              
134             # Grab stuff from the work_deltas table
135             union_all_select => [
136             'wd.gtime_start AS start',
137             q{strftime('%Y-%m-%d', }
138             . q{ wd.gtime_start, 'unixepoch') AS start_ymd},
139             'wd.gtime_stop AS stop',
140             'wd.stop - wd.start AS delta',
141             'n.id AS id',
142             'i.shortname AS shortname',
143             'n.path AS path',
144             q{COALESCE(wd.start_comment || ', ' || wd.stop_comment,}
145             . q{wd.start_comment, wd.stop_comment, '-') AS comment},
146             '0 AS billable',
147             'wd.change_id AS change_id',
148             ],
149             from => 'work_deltas wd',
150             inner_join => 'nodes n',
151             on => 'n.id = wd.node_id',
152             inner_join => 'changes c',
153             on => {
154             'c.id' => \'wd.change_id',
155             $identity_id ? ( 'c.identity_id' => $identity_id, ) : (),
156             },
157             inner_join => 'identities i',
158             on => 'i.id = c.identity_id',
159             ),
160              
161             # Sum the above grouped by day
162             ',' => 'worked_summary',
163             as => sq(
164             select => [
165             'w.start_ymd AS start_ymd',
166             'SUM(w.delta) AS delta',
167             "julianday(date('now','localtime'))"
168             . " - julianday(w.start_ymd) AS day"
169             ],
170             from => 'worked w',
171             group_by => [ 'start_ymd', 'day' ],
172             ),
173              
174             # Now format all of that nicely
175             select => [
176             'w.start_ymd AS start_ymd',
177             'w.start AS start',
178             "strftime('%H:%M', w.start, 'unixepoch') AS start_hm",
179             'w.stop AS stop',
180             "COALESCE(strftime('%H:%M', w.stop, 'unixepoch'),"
181             . "'') AS stop_hm",
182             q{ printf('%+0.2d:%0.2d', }
183             . q{ round(w.delta / 3600), }
184             . q{ (w.delta - 3600 * round(w.delta / 3600)) / 60)
185             AS delta_hm },
186             'w.shortname AS shortname',
187             'w.id AS id',
188             'w.path AS path',
189             'w.comment AS comment',
190             'w.billable AS billable',
191             'w.change_id AS change_id',
192             ],
193             from => 'worked w',
194              
195             # total/summary row
196             union_all_select => [
197             'ws.start_ymd AS start_ymd',
198             '-1 AS start',
199             '-1 AS start_hm',
200             '-1 AS stop',
201             '-1 AS stop_hm',
202             q{ printf('%+0.2d:%0.2d', }
203             . q{ round(ws.delta / 3600), }
204             . q{ (ws.delta -}
205             . q{ 3600 * round(ws.delta / 3600)) / 60) AS delta_hm},
206             '-1 AS shortname',
207             'NULL AS id',
208             'NULL AS path',
209             'ws.day AS comment',
210             'NULL AS billable',
211             'NULL AS change_id',
212             ],
213             from => 'worked_summary ws',
214             order_by => [ 'start_ymd DESC', 'start ASC', 'stop ASC' ]
215             );
216              
217 0           $sth->execute;
218 0           $self->start_pager;
219              
220 0           my $i = 0;
221 0           my $format = ' l l l r r ';
222 0           my @empty = ( '-', '', '', '', '' );
223 0           my $table = Text::FormatTable->new($format);
224 0           my $first = 1;
225              
226 0           while ( my $ref = $sth->hashref ) {
227              
228 0 0         if ( $ref->{start_hm} eq '-1' ) {
229              
230             # Every 200 rows or so spit out the table and start again.
231 0 0         if ( $i > 200 ) {
    0          
232 0           print $table->render( $self->term_width ), "\n";
233 0           $table = Text::FormatTable->new($format);
234 0           $i = 1;
235             }
236             elsif ($i) {
237 0           $table->row(@empty);
238             }
239              
240             $table->head(
241             $yellow . $ref->{start_ymd},
242             0 == $ref->{comment} ? 'Activity [today]'
243             : 1 == $ref->{comment} ? 'Activity [yesterday]'
244             : "Activity [$ref->{comment} days ago]",
245             'Topic',
246             'CID',
247 0 0         $ref->{delta_hm} . $reset
    0          
248             );
249              
250             }
251             else {
252             $table->row(
253             ( $ref->{stop_hm} ? '' : $bold )
254             . "$ref->{start_hm} - $ref->{stop_hm}",
255             $ref->{comment},
256             $ref->{path},
257             $ref->{change_id},
258 0 0         $ref->{delta_hm} . $reset,
259              
260             );
261             }
262              
263 0           $i++;
264             }
265              
266 0           print $table->render( $self->term_width ), "\n";
267              
268 0           return $self->ok('WorkLogShort');
269             }
270              
271             1;
272             __END__