chiark / gitweb /
Data ages and other fixes
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 24 Aug 2009 03:14:00 +0000 (04:14 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 24 Aug 2009 03:14:00 +0000 (04:14 +0100)
yarrg/web/routetrade

index 0adf9920bbbc6bb96bd0f05ad0f7d4fa78ece764..496156be5ca5576f473586ca627f64c9db55870a 100644 (file)
@@ -35,8 +35,6 @@
 ========== TODO ==========
 16:36 <ceb> alpha,byrne,papaya,turtle,jorvik,luthien is my example
 
-16:46 <ceb> Also trading plan not functional but I guess you know that :-)
-
 use POST for update.  Hrrm.
 
 LATER OR NOT AT ALL
@@ -54,8 +52,13 @@ $dbh
 @archipelagoes
 $qa
 </%args>
+<&| script &>
+  da_pageload= Date.now();
+</&script>
+
 <%perl>
 
+my $now= time;
 my $loss_per_league= 1e-7;
 
 my @flow_conds;
@@ -189,7 +192,12 @@ if ($qa->{ShowStalls}) {
 $addcols->({ Text => 1 }, qw(commodname));
 $addcols->({ DoReverse => 1 },
        qw(     org_price org_qty dst_price dst_qty
-               Margin unitprofit MaxQty
+       ));
+$addcols->({ DoReverse => 1, SortColKey => 'MarginSortKey' },
+       qw(     Margin
+       ));
+$addcols->({ DoReverse => 1 },
+       qw(     unitprofit MaxQty
                MaxCapital MaxProfit
        ));
 
@@ -216,6 +224,8 @@ $addcols->({ DoReverse => 1 },
        $f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'};
        $f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'};
 
+       $f->{MarginSortKey}= sprintf "%d",
+               $f->{'dst_price'} * 10000 / $f->{'org_price'};
        $f->{Margin}= sprintf "%3.1f%%",
                $f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0;
 
@@ -489,7 +499,9 @@ $addcols->({ Total => 0, DoReverse => 1 }, qw(
 %              my $v= $flow->{$col->{Name}};
 %              $col->{Total} += $v if defined $col->{Total};
 %              $v='' if !$col->{Text} && !$v;
-%              $ts_sortkeys{$ci}{$rowid}= $v;
+%              my $sortkey= $col->{SortColKey} ?
+%                      $flow->{$col->{SortColKey}} : $v;
+%              $ts_sortkeys{$ci}{$rowid}= $sortkey;
 <td <% $col->{Text} ? '' : 'align=right' %>><% $v |h %>
 %      }
 % }
@@ -521,11 +533,12 @@ $addcols->({ Total => 0, DoReverse => 1 }, qw(
 %
 % my $iquery= $dbh->prepare('SELECT islandname FROM islands
 %                              WHERE islandid = ?');
+% my %da_ages;
 %
 <h1>Voyage trading plan</h1>
 <table>
 % foreach my $i (0..$#islandids) {
-<tr><td colspan=4><strong>
+<tr><td colspan=<% 2+!!$qa->{ShowStalls} %>><strong>
 %      $iquery->execute($islandids[$i]);
 %      my ($islandname) = $iquery->fetchrow_array();
 %      if (!$i) {
@@ -534,6 +547,7 @@ Start at <% $islandname |h %>
 Sail to <% $islandname |h %>
 %      }
 </strong>
+%    my $age_reported= 0;
 %    foreach my $od (qw(dst org)) {
 %      my $sign= $od eq 'dst' ? -1 : +1;
 %      my %todo;
@@ -549,10 +563,17 @@ Sail to <% $islandname |h %>
 %              $$todo= { } unless $$todo;
 %              $$todo->{'commodname'}= $f->{'commodname'};
 %              $$todo->{'stallname'}= $stallname;
-%              $$todo->{Price} += $price;
+%              $$todo->{Price}= $price;
+%              $$todo->{Timestamp}= $f->{"${od}_timestamp"};
 %              $$todo->{Qty} += $f->{OptQty};
-%              $$todo->{Total} = $$todo->{Price} * $$todo->{Qty};
-%              $$todo->{Timestamp} = $f->{"${od}_timestamp"};
+%              $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
+%      }
+%      if (%todo && !$age_reported++) {
+<td colspan=2>
+%              my $age= $now - (values %todo)[0]->{Timestamp};
+%              my $cellid= "da_${i}";
+%              $da_ages{$cellid}= $age;
+(Data age: <span id="<% $cellid %>"><% prettyprint_age($age) %></span>)
 %      }
 %      my $total= 0;
 %      my $dline= 0;
@@ -575,9 +596,8 @@ Deliver
 %              $dline ^= 1;
 %      }
 %      if (%todo) {
-<tr><td>
-<td colspan=<% 2+!!$qa->{ShowStalls} %>>
-<% (values %todo)[0]->{Timestamp} %>
+<tr>
+<td colspan=<% 3+!!$qa->{ShowStalls} %>>
 <td align=right>
 %              if ($od eq 'org') {
 Outlay
@@ -589,6 +609,7 @@ Proceeds
 %    }
 % }
 </table>
+<& query_age:dataages, id2age => \%da_ages &>
 %
 % } # ========== TRADING PLAN ==========