#!/usr/bin/perl

use warnings;
use strict;

# create.pl
#
# 10/17/08 12:29:51 CEST Dobrica Pavlinusic <dpavlin@rot13.org>

use GD::Graph::sparklines;
use Data::Dump qw/dump/;
use File::Slurp;
use JSON;

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;
	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;
			}

			$max_month = $month if $month > $max_month;

			$data->{$instance}->[ $month ] = $count;
		}

		warn "# max_month: $max_month" if $debug;

		warn "# data = ",dump( $data ) if $debug;

		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;
		}

	} 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;
			}
		}
	}

	close($fh);

	warn "# json = ",dump( $json ) if $debug;

}

warn "# json = ",dump( $json ) if $debug;
my @items;
foreach my $instance ( keys %$json ) {
	my $item = {
		id => $instance,
		label => $instance,
		uri => "http://www.$instance.skole.hr",
		visits_sum => 0,
		visits_anonymous_sum => 0,
		changes_sum => 0,
		categories_changed_sum => 0,
		unique_users_sum => 0,
	};
	foreach my $type ( keys %{ $json->{$instance} } ) {
		foreach my $p ( keys %{ $json->{$instance}->{$type} } ) {
			my $val = $json->{$instance}->{$type}->{$p};
			$item->{ $type . '_' . $p } = $val;
		}
	}
	push @items, $item;
}

#warn dump( @items );

my $items_json = encode_json({
	properties => {
		visits_sum => { valueType => 'number' },
		visits_anonymous_sum => { valueType => 'number' },
		changes_sum => { valueType => 'number' },
		categories_changed_sum => { valueType => 'number' },
		unique_users_sum => { valueType => 'number' },
		users_total => { valueType => 'number' },
		users_active => { valueType => 'number' },
	},
	items => [ @items ]
});
#$items_json =~ s/(_sum":)"(\d+)"/$1$2/gs;
write_file( 'sparklines.js', $items_json );

