10 |
use GD::Graph::sparklines; |
use GD::Graph::sparklines; |
11 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
12 |
use File::Slurp; |
use File::Slurp; |
13 |
|
use JSON; |
14 |
|
|
15 |
my $data; |
my $debug = 0; |
16 |
|
|
17 |
|
my $json; |
18 |
my $max_month = 0; |
my $max_month = 0; |
19 |
|
|
20 |
foreach my $file ( @ARGV ) { |
foreach my $file ( @ARGV ) { |
21 |
|
|
22 |
open(my $fh, '<', $file) or die "$file: $!"; |
open(my $fh, '<', $file) or die "$file: $!"; |
23 |
|
|
24 |
|
my $data; |
25 |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
26 |
|
|
27 |
while(<$fh>) { |
while(<$fh>) { |
30 |
|
|
31 |
if ( ! defined $data->{$instance} ) { |
if ( ! defined $data->{$instance} ) { |
32 |
$data->{$instance} = []; |
$data->{$instance} = []; |
33 |
|
$json->{$instance}->{$type}->{sum} = 0; |
34 |
|
$json->{$instance}->{$type}->{min} = 0; |
35 |
|
$json->{$instance}->{$type}->{max} = 0; |
36 |
} |
} |
37 |
|
|
38 |
$max_month = $month if $month > $max_month; |
$max_month = $month if $month > $max_month; |
40 |
$data->{$instance}->[ $month ] = $count; |
$data->{$instance}->[ $month ] = $count; |
41 |
} |
} |
42 |
|
|
43 |
warn dump( $data ), "\nmax_month: $max_month"; |
warn dump( $data ), "\nmax_month: $max_month" if $debug; |
44 |
|
|
45 |
foreach my $instance ( keys %$data ) { |
foreach my $instance ( keys %$data ) { |
46 |
|
|
50 |
push @x, $month; |
push @x, $month; |
51 |
my $y = $data->{$instance}->[$month] || 0; |
my $y = $data->{$instance}->[$month] || 0; |
52 |
push @y, $y; |
push @y, $y; |
53 |
|
$json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y; |
54 |
|
$json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y; |
55 |
|
$json->{$instance}->{$type}->{sum} += $y; |
56 |
} |
} |
57 |
warn "x = ",dump( @x ), "\ny = ", dump( @y ); |
warn "x = ",dump( @x ), "\ny = ", dump( @y ) if $debug; |
58 |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
59 |
write_file( "s/$instance-$type.png", $gd->png ); |
my $path = "s/$instance-$type.png"; |
60 |
|
write_file( $path, $gd->png ); |
61 |
|
$json->{$instance}->{$type}->{s} = $path; |
62 |
} |
} |
63 |
|
|
64 |
close($fh); |
close($fh); |
65 |
} |
} |
66 |
|
|
67 |
|
warn "# json = ",dump( $json ) if $debug; |
68 |
|
my @items; |
69 |
|
foreach my $instance ( keys %$json ) { |
70 |
|
my $item = { |
71 |
|
id => $instance, |
72 |
|
label => $instance, |
73 |
|
uri => "http://www.$instance.skole.hr", |
74 |
|
}; |
75 |
|
foreach my $type ( keys %{ $json->{$instance} } ) { |
76 |
|
foreach my $p ( keys %{ $json->{$instance}->{$type} } ) { |
77 |
|
$item->{ $type . '_' . $p } = $json->{$instance}->{$type}->{$p}; |
78 |
|
} |
79 |
|
} |
80 |
|
push @items, $item; |
81 |
|
} |
82 |
|
|
83 |
|
write_file( 'sparklines.js', to_json({ items => \@items }) ); |
84 |
|
|