1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
# create.pl |
7 |
# |
8 |
# 10/17/08 12:29:51 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
9 |
|
10 |
use GD::Graph::sparklines; |
11 |
use Data::Dump qw/dump/; |
12 |
use File::Slurp; |
13 |
use JSON; |
14 |
|
15 |
my $json; |
16 |
|
17 |
my $max_month = 0; |
18 |
|
19 |
foreach my $file ( @ARGV ) { |
20 |
|
21 |
open(my $fh, '<', $file) or die "$file: $!"; |
22 |
|
23 |
my $data; |
24 |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
25 |
|
26 |
while(<$fh>) { |
27 |
chomp; |
28 |
my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_); |
29 |
|
30 |
if ( ! defined $data->{$instance} ) { |
31 |
$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; |
38 |
|
39 |
$data->{$instance}->[ $month ] = $count; |
40 |
} |
41 |
|
42 |
warn dump( $data ), "\nmax_month: $max_month"; |
43 |
|
44 |
foreach my $instance ( keys %$data ) { |
45 |
|
46 |
my $graph = GD::Graph::sparklines->new(128, 16); |
47 |
my ( @x, @y ); |
48 |
foreach my $month ( 1 .. $max_month ) { |
49 |
push @x, $month; |
50 |
my $y = $data->{$instance}->[$month] || 0; |
51 |
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 ); |
57 |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
58 |
my $path = "s/$instance-$type.png"; |
59 |
write_file( $path, $gd->png ); |
60 |
$json->{$instance}->{$type}->{s} = $path; |
61 |
} |
62 |
|
63 |
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 |
|