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 $json; |
16 |
|
|
17 |
my $max_month = 0; |
my $max_month = 0; |
18 |
|
|
20 |
|
|
21 |
open(my $fh, '<', $file) or die "$file: $!"; |
open(my $fh, '<', $file) or die "$file: $!"; |
22 |
|
|
23 |
|
my $data; |
24 |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
25 |
|
|
26 |
while(<$fh>) { |
while(<$fh>) { |
29 |
|
|
30 |
if ( ! defined $data->{$instance} ) { |
if ( ! defined $data->{$instance} ) { |
31 |
$data->{$instance} = []; |
$data->{$instance} = []; |
32 |
|
$json->{$instance}->{$type}->{sum} = 0; |
33 |
|
$json->{$instance}->{$type}->{min} = 0; |
34 |
|
$json->{$instance}->{$type}->{max} = 0; |
35 |
} |
} |
36 |
|
|
37 |
$max_month = $month if $month > $max_month; |
$max_month = $month if $month > $max_month; |
49 |
push @x, $month; |
push @x, $month; |
50 |
my $y = $data->{$instance}->[$month] || 0; |
my $y = $data->{$instance}->[$month] || 0; |
51 |
push @y, $y; |
push @y, $y; |
52 |
|
$json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y; |
53 |
|
$json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y; |
54 |
|
$json->{$instance}->{$type}->{sum} += $y; |
55 |
} |
} |
56 |
warn "x = ",dump( @x ), "\ny = ", dump( @y ); |
warn "x = ",dump( @x ), "\ny = ", dump( @y ); |
57 |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
58 |
write_file( "s/$instance-$type.png", $gd->png ); |
my $path = "s/$instance-$type.png"; |
59 |
|
write_file( $path, $gd->png ); |
60 |
|
$json->{$instance}->{$type}->{s} = $path; |
61 |
} |
} |
62 |
|
|
63 |
close($fh); |
close($fh); |
64 |
} |
} |
65 |
|
|
66 |
|
warn "# json = ",dump( $json ); |
67 |
|
my @items; |
68 |
|
foreach my $instance ( keys %$json ) { |
69 |
|
foreach my $type ( keys %{ $json->{$instance} } ) { |
70 |
|
push @items, { |
71 |
|
id => $instance, |
72 |
|
label => $instance, |
73 |
|
url => "http://www.$instance.skole.hr", |
74 |
|
%{ $json->{$instance}->{$type} }, |
75 |
|
}; |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
write_file( 'sparklines.js', to_json({ items => \@items }) ); |
80 |
|
|