Commit 9fdb356e6a691a26acecf5ad87ddc62eacf43916
- Diff rendering mode:
- inline
- side by side
timelog-graph
(122 / 0)
|   | |||
| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use feature 'say'; | ||
| 5 | use DateTime; | ||
| 6 | use GD::Graph::bars; | ||
| 7 | use Getopt::Euclid; | ||
| 8 | |||
| 9 | sub calc_delta { | ||
| 10 | my ($hour_start, $min_start) = split /:/, $_[0]; | ||
| 11 | my ($hour_end, $min_end) = split /:/, $_[1]; | ||
| 12 | my $start = DateTime->new(year => 2000, hour => $hour_start, minute => $min_start); | ||
| 13 | my $end = DateTime->new(year => 2000, hour => $hour_end, minute => $min_end); | ||
| 14 | my $duration = $end - $start; | ||
| 15 | $duration->delta_minutes; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub parse_timelog { | ||
| 19 | my %tasks_by_date; | ||
| 20 | open LOG, '<', $_[0] or die $!; | ||
| 21 | while (<LOG>) { | ||
| 22 | next if /^\s*$/; chomp; | ||
| 23 | my ($date, $time, @task) = split /\s/; | ||
| 24 | $time =~ s/:$//; | ||
| 25 | my $duration = exists($tasks_by_date{$date}) ? calc_delta($tasks_by_date{$date}->[-1]{time}, $time) : 0; | ||
| 26 | (my $group, @task) = split /:\s*/, "@task"; | ||
| 27 | push @{$tasks_by_date{$date}}, {group => $group, task => "@task", time => $time, minutes => $duration}; | ||
| 28 | } | ||
| 29 | close LOG; | ||
| 30 | return %tasks_by_date; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub plot_graph { | ||
| 34 | my ($date, %data) = @_; | ||
| 35 | my %duration_by_group; | ||
| 36 | foreach (@{$data{$date}}) { | ||
| 37 | $duration_by_group{$_->{group}} += $_->{minutes}; | ||
| 38 | } | ||
| 39 | my @data = ( | ||
| 40 | [ map { my $d = DateTime::Duration->new(minutes => $duration_by_group{$_}); sprintf("%s: %d:%02d", $_, $d->hours, $d->minutes) } keys %duration_by_group ], | ||
| 41 | [ map { $duration_by_group{$_} / 60 } keys %duration_by_group ], | ||
| 42 | ); | ||
| 43 | my $graph = GD::Graph::bars->new(700, 500); | ||
| 44 | $graph->set( | ||
| 45 | transparent => 0, | ||
| 46 | x_label => 'Grupo', | ||
| 47 | y_label => 'Horas', | ||
| 48 | #y_max_value => 24, | ||
| 49 | #y_tick_number => 24, | ||
| 50 | title => "Horas trabalhadas por Grupo em $date", | ||
| 51 | dclrs => [qw( gray green red blue )], | ||
| 52 | cycle_clrs => 1, | ||
| 53 | bar_spacing => 5, | ||
| 54 | ) or die $graph->error; | ||
| 55 | my $gd = $graph->plot(\@data) or die $graph->error; | ||
| 56 | open IMG, '>', "$date.png" or die $!; | ||
| 57 | binmode IMG; | ||
| 58 | print IMG $gd->png; | ||
| 59 | close IMG; | ||
| 60 | say "grafico salvo em: $date.png"; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $today = DateTime->now; | ||
| 64 | $today->add(days => -$ARGV{'<days>'}); | ||
| 65 | my %tasks_by_date = parse_timelog('/home/joenio/.gtimelog/timelog.txt'); | ||
| 66 | if (exists $tasks_by_date{$today->ymd}) { | ||
| 67 | plot_graph($today->ymd, %tasks_by_date); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | warn "nao existem registros no dia: " . $today->ymd; | ||
| 71 | } | ||
| 72 | |||
| 73 | __END__ | ||
| 74 | |||
| 75 | =head1 NAME | ||
| 76 | |||
| 77 | timelog-graph - Cria gráfico de barras a partir dos registros do gtimelog | ||
| 78 | |||
| 79 | =head1 VERSION | ||
| 80 | |||
| 81 | 0.1 | ||
| 82 | |||
| 83 | =head1 USAGE | ||
| 84 | |||
| 85 | timelog-graph [options] | ||
| 86 | |||
| 87 | =head1 OPTIONS | ||
| 88 | |||
| 89 | =over | ||
| 90 | |||
| 91 | =item <days> | ||
| 92 | |||
| 93 | Gerar relatorio de X dias atras. | ||
| 94 | |||
| 95 | =for Euclid: | ||
| 96 | days.type: int >= 0 | ||
| 97 | days.default: 0 | ||
| 98 | |||
| 99 | =item --version | ||
| 100 | |||
| 101 | =item --usage | ||
| 102 | |||
| 103 | =item --help | ||
| 104 | |||
| 105 | =item --man | ||
| 106 | |||
| 107 | Print the usual program information | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | Remainder of documentation starts here... | ||
| 112 | |||
| 113 | =head1 AUTHOR | ||
| 114 | |||
| 115 | Joenio Costa | ||
| 116 | |||
| 117 | =head1 COPYRIGHT | ||
| 118 | |||
| 119 | Copyright (c) 2009, Joenio Costa. All Rights Reserved. | ||
| 120 | This module is free software. It may be used, redistributed | ||
| 121 | and/or modified under the terms of the Perl Artistic License | ||
| 122 | (see http://www.perl.com/perl/misc/Artistic.html) |

