1 # $Id: Stats.pm,v 1.6 2007-01-24 09:28:02 mike Exp $
3 package ZOOM::IRSpy::Stats;
8 use ZOOM::IRSpy::Utils qw(irspy_xpath_context);
12 ZOOM::IRSpy::Stats - statistics generated for IRSpy about its targets
16 $stats = new ZOOM::IRSpy::Stats($dbname);
21 Provides a simple API to obtaining statistics about targets registered
22 in IRSpy. This is done just by creating a Stats object. Once this
23 object is made, it can be crudely printed using the built-in debugging
24 C<print()> method, or the application can walk the structure to
31 $stats = new ZOOM::IRSpy::Stats($dbname, "dc.creator=wedel");
33 $stats = new ZOOM::IRSpy::Stats($dbname,
34 new ZOOM::Query::PQF('@attr 1=1003 wedel');
36 $spy = new ZOOM::Connection("target/string/for/irspy/database");
37 $stats = new ZOOM::IRSpy::Stats($spy, $query);
39 Creates a new C<ZOOM::IRSpy::Stats> object and populates it with
40 statistics for the targets in the nominated database. This process
41 involves analysing the nominated IRSpy database at some length, and
42 which therefore takes some time
44 Either one or two arguments are required:
48 =item $conn (mandatory)
50 An indication of the IRSpy database that statistics are required for.
51 This may be in the form of a C<ZOOM::Connection> object or a
52 database-name string such as C<localhost:8018/IR-Explain---1>.
54 =item $query (optional)
56 The query with which to select a subset of the database to be
57 analysed. This may be in the form of a C<ZOOM::Query> object (using
58 any of the supported subclasses) or a CQL string. If this is omitted,
59 then all records in the database are included in the generated
68 my($conn, $query) = @_;
70 $query ||= "cql.allRecords=1",
71 $conn = new ZOOM::Connection($conn) if !ref $conn;
72 $query = new ZOOM::Query::CQL($query) if !ref $query;
74 my $oldSyntax = $conn->option("preferredRecordSyntax");
75 my $oldESN = $conn->option("elementSetName");
76 my $oldPC = $conn->option("presentChunk");
77 $conn->option(preferredRecordSyntax => "xml");
78 $conn->option(elementSetName => "zeerex");
79 # $conn->option(presentChunk => 10);
81 my $rs = $conn->search($query);
85 host => $conn->option("host"),
92 $this->_gather_stats();
93 $conn->option(preferredRecordSyntax => $oldSyntax);
94 $conn->option(elementSetName => $oldESN);
95 $conn->option(presentChunk => $oldPC);
104 foreach my $i (0 .. $this->{n}-1) {
105 my $rec = $this->{rs}->record($i);
106 my $xc = irspy_xpath_context($rec);
108 # The ten most commonly supported Bib-1 Use attributes
109 foreach my $node ($xc->findnodes('e:indexInfo/e:index[@search="true"]/e:map/e:attr[@type=1 and @set="bib-1"]')) {
110 $this->{bib1AccessPoints}->{$node->findvalue(".")}++;
113 # Record syntax support by database
114 foreach my $node ($xc->findnodes('e:recordInfo/e:recordSyntax/@name')) {
115 $this->{recordSyntaxes}->{$node->findvalue(".")}++;
119 foreach my $node ($xc->findnodes('i:status/i:explain[@ok="1"]/@category')) {
120 $this->{explain}->{$node->findvalue(".")}++;
123 # Z39.50 Protocol Services Support
124 foreach my $node ($xc->findnodes('e:configInfo/e:supports')) {
125 my $supports = $node->findvalue('@type');
126 if ($node->findvalue(".") && $supports =~ s/^z3950_//) {
127 $this->{z3950_init_opt}->{$supports}++;
131 # Z39.50 Server Atlas
132 ### TODO -- awkward, should be considered an enhancement
135 my $host = $xc->findvalue('e:serverInfo/e:host');
137 $this->{domains}->{$host}++;
140 ### Requires XSLT fix
149 Prints an ugly but human-readable summary of the statistics on
157 print "database = '", $this->{conn}->option("host"), "'\n";
158 print "query = '", $this->{query}, "'\n";
159 print "result set = '", $this->{rs}, "'\n";
160 print "count = '", $this->{n}, "'\n";
163 print "\nTOP 10 BIB-1 ATTRIBUTES\n";
164 $hr = $this->{bib1AccessPoints};
165 foreach my $key ((sort { $hr->{$b} <=> $hr->{$a}
166 || $a <=> $b } keys %$hr)[0..9]) {
167 print sprintf("%6d%20s%5d (%d%%)\n",
168 $key, "", $hr->{$key}, 100*$hr->{$key}/$this->{n});
171 print "\nRECORD SYNTAXES\n";
172 $hr = $this->{recordSyntaxes};
173 foreach my $key (sort { $hr->{$b} <=> $hr->{$a}
174 || $a cmp $b } keys %$hr) {
175 print sprintf("%-26s%5d (%d%%)\n",
176 $key, $hr->{$key}, 100*$hr->{$key}/$this->{n});
179 print "\nEXPLAIN SUPPORT\n";
180 $hr = $this->{explain};
181 foreach my $key (sort { $hr->{$b} <=> $hr->{$a}
182 || $a cmp $b } keys %$hr) {
183 print sprintf("%-26s%5d (%d%%)\n",
184 $key, $hr->{$key}, 100*$hr->{$key}/$this->{n});
187 print "\nZ39.50 PROTOCOL SERVICES SUPPORT\n";
188 $hr = $this->{z3950_init_opt};
189 foreach my $key (sort { $hr->{$b} <=> $hr->{$a}
190 || $a cmp $b } keys %$hr) {
191 print sprintf("%-26s%5d (%d%%)\n",
192 $key, $hr->{$key}, 100*$hr->{$key}/$this->{n});
195 print "\nTOP-LEVEL DOMAINS\n";
196 $hr = $this->{domains};
197 foreach my $key (sort { $hr->{$b} <=> $hr->{$a}
198 || $a cmp $b } keys %$hr) {
199 print sprintf("%-26s%5d (%d%%)\n",
200 $key, $hr->{$key}, 100*$hr->{$key}/$this->{n});
211 Mike Taylor, E<lt>mike@indexdata.comE<gt>
213 =head1 COPYRIGHT AND LICENSE
215 Copyright (C) 2006 by Index Data ApS.
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself, either Perl version 5.8.7 or,
219 at your option, any later version of Perl 5 you may have available.