lib/CMS/Drupal/Modules/MembershipEntity/Cookbook.pm untested

filter: Statementstmt 0.0% 0 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 10 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package CMS::Drupal::Modules::MembershipEntity::Cookbook;
2 $CMS::Drupal::Modules::MembershipEntity::Cookbook::VERSION = '0.96';
3 # ABSTRACT: Guide and tutorials for using the Perl-Drupal Membership Entity interface
4  
5 use strict;
6 use warnings;
7  
8  
9  
10  
11  
12 1; # return true
13  
14 __END__
15  
16 =pod
17  
18 =encoding UTF-8
19  
20 =head1 NAME
21  
22 CMS::Drupal::Modules::MembershipEntity::Cookbook - Guide and tutorials for using the Perl-Drupal Membership Entity interface
23  
24 =head1 VERSION
25  
26 version 0.96
27  
28 =head1 SYNOPSIS
29  
30 This manual contains a collection of tutorials and tips for using
31 the CMS::Drupal::Modules::MembershipEntity distribution.
32  
33 =head1 DESCRIPTION
34  
35 The individual packages in the CMS::Drupal::Modules::MembershipEntity
36 distribution each have their own POD of course, but the author hopes
37 that this documentation will help a new user put it all together.
38 Maybe you are a non-programmer or a non-Perl user and you are here
39 because you use Drupal's MembershipEntity modules and you need the
40 additional tools this distribution provides.
41  
42 =head2 Code examples
43  
44 In the interests of brevity and readability I have omitted the standard
45 opening lines from the code samples below. If you are copy-pasting the
46 examples and trying them out on your system, you should prepend the
47 following to each snippet:
48  
49 #!/usr/bin/perl -w
50 use strict;
51 use feature 'say';
52  
53 The examples also skip the "use Foo::Bar"" lines, except for those
54 examples in each section that specifically describe how to "use"
55 them. You'll need to include those lines in your code too!
56  
57 Note that these examples use the feature "say" which became available in
58 Perl v5.10 ... you can of course replace with "print" if you like: I
59 prefer "say" in examples (and in my code!) because you can omit the
60 newlines and their quotation marks.
61  
62 =head1 INSTALLATION AND TESTING
63  
64 Install the modules however you normally do. The easiest way is to get
65 them from CPAN:
66  
67 $ cpan install CMS::Drupal
68 $ cpan install CMS::Drupal::Modules::MembershipEntity
69  
70 If you want the modules to test themselves against your Drupal database
71 you will need to set the DRUPAL_TEST_CREDS environment variable as
72 described in the Testing section of the documentation for the parent
73 CMS::Drupal module. Essentially you will need to provide at least
74 the database name and driver, so your minimum testing config would be:
75  
76 $ set DRUPAL_TEST_CREDS=database,foo,driver,Pg
77  
78 or something similar.
79  
80 If you come to this tutorial after you've installed the modules you
81 can still test by finding the CPAN build directory and just running
82  
83 $ perl t/02_valid_drupal.t
84  
85 =head1 USING THE MODULES
86  
87 You'll need to load the libraries in any script or program you write.
88 First you need to get a connection to your Drupal database. This is
89 done through the L<CMS::Drupal|CMS::Drupal> parent module:
90  
91 #!/usr/bin/perl -w
92 use strict;
93 use CMS::Drupal;
94
95 my $drupal = CMS::Drupal->new;
96 my $dbh = $drupal->dbh(
97 'database' => "my_db",
98 'driver' => "mysql",
99 'username' => "my_user",
100 'password' => "my_password"
101 );
102  
103 Once you have a DB connection you can make use of the Membership Entity
104 modules. Again, you'll have to load them with use():
105  
106 #!/usr/bin/perl -w
107 use strict;
108 use CMS::Drupal;
109 use CMS::Drupal::Modules::MembershipEntity;
110  
111 my $drupal = CMS::Drupal->new;
112 my $dbh = $drupal->dbh(
113 'database' => "my_db",
114 'driver' => "mysql",
115 'username' => "my_user",
116 'password' => "my_password"
117 );
118
119 my $ME = CMS::Drupal::Modules::MembershipEntity->new( dbh => $dbh );
120 my $h = $ME->fetch_memberships('all');
121  
122 Now you have a hashref containing all our Memberships, and you can work
123 with each one:
124  
125 foreach my $mid ( keys %{ $h } ) {
126
127 if ( $h->{ $mid }->is_active ) {
128 $active_count++;
129 }
130
131 # pass the Membership object to a sub you wrote
132 frobinate_member( $h->{ $mid } );
133  
134 }
135  
136 If you just want to fetch one Membership, just pass a single mid to the
137 fetch_memberships() method, and you'll get a single object returned
138 instead of a hashref:
139  
140 my $mem = $ME->fetch_memberships( 12345 );
141 print $mem->type;
142 frobinate_member( $mem );
143  
144 =head1 DATA ANALYSIS
145  
146 The creation of this distribution was originally motivated by the lack of
147 tools for doing even rudimentary data analysis in Drupal's MembershipEntity
148 modules. This section explains the various ways it can help you with that.
149  
150 =head1 Stats.pm
151  
152 The main tool for analyzing your Membership base is the module
153 L<CMS::Drupal::Modules::MembershipEntity::Stats|Stats>.
154  
155 =head2 Usage and Importing Methods
156  
157 Start out by using Stats.pm in your program after you have loaded
158 the MembershipEntity modules as decribed above.
159  
160 Notice that because Stats.pm exports all its useful methods, you can
161 import them into MembershipEntity.pm and thus make them available in
162 your $ME object:
163  
164 #!/usr/bin/perl -w
165 use strict;
166 use CMS::Drupal;
167 use CMS::Drupal::Modules::MembershipEntity;
168 use CMS::Drupal::Modules::MembershipEntity::Stats
169 { into => 'CMS::Drupal::Modules::MembershipEntity' };
170
171 my $drupal = CMS::Drupal->new;
172 my $dbh = $drupal->dbh;
173 my $ME = CMS::Drupal::Modules::MembershipEntity->new( dbh => $dbh );
174 $ME->fetch_memberships('all');
175  
176 my $active = $ME->count_active_memberships;
177 say "We have $active active Memberships";
178  
179 =head2 Types of methods
180  
181 The methods in Stats.pm can be grouped into three basic categories,
182 which are signalled through the prefix of the methods' names:
183  
184 =over 4
185  
186 =item *
187  
188 count_
189  
190 These methods may be called with or without a set of Memberships. If
191 called without a set (see below), they will operate on all your
192 Memberships.
193  
194 =item *
195  
196 count_set_
197  
198 These methods B<must> be called with a set of Memberships.
199  
200 =item *
201  
202 count_daily_
203  
204 These methods B<must> be called with a range of dates (see below), and
205 may be called with a set of Memberships (see below).
206  
207 =back
208  
209 =head2 Working with a set of Memberships
210  
211 Methods whose names begine with the prefix 'count_set_' must be called
212 with a set of Memberships, even if you want all Memberships included.
213 The set of Memberships is already contained in your MembershipEntity
214 object if you called fetch_memberships() on your MembershipEntity object
215 as described above:
216  
217 my @mids = ( 123, 456, 665, 667 );
218 $ME->fetch_memberships( @mids );
219  
220 or
221  
222 $ME->fetch_memberships('all');
223  
224 If you do not call fetch_memberships() first, calling a method whose
225 name begins with the prefix 'count_set_' will cause your program to
226 die.
227  
228 On the other hand, if you want to limit the statistics returned by
229 a 'count_' method or 'count_daily_' method to a subset of your
230 Memberships, you can do so by first calling fetch_memberships() with
231 a list of mids.
232  
233 =head2 Performance
234  
235 For reasons of performance, and especially if you have a lot of
236 Memberships, you should use Stats.pm B<without> first calling
237 fetch_memberships(), if you do not need to use any of the
238 count_set_*() methods. This is because it will get its counts
239 by querying the database directly rather than by instantiating several
240 objects for each Membership.
241  
242 If you are working with stats for your entire Membership base, you
243 should therefore not call fetch_memberships(). If you want to use
244 the count_set_*() methods on all your Memberships, you will have to
245 call fetch_memberships('all') first, but you can then remove the set
246 and go back to the fast methods as shown below
247  
248 my $all = $ME->count_all_memberships; # fast
249
250 # load the Memberships into a hashref
251 $ME->fetch_memberships('all');
252
253 # get a count that calls methods on the objects
254 my $num = $ME->count_set_were_renewal_memberships();
255
256 delete $ME->{'_hashref'};
257  
258 my $active = $ME->count_active_memberships; # back to the fast way
259  
260 =head2 Working with dates
261  
262 The methods in Stats.pm whose names begin with the prefix count_daily_
263 return counts for a date or range of dates. They can be optionally
264 limited to search on a set of Memberships if the fetch_memberships()
265 method is called in advance. These methods take dates in ISOish
266 format, i.e. something like:
267  
268 2001-01-01T12:00:00
269  
270 (called ISO-ish because there is no time zone, as the ISO 8601 format
271 specifies).
272  
273 You can use these methods to look at a date in the past:
274  
275 my $num =
276 $ME->count_daily_expired_memberships('2015-06-15T00:00:00');
277  
278 or at a range of dates:
279  
280 my $counts =
281 $ME->count_daily_expired_memberships('2015-06-15T00:00:00',
282 '2015-07-15T00:00:00',
283 '2015-08-15T00:00:00');
284
285 while (my ($date, $count) = each %{ $counts }) {
286 $quarterly_total += $count;
287 ...
288 }
289  
290 =head3 What time to use?
291  
292 If you are forensically building a record of your Memberships, you
293 should probably set the time element of your date(s) to 00:00:00,
294 since the searchlooks for Terms with a start date <= and an end
295 date > the date given, and this strategy will give a daily report
296 that is closest to the historical truth if you store your statistics
297 indexed by a date.
298  
299 =head3 Building a range of dates
300  
301 Working with dates can be cumbersome, and typing them manually is
302 prone to errors. The Stats.pm module provides a method to build an
303 array of datetime strings that you can pass to its other methods.
304  
305 You call it with either one or two dates: the first is required
306 and is the start date in the range. The second argument, if provided,
307 is used for the end date in the range. If no end date is provided,
308 the module will use today's date. Times are set to 00:00:00.
309  
310 my @dates = $ME->build_date_range('2014-01-01','2014-12-31');
311 # one year's worth of dates
312  
313 my @dates = $ME->build_date_range('2001-01-01);
314 # Every date in the millenium so far
315  
316 =head3 An example
317  
318 Here's an example of a simple program that reports the previous
319 week's statistics.
320  
321 #!/usr/bin/perl -w
322 use strict;
323 use DateTime;
324 use CMS::Drupal;
325 use CMS::Drupal::Modules::MembershipEntity;
326 use CMS::Drupal::Modules::MembershipEntity::Stats
327 { into => 'CMS::Drupal::Modules::MembershipEntity' };
328  
329 my $drupal = CMS::Drupal->new;
330 my $dbh = $drupal->dbh;
331 my $ME = CMS::Drupal::Modules::MembershipEntity->new({dbh => $dbh});
332  
333 my $today = DateTime->now()->set_time_zone('UTC');
334 my $a_week_ago = $today->subtract( days => 6 )->ymd();
335 # 6 days because the set includes today
336  
337 my @days = @{ $ME->build_date_range( $a_week_ago ) };
338  
339 my $report = <<EOT;
340 MembershipEntity Report
341 ---------------------------
342 Date Exp New Active
343 ---------------------------
344 EOT
345  
346 my $exp = $ME->count_daily_term_expirations( @days );
347 my $new = $ME->count_daily_new_terms( @days );
348 my $act = $ME->count_daily_active_memberships( @days );
349  
350 foreach my $date ( @days ) {
351 my @line = substr $date, 0, 10;
352 for ( $exp, $new, $act ) {
353 push @line, $_->{ $date };
354 }
355 $report .= (join ' | ', @line) . "\n";
356 }
357 $report .= '-' x 25 . "\n";
358
359 print $report;
360  
361 __END__
362  
363 This program outputs something like:
364  
365 MembershipEntity Report
366 ---------------------------
367 Date Exp New Active
368 ---------------------------
369 2015-07-09 | 0 | 0 | 580
370 2015-07-10 | 0 | 0 | 580
371 2015-07-11 | 0 | 1 | 580
372 2015-07-12 | 0 | 1 | 580
373 2015-07-13 | 1 | 0 | 581
374 2015-07-14 | 0 | 1 | 580
375 2015-07-15 | 1 | 0 | 580
376 2015-07-16 | 0 | 0 | 580
377 -------------------------
378  
379 Now all you have to do is get the data for multiple weeks, sum
380 up the daily totals for the week, and you have the beginnings
381 of a useful report!
382  
383 =head3 Automated daily report
384  
385 If you want to build up a statistical record about your Memberships,
386 you'll probably want to export counts from the database into a
387 spreadsheet or other local database, so you can analyse them and make
388 graphs and so on.
389  
390 The simplest way to do this is to write a program similar to the one
391 above and run it from cron every day. The Stats.pm module provides
392 a handy method for retrieving yesterday's data.
393  
394 If you call $ME->report_yesterday() you'll get an anonymous hash
395 like this:
396  
397 {
398 'count_daily_term_expirations' => '1',
399 'count_daily_new_memberships' => '2',
400 'count_daily_were_renewal_memberships' => '224',
401 'count_daily_total_memberships' => '1498',
402 'count_daily_new_terms' => '3',
403 'count_daily_term_activations' => '2',
404 'count_daily_active_memberships' => '580',
405 'count_daily_renewals' => '2',
406 'date' => '2013-02-14T00:00:00'
407 }
408  
409 You can optionally pass an anonymous array of method names that you
410 want exlcuded from the output. Check the documentation for Stats.pm
411 for a list of all the count_daily_*() methods.
412  
413 So now you can do something in your nightly program something like:
414  
415 ## Assumes you have a database with a table called 'daily'
416  
417 my $sql = qq/
418 INSERT INTO daily
419 ('date', 'active', 'expirations', 'new_terms', 'new_memberships', 'renewals')
420 VALUES ( ?, ?, ?, ?, ?, ? )
421 /;
422  
423 my $sth = $dbh->prepare( $sql );
424  
425 my @exclude = qw/ count_daily_total_memberships
426 count_daily_term_activations
427 count_daily_were_renewal_memberships /;
428  
429 my $yesterday = $ME->report_yesterday( \@exclude );
430  
431 $sth->execute( $yesterday->{'date'},
432 $yesterday->{'count_daily_active_memberships'},
433 $yesterday->{'count_daily_term_expirations'},
434 $yesterday->{'count_daily_new_terms'},
435 $yesterday->{'count_daily_new_memberships'},
436 $yesterday->{'count_daily_renewals'} );
437  
438 . . . or of course you could send an email, update a text log, whatever...
439  
440 =head1 AUTHOR
441  
442 Nick Tonkin <tonkin@cpan.org>
443  
444 =head1 COPYRIGHT AND LICENSE
445  
446 This software is copyright (c) 2015 by Nick Tonkin.
447  
448 This is free software; you can redistribute it and/or modify it under
449 the same terms as the Perl 5 programming language system itself.
450  
451 =cut