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

Annotation of /sparklines/create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide 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 dpavlin 19 #!/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 dpavlin 21 use JSON;
14 dpavlin 19
15 dpavlin 31 my $debug = $ENV{DEBUG} || 0;
16 dpavlin 23
17 dpavlin 21 my $json;
18 dpavlin 19 my $max_month = 0;
19    
20     foreach my $file ( @ARGV ) {
21    
22 dpavlin 31 warn "<< $file ", -s $file, " bytes\n";
23    
24 dpavlin 19 open(my $fh, '<', $file) or die "$file: $!";
25    
26 dpavlin 21 my $data;
27 dpavlin 31 if ($file =~ /monthly-([^\.]+)/) {
28     my $type = $1;
29 dpavlin 19
30 dpavlin 31 while(<$fh>) {
31     chomp;
32     my ( $instance, $count, $month, $from, $to ) = split(/\t/,$_);
33 dpavlin 19
34 dpavlin 31 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 dpavlin 19 }
45    
46 dpavlin 31 warn "# max_month: $max_month" if $debug;
47 dpavlin 19
48 dpavlin 31 warn "# data = ",dump( $data ) if $debug;
49 dpavlin 19
50 dpavlin 31 foreach my $instance ( keys %$data ) {
51 dpavlin 19
52 dpavlin 31 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 dpavlin 19
69 dpavlin 31 } 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 dpavlin 19 }
81     }
82    
83     close($fh);
84 dpavlin 31
85     warn "# json = ",dump( $json ) if $debug;
86    
87 dpavlin 19 }
88 dpavlin 21
89 dpavlin 23 warn "# json = ",dump( $json ) if $debug;
90 dpavlin 21 my @items;
91     foreach my $instance ( keys %$json ) {
92 dpavlin 23 my $item = {
93     id => $instance,
94     label => $instance,
95     uri => "http://www.$instance.skole.hr",
96 dpavlin 27 visits_sum => 0,
97     visits_anonymous_sum => 0,
98     changes_sum => 0,
99     categories_changed_sum => 0,
100     unique_users_sum => 0,
101 dpavlin 23 };
102 dpavlin 21 foreach my $type ( keys %{ $json->{$instance} } ) {
103 dpavlin 23 foreach my $p ( keys %{ $json->{$instance}->{$type} } ) {
104 dpavlin 27 my $val = $json->{$instance}->{$type}->{$p};
105     $item->{ $type . '_' . $p } = $val;
106 dpavlin 23 }
107 dpavlin 21 }
108 dpavlin 23 push @items, $item;
109 dpavlin 21 }
110    
111 dpavlin 28 #warn dump( @items );
112 dpavlin 25
113 dpavlin 28 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 dpavlin 31 users_total => { valueType => 'number' },
121     users_active => { valueType => 'number' },
122 dpavlin 28 },
123     items => [ @items ]
124     });
125     #$items_json =~ s/(_sum":)"(\d+)"/$1$2/gs;
126 dpavlin 27 write_file( 'sparklines.js', $items_json );
127 dpavlin 21

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26