Stats.pm

filter: Statementstmt 83.3% 35 / 42 Branchbran 50.0% 6 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 6 / 8 Podpod 100% 4 / 4 totaltotal 75.3% 52 / 69 CC 9 SCAR 13.9
CC
9
Cov
75%
log
6.5
new
4.4
init_store
2.0
CRAP 4.0
↑ Summary Stats/DBI.pm »
1 package Dezi::Stats;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.001006';
9  
10 =head1 NAME
11  
12 Dezi::Stats - log statistics for your Dezi server
13  
14 =head1 SYNOPSIS
15  
16 # example Dezi server application
17 use Dezi::Server;
18 use Plack::Runner;
19 use Dezi::Stats;
20
21 my $app = Dezi::Server->app({
22 engine_config => {
23 type => 'Lucy',
24 index => ['path/to/your/index'],
25 },
26 stats_logger => Dezi::Stats->new(
27 type => 'DBI',
28 dsn => "DBI:mysql:database=$database;host=$hostname;port=$port",
29 username => 'myuser',
30 password => 'mysecret',
31 ),
32 });
33
34 my $runner = Plack::Runner->new();
35 $runner->run($app);
36  
37 =head1 DESCRIPTION
38  
39 Dezi::Stats logs statistics about requests to a Dezi server.
40 There are multiple backend storage options, including DBI-based
41 storage (MySQL, Postgresql, SQLite, etc), file-based, etc.
42  
43 =head1 METHODS
44  
45 =head2 new( I<config> )
46  
47 Returns a new Dezi::Stats object. I<config> should be a series
48 of key/value pairs (a hash). Supported I<config> params are:
49  
50 =over
51  
52 =item type
53  
54 The backend storage type. Defaults to 'File' (see L<Dezi::Stats::File>).
55  
56 =item dsn
57  
58 If B<type> is C<DBI> then the B<dsn> value will be passed directly
59 to the DBI->connect() method.
60  
61 =item username
62  
63 If B<type> is C<DBI> then the B<username> value will be passed directly
64 to the DBI->connect() method.
65  
66 =item password
67  
68 If B<type> is C<DBI> then the B<password> value will be passed directly
69 to the DBI->connect() method.
70  
71 =item table_name
72  
73 If B<type> is C<DBI> then the B<table_name> value will be used
74 to insert rows. Defaults to C<dezi_stats>.
75  
76 =item quote
77  
78 If B<type> is C<DBI> then the B<quote> value will be used
79 to quote column names on insert. Defaults to C<false>.
80  
81 =item quote_char
82  
83 If B<type> is C<DBI> then the B<quote_char> value will be used
84 when B<quote> is true. Defaults to backtick.
85  
86 =item path
87  
88 If B<type> is C<File> then the B<path> value is the filesystem path
89 to the log file. See L<Dezi::Stats::File>.
90  
91 =back
92  
93 =cut
94  
95 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
96 2 my $class = shift;
97 2 my $self;
98 2 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
99 0 $self = shift;
100 }
101 else {
102 2 $self = {@_};
103 }
104 2 ▶ $self->{type} ||= 'File';
Condition50%1/2
$self->{"type"} ||= "File"
l!l
20
MC/DC0%0/1
$self->{"type"} ||= "File"
$self->{"type"}
Truth table50%1/2
A
A= $self->{"type"}
Aresult
11
00
105 2 my $driver;
106 2 ▶ if ( $self->{type} =~ m/^\+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} =~ /^\+/) { } 0 2 2
107 0 $driver = $self->{type};
108 0 $driver =~ s/^\+//;
109 }
110 else {
111 2 $driver = 'Dezi::Stats::' . $self->{type};
112 }
113 2 load $driver;
114 2 bless $self, $driver;
115 2 $self->init_store();
116 2 return $self;
117 }
118  
119 =head2 init_store
120  
121 All subclasses must implement this abstract method.
122 Called internally in new().
123  
124 =cut
125  
126 0 ▶sub init_store {
Subroutine init_store: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self = shift;
128 0 croak "$self must implement init_store()";
129 }
130  
131 =head2 log( I<plack_request>, I<sos_response> )
132  
133 Required method for Search::OpenSearch::Server stats_logger()
134 API.
135  
136 Expects 2 objects: the current Plack::Request and the resulting
137 Search::OpenSearch::Response.
138  
139 Calls insert() after pulling data from the request
140 and the response.
141  
142 =cut
143  
144 2 ▶sub log {
Subroutine log: called CC=6 SCAR=18.7
Pod documented
145 2 my $self = shift;
146 2 ▶ my $request = shift or croak "Plack::Request object required";
Branch: 1 branch
BranchTrueFalseTotal
my $request = shift() or croak("Plack::Request object required") 2 0 2
147 2 ▶ my $response = shift or croak "Response object required";
Branch: 1 branch
BranchTrueFalseTotal
my $response = shift() or croak("Response object required") 2 0 2
148 2 ▶ my %stats = (
Branch: 1 branch
BranchTrueFalseTotal
$request->can('remote_user') ? : 0 2 2
149 remote_user => (
150 $request->can('remote_user')
151 ? $request->remote_user
152 : $request->user
153 ),
154 tstamp => time(),
155 build_time => $response->build_time,
156 search_time => $response->search_time,
157 path => $request->uri->path,
158 total => $response->total,
159 );
160 2 my $params = $request->parameters;
161 2 ▶ if ( $response->isa('Search::OpenSearch::Result') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->isa('Search::OpenSearch::Result')) { } 0 2 2
162  
163 # a REST request on a specific doc
164  
165 }
166 else {
167  
168 # a SOS::Response
169 # TODO document and/or whitelist these
170 2 for my $p (qw( q s o p h c L f r t b )) {
171 22 $stats{$p} = $params->{$p};
172 }
173 }
174 2 $self->insert( \%stats );
175 }
176  
177 =head2 insert( I<hash_ref> )
178  
179 Called by log. All subclasses must implement this abstract method.
180  
181 =cut
182  
183 0 ▶sub insert {
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
184 0 my $self = shift;
185 0 croak "$self must implement insert()";
186 }
187  
188 3 1;
189  
190 __END__
↑ Summary Stats/DBI.pm »