chiark / gitweb /
routesearch: concurrency limit
[ypp-sc-tools.db-live.git] / yarrg / web / query_routesearch
index bbeba5b522e59ce506ec29d9fb334fa01ae93334..8937f285e9f9c77e6f3a77585b5471b6bd3de44e 100644 (file)
@@ -46,13 +46,20 @@ $emsgokorprint
 </%args>
 
 <%perl>
+use BSD::Resource;
+
 my $emsg;
 my @warningfs;
 my @islandids;
 
+my $maxmaxdist=35;
+my $maxcpu=90;
+my $concur_lim=5;
+
 my $qa= \%ARGS;
-my $routeparams= { EmsgRef => \$emsg, SayOptionalCapital => 1 };
+my $routeparams= { EmsgRef => \$emsg, SayRequiredCapacity => 1 };
 my $maxdist;
+my $maxcountea=15;
 
 </%perl>
 
@@ -70,11 +77,15 @@ This feature is not available from the "drop down menus" interface.
  &>
 
 <&| enter_advrouteopts, qa=>$qa, dbh=>$dbh, routeparams=>$routeparams &>
+<td>
+&nbsp;
+&nbsp;
 <td>
  Maximum distance:
  <&| qtextstring, qa => $qa, dbh => $dbh, prefix => 'ml',
     thingstring => 'distance', emsgstore => \$emsg,
     onresults => sub { ($maxdist)= @_; } &>
+   size=10
  </&>
 </&>
 
@@ -87,7 +98,178 @@ This feature is not available from the "drop down menus" interface.
 </form>
 <%perl>
 
+if (!$emsg && $maxdist > $maxmaxdist) {
+       $emsg= "Searching for routes of more than $maxmaxdist leagues is not".
+               " supported, sorry.";
+}
+
 $emsgokorprint->($emsg) or return;
 @islandids or return;
