lib/CMS/Drupal/Modules/MembershipEntity/Stats.pm

filter: Statementstmt 29.2% 76 / 260 Branchbran 25.0% 23 / 92 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.5% 15 / 27 Podpod 100% 18 / 18 totaltotal 31.6% 132 / 417 CC 67 SCAR 39.8
CC
67
Cov
27%
count_daily_were_renewal_memberships
240.0
count_daily_renewals
56.0
build_date_range
42.0
CRAP 53.5
↑ Summary lib/CMS/Drupal/Modules/MembershipEntity.pm »
1 package CMS::Drupal::Modules::MembershipEntity::Stats;
2 2 $CMS::Drupal::Modules::MembershipEntity::Stats::VERSION = '0.96';
3 # ABSTRACT: Generate statistics about MembershipEntity memberships on a Drupal site.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 our @EXPORT = qw/
12 count_total_memberships
13 count_expired_memberships
14 count_active_memberships
15 count_cancelled_memberships
16 count_pending_memberships
17 count_set_were_renewal_memberships
18 count_daily_were_renewal_memberships
19 count_daily_total_memberships
20 count_daily_term_expirations
21 count_daily_term_activations
22 count_daily_new_memberships
23 count_daily_new_terms
24 count_daily_renewals
25 count_daily_active_memberships
26 build_date_range
27 time_plus_one_day
28 datetime_plus_one_day
29 report_yesterday
30 /;
31  
32 2 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use DateTime::Event::Recurrence;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use CMS::Drupal::Modules::MembershipEntity::Membership;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use Carp qw/ carp croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 has dbh => ( is => 'ro', isa => InstanceOf['DBI::db'], required => 1 );
40 2 has prefix => ( is => 'ro', isa => Maybe[Str] );
41  
42  
43 sub count_total_memberships {
44 1 ▶ my $self = shift;
Subroutine count_total_memberships: called CC=2 SCAR=7.6
Pod documented
45 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
46 1 $self->{'stats'}->{'_count_total_memberships'} = scalar keys %{ $self->{'_memberships'} };
47 } else {
48 0 my $sql = q{ SELECT COUNT(mid) FROM membership_entity };
49 0 $self->{'stats'}->{'_count_total_memberships'} = $self->{'dbh'}->selectrow_array( $sql );
50 }
51 1 return $self->{'stats'}->{'_count_total_memberships'};
52 }
53  
54  
55 sub count_expired_memberships {
56 1 ▶ my $self = shift;
Subroutine count_expired_memberships: called CC=4 SCAR=14.1
Pod documented
57 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
58 1 my $count = 0;
59 1 ▶ while ( my ($mid, $mem) = each %{ $self->{'_memberships'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_memberships"};}) 29 1 30
60 29 ▶ $count++ if $mem->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $mem->is_expired 13 16 29
61 }
62 1 $self->{'_count_expired_memberships'} = $count;
63 } else {
64 0 my $sql = q{ select count(mid) from membership_entity where status = 0 };
65 0 $self->{'_count_expired_memberships'} = $self->{'dbh'}->selectrow_array( $sql );
66 }
67 1 return $self->{'_count_expired_memberships'};
68 }
69  
70  
71 sub count_active_memberships {
72 1 ▶ my $self = shift;
Subroutine count_active_memberships: called CC=4 SCAR=14.1
Pod documented
73 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
74 1 my $count = 0;
75 1 ▶ while ( my ($mid, $mem) = each %{ $self->{'_memberships'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_memberships"};}) 29 1 30
76 29 ▶ $count++ if $mem->is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $mem->is_active 16 13 29
77 }
78 1 $self->{'_count_active_memberships'} = $count;
79 } else {
80 0 my $sql = q{ SELECT COUNT(mid) FROM membership_entity WHERE status = 1 };
81 0 $self->{'_count_active_memberships'} = $self->{'dbh'}->selectrow_array( $sql );
82 }
83 1 return $self->{'_count_active_memberships'};
84 }
85  
86  
87 sub count_cancelled_memberships {
88 1 ▶ my $self = shift;
Subroutine count_cancelled_memberships: called CC=4 SCAR=14.5
Pod documented
89 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
90 1 my $count = 0;
91 1 ▶ while ( my ($mid, $mem) = each %{ $self->{'_memberships'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_memberships"};}) 29 1 30
92 29 ▶ $count++ if $mem->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $mem->is_cancelled 0 29 29
93 }
94 1 $self->{'_count_cancelled_memberships'} = $count;
95 } else {
96 0 my $sql = q{ SELECT COUNT(mid) FROM membership_entity WHERE status = 2 };
97 0 $self->{'_count_cancelled_memberships'} = $self->{'dbh'}->selectrow_array( $sql );
98 }
99 1 return $self->{'_count_cancelled_memberships'};
100 }
101  
102  
103 sub count_pending_memberships {
104 1 ▶ my $self = shift;
Subroutine count_pending_memberships: called CC=4 SCAR=14.5
Pod documented
105 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
106 1 my $count = 0;
107 1 ▶ while ( my ($mid, $mem) = each %{ $self->{'_memberships'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_memberships"};}) 29 1 30
108 29 ▶ $count++ if $mem->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
if $mem->is_pending 0 29 29
109 }
110 1 $self->{'_count_pending_memberships'} = $count;
111 } else {
112 0 my $sql = q{ select count(mid) from membership_entity where status = 3 };
113 0 $self->{'_count_pending_memberships'} = $self->{'dbh'}->selectrow_array( $sql );
114 }
115 1 return $self->{'_count_pending_memberships'};
116 }
117  
118  
119 sub count_set_were_renewal_memberships {
120 1 ▶ my $self = shift;
Subroutine count_set_were_renewal_memberships: called CC=4 SCAR=14.6
Pod documented
121 1 ▶ if ($self->{'_memberships'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_memberships'}) { } 1 0 1
122 1 $self->{'_count_were_renewal_memberships'} = 0;
123 1 ▶ while ( my ($mid, $mem) = each %{ $self->{'_memberships'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_memberships"};}) 29 1 30
124 29 ▶ if ( $mem->current_was_renewal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->current_was_renewal) 0 29 29
125 0 $self->{'_count_were_renewal_memberships'}++;
126 }
127 }
128 } else {
129 0 croak qq/
130 Died.
131 count_were_renewal_memberships() must be called with a set of
132 Memberships. You probably forgot to call fetch_memberships()
133 on your MembershipEntity object before calling this method.
134 /;
135 }
136 1 return $self->{'_count_were_renewal_memberships'};
137 }
138  
139  
140 sub count_daily_were_renewal_memberships {
141 0 ▶ my $self = shift;
Subroutine count_daily_were_renewal_memberships: not called CC=15 SCAR=54.8
Pod documented
142 0 my @dates = @_;
143 0 my %counts;
144  
145 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
146 Warning!
147 Called a count_daily_ method with no dates. This will return nothing!
148 / if ! @dates;
149  
150 ## First get all the terms
151 0 my $sql = qq/
152 SELECT mid, id AS tid, start, end
153 FROM membership_entity_term
154 /;
155  
156 0 my %current_mids;
157 my %current_tids;
158 0 my %ordered_terms;
159  
160 ## get all the terms from the DB
161 0 my $terms = $self->{'dbh'}->selectall_hashref( $sql, 'tid' );
162
163 0 foreach my $term ( values %{ $terms } ) {
164 # indexed by mid, but terms indexed by start time for easier sorting
165 0 $ordered_terms{ $term->{'mid'} }->{ $term->{'start'} } = $term->{'tid'};
166 }
167  
168 ## loop through the dates
169 0 foreach my $datetime ( @dates ) {
170  
171 ## find the ones that were current on the date
172 0 foreach my $term ( values %{ $terms } ) {
173 0 ▶ next unless $datetime gt $term->{'start'} and $term->{'end'} gt $datetime;
Branch: 1 branch
BranchTrueFalseTotal
unless $datetime gt $term->{"start"} and $term->{"end"} gt $datetime 0 0 0
Condition0%0/3
$datetime gt $term->{"start"} and $term->{"end"} gt $datetime
!ll&&!rl&&r
000
MC/DC0%0/2
$datetime gt $term->{"start"} and $term->{"end"} gt $datetime
$datetime gt $term->{"start"} $term->{"end"} gt $datetime
Truth table0%0/3
A and B
A= $datetime gt $term->{"start"}B= $term->{"end"} gt $datetime
ABresult
0X0
100
111
174
175 0 $current_mids{ $term->{'mid'} }++;
176 0 $current_tids{ $term->{'tid'} }++;
177 }
178  
179 # Now process each mid
180 0 foreach my $mid ( keys %ordered_terms ) {
181
182 # only keep it if it had a current term, i.e. was active
183 0 ▶ if ( ! exists $current_mids{ $mid } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $current_mids{$mid}) 0 0 0
184 0 delete $ordered_terms{ $mid };
185 0 next;
186 }
187
188 # only keep it if it has at least two terms
189 0 ▶ if ( scalar keys %{ $ordered_terms{ $mid } } < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$ordered_terms{$mid};} < 2) 0 0 0
190 0 delete $ordered_terms{ $mid };
191 0 next;
192 }
193 }
194
195 # if the mem is still here, it has a current term and more than one term.
196 # shift the earliest one off; the rest are renewals; is one of them current?
197 0 my %were_renewal_memberships;
198  
199 0 foreach my $mid ( keys %ordered_terms ) {
200
201 0 my $term_count = 0;
202
203 0 foreach my $start (sort keys %{ $ordered_terms{ $mid } } ) {
204 0 $term_count++;
205 0 ▶ next if $term_count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $term_count == 1 0 0 0
206
207 0 ▶ if ( exists $current_tids{ $ordered_terms{ $mid }->{ $start } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $current_tids{$ordered_terms{$mid}{$start}}) 0 0 0
208 0 $were_renewal_memberships{ $mid } = 1;
209 }
210 }
211 }
212  
213 0 $counts{ $datetime } = scalar keys %were_renewal_memberships;
214 }
215  
216 return (scalar keys %counts == 1) ?
217 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
218 \%counts;
219  
220 } # end sub
221  
222  
223 sub count_daily_term_expirations {
224 0 ▶ my $self = shift;
Subroutine count_daily_term_expirations: not called CC=4 SCAR=30.0
Pod documented
225 0 my @dates = @_;
226 0 my %counts;
227  
228 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
229 Warning!
230 Called a count_daily_ method with no dates. This will return nothing!
231 / if ! @dates;
232  
233 0 my $sql = qq/
234 SELECT COUNT(DISTINCT mid)
235 FROM membership_entity_term
236 WHERE end >= ?
237 AND end < ?
238 AND status NOT IN (2,3)
239 /;
240
241 0 my $sth = $self->{'dbh'}->prepare( $sql );
242  
243 0 foreach my $datetime (@dates) {
244 0 $sth->execute( $datetime, $self->datetime_plus_one_day( $datetime ) );
245 0 $counts{ $datetime } = $sth->fetchrow_array;
246 }
247  
248 return (scalar keys %counts == 1) ?
249 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
250 \%counts;
251
252 } # end sub
253  
254  
255 sub count_daily_term_activations {
256 0 ▶ my $self = shift;
Subroutine count_daily_term_activations: not called CC=4 SCAR=30.0
Pod documented
257 0 my @dates = @_;
258 0 my %counts;
259  
260 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
261 Warning!
262 Called a count_daily_ method with no dates. This will return nothing!
263 / if ! @dates;
264  
265 0 my $sql = qq/
266 SELECT COUNT(DISTINCT mid)
267 FROM membership_entity_term
268 WHERE start >= ?
269 AND start < ?
270 AND status NOT IN (2,3)
271 /;
272
273 0 my $sth = $self->{'dbh'}->prepare( $sql );
274  
275 0 foreach my $datetime (@dates) {
276 0 $sth->execute( $datetime, $self->datetime_plus_one_day( $datetime ) );
277 0 $counts{ $datetime } = $sth->fetchrow_array;
278 }
279  
280 return (scalar keys %counts == 1) ?
281 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
282 \%counts;
283
284 } # end sub
285  
286  
287 sub count_daily_new_memberships {
288 0 ▶ my $self = shift;
Subroutine count_daily_new_memberships: not called CC=4 SCAR=30.0
Pod documented
289 0 my @dates = @_;
290 0 my %counts;
291  
292 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
293 Warning!
294 Called a count_daily_ method with no dates. This will return nothing!
295 / if ! @dates;
296  
297 0 my $sql = qq/
298 SELECT COUNT(DISTINCT mid)
299 FROM membership_entity
300 WHERE created >= ?
301 AND created < ?
302 AND status NOT IN (3)
303 /;
304  
305 0 my $sth = $self->{'dbh'}->prepare( $sql );
306  
307 0 foreach my $datetime (@dates) {
308 0 my ( $start, $over ) = $self->time_plus_one_day( $datetime );
309 0 $sth->execute( $start, $over );
310 0 $counts{ $datetime } = $sth->fetchrow_array;
311 }
312  
313 return (scalar keys %counts == 1) ?
314 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
315 \%counts;
316  
317 } # end sub
318  
319  
320 sub count_daily_new_terms {
321 0 ▶ my $self = shift;
Subroutine count_daily_new_terms: not called CC=4 SCAR=30.0
Pod documented
322 0 my @dates = @_;
323 0 my %counts;
324  
325 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
326 Warning!
327 Called a count_daily_ method with no dates. This will return nothing!
328 / if ! @dates;
329  
330 0 my $sql = qq/
331 SELECT COUNT(DISTINCT mid)
332 FROM membership_entity_term
333 WHERE created >= ?
334 AND created < ?
335 AND status NOT IN (3)
336 /;
337  
338 0 my $sth = $self->{'dbh'}->prepare( $sql );
339  
340 0 foreach my $datetime (@dates) {
341 0 my ( $start, $over ) = $self->time_plus_one_day( $datetime );
342 0 $sth->execute( $start, $over );
343 0 $counts{ $datetime } = $sth->fetchrow_array;
344 }
345  
346 return (scalar keys %counts == 1) ?
347 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
348 \%counts;
349  
350 } # end sub
351  
352  
353  
354 sub count_daily_renewals {
355 0 ▶ my $self = shift;
Subroutine count_daily_renewals: not called CC=7 SCAR=40.3
Pod documented
356 0 my @dates = @_;
357 0 my %counts;
358  
359 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
360 Warning!
361 Called a count_daily_ method with no dates. This will return nothing!
362 / if ! @dates;
363  
364 0 my $sql1 = qq/
365 SELECT id, mid
366 FROM membership_entity_term
367 WHERE created >= ?
368 AND created < ?
369 AND status NOT IN (3)
370 /;
371  
372 0 my $sth1 = $self->{'dbh'}->prepare( $sql1 );
373  
374 0 my $sql2 = qq/
375 SELECT id
376 FROM membership_entity_term
377 WHERE mid = ?
378 /;
379  
380 0 my $sth2 = $self->{'dbh'}->prepare( $sql2 );
381  
382 0 foreach my $datetime (@dates) {
383 0 $counts{ $datetime } = 0;
384 0 my ( $start, $over ) = $self->time_plus_one_day( $datetime );
385 0 $sth1->execute( $start, $over );
386 0 ▶ while ( my ($id, $mid) = $sth1->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth1->fetchrow_array) 0 0 0
387 0 my $was_renewal = 0;
388 0 $sth2->execute( $mid );
389 0 for ( $sth2->fetchrow_array ) {
390 0 ▶ $was_renewal = 1 if $_ < $id;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $id 0 0 0
391 }
392 0 $counts{ $datetime } += $was_renewal;
393 }
394 }
395  
396 return (scalar keys %counts == 1) ?
397 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
398 \%counts;
399  
400 } # end sub
401  
402  
403 sub count_daily_active_memberships {
404 0 ▶ my $self = shift;
Subroutine count_daily_active_memberships: not called CC=4 SCAR=30.0
Pod documented
405 0 my @dates = @_;
406 0 my %counts;
407  
408 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
409 Warning!
410 Called a count_daily_ method with no dates. This will return nothing!
411 / if ! @dates;
412  
413 0 my $sql = qq/
414 SELECT COUNT(DISTINCT mid)
415 FROM membership_entity_term
416 WHERE start <= ? AND end > ?
417 AND status NOT IN (2,3)
418 /;
419  
420 0 my $sth = $self->{'dbh'}->prepare( $sql );
421  
422 0 foreach my $date (@dates) {
423 0 $sth->execute( $date, $date );
424 0 $counts{ $date } = $sth->fetchrow_array;
425 }
426  
427 return (scalar keys %counts == 1) ?
428 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
429 \%counts;
430  
431 } # end sub
432  
433  
434 sub count_daily_total_memberships {
435 0 ▶ my $self = shift;
Subroutine count_daily_total_memberships: not called CC=4 SCAR=30.0
Pod documented
436 0 my @dates = @_;
437 0 my %counts;
438  
439 0 ▶ carp qq/
Branch: 1 branch
BranchTrueFalseTotal
unless @dates 0 0 0
440 Warning!
441 Called a count_daily_ method with no dates. This will return nothing!
442 / if ! @dates;
443  
444 0 my $sql = qq/
445 SELECT COUNT(mid)
446 FROM membership_entity
447 WHERE created <= ?
448 AND status NOT IN (3)
449 /;
450  
451 0 my $sth = $self->{'dbh'}->prepare( $sql );
452  
453 0 foreach my $datetime (@dates) {
454 0 my ( $start, $over ) = $self->time_plus_one_day( $datetime );
455 0 $sth->execute( $over );
456 0 $counts{ $datetime } = $sth->fetchrow_array;
457 }
458  
459 return (scalar keys %counts == 1) ?
460 0 ▶ $counts{ $dates[0] } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %counts == 1 ? : 0 0 0
461 \%counts;
462  
463 } # end sub
464  
465  
466 ##################################################
467 ##
468 ## Utility subs
469 ##
470  
471  
472  
473 sub build_date_range {
474  
475 0 ▶ my $self = shift;
Subroutine build_date_range: not called CC=6 SCAR=37.4
Pod documented
476 0 my $opt_start = shift;
477 0 my $opt_end = shift;
478  
479 0 my $dt_start;
480 0 ▶ if ( ! $opt_start or $opt_start !~ m/[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt_start && $opt_start =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) { } 0 0 0
Condition0%0/3
$opt_start && $opt_start =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_start && $opt_start =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
$opt_start $opt_start =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
Truth table0%0/3
A && B
A= $opt_startB= $opt_start =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
ABresult
0X0
100
111
481 0 croak qq/
482 Died.
483 build_date_range() requires a beginning date in format 'YYYY-MM-DD'.
484 /;
485 } else {
486 0 my ($y,$m,$d) = split( '-', $opt_start);
487 0 $dt_start = DateTime->new( year => $y, month => $m, day => $d );
488 }
489 0 $dt_start->set_time_zone('UTC');
490
491 0 my $dt_end;
492 0 ▶ if ( $opt_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_end) { } 0 0 0
493 0 ▶ if ( $opt_end !~ m/[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt_end =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) { } 0 0 0
494 0 croak qq/
495 Died.
496 build_date_range() requires that the end date,
497 if supplied, be in the format 'YYYY-MM-DD'.
498 /;
499 } else {
500 0 my ($y,$m,$d) = split( '-', $opt_end);
501 0 $dt_end = DateTime->new( year => $y, month => $m, day => $d );
502 }
503 } else {
504 # default to today;
505 0 $dt_end = DateTime->now;
506 0 $dt_end->set_hour('00');
507 0 $dt_end->set_minute('00');
508 0 $dt_end->set_second('00');
509 }
510 0 $dt_end->set_time_zone('UTC');
511  
512 0 my $set = DateTime::Event::Recurrence->daily();
513 0 my $itr = $set->iterator( start => $dt_start, end => $dt_end );
514
515 0 my @dates;
516  
517 0 ▶ while ( my $dt = $itr->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dt = $itr->next) 0 0 0
518 0 push @dates, $dt->datetime;
519 }
520  
521 0 return \@dates;
522  
523 } # end sub
524  
525  
526 sub datetime_plus_one_day {
527 0 ▶ my $self = shift;
Subroutine datetime_plus_one_day: not called CC=3 SCAR=24.8
Pod documented
528 0 my $datetime = shift;
529  
530 0 ▶ if ( ! $datetime or $datetime !~ m/[0-9]{4}-[0-9]{2}-[0-9]{2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) 0 0 0
Condition0%0/3
$datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
!ll&&!rl&&r
000
MC/DC0%0/2
$datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
$datetime $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
Truth table0%0/3
A and B
A= $datetimeB= $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
ABresult
0X0
100
111
531 0 croak qq/
532 Died.
533 datetime_plus_one() requires a datetime
534 in ISO-ish format (YYYY-MM-DDTHH:MM:SS).
535 /;
536 }
537  
538 0 my ($y, $m, $d) = split /[-| |T|:]/, $datetime;
539 0 return DateTime->new( year => $y, month => $m, day => $d )
540 ->set_time_zone( 'UTC' )
541 ->clone()
542 ->add( days => 1 )
543 ->datetime();
544  
545 } # end sub
546  
547  
548  
549 sub time_plus_one_day {
550 0 ▶ my $self = shift;
Subroutine time_plus_one_day: not called CC=3 SCAR=24.8
Pod documented
551 0 my $datetime = shift;
552  
553 0 ▶ if ( ! $datetime or $datetime !~ m/[0-9]{4}-[0-9]{2}-[0-9]{2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) 0 0 0
Condition0%0/3
$datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
!ll&&!rl&&r
000
MC/DC0%0/2
$datetime and $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
$datetime $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
Truth table0%0/3
A and B
A= $datetimeB= $datetime =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
ABresult
0X0
100
111
554 0 croak qq/
555 Died.
556 time_plus_one() requires a datetime in
557 ISO-ish format (YYYY-MM-DDTHH:MM:SS).
558 /;
559 }
560  
561 0 my @dateparts = reverse ( split /[-| |T|:]/, $datetime );
562 0 $dateparts[4]--;
563 0 my $time = timelocal( @dateparts );
564 0 my $plus_one = ($time + (24*3600));
565  
566 0 return( $time, $plus_one );
567  
568 } # end sub
569  
570  
571 sub report_yesterday {
572 0 ▶ my $self = shift;
Subroutine report_yesterday: not called CC=4 SCAR=30.0
Pod documented
573 0 my %args = @_;
574
575 0 my %methods = map { $_ => 1 } ( qw/
576 count_daily_total_memberships
577 count_daily_total_memberships
578 count_daily_term_expirations
579 count_daily_term_activations
580 count_daily_new_memberships
581 count_daily_new_terms
582 count_daily_active_memberships
583 count_daily_renewals
584 count_daily_were_renewal_memberships
585 /);
586  
587 0 ▶ if ( $args{'exclude'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"exclude"}) 0 0 0
588 0 delete $methods{ $_ } for @{ $args{'exclude'} };
589 }
590  
591 0 my $yesterday = DateTime->now()
592 ->clone()
593 ->subtract(days => 1)
594 ->set( hour => 0, minute => 0, second => 0 )
595 ->datetime();
596
597 0 my %data;
598  
599 0 for ( keys %methods ) {
600 0 $data{ $_ } = $self->$_( $yesterday );
601 }
602
603 0 $data{ 'date' } = $yesterday;
604  
605 0 return \%data;
606  
607 } # end sub
608  
609  
610 2 1; ## return true to end package CMS::Drupal::Modules::MembershipEntity
611  
612 __END__
613  
614 =pod
615  
616 =encoding UTF-8
617  
618 =head1 NAME
619  
620 CMS::Drupal::Modules::MembershipEntity::Stats - Generate statistics about MembershipEntity memberships on a Drupal site.
621  
622 =head1 VERSION
623  
624 version 0.96
625  
626 =head1 SYNOPSIS
627  
628 use CMS::Drupal::Modules::MembershipEntity;
629 use CMS::Drupal::Modules::MembershipEntity::Stats { into => 'CMS::Drupal::Modules::MembershipEntity' };
630
631 my $ME = CMS::Drupal::Modules::MembershipEntity->new( dbh => $dbh );
632 $ME->fetch_memberships();
633
634 print $ME->count_active_memberships;
635 print $ME->pct_active_memberships_were_renewal;
636
637 ...
638  
639 =head1 DESCRIPTION
640  
641 This module provides some basic statistical analysis about your Drupal
642 site Memberships. It operates on the set of Memberships contained in
643 $ME->{'_memberships'} in other words whichever ones you fetched with
644 your call to $ME->fetch_memberships().
645  
646 It has some methods for doing retroactive reporting on the DB records
647 so you can initialize a reporting system with some statistical
648 baselines.
649  
650 See L<CMS::Drupal::Modules::MembershipEntity::Cookbook|the Cookbook>
651 for more information and examples of usage.
652  
653 =head1 METHODS
654  
655 =head2 count_total_memberships( )
656  
657 Returns the number of Memberships in the set.
658  
659 =head2 count_expired_memberships( )
660  
661 Returns the number of Memberships from the set that have status of 'expired'.
662  
663 =head2 count_active_memberships( )
664  
665 Returns the number of Memberships from the set that have status of 'active'.
666  
667 =head2 count_cancelled_memberships( )
668  
669 Returns the number of Memberships from the set that have status of 'cancelled'.
670  
671 =head2 count_pending_memberships( )
672  
673 Returns the number of Memberships from the set that have status of 'pending'.
674  
675 =head2 count_set_were_renewal_memberships( )
676  
677 Returns the number of Memberships from the set whose current Term was a renewal.
678  
679 Dies if $ME->{'_memberships'} is not defined.
680  
681 =head2 count_daily_were_renewal_memberships( @list_of_dates )
682  
683 Returns the number of Memberships within the set that were renewals,
684 i.e. whose currently active term was not the first term, on a given
685 date, or range of dates. Takes a date-time or a range of date-times
686 in ISO-ish format.
687  
688 Returns a scalar value when called with one date, or a hashref of
689 counts indexed by dates, if called with an array of date-times.
690  
691 =head2 count_daily_term_expirations( @list_of_dates )
692  
693 Returns the number of Membership Terms belonging to Members
694 in the set that expired in the 24-hour period beginning with
695 the date supplied. Takes dates in ISO-ish format.
696  
697 Returns a scalar when called with one date, or a hashref of counts
698 indexed by dates, if called with an array of date-times.
699  
700 =head2 count_daily_term_activations( @list_of_dates )
701  
702 Returns the number of Membership Terms belonging to Members in the
703 set that began in the 24-hour period beginning with the date
704 supplied. Takes dates in ISO-ish format.
705  
706 Returns a scalar value when called with one date, or a hashref of
707 counts indexed by dates, if called with an array of date-times.
708  
709 =head2 count_daily_new_memberships( @list_of_dates )
710  
711 Returns the number of Memberships in the set that were created in the
712 24-hour period beginning with the date supplied. Takes dates in ISO-ish
713 format.
714  
715 Returns a scalar value when called with one date, or a hashref of
716 counts indexed by dates, if called with an array of date-times.
717  
718 =head2 count_daily_new_terms( @list_of_dates )
719  
720 Returns the number of Terms (belonging to Memberships in the set)
721 that were created in the 24-hour period beginning with the date
722 supplied.
723  
724 Returns a scalar value when called with one date, or a hashref of
725 counts indexed by dates, if called with an array of date-times.
726  
727 =head2 count_daily_renewals( @list_of_dates )
728  
729 Retruns the number of Membership Terms belonging to Memberships in
730 the set that were created in the 24-hour period beginning with the
731 date supplied and that were the second or subsequent Term belonging
732 to that Membership.
733  
734 Returns a scalar value when called with one date, or a hashref of
735 counts indexed by dates, if called with an array of date-times.
736  
737 =head2 count_daily_active_memberships( @list_of_dates )
738  
739 Returns the number of Memberships within the set with status of
740 'active' on a given date, or range of dates. Takes a date-time
741 or a range of date-times in ISO-ish format.
742  
743 Returns a scalar value when called with one date, or a hashref of
744 counts indexed by dates, if called with an array of date-times.
745  
746 Note that this report may not be 100% accurate, as data in the DB
747 may have changed since a given date,particularly the status
748 of Terms.
749  
750 =head2 count_daily_total_memberships( @list_of_dates )
751  
752 Returns the total number of Memberships (including those that
753 have expired) that had been created by the end of the 24-hour
754 period beginning with the datetime supplied.
755  
756 Returns a scalar value when called with one date, or a hashref of
757 counts indexed by dates, if called with an array of datetimes.
758  
759 =head2 build_date_range( $datetime [, $datetime2] )
760  
761 Builds a range of dates in ISO 8601 format. Takes dates in YYYY-MM-DD
762 format. First date is the earliest date in the range. Second date is
763 the latest date in the range: if omitted, this defaults to today's
764 date. Returns an arrayref of datetime strings.
765  
766 =head2 datetime_plus_one_day( $datetime )
767  
768 Returns a timestamp representing the datetime one day
769 after the datetime supplied.
770  
771 =head2 time_plus_one_day( $datetime )
772  
773 Returns a pair of epoch timestamps representing the datetime supplied
774 and the datetime 24 hours later.
775  
776 =head2 report_yesterday( [exclude => \@excluded_methods] )
777  
778 This convenience method returns a hashref with the statistics from the
779 day before today. If called with no arguments it returns data from
780 all the count_daily_*() methods. Hashref returned includes the
781 datetime used, indexed with 'date'.
782  
783 Optionally takes an argument 'exclude', the value of which must be an
784 anonymous array of method names you wish to exclude from the data
785 returned.
786  
787 =head1 AUTHOR
788  
789 Nick Tonkin <tonkin@cpan.org>
790  
791 =head1 COPYRIGHT AND LICENSE
792  
793 This software is copyright (c) 2015 by Nick Tonkin.
794  
795 This is free software; you can redistribute it and/or modify it under
796 the same terms as the Perl 5 programming language system itself.
797  
798 =cut
↑ Summary lib/CMS/Drupal/Modules/MembershipEntity.pm »