]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Graph.pm
cache graphs, and return not modified
[ndwebbie.git] / ND / Web / Pages / Graph.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package ND::Web::Pages::Graph;
21 use strict;
22 use warnings FATAL => 'all';
23 use ND::Include;
24 use ND::Web::Graph;
25 use DBI qw(:sql_types);
26 use DBD::Pg qw(:pg_types);
27
28
29 use base qw/ND::Web::Image/;
30
31 $ND::Web::Page::PAGES{graph} = 'ND::Web::Pages::Graph';
32
33 sub render_body {
34         my $self = shift;
35         my $DBH = $self->{DBH};
36
37         my %graph_settings = (
38                 y_number_format => sub { prettyValue abs $_[0]},
39                 y1_label => 'size',
40                 y2_label => 'rest',
41         );
42
43         my %req;
44         my $type;
45         if ($self->{URI} =~ m{^/\w+/(stats|ranks)/(.*)}){
46                 $type = $1;
47                 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
48                         $req{x} = $1;
49                         $req{y} = $2;
50                         $req{z} = $3;
51                         if (defined $3){
52                                 ($req{id}) = $DBH->selectrow_array(q{SELECT planetid($1,$2,$3,$4)},undef,$1,$2,$3,$self->{TICK});
53                         }else{
54                                 $type = "gal$type";
55                                 $req{id} = 100*$2+$2;
56                         }
57                 }
58         }elsif($self->{URI} =~ m{^/\w+/(alliance(?:avg)?)/(\d+)}){
59                 $type = $1;
60                 $req{id} = $2;
61         }elsif($self->{URI} =~ m{^/\w+/planetvsnd/(\d+)}){
62                 $type = 'planetvsnd';
63                 $req{id} = $1;
64         }
65
66         die 'no real type' unless $type;
67
68         my $findGraph = $DBH->prepare(q{SELECT EXTRACT(EPOCH FROM last_modified) AS last_modified FROM graphs WHERE id = $1 AND tick = $2 AND type = $3});
69         $findGraph->execute($req{id},$self->{TICK},$type) or die $DBH->errstr;
70         if (my $graph = $findGraph->fetchrow_hashref){
71                 $self->{R}->set_last_modified($graph->{last_modified});
72                 if ((my $rc = $self->{R}->meets_conditions) != Apache2::Const::OK){
73                         $self->{R}->status($rc);
74                         return;
75                 }
76                 my $findGraph = $DBH->prepare(q{SELECT img FROM graphs WHERE id = $1 AND tick = $2 AND type = $3});
77                 $findGraph->execute($req{id},$self->{TICK},$type) or die $DBH->errstr;
78                 $graph = $findGraph->fetchrow_hashref;
79                 return $graph->{img}
80         }
81
82         my $img;
83         my $delGraph = $DBH->prepare(q{DELETE FROM graphs WHERE id = $1 AND type = $2});
84         my $addGraph = $DBH->prepare(q{INSERT INTO graphs (type,id,tick,img) VALUES($1,$2,$3,$4)});
85         if ($type eq 'stats' || $type eq 'ranks'){
86                 my $type = $1;
87                 my $findGraph;
88                 my ($x,$y,$z) = ($req{x},$req{y},$req{z});
89                 my $query;
90                 if ($type eq 'stats'){
91                                 $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
92                 }elsif($type eq 'ranks'){
93                         $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
94                 }
95                 $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
96                 $graph_settings{title} = $type;
97                 $graph_settings{two_axes} = 1;
98                 $graph_settings{use_axis} = [2,1,2,2];
99                 $graph_settings{y_max_value} = 0 if $type eq 'ranks';
100                 $img = graphFromQuery 500,300,\%graph_settings,$query;
101         }elsif ($type eq 'galstats' || $type eq 'galranks'){
102                 my $query;
103                 my ($x,$y) = ($req{x},$req{y});
104                 if ($type eq 'galstats'){
105                         $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM galaxies WHERE x = $1 AND y = $2 ORDER BY tick ASC});
106                 }elsif($type eq 'galranks'){
107                         $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM galaxies WHERE x = $1 AND y = $2  ORDER BY tick ASC});
108                 }
109                 $query->execute($x,$y) or die $DBH->errstr;
110                 $graph_settings{title} = $type;
111                 $graph_settings{two_axes} = 1;
112                 $graph_settings{use_axis} = [2,1,2,2];
113                 $graph_settings{y_max_value} = 0 if $type eq 'ranks';
114                 $img = graphFromQuery 500,300,\%graph_settings,$query;
115         }elsif ($type eq 'alliance' || $type eq 'allianceavg'){
116
117
118                 $graph_settings{title} = 'Alliance vs known members';
119                 $graph_settings{two_axes} = 1;
120                 $graph_settings{use_axis} = [1,2,1,2];
121                 $graph_settings{y2_label} = 'score';
122
123                 my $query;
124                 unless (defined $1){
125                         $query = $DBH->prepare(q{SELECT a.tick,a.size,a.score,memsize, memscore FROM (SELECT tick,SUM(size) AS memsize,SUM(score) AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.alliance_id = $1 GROUP BY tick) p JOIN alliance_stats a ON a.tick = p.tick
126 WHERE a.id = $1 ORDER BY tick});
127                 }else{
128                         $graph_settings{title} = 'Average alliance vs known members';
129                         $query = $DBH->prepare(q{SELECT a.tick,a.size/members AS size,a.score/members AS score,memsize, memscore FROM (SELECT tick,AVG(size) AS memsize,AVG(score) AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.alliance_id = $1 GROUP BY tick) p JOIN alliance_stats a ON a.tick = p.tick
130 WHERE a.id = $1 ORDER BY tick});
131                 }
132                 $query->execute($2) or die $DBH->errstr;
133
134                 $img = graphFromQuery 500,300,\%graph_settings,$query;
135         }elsif ($type eq 'planetvsnd'){
136                 $graph_settings{title} = 'You vs ND AVG';
137                 $graph_settings{two_axes} = 1;
138                 $graph_settings{use_axis} = [1,2,1,2];
139                 $graph_settings{y2_label} = 'score';
140
141                 my $query = $DBH->prepare(q{SELECT a.tick,a.size/members as NDsize,a.score/members AS NDscore,memsize, memscore FROM (SELECT tick,size AS memsize,score AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.id = $1) p JOIN alliance_stats a ON a.tick = p.tick
142                         WHERE a.id = 1 ORDER BY tick});
143                 $query->execute($req{id}) or die $DBH->errstr;
144
145                 $img = graphFromQuery 500,300,\%graph_settings,$query;
146         }
147
148         die 'no image' unless defined $img;
149
150         $delGraph->execute($req{id},$type) or die $DBH->errstr;
151         $addGraph->bind_param('$1',$type,{TYPE => DBI::SQL_VARCHAR }) or die $DBH->errstr;
152         $addGraph->bind_param('$2',$req{id},{TYPE => DBI::SQL_INTEGER }) or die $DBH->errstr;
153         $addGraph->bind_param('$3',$self->{TICK},{TYPE => DBI::SQL_INTEGER }) or die $DBH->errstr;
154         $addGraph->bind_param('$4',$img,{TYPE => DBI::SQL_VARBINARY }) or die $DBH->errstr;
155         $addGraph->execute or die $DBH->errstr;
156         $self->{R}->set_last_modified(time);
157
158         return $img;
159 };
160
161 1;