chiark / gitweb /
Pass $dbh down through the stack, for better lifetime
[ypp-sc-tools.main.git] / yarrg / web / query_age
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component generates the core of the `data age' query.
33
34
35 </%doc>
36
37 <%args>
38 $quri
39 $dbh
40 </%args>
41
42 <%once>
43 my $meta_prettyprint_age= sub {
44     my ($age,$floor,$plus) = @_;
45     return <<END;
46         $age < 60 ?             'less than a minute'                    :
47         $age < 60*2 ?           '1 minute'                              :
48         $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
49         $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
50                                 $floor ($age/86400) $plus ' days';
51 END
52 };
53
54 my $prettyprint_age;
55 eval '
56   $prettyprint_age= sub {
57                 my ($age) = @_;
58                 '.$meta_prettyprint_age->('$age','floor','.').'
59   };
60 ' or die "$@";
61
62 </%once>
63
64 <%perl>
65 my $now= time;
66
67 my $row;
68 my $sth= $dbh->prepare("SELECT archipelago, islandid, islandname, timestamp
69                                 FROM uploads NATURAL JOIN islands
70                                 ORDER BY archipelago, islandid");
71 $sth->execute();
72
73 </%perl>
74
75 <&| script &>
76   da_pageload= Date.now();
77 </&script>
78
79 <h1>Market data age</h1>
80
81 <table>
82 <tr>
83 <th>Archipelago
84 <th>Island
85 <th>Age
86 </tr>
87 % my %da_ages;
88 % $da_ages{'daid_loaded'}= 0;
89 % while ($row=$sth->fetchrow_hashref) {
90 %       my $elid= "daid_$row->{'islandid'}";
91 %       my $age= $now - $row->{'timestamp'};
92 %       $da_ages{$elid}= $age;
93 <tr> <td><% $row->{'archipelago'} |h
94  %>  <td><% $row->{'islandname'} |h
95  %>  <td id="<% $elid %>"><% $prettyprint_age->($age) %> </tr>
96 % }
97 </table>
98
99 <p>
100 Time since this page loaded:
101 <span id="daid_loaded">(not known; times above not updating)</span>
102
103 <form action="lookup" method="get">
104 <input type=submit name=submit value="Reload">
105 <& "lookup:formhidden", ours => sub { 0; } &>
106 </form>
107
108 <&| script &>
109   da_ages= <% to_json_protecttags(\%da_ages) %>;
110
111   function da_Refresh() {
112     var now= Date.now();
113     debug('updating now='+now);
114     for (var elid in da_ages) {
115       var el= document.getElementById(elid);
116       var oldage= da_ages[elid];
117       var age= oldage + (now - da_pageload) / 1000;
118       var newhtml= <% $meta_prettyprint_age->('age','Math.floor','+') %>
119 % if ($ARGS{debug}) {
120       if (elid == 'daid_loaded')
121         debug('element elid='+elid+' oldage='+oldage+' age='+age+': '+newhtml);
122 % }
123       el.innerHTML= newhtml;
124     }
125   }
126
127   window.onload= da_Refresh;
128   window.setInterval(da_Refresh, 10000);
129 </&script>
130
131 <%init>
132 use POSIX;
133 use CommodsWeb;
134 </%init>