merge
[irspy-moved-to-github.git] / web / htdocs / details / found.mc
1 %# $Id: found.mc,v 1.33 2009-04-15 18:16:46 wosch Exp $
2 <%once>
3 sub print_navlink {
4     my($params, $cond, $caption, $skip) = @_;
5
6     if ($cond) {
7         print('     <a href="', navlink($params, $caption, $skip),
8               '"', ">$caption</a>\n");
9     } else {
10         print qq[     <span class="disabled">$caption</span>\n];
11     }
12 }
13
14 sub navlink {
15     my($params, $caption, $skip) = @_;
16     local $params->{_skip} = $skip;
17     my $url = "?" . join("&", map { "$_=" . $params->{$_}  } sort keys %$params);
18     $url = xml_encode($url);
19     return $url;
20 }
21
22 # Identical to the same-named function in full.mc
23 # So maybe this should go into IRSpy::Utils.pm?
24 # Name changed (append 2) to prevent inadvertent clashes in Mason namespace
25 #
26 sub calc_reliability2 {
27     my($xc) = @_;
28
29     my @allpings = $xc->findnodes("i:status/i:probe");
30     my $nall = @allpings;
31     return "[untested]" if $nall == 0;
32     my @okpings = $xc->findnodes('i:status/i:probe[@ok = "1"]');
33     my $nok = @okpings;
34     return "$nok/$nall = " . int(100*$nok/$nall) . "%";
35 }
36
37
38 # Just make this once; forge the connection on first use
39 our $conn = undef;
40 </%once>
41 <%perl>
42 my %params = map { ( $_, utf8param($r, $_)) } grep { &utf8param($r, $_) } &utf8param($r);
43 my $query;
44 if ($params{_query}) {
45     $query = $params{_query};
46 } else {
47     $query = "";
48     foreach my $key (keys %params) {
49         next if $key =~ /^_/;
50         my $val = $params{$key};
51         next if $val eq "";
52         $query .= " and " if $query ne "";
53         $query .= "$key = ($val)";
54     }
55 }
56 $query = 'cql.allRecords=1' if $query eq "";
57
58 my $sort = $params{"_sort"};
59 if ($sort) {
60     my $modifiers = "";
61     if ($sort =~ s/(\/.*)//) {
62         $modifiers = $1;
63     }
64     $query .= " or $sort=/sort";
65     $query .= "-desc" if $params{_desc};
66     $query .= $modifiers;
67     $query .= " 0";
68 }
69
70 my $tried_to_open = 0;
71 if (!defined $conn) {
72   OPEN:
73     $conn = new ZOOM::Connection("localhost:8018/IR-Explain---1");
74     $conn->option(elementSetName => "zeerex");
75     $conn->option(count => 20);
76 }
77
78 my $rs;
79 eval { $rs = $conn->search(new ZOOM::Query::CQL($query)) };
80 if ($@ && ref $@ && $@->isa('ZOOM::Exception') &&
81     $@->code() == ZOOM::Error::CONNECTION_LOST && !$tried_to_open) {
82     $tried_to_open = 1;
83     goto OPEN;
84 } elsif ($@) {
85     die $@;
86 }
87
88 my $n = $rs->size();
89
90 my $skip = $params{"_skip"} || 0;
91 my $count = $params{"_count"} || 10;
92
93 my $first = $skip+1;
94 my $last = $first+$count-1;
95 $last = $n if $last > $n;
96 </%perl>
97      <form method="get" action=""><p>
98       <input type="text" name="_query" size="60" value="<% xml_encode($query) %>"/>
99       <input type="submit" name="_search" value="Search"/>
100      </p></form>
101      <p>
102 % if ($n == 0) {
103       No matches
104 % } elsif ($first > $n) {
105 %# "Can't happen"
106       Past end of <% $n %> records
107 % } else {
108       Records <% $first %> to <% $last %> of <% $n %><br/>
109 <%perl>
110 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
111 print_navlink(\%params, $last < $n, "Next", $skip+$count);
112 </%perl>
113 % }
114      </p>
115 % if ($n > 0 && $first <= $n) {
116      <table width="100%">
117       <tr class="thleft">
118        <th>#</th>
119        <th>Title</th>
120        <th>Host Connection Reliability <& /help/link.mc, help => "info/reliability" &>
121        </th>
122        <th>Host</th>
123        <th>Port</th>
124        <th>DB</th>
125        <th></th>
126        <th></th>
127       </tr>
128 % my @ids;
129 % $rs->records($first-1, $last-$first+1); ## Force pre-cache
130 % foreach my $i ($first .. $last) {
131 <%perl>
132 my $xc = irspy_xpath_context($rs->record($i-1));
133 my $title = $xc->find("e:databaseInfo/e:title") || "[UNTITLED]";
134 my $reliability = calc_reliability2($xc);
135 my $host = $xc->find("e:serverInfo/e:host");
136 my $port = $xc->find("e:serverInfo/e:port");
137 my $db = $xc->find("e:serverInfo/e:database");
138 my $id = irspy_record2identifier($xc);
139 push @ids, $id;
140 </%perl>
141       <tr style="background: <% ($i % 2) ? '#ffffc0' : 'white' %>">
142        <td><% $i %></td>
143        <td><a href="<% xml_encode("/full.html?id=" . uri_escape($id))
144                 %>"><% xml_encode($title) %></a></td>
145        <td><% xml_encode($reliability, "", { nbsp => 1 }) %></td>
146        <td><% xml_encode($host, "") %></td>
147        <td><% xml_encode($port, "") %></td>
148        <td><% xml_encode($db, "") %></td>
149        <td>
150         <a href="<% xml_encode("/admin/check.html?id=" . uri_escape($id))
151                 %>" title="Test this target">Test</a
152         >&nbsp;<a href="<% xml_encode("/admin/edit.html?op=edit&id=" .
153                 uri_escape($id))
154                 %>" title="Edit this target's record">Edit</a
155         >&nbsp;<a href="<% xml_encode("/raw.html?id=" . uri_escape($id))
156                 %>" title="Raw XML record">XML</a>
157        </td>
158       </tr>
159 % }
160      </table>
161 <%perl>
162 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
163 print_navlink(\%params, $last < $n, "Next", $skip+$count);
164 </%perl>
165      <p>
166       <a href="<% "/admin/check.html?" .
167         xml_encode(join("&", map { "id=" . uri_escape($_) } @ids))
168         %>">[Test all targets on this list]</a>
169      </p>
170      <p>
171       <a href="<% "/stats.html?query=" . xml_encode(uri_escape($query))
172         %>">[Statistics for targets on this list]</a>
173      </p>
174 % }