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