+defined $routeparams->{MaxMass} or defined $routeparams->{MaxVolume} or return;
+
+#---------- prepare island names ----------
+
+my $islandname_stmt= $dbh->prepare(<<END);
+       SELECT islandname, archipelago
+         FROM islands
+        WHERE islandid = ?
+END
+
+my $isleinfo = sub {
+       my ($id) = @_;
+       $islandname_stmt->execute($id);
+       my $row= $islandname_stmt->fetchrow_hashref();
+       local $_= $row->{'islandname'};
+       s/ Island$//;
+       return $_, $row->{'archipelago'};
+};
+
+#---------- compute the results ----------
+
+my @rsargs= ($concur_lim, '-DN');
+my $concur_fail;
+
+foreach my $k (qw(MaxMass MaxVolume MaxCapital)) {
+       my $v= $routeparams->{$k};
+       push @rsargs, (defined $v ? $v : -1);
+}
+push @rsargs, defined $routeparams->{LossPerLeaguePct}
+       ? $routeparams->{LossPerLeaguePct}*0.01 : 1e-9;
+push @rsargs, '0';
+push @rsargs, 'search',$maxdist, $maxcountea,$maxcountea, 'any', @islandids;
+
+m/[^-.0-9a-zA-Z]/ and die "$_ $& ?" foreach @rsargs;
+
+if ($qa->{'debug'}) {
+</%perl>
+[[ <% "@rsargs" |h %> ]]<br><pre>
+<%perl>
+}
+
+unshift @rsargs,
+       sourcebasedir().'/yarrg/routesearch',
+       '-d', dbw_filename($qa->{'Ocean'}),
+       '-C', webdatadir().'/_concur.', '.lock';
+
+# touch _concur.0{0,1,2,3,4}.lock
+# really chgrp www-data _concur.0?.lock
+
+my %results; # $results{$ap}{"5 6 9 10"} = { stuff }
+
+my $fh= new IO::File;
+my $child= $fh->open("-|"); defined $child or die $!;
+if (!$child) {
+       my $cpu= BSD::Resource::RLIMIT_CPU;
+       my ($soft,$hard)= getrlimit($cpu);
+       setrlimit($cpu,$maxcpu,$hard) or die $! if $hard<=$maxcpu;
+       exec @rsargs;
+       die $!;
+}
+
+while (<$fh>) {
+       chomp;
+       if ($qa->{'debug'}) {
+</%perl>
+<% $_ |h %>
+<%perl>
+       }
+       next unless m/^\s*\@/;
+       if (m/^\@\@\@ concurrency limit exceeded/) {
+               $concur_fail= 1;
+               last;
+       }
+       die unless m/^ \@ *\d+ ([ap])\# *\d+ \|.*\| *(\d+)lg *\| *\d+ +(\d+) +(\d+) *\| ([0-9 ]+)$/;
+       my ($ap,$isles) = (uc $1,$5);
+       next if $results{$ap} && %{$results{$ap}} >= $maxcountea;
+       my $item= { A => $3, P => $4, Leagues => $2 };
+       my (@i, @a);
+       foreach (split / /, $isles) {
+               my ($name,$arch)= $isleinfo->($_);
+               push @i, $name;
+               push @a, $arch unless @a && $a[-1] eq $arch;
+       }
+       $item->{Isles}= [ @i ];
+       $item->{Archs}= [ @a ];
+       $item->{Start}= $i[0];
+       $item->{Finish}= $i[-1];
+       $item->{Vias}= [ ];
+       my $i;
+       for ($i=1; $i < @i-1; $i++) {
+               push @{ $item->{Vias} }, $i[$i];
+       }
+       $results{$ap}{$isles}= $item;
+}
+
+if ($qa->{'debug'}) {
+       print "</pre>\n";
+}
+
+if ($concur_fail) {
+</%perl>
+<h2>Server too busy</h2>
+
+Sorry, but there are already <% $concur_lim |h %> route searches
+running.  We limit the number which can run at once to avoid
+overloading the server system and to make sure that the rest of the
+YARRG website still runs quickly.
+<p>
+
+If you submitted several searches and gave up on them (eg by hitting
+`back' or `stop' in your browser), be aware that that doesn't
+generally stop the search process at the server end.  So it's best to
+avoid asking for large searches that you're not sure about.
+
+<p>
+Otherwise, please try later.  Searches are limited to <% $maxcpu |h %>
+seconds of CPU time so more processing resources should be available soon.
+
+<%perl>
+       return;
+}
+
+</%perl>
+% foreach my $ap (qw(A P)) {
+<h2>ap=<% $ap %></h2>
+<table rules=groups>
+<colgroup span=2>
+<colgroup span=1>
+<colgroup span=1>
+<colgroup span=3>
+<tbody>
+<tr>
+<th colspan=2>Profit
+<th>Dist.
+<th>Archipelagoes
+<th colspan=3>Route
+<tr>
+<th>Abs.
+<th>Per.lg.
+<th>
+<th>
+<th>Start
+<th>Via
+<th>Finish
+<tbody>
+%      my $datarow=0;
+%      foreach my $isles (sort {
+%                      $results{$ap}{$b}{$ap} <=>
+%                      $results{$ap}{$a}{$ap}
+%              } keys %{$results{$ap}}) {
+%              my $item= $results{$ap}{$isles};
+<tr class="datarow<% $datarow %>">
+<td align=right><% $item->{A} |h %>
+<td align=right><% $item->{P} |h %>
+<td align=right><% $item->{Leagues} |h %>
+<td align=left><% join ', ', @{ $item->{Archs} } |h %>
+<td align=left><% $item->{Start} |h %>,
+<td align=left><% join ' ', map { $_.',' } @{ $item->{Vias} } |h %>
+<td align=left><% $item->{Finish} |h %>
+</td>
+%              $datarow ^= 1;
+%      } # $isles
+</table>
+% } # $ap
+<%perl>
+
 
 </%perl>