-
Notifications
You must be signed in to change notification settings - Fork 1
/
logtimes.pl
405 lines (317 loc) · 11.7 KB
/
logtimes.pl
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
#!/usr/bin/perl
# TODO: Kernarbeitszeiten beachten
$|++;
use strict;
use warnings;
use POSIX qw/strftime/;
use Git::Repository;
use Text::CSV;
use DateTime;
use DateTime::Format::Strptime;
use Date::Calc qw/Day_of_Week Days_in_Month/;
use Date::Holidays;
use Data::Dumper;
use Term::ANSIColor;
use Text::Table;
use List::Util qw(min max);
use autodie;
use Memoize::Storable;
use Memoize;
sub get_git_log_between_dates {
my $repo_path = shift;
my $start_time = shift;
my $end_of_day = shift;
my $repo = Git::Repository->new(work_tree => $repo_path);
my @commits = $repo->run('log', '--date=local', '--pretty=%at,%h', "--since=$start_time", "--until=$end_of_day");
return @commits;
}
my $memoize_persistent_store = "memoized.db";
tie my %cache => 'Memoize::Storable', $memoize_persistent_store;
memoize 'get_git_log_between_dates', SCALAR_CACHE => [HASH => \%cache];
my $pause_threshold = 5000;
my %month_names = (
1 => "Januar",
2 => "Februar",
3 => "März",
4 => "April",
5 => "Mai",
6 => "Juni",
7 => "Juli",
8 => "August",
9 => "September",
10 => "Oktober",
11 => "November",
12 => "Dezember"
);
sub dier {
die Dumper \@_;
}
# Set the Git repository paths from the ~/.repos file
my $repos_file = "$ENV{HOME}/.repos";
open my $repos_fh, '<', $repos_file or die "Could not open $repos_file: $!";
my @repos = <$repos_fh>;
chomp(@repos);
# Define the date range
my $start_date = shift || die("No start date given");
my $start_time = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d')->parse_datetime($start_date);
my $original_start_time = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d')->parse_datetime($start_date);
my $end_time = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d')->parse_datetime($start_date);
$end_time->add(months => 1);
if (!$end_time) {
die("End time could not be found. Is it above the max number of days per month?");
}
# Create a CSV object for writing timetable.csv
my $csv_timetable = Text::CSV->new({ binary => 1, eol => $/ });
open my $timetable_fh, '>', 'timetable.csv' or die "Could not open timetable.csv: $!";
$csv_timetable->print($timetable_fh, ['tag', 'uhrzeit_erster_commit', 'uhrzeit_letzter_commit']);
# Create a CSV object for writing table2.csv
my $csv_table2 = Text::CSV->new({ binary => 1, eol => $/ });
open my $table2_fh, '>', 'table2.csv' or die "Could not open table2.csv: $!";
$csv_table2->print($table2_fh, ['tag', 'arbeitszeit', 'anzahl_commits_an_dem_tag', 'erster_commit', 'letzter_commit']);
# Initialize total working hours and commits count
my $total_working_hours = 0;
my $total_pauses = 0;
my $total_commits_count = 0;
# Initialize Gnuplot data arrays
my @plot_dates;
my @plot_commits;
my %global_working_hours = ();
my %daily_commits = ();
my %global_pause_times = ();
# Determine holidays (in this case, for Germany)
my $holidays = Date::Holidays->new(
countrycode => 'DE',
nocheck => 1,
WHERE => ['common', 'sn']
);
# Iterate through each day in the date range
while ($start_time <= $end_time) {
my $current_date = $start_time->strftime('%Y-%m-%d');
my $strp = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d');
my $end_of_day = $start_time->clone()->set(hour => 23, minute => 59, second => 59);
my @all_commits = ();
foreach my $repo_path (@repos) {
print "\b\b\b" x 150;
print " " x 150;
print "\b\b\b" x 150;
print scalar(@all_commits)." commits found for $start_time -> $repo_path";
eval {
my @commits = get_git_log_between_dates($repo_path, $start_time, $end_of_day);
push @all_commits, @commits;
@all_commits = sort @all_commits;
};
if($@) {
print "\n";
die $@;
}
}
if (@all_commits) {
#@all_commits = reverse @all_commits;
my @commit_times;
my @commit_times_full;
my @commit_hashes;
my $commits_count = 0;
foreach my $commit (@all_commits) {
my ($timestamp, $hash) = split(/,/, $commit);
my $commit_time = DateTime->from_epoch(epoch => $timestamp);
push @commit_times, $commit_time->strftime('%H:%M');
push @commit_times_full, $commit_time->strftime('%Y-%m-%d %H:%M:%S');
push @commit_hashes, $hash;
$commits_count++;
}
my $format = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
on_error => 'croak',
);
@all_commits = sort { (split(/,/, $a))[0] <=> (split(/,/, $b))[0] } @all_commits;
my $prev_commit_time = (split /,/, $all_commits[0])[0];
my $current_day_pause_time = 0;
foreach my $commit (@all_commits) {
my ($timestamp, $hash) = split(/,/, $commit);
my $commit_time = $timestamp;
# Check if this is the first commit, or the time gap is more than the pause threshold
my $commit_time_diff = $commit_time - $prev_commit_time;
if ($commit_time_diff >= $pause_threshold) {
$current_day_pause_time += $commit_time_diff;
}
$prev_commit_time = $commit_time;
}
my $first_commit_time = $commit_times[0];
my $first_commit_time_full = $format->parse_datetime($commit_times_full[0]);
my $first_commit_hash = $commit_hashes[0];
my $last_commit_time = $commit_times[-1];
my $last_commit_time_full = $format->parse_datetime($commit_times_full[-1]);
my $last_commit_hash = $commit_hashes[-1];
my $working_hours = $last_commit_time_full->subtract_datetime($first_commit_time_full);
my $working_hours_formatted = $working_hours->hours . ':' . sprintf("%02d", $working_hours->minutes);
$global_working_hours{$current_date} = $working_hours_formatted;
$global_pause_times{$current_date} = $current_day_pause_time;
$daily_commits{$current_date} = scalar @all_commits;
$total_working_hours += abs($working_hours->in_units('hours'));
$total_commits_count += $commits_count;
$csv_timetable->print($timetable_fh, [$current_date, $first_commit_time, $last_commit_time]);
$csv_table2->print($table2_fh, [$current_date, $working_hours_formatted, $commits_count, $first_commit_hash, $last_commit_hash]);
push @plot_dates, $current_date;
push @plot_commits, $commits_count;
}
$start_time->add(days => 1);
}
close $timetable_fh;
close $table2_fh;
# Define the year and month
my $current_year = $original_start_time->year;
my $current_month = $original_start_time->month;
print "\n\n".colored($month_names{$current_month}, 'on_magenta bold')."\n\n";
# Clean up temporary files
unlink 'timetable.csv';
unlink 'table2.csv';
# Create a hash of workdays
my %workdays = ();
my %pause_times = ();
# Iterate through each day in the month
while ($current_month == $original_start_time->month) {
my $day = $original_start_time->day;
my $current_date = $original_start_time->strftime('%Y-%m-%d');
my $current_month = $original_start_time->strftime('%m');
my $current_year = $original_start_time->strftime('%Y');
my $day_of_week = Day_of_Week($current_year, $current_month, $day);
my $dh = Date::Holidays->new(
countrycode => 'de'
);
my $is_holiday = $dh->is_holiday(
year => $current_year,
month => $current_month,
day => $day,
regions => ['common', 'sn'],
);
# Check if it's a weekend (Saturday or Sunday)
if ($day_of_week == 6 || $day_of_week == 7) {
$workdays{$current_date} = 'WEEKEND';
} elsif ($is_holiday) {
# Check if it's a holiday
$workdays{$current_date} = 'HOLIDAY';
} else {
# Check for overtime or undertime (assuming 8 hours per day is normal)
my $working_hours = $global_working_hours{$current_date};
if ($working_hours) {
my ($hours, $minutes) = split(':', $working_hours);
my $total_minutes = $hours * 60 + $minutes;
if ($total_minutes > 8 * 60) {
$workdays{$current_date} = 'OVERTIME';
} elsif ($total_minutes < 8 * 60) {
$workdays{$current_date} = 'UNDERTIME';
}
}
my $pause_time = $global_pause_times{$current_date};
if ($pause_time) { # in seconds
$pause_times{$current_date}{original} = $pause_time;
$pause_times{$current_date}{nice} = parse_duration($pause_time);
}
}
$original_start_time->add(days => 1);
}
sub parse_duration {
my $seconds = shift;
my $hours = int( $seconds / (60*60) );
my $mins = ( $seconds / 60 ) % 60;
my $secs = $seconds % 60;
return sprintf("%02d:%02d:%02d", $hours,$mins,$secs);
}
# Reinitialize the start time for printing the calendar
$original_start_time = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d')->parse_datetime($start_date);
# Create a table for displaying the calendar
my $table = Text::Table->new('DOM, ', 'Day, ', 'No comm.,', 'Working Hours', "Pauses");
my @weekend_days;
my $number_workdays = 0;
while ($current_month == $original_start_time->month) {
my $day = $original_start_time->day;
my $current_date = $original_start_time->strftime('%Y-%m-%d');
my $day_of_week = Day_of_Week($current_year, $current_month, $day);
my $color = 'reset';
# Determine the color based on workday type
if (exists $workdays{$current_date}) {
if ($workdays{$current_date} eq 'WEEKEND') {
$color = 'green';
push @weekend_days, [$day, $global_working_hours{$current_date} || '0:00'];
} elsif ($workdays{$current_date} eq 'HOLIDAY') {
$color = 'on_blue';
} else {
if ($workdays{$current_date} eq 'OVERTIME') {
$color = 'on_green';
} elsif ($workdays{$current_date} eq 'UNDERTIME') {
$color = 'on_red';
}
}
}
my $pause_col = "";
if(exists $pause_times{$current_date}) {
my $nice = $pause_times{$current_date}{nice};
$pause_col = $nice;
my $original = $pause_times{$current_date}{original};
if($original > $pause_threshold) {
if(!$workdays{$current_date} eq 'WEEKEND' && $workdays{$current_date} eq 'HOLIDAY') {
$pause_col = colored($nice, "red");
}
}
}
my $number_commits = 0;
if(exists $daily_commits{$current_date}) {
$number_commits = $daily_commits{$current_date};
}
# Calculate the working hours for the day
my $working_hours = $global_working_hours{$current_date} || '0:00';
my @dow_to_d = qw/So Mo Di Mi Do Fr Sa Sa/;
my $dow = $dow_to_d[$day_of_week];
if(($dow eq "So" || $dow eq "Sa") && $workdays{$current_date} ne "HOLIDAY") {
$dow = colored($dow, "on_green");
$number_commits = colored($number_commits, "red");
}
if(exists $workdays{$current_date} && $workdays{$current_date} eq "HOLIDAY") {
$number_commits = colored($number_commits, "red");
}
my $colored_text = colored($working_hours, $color);
if($dow =~ m#^S[ao]$#) {
$colored_text = colored($colored_text, "underline");
} else {
if(($color ne 'on_blue' && $color ne 'green') || $color ne "reset") {
$number_workdays++;
}
}
# Add day and working hours to the table
$table->add($day, $dow, $number_commits, $colored_text, $pause_col);
# Increment the day
$original_start_time->add(days => 1);
}
my $expected_working_hours = $number_workdays * 8;
# Output total working hours and commits count
print " " x 100;
print "\b\b\b" x 100;
print " " x 100;
print "\b\b\b" x 100;
my $sum_pause_times = 0;
for my $i (map { $global_pause_times{$_} } keys %global_pause_times) {
$sum_pause_times += $i;
}
my $total_working_hours_no_pauses = $total_working_hours - int($sum_pause_times / 3600);
print "Work days: $number_workdays\n";
print "Expected working hours: $expected_working_hours hours\n";
print "Total Working Hours: $total_working_hours hours\n";
print "Total Working Hours exclusing pauses: $total_working_hours_no_pauses hours\n";
my $over_or_undertime = abs($expected_working_hours) - abs($total_working_hours);
if($expected_working_hours <= $total_working_hours) {
print "Overtime ".abs($over_or_undertime)." hours\n";
} else {
print "Undertime ".abs($over_or_undertime)." hours\n";
}
print "Total Commits Count: $total_commits_count\n";
# Print any remaining days at the end of the month
if ($table->body() || @weekend_days) {
print "\n";
print colored(colored("Wochenende", "green"), "underline")."\n";
print colored("Feiertag", "on_blue")."\n";
print colored("Überstunden", "on_green")."\n";
print colored("Unterstunden", "on_red")."\n";
print colored("Wochenende", "underline")."\n";
print $table;
print "\n";
}