/[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 31 - (show annotations)
Mon Oct 20 21:04:14 2008 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 3101 byte(s)
added users total and active
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 = $ENV{DEBUG} || 0;
16
17 my $json;
18 my $max_month = 0;
19
20 foreach my $file ( @ARGV ) {
21
22 warn "<< $file ", -s $file, " bytes\n";
23
24 open(my $fh, '<', $file) or die "$file: $!";
25
26 my $data;
27 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 $max_month = $month if $month > $max_month;
42
43 $data->{$instance}->[ $month ] = $count;
44 }
45
46 warn "# max_month: $max_month" if $debug;
47
48 warn "# data = ",dump( $data ) if $debug;
49
50 foreach my $instance ( keys %$data ) {
51
52 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 } else {
70 $file =~ s{^(.*/)?([^/]+)\.tsv$}{$2} || die "can't parse '$file'";
71 my @c = split(/-/, $file);
72 my $name = shift @c;
73 while(<$fh>) {
74 chomp;
75 my @d = split(/\t/,$_);
76 my $instance = shift @d;
77 foreach my $f ( @c ) {
78 $json->{$instance}->{ $name }->{ $f } = shift @d;
79 }
80 }
81 }
82
83 close($fh);
84
85 warn "# json = ",dump( $json ) if $debug;
86
87 }
88
89 warn "# json = ",dump( $json ) if $debug;
90 my @items;
91 foreach my $instance ( keys %$json ) {
92 my $item = {
93 id => $instance,
94 label => $instance,
95 uri => "http://www.$instance.skole.hr",
96 visits_sum => 0,
97 visits_anonymous_sum => 0,
98 changes_sum => 0,
99 categories_changed_sum => 0,
100 unique_users_sum => 0,
101 };
102 foreach my $type ( keys %{ $json->{$instance} } ) {
103 foreach my $p ( keys %{ $json->{$instance}->{$type} } ) {
104 my $val = $json->{$instance}->{$type}->{$p};
105 $item->{ $type . '_' . $p } = $val;
106 }
107 }
108 push @items, $item;
109 }
110
111 #warn dump( @items );
112
113 my $items_json = encode_json({
114 properties => {
115 visits_sum => { valueType => 'number' },
116 visits_anonymous_sum => { valueType => 'number' },
117 changes_sum => { valueType => 'number' },
118 categories_changed_sum => { valueType => 'number' },
119 unique_users_sum => { valueType => 'number' },
120 users_total => { valueType => 'number' },
121 users_active => { valueType => 'number' },
122 },
123 items => [ @items ]
124 });
125 #$items_json =~ s/(_sum":)"(\d+)"/$1$2/gs;
126 write_file( 'sparklines.js', $items_json );
127

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26