/[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

Diff of /sparklines/create.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 28 by dpavlin, Mon Oct 20 18:59:48 2008 UTC revision 31 by dpavlin, Mon Oct 20 21:04:14 2008 UTC
# Line 12  use Data::Dump qw/dump/; Line 12  use Data::Dump qw/dump/;
12  use File::Slurp;  use File::Slurp;
13  use JSON;  use JSON;
14    
15  my $debug = 0;  my $debug = $ENV{DEBUG} || 0;
16    
17  my $json;  my $json;
18  my $max_month = 0;  my $max_month = 0;
19    
20  foreach my $file ( @ARGV ) {  foreach my $file ( @ARGV ) {
21    
22            warn "<< $file ", -s $file, " bytes\n";
23    
24          open(my $fh, '<', $file) or die "$file: $!";          open(my $fh, '<', $file) or die "$file: $!";
25    
26          my $data;          my $data;
27          my $type = $1 if ($file =~ /monthly-([^\.]+)/);          if ($file =~ /monthly-([^\.]+)/) {
28                    my $type = $1;
29    
30                    while(<$fh>) {
31                            chomp;
32                            my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_);
33    
34                            if ( ! defined $data->{$instance} ) {
35                                    $data->{$instance} = [];
36                                    $json->{$instance}->{$type}->{sum} = 0;
37            #                       $json->{$instance}->{$type}->{min} = 0;
38            #                       $json->{$instance}->{$type}->{max} = 0;
39                            }
40    
41          while(<$fh>) {                          $max_month = $month if $month > $max_month;
42                  chomp;  
43                  my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_);                          $data->{$instance}->[ $month ] = $count;
   
                 if ( ! defined $data->{$instance} ) {  
                         $data->{$instance} = [];  
                         $json->{$instance}->{$type}->{sum} = 0;  
 #                       $json->{$instance}->{$type}->{min} = 0;  
 #                       $json->{$instance}->{$type}->{max} = 0;  
44                  }                  }
45    
46                  $max_month = $month if $month > $max_month;                  warn "# max_month: $max_month" if $debug;
47    
48                  $data->{$instance}->[ $month ] = $count;                  warn "# data = ",dump( $data ) if $debug;
         }  
49    
50          warn dump( $data ), "\nmax_month: $max_month" if $debug;                  foreach my $instance ( keys %$data ) {
51    
52          foreach my $instance ( keys %$data ) {                          my $graph = GD::Graph::sparklines->new($max_month * 10, 16);
53                            my ( @x, @y );
54                            foreach my $month ( 1 .. $max_month ) {
55                                    push @x, $month;
56                                    my $y = $data->{$instance}->[$month] || 0;
57                                    push @y, $y;
58    #                               $json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y;
59    #                               $json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y;
60                                    $json->{$instance}->{$type}->{sum} += $y;
61                            }
62                            warn "x = ",dump( @x ), "\ny = ", dump( @y ) if $debug;
63                            my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error;
64                            my $path = "s/$instance-$type.png";
65                            write_file( $path, $gd->png );
66                            $json->{$instance}->{$type}->{s} = $path;
67                    }
68    
69                  my $graph = GD::Graph::sparklines->new(128, 16);          } else {
70                  my ( @x, @y );                  $file =~ s{^(.*/)?([^/]+)\.tsv$}{$2} || die "can't parse '$file'";
71                  foreach my $month ( 1 .. $max_month ) {                  my @c = split(/-/, $file);
72                          push @x, $month;                  my $name = shift @c;
73                          my $y = $data->{$instance}->[$month] || 0;                  while(<$fh>) {
74                          push @y, $y;                          chomp;
75  #                       $json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y;                          my @d = split(/\t/,$_);
76  #                       $json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y;                          my $instance = shift @d;
77                          $json->{$instance}->{$type}->{sum} += $y;                          foreach my $f ( @c ) {
78                                    $json->{$instance}->{ $name }->{ $f } = shift @d;
79                            }
80                  }                  }
                 warn "x = ",dump( @x ), "\ny = ", dump( @y ) if $debug;  
                 my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error;  
                 my $path = "s/$instance-$type.png";  
                 write_file( $path, $gd->png );  
                 $json->{$instance}->{$type}->{s} = $path;  
81          }          }
82    
83          close($fh);          close($fh);
84    
85            warn "# json = ",dump( $json ) if $debug;
86    
87  }  }
88    
89  warn "# json = ",dump( $json ) if $debug;  warn "# json = ",dump( $json ) if $debug;
# Line 95  my $items_json = encode_json({ Line 117  my $items_json = encode_json({
117                  changes_sum => { valueType => 'number' },                  changes_sum => { valueType => 'number' },
118                  categories_changed_sum => { valueType => 'number' },                  categories_changed_sum => { valueType => 'number' },
119                  unique_users_sum => { valueType => 'number' },                  unique_users_sum => { valueType => 'number' },
120                    users_total => { valueType => 'number' },
121                    users_active => { valueType => 'number' },
122          },          },
123          items => [ @items ]          items => [ @items ]
124  });  });

Legend:
Removed from v.28  
changed lines
  Added in v.31

  ViewVC Help
Powered by ViewVC 1.1.26