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 $debug = 0; |
16 |
|
17 |
my $json; |
18 |
my $max_month = 0; |
19 |
|
20 |
foreach my $file ( @ARGV ) { |
21 |
|
22 |
open(my $fh, '<', $file) or die "$file: $!"; |
23 |
|
24 |
my $data; |
25 |
my $type = $1 if ($file =~ /monthly-([^\.]+)/); |
26 |
|
27 |
while(<$fh>) { |
28 |
chomp; |
29 |
my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_); |
30 |
|
31 |
if ( ! defined $data->{$instance} ) { |
32 |
$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; |
39 |
|
40 |
$data->{$instance}->[ $month ] = $count; |
41 |
} |
42 |
|
43 |
warn dump( $data ), "\nmax_month: $max_month" if $debug; |
44 |
|
45 |
foreach my $instance ( keys %$data ) { |
46 |
|
47 |
my $graph = GD::Graph::sparklines->new(128, 16); |
48 |
my ( @x, @y ); |
49 |
foreach my $month ( 1 .. $max_month ) { |
50 |
push @x, $month; |
51 |
my $y = $data->{$instance}->[$month] || 0; |
52 |
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 ) if $debug; |
58 |
my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error; |
59 |
my $path = "s/$instance-$type.png"; |
60 |
write_file( $path, $gd->png ); |
61 |
$json->{$instance}->{$type}->{s} = $path; |
62 |
} |
63 |
|
64 |
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 |
|