chiark / gitweb /
Fix some warnings
[ypp-sc-tools.web-live.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 <%once>
37 my $meta_prettyprint_age= sub {
38     my ($age,$floor,$plus) = @_;
39     return <<END;
40         $age < 60 ?             'less than a minute'                    :
41         $age < 60*2 ?           '1 minute'                              :
42         $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
43         $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
44                                 $floor ($age/86400) $plus ' days';
45 END
46 };
47
48 my $prettyprint_age;
49 eval '
50   $prettyprint_age= sub {
51                 my ($age) = @_;
52                 '.$meta_prettyprint_age->('$age','floor','.').'
53   };
54 ' or die "$@";
55
56 </%once>
57
58 <%perl>
59
60 my $now= time;
61
62 my $row;
63 my $sth= $dbh->prepare("SELECT archipelago, islandid, islandname, timestamp
64                                 FROM uploads NATURAL JOIN islands
65                                 ORDER BY archipelago, islandid");
66 $sth->execute();
67
68 </%perl>
69
70 <&| script &>
71   da_pageload= Date.now();
72 </&script>
73
74 <h1>Market data age</h1>
75
76 <table>
77 <tr>
78 <th>Archipelago
79 <th>Island
80 <th>Age
81 </tr>
82 % my %da_ages;
83 % $da_ages{'daid_loaded'}= 0;
84 % while ($row=$sth->fetchrow_hashref) {
85 %       my $elid= "daid_$row->{'islandid'}";
86 %       my $age= $now - $row->{'timestamp'};
87 %       $da_ages{$elid}= $age;
88 <tr> <td><% $row->{'archipelago'} |h
89  %>  <td><% $row->{'islandname'} |h
90  %>  <td id="<% $elid %>"><% $prettyprint_age->($age) %> </tr>
91 % }
92 </table>
93
94 <p>
95 Time since this page loaded:
96 <span id="daid_loaded">(not known; times above not updating)</span>
97
98 <form action="lookup" method="get">
99 % foreach my $a (keys %ARGS) {
100 <input type="hidden" name="<% $a |h %>" value="<% $ARGS{$a} |h %>">
101 % }
102 <input type=submit name=submit value="Reload">
103 </form>
104
105 <&| script &>
106   da_ages= <% to_json_protecttags(\%da_ages) %>;
107
108   function da_Refresh() {
109     var now= Date.now();
110     debug('updating now='+now);
111     for (var elid in da_ages) {
112       var el= document.getElementById(elid);
113       var oldage= da_ages[elid];
114       var age= oldage + (now - da_pageload) / 1000;
115       var newhtml= <% $meta_prettyprint_age->('age','Math.floor','+') %>
116 % if ($ARGS{debug}) {
117       if (elid == 'daid_loaded')
118         debug('element elid='+elid+' oldage='+oldage+' age='+age+': '+newhtml);
119 % }
120       el.innerHTML= newhtml;
121     }
122   }
123
124   window.onload= da_Refresh;
125   window.setInterval(da_Refresh, 10000);
126 </&script>
127
128 <%init>
129 use POSIX;
130 use CommodsWeb;
131 my $dbh= dbw_connect('Midnight');
132 </%init>
133 <%cleanup>
134 $dbh->rollback();
135 </%cleanup>