@@ -4,6 +4,8 @@ class Stats {
44 private CLIParser $ parser ;
55 private CLIOutput $ output ;
66
7+ private bool $ todayview = false ;
8+
79 public function __construct (CLIParser $ parser , CLIOutput $ output ) {
810 $ this ->parser = $ parser ;
911 $ this ->output = $ output ;
@@ -23,15 +25,19 @@ private function parseCommands(array $commands) : void {
2325 switch ( $ commands [0 ] ) {
2426 case "day " :
2527 $ this ->backUntil (time () - 86400 , array_slice ($ commands , 1 ));
28+ break ;
2629 case "week " :
2730 $ this ->backUntil (time () - 604800 , array_slice ($ commands , 1 ));
31+ break ;
2832 case "month " :
2933 $ this ->backUntil (time () - 2628000 , array_slice ($ commands , 1 ));
34+ break ;
3035 case "all " :
3136 $ this ->backUntil (0 , array_slice ($ commands , 1 ));
3237 break ;
3338 case "today " :
3439 default :
40+ $ this ->todayview = true ;
3541 $ this ->backUntil (strtotime ("today " ), array_slice ($ commands , 1 ));
3642 }
3743 }
@@ -74,12 +80,96 @@ private function backUntil(int $timestamp, array $f) : void {
7480 $ this ->printDataset ($ s ->getAllDatasets ());
7581 }
7682
77- private function printDataset (array $ data ){
78- print_r ($ data );
79- /**
80- * ToDo
81- */
83+ private function printDataset (array $ data ) : void {
84+ $ combi = array ();
85+ foreach ( $ data as $ d ){
86+ $ key = $ d ['category ' ] . '++ ' . $ d ['name ' ];
87+ if ( !isset ($ combi [$ key ])){
88+ $ combi [$ key ] = array (
89+ 'category ' => $ d ['category ' ],
90+ 'name ' => $ d ['name ' ],
91+ 'duration ' => 0 ,
92+ 'times ' => 0
93+ );
94+ }
95+ $ combi [$ key ]['times ' ]++;
96+ $ combi [$ key ]['duration ' ] += $ d ['duration ' ];
97+ }
98+ $ combi = array_values ($ combi );
99+
100+ $ table = array ();
101+ foreach ( $ combi as $ d ){
102+ $ table [] = array (
103+ 'Category ' => $ d ['category ' ],
104+ 'Name ' => $ d ['name ' ],
105+ 'Time ' => $ d ['duration ' ],
106+ 'Work Items ' => str_pad ($ d ['times ' ], 4 , " " , STR_PAD_LEFT )
107+ );
108+ }
109+
110+ array_multisort (
111+ array_column ( $ table , 'Category ' ), SORT_ASC ,
112+ array_column ( $ table , 'Time ' ), SORT_DESC ,
113+ array_column ( $ table , 'Name ' ), SORT_ASC ,
114+ $ table
115+ );
116+
117+ foreach ( $ table as &$ d ){
118+ $ d ['Time ' ] = $ this ->secToTime ($ d ['Time ' ]);
119+ }
120+
121+ $ this ->output ->table ($ table );
122+
123+ if ( $ this ->todayview ){
124+ $ this ->printTodayView ($ data );
125+ }
126+ }
127+
128+ private function secToTime (int $ t ) : string {
129+ return str_pad (
130+ ($ t >= 3600 ? intval ($ t /3600 ) . 'h ' : '' ) .
131+ str_pad (
132+ intval (($ t % 3600 ) / 60 ) . 'm ' ,
133+ 3 ,
134+ " " ,
135+ STR_PAD_LEFT
136+ ),
137+ 8 ,
138+ " " ,
139+ STR_PAD_LEFT
140+ );
141+ }
142+
143+ private function printTodayView (array $ data ) : void {
144+ array_multisort (
145+ array_column ( $ data , 'begin ' ), SORT_ASC ,
146+ $ data
147+ );
148+
149+ $ table = array ();
150+ $ i = 0 ;
151+ $ lastval = null ;
152+ foreach ( $ data as $ d ){
153+ if ( $ lastval === $ d ['category ' ] . '++ ' . $ d ['name ' ] ){
154+ $ table [$ i -1 ]['Time ' ] += $ d ['duration ' ];
155+ }
156+ else {
157+ $ table [$ i ++] = array (
158+ 'Begin ' => date ('H:i ' , $ d ['begin ' ]),
159+ 'Category ' => $ d ['category ' ],
160+ 'Name ' => $ d ['name ' ],
161+ 'Time ' => $ d ['duration ' ]
162+ );
163+ }
164+ $ lastval = $ d ['category ' ] . '++ ' . $ d ['name ' ];
165+ }
166+
167+ foreach ( $ table as &$ d ){
168+ $ d ['Time ' ] = $ this ->secToTime ($ d ['Time ' ]);
169+ }
170+
171+ $ this ->output ->table ($ table );
82172 }
83173
84174}
85- ?>
175+ ?>
0 commit comments