--- sparklines/create.pl 2008/10/20 18:59:48 28 +++ sparklines/create.pl 2008/10/20 21:04:14 31 @@ -12,56 +12,78 @@ use File::Slurp; use JSON; -my $debug = 0; +my $debug = $ENV{DEBUG} || 0; my $json; my $max_month = 0; foreach my $file ( @ARGV ) { + warn "<< $file ", -s $file, " bytes\n"; + open(my $fh, '<', $file) or die "$file: $!"; my $data; - my $type = $1 if ($file =~ /monthly-([^\.]+)/); + if ($file =~ /monthly-([^\.]+)/) { + my $type = $1; + + while(<$fh>) { + chomp; + my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_); + + if ( ! defined $data->{$instance} ) { + $data->{$instance} = []; + $json->{$instance}->{$type}->{sum} = 0; + # $json->{$instance}->{$type}->{min} = 0; + # $json->{$instance}->{$type}->{max} = 0; + } - while(<$fh>) { - chomp; - my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_); - - if ( ! defined $data->{$instance} ) { - $data->{$instance} = []; - $json->{$instance}->{$type}->{sum} = 0; -# $json->{$instance}->{$type}->{min} = 0; -# $json->{$instance}->{$type}->{max} = 0; + $max_month = $month if $month > $max_month; + + $data->{$instance}->[ $month ] = $count; } - $max_month = $month if $month > $max_month; + warn "# max_month: $max_month" if $debug; - $data->{$instance}->[ $month ] = $count; - } + warn "# data = ",dump( $data ) if $debug; - warn dump( $data ), "\nmax_month: $max_month" if $debug; + foreach my $instance ( keys %$data ) { - foreach my $instance ( keys %$data ) { + my $graph = GD::Graph::sparklines->new($max_month * 10, 16); + my ( @x, @y ); + foreach my $month ( 1 .. $max_month ) { + push @x, $month; + my $y = $data->{$instance}->[$month] || 0; + push @y, $y; +# $json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y; +# $json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y; + $json->{$instance}->{$type}->{sum} += $y; + } + 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; + } - my $graph = GD::Graph::sparklines->new(128, 16); - my ( @x, @y ); - foreach my $month ( 1 .. $max_month ) { - push @x, $month; - my $y = $data->{$instance}->[$month] || 0; - push @y, $y; -# $json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y; -# $json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y; - $json->{$instance}->{$type}->{sum} += $y; + } else { + $file =~ s{^(.*/)?([^/]+)\.tsv$}{$2} || die "can't parse '$file'"; + my @c = split(/-/, $file); + my $name = shift @c; + while(<$fh>) { + chomp; + my @d = split(/\t/,$_); + my $instance = shift @d; + foreach my $f ( @c ) { + $json->{$instance}->{ $name }->{ $f } = shift @d; + } } - 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; } close($fh); + + warn "# json = ",dump( $json ) if $debug; + } warn "# json = ",dump( $json ) if $debug; @@ -95,6 +117,8 @@ changes_sum => { valueType => 'number' }, categories_changed_sum => { valueType => 'number' }, unique_users_sum => { valueType => 'number' }, + users_total => { valueType => 'number' }, + users_active => { valueType => 'number' }, }, items => [ @items ] });