/[simile]/sparklines/create.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sparklines/create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations)
Sun Oct 19 16:05:02 2008 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 2253 byte(s)
init all *_sum to 0 and mit real numbers

(didn't help much with sorting issue, but at least it's correct)
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 visits_sum => 0,
75 visits_anonymous_sum => 0,
76 changes_sum => 0,
77 categories_changed_sum => 0,
78 unique_users_sum => 0,
79 };
80 foreach my $type ( keys %{ $json->{$instance} } ) {
81 foreach my $p ( keys %{ $json->{$instance}->{$type} } ) {
82 my $val = $json->{$instance}->{$type}->{$p};
83 $item->{ $type . '_' . $p } = $val;
84 }
85 }
86 push @items, $item;
87 }
88
89 warn dump( @items );
90
91 my $items_json = to_json({ items => \@items });
92 $items_json =~ s/(_sum":)"(\d+)"/$1$2/gs;
93 write_file( 'sparklines.js', $items_json );
94

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26