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