Merge lp:~omacneil/mvhub/cron_script_to_set_program_max_last_update_1277300 into lp:~rpulaparti/mvhub/cron_script_to_set_program_max_last_update_1277300

Proposed by Dan MacNeil
Status: Merged
Merged at revision: 686
Proposed branch: lp:~omacneil/mvhub/cron_script_to_set_program_max_last_update_1277300
Merge into: lp:~rpulaparti/mvhub/cron_script_to_set_program_max_last_update_1277300
Diff against target: 459 lines (+93/-132)
15 files modified
app-mvhub/DocumentRoot/static/mvh/html/links.shtml (+1/-6)
app-mvhub/DocumentRoot/static/mvh/inc/footer.inc (+1/-1)
app-mvhub/DocumentRoot/static/nsp/html/links.shtml (+8/-13)
app-mvhub/DocumentRoot/static/nsp/inc/footer.inc (+1/-1)
app-mvhub/bin/sync_program_update_dates (+7/-4)
app-mvhub/bin/sync_program_update_dates_and_send_notification_email (+6/-2)
app-mvhub/conf/sql_select.lib (+20/-10)
app-mvhub/conf/sql_update.lib (+1/-2)
app-mvhub/project-tools/templates/template.conf (+1/-0)
app-mvhub/setup/etc/cron.d/mvhub-cron (+3/-3)
lib-mvhub/lib/MVHub.pm (+2/-2)
lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm (+26/-69)
lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t (+14/-19)
lib-mvhub/t/conf/all.conf (+1/-0)
lib-mvhub/t/lib/TestData.pm (+1/-0)
To merge this branch: bzr merge lp:~omacneil/mvhub/cron_script_to_set_program_max_last_update_1277300
Reviewer Review Type Date Requested Status
RPulaparti Pending
Review via email: mp+265915@code.launchpad.net

Description of the change

merged trunk

eleminated some branches / duplicate code

better complies with wiki.thecsl.org/mediawiki/index.php/Standards:Code_style_guide

added path config so scripts run from more than ./

added exit 0 so sripts return NO_ERROR if they get to the end without trouble

changed SQL so print and do can use the same queries and what is printed is what is updated to facilitate eyeball sanity check / dry running

BUGS:

I added a lot of over-head calling sub routines and doing individual SQL UPDATE statements. If we were updating millions instead of 10s of records, this would be a big deal. As it is, the sync_* scripts take less than a second to run.

TDOO

You will have to run:

mv_update_development

   to bring the conf file changes into your enviroment.

You might run in the --execute mode and eyeball the datbase before and after , by our design it runs without feedback ( so as not to force cron to send emails when everything works)

Everytime you run:

  mv_update_development

...it refreshes the big postgresql test data.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'app-mvhub/DocumentRoot/static/mvh/html/links.shtml'
2--- app-mvhub/DocumentRoot/static/mvh/html/links.shtml 2014-01-05 16:38:45 +0000
3+++ app-mvhub/DocumentRoot/static/mvh/html/links.shtml 2015-07-26 16:05:28 +0000
4@@ -45,12 +45,7 @@
5 highest paid employees at an agency. Not directly
6 oriented to finding services.
7 </p>
8- <p><a href="http://www.catalogueforphilanthropy.org/">
9- Massachusetts Philanthropic Directory
10- </a><br>
11- Primary audience is donors.
12- 3500+ private initiatives oriented toward the public good.
13- </p>
14+
15 <p><a href="http://bostonnavigator.org/">
16 Boston Navigator
17 </a><br>
18
19=== modified file 'app-mvhub/DocumentRoot/static/mvh/inc/footer.inc'
20--- app-mvhub/DocumentRoot/static/mvh/inc/footer.inc 2014-01-09 19:45:44 +0000
21+++ app-mvhub/DocumentRoot/static/mvh/inc/footer.inc 2015-07-26 16:05:28 +0000
22@@ -10,7 +10,7 @@
23 <!-- start footer.inc -->
24 <div id='footer'>
25 &copy; 2004 <a href="http://advancementplus.org/">Advancement Plus </a>
26- &copy; 2007-2014
27+ &copy; 2007-2015
28 <a href="http://thecsl.org">Community Software Lab</a>
29 </div>
30 <script type="text/javascript">
31
32=== modified file 'app-mvhub/DocumentRoot/static/nsp/html/links.shtml'
33--- app-mvhub/DocumentRoot/static/nsp/html/links.shtml 2014-01-05 16:38:45 +0000
34+++ app-mvhub/DocumentRoot/static/nsp/html/links.shtml 2015-07-26 16:05:28 +0000
35@@ -48,13 +48,6 @@
36 oriented to finding services.
37 </p>
38
39- <p><a href="http://www.catalogueforphilanthropy.org/">
40- Massachusetts Philanthropic Directory
41- </a><br>
42- Primary audience is donors.
43- 3500+ private initatives
44- oriented toward the public good.
45- </p>
46 <p><a href="http://www.bostonnavigator.org/">
47 Boston Navigator
48 </a><br>
49@@ -63,12 +56,14 @@
50 school, on week ends and during summer.
51 </p>
52
53- <p><a href="http://eccf.org/nonprofitdirectory113.html">ECCF
54- Nonprofit Directory</a>
55- <br>
56- Essex Count Community Foundation's Directory of
57- Non-Profits in Essex County Massachusetts,
58- including the North Shore.
59+ <p>
60+ <a href="http://eccf.org/npd">
61+ ECCF Nonprofit Directory
62+ </a>
63+ <br>
64+ Essex Count Community Foundation's Directory of
65+ Non-Profits in Essex County Massachusetts,
66+ including the North Shore.
67 </p>
68
69 </div><!-- id='content' -->
70
71=== modified file 'app-mvhub/DocumentRoot/static/nsp/inc/footer.inc'
72--- app-mvhub/DocumentRoot/static/nsp/inc/footer.inc 2014-01-09 19:45:44 +0000
73+++ app-mvhub/DocumentRoot/static/nsp/inc/footer.inc 2015-07-26 16:05:28 +0000
74@@ -9,7 +9,7 @@
75 <!-- start footer.inc -->
76 <div id='footer'>
77 &copy; 2009-2014 <a href="http://nscap.org/">North Shore Community Action Programs </a>
78- &copy; 2002-2014
79+ &copy; 2002-2015
80 <a href="http://thecsl.org">Community Software Lab</a>
81 </div>
82 <!-- stop footer.inc -->
83
84=== renamed file 'app-mvhub/bin/sync_program_update_dates.pl' => 'app-mvhub/bin/sync_program_update_dates'
85--- app-mvhub/bin/sync_program_update_dates.pl 2015-06-14 07:34:17 +0000
86+++ app-mvhub/bin/sync_program_update_dates 2015-07-26 16:05:28 +0000
87@@ -19,7 +19,7 @@
88
89 use MVHub::Utils::DB;
90 use MVHub::Utils::SyncProgramUpdateDates
91- qw(sync_program_dates do_sql print_sql);
92+ qw( print_sync do_sync sync_program_dates );
93
94 { # main
95 # Command Line option handling
96@@ -34,11 +34,14 @@
97
98 if ($EXECUTE) {
99 MVHub::Utils::SyncProgramUpdateDates::sync_program_dates( $dbh,
100- \&do_sql );
101+ \&do_sync );
102 }
103 else {
104+ printf "%-15s %-9s %-41s %-15s \n",
105+ qw/max_update_date agency_id contact_email update_date /;
106 MVHub::Utils::SyncProgramUpdateDates::sync_program_dates( $dbh,
107- \&print_sql );
108+ \&print_sync );
109 }
110-
111 }
112+
113+exit 0 # 0 is UNIX for 'no error'
114
115=== renamed file 'app-mvhub/bin/update_dates_and_send_notification_emails.pl' => 'app-mvhub/bin/sync_program_update_dates_and_send_notification_email'
116--- app-mvhub/bin/update_dates_and_send_notification_emails.pl 2015-06-14 07:34:17 +0000
117+++ app-mvhub/bin/sync_program_update_dates_and_send_notification_email 2015-07-26 16:05:28 +0000
118@@ -27,10 +27,13 @@
119 'execute' => \$EXECUTE
120 ) or die "GetOptions failed";
121
122+my $CFG = MVHub::Wrap::ConfigSimple->new($CFG_FILENAME);
123+my $BIN_DIR = $CFG->param('ABSOLUTE_PATH.bin_dir');
124+
125 my @update_dates_args
126- = ( './sync_program_update_dates.pl', '--config', $CFG_FILENAME );
127+ = ( "$BIN_DIR/sync_program_update_dates.pl", '--config', $CFG_FILENAME );
128 my @notification_email_args
129- = ( './notification_email.pl', '--config', $CFG_FILENAME );
130+ = ( "$BIN_DIR/notification_email.pl", '--config', $CFG_FILENAME );
131
132 if ($EXECUTE) {
133 push( @update_dates_args, '--execute' );
134@@ -40,3 +43,4 @@
135 ( system(@update_dates_args) == 0 ) or die "Sync program dates failed.\n";
136 ( system(@notification_email_args) == 0 ) or die "Notification emails failed";
137
138+exit 0 # zero is unix for 'no error'
139
140=== modified file 'app-mvhub/conf/sql_select.lib'
141--- app-mvhub/conf/sql_select.lib 2015-05-03 16:04:46 +0000
142+++ app-mvhub/conf/sql_select.lib 2015-07-26 16:05:28 +0000
143@@ -826,16 +826,6 @@
144 WHERE agency_id = ? AND hidden_reason IS NULL
145 ORDER BY program_name
146
147-[PROGRAM_X_AGENCY_MAX_LAST_UPDATED]
148-SELECT
149- agency_id, contact_email, MAX(last_updated)
150-FROM
151- program
152-GROUP BY
153- agency_id,contact_email
154-ORDER BY
155- agency_id,contact_email
156-
157 [PROGRAM_X_AGENCY_PROGRAM_ID]
158 SELECT *
159 FROM program
160@@ -947,6 +937,26 @@
161 program
162 WHERE program_id = ?
163
164+[PROGRAM_X_LAST_UPDATE_MAX_LAST_UPDATE]
165+ SELECT
166+ pm.max_last_updated, p.agency_id,LOWER(p.contact_email) AS contact_email,p.last_updated AS update_need_sync
167+ FROM
168+ program p
169+ INNER JOIN
170+ (
171+ SELECT
172+ agency_id, LOWER(contact_email) AS contact_email, MAX(last_updated) AS max_last_updated
173+ FROM
174+ program p
175+ GROUP BY
176+ agency_id,contact_email
177+ ORDER BY
178+ agency_id,contact_email
179+ ) pm ON
180+ p.agency_id = pm.agency_id
181+ AND LOWER(p.contact_email) = pm.contact_email
182+ AND p.last_updated < pm.max_last_updated
183+
184 [PROGRAM_X_NAMES_THAT_REPEAT_AGENCY_NAME]
185 SELECT program_name, agency_name FROM program p, agency a
186 WHERE p.agency_id = a.agency_id
187
188=== modified file 'app-mvhub/conf/sql_update.lib'
189--- app-mvhub/conf/sql_update.lib 2015-05-03 16:04:46 +0000
190+++ app-mvhub/conf/sql_update.lib 2015-07-26 16:05:28 +0000
191@@ -90,5 +90,4 @@
192 last_updated = ?
193 WHERE agency_id = ?
194 AND contact_email = ?
195- AND last_updated < ?
196-
197\ No newline at end of file
198+ AND last_updated = ?
199
200=== modified file 'app-mvhub/project-tools/templates/template.conf'
201--- app-mvhub/project-tools/templates/template.conf 2012-12-29 16:37:34 +0000
202+++ app-mvhub/project-tools/templates/template.conf 2015-07-26 16:05:28 +0000
203@@ -118,6 +118,7 @@
204
205 [RELATIVE_PATH]
206 conf_dir=link-to-live-code/app-mvhub/conf/
207+bin_dir=link-to-live-code/app-mvhub/bin/
208 document_root=link-to-live-code/app-mvhub/DocumentRoot/
209 template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
210 template_html_dir=link-to-live-code/app-mvhub/conf/templates/html/
211
212=== modified file 'app-mvhub/setup/etc/cron.d/mvhub-cron'
213--- app-mvhub/setup/etc/cron.d/mvhub-cron 2015-05-17 01:05:47 +0000
214+++ app-mvhub/setup/etc/cron.d/mvhub-cron 2015-07-26 16:05:28 +0000
215@@ -36,11 +36,11 @@
216 35 2 * * * production $BIN_DIR/sitemap_generate.pl --config=$CONF_DIR/$NSP_CONF_FILE
217
218 # restarting apache because we rotate/resolved logs and it is
219-# bad karma to keep writing to renamed file
220+# bad karma to keep writing to re-named file
221 30 0 * * * root /usr/sbin/apache2ctl graceful
222
223 45 0 * * * root $BIN_DIR/rm_session_files $CONF_DIR/$MVH_CONF_FILE
224 46 0 * * * root $BIN_DIR/rm_session_files $CONF_DIR/$NSP_CONF_FILE
225
226-51 8 1,15 * * production $BIN_DIR/update_dates_and_send_notification_emails.pl --config=$CONF_DIR/$MVH_CONF_FILE --execute
227-59 8 1,15 * * production $BIN_DIR/update_dates_and_send_notification_emails.pl --config=$CONF_DIR/$NSP_CONF_FILE --execute
228+51 8 1,15 * * production $BIN_DIR/sync_program_update_dates_and_send_notification_email --config=$CONF_DIR/$MVH_CONF_FILE --execute
229+59 8 1,15 * * production $BIN_DIR/sync_program_update_dates_and_send_notification_email --config=$CONF_DIR/$NSP_CONF_FILE --execute
230
231=== modified file 'lib-mvhub/lib/MVHub.pm'
232--- lib-mvhub/lib/MVHub.pm 2014-01-06 02:33:44 +0000
233+++ lib-mvhub/lib/MVHub.pm 2015-07-26 16:05:28 +0000
234@@ -17,11 +17,11 @@
235
236 =head1 VERSION
237
238-Version 0.42
239+Version 0.43
240
241 =cut
242
243-our $VERSION = '0.42';
244+our $VERSION = '0.43';
245
246 =head1 AUTHOR
247
248
249=== modified file 'lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm'
250--- lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm 2015-06-14 07:34:17 +0000
251+++ lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm 2015-07-26 16:05:28 +0000
252@@ -14,6 +14,7 @@
253 use Carp;
254 use CGI;
255 use DBI;
256+use Params::Validate;
257
258 use SQL::Library;
259
260@@ -21,83 +22,39 @@
261
262 use base qw( Exporter );
263 our @EXPORT = qw(
264+ do_sync
265+ print_sync
266 sync_program_dates
267- do_sql
268- print_sql
269 );
270
271+sub do_sync {
272+ Params::Validate::validate_pos( @_, 1, 1 );
273+ my ( $dbh, $program_row_aref ) = @_;
274+
275+ my $sql
276+ = MVHub::Utils::DB::get_sql_update_statement('PROGRAM_X_SYNC_DATES');
277+ $dbh->do( $sql, undef, @$program_row_aref );
278+}
279+
280+sub print_sync {
281+ Params::Validate::validate_pos( @_, 1, 1 );
282+ my ( $dbh, $program_row_aref ) = @_;
283+
284+ printf "%-15s %-9s %-41s %-15s\n", @$program_row_aref;
285+}
286+
287 sub sync_program_dates {
288- my $dbh = shift or croak 'missing param: $dbh';
289- my $sync_dates_sub_ref = shift
290- or croak 'missing param: $sync_dates_sub_ref';
291-
292- &$sync_dates_sub_ref($dbh);
293-}
294-
295-sub do_sql {
296- my $dbh = shift or croak 'missing param: $dbh';
297+ Params::Validate::validate_pos( @_, 1, 1 );
298+ my ( $dbh, $sub_ref ) = @_;
299
300 my $sql = MVHub::Utils::DB::get_sql_select_statement(
301- 'PROGRAM_X_AGENCY_MAX_LAST_UPDATED');
302+ 'PROGRAM_X_LAST_UPDATE_MAX_LAST_UPDATE');
303
304 my $programs_to_update_aref = $dbh->selectall_arrayref($sql);
305- my $update_count = 0;
306-
307- $sql = MVHub::Utils::DB::get_sql_update_statement('PROGRAM_X_SYNC_DATES');
308-
309- foreach my $row (@$programs_to_update_aref) {
310- my ( $agency_id, $contact_email, $max_last_updated ) = @$row;
311- my @bind_variables = (
312- $max_last_updated, $agency_id,
313- $contact_email, $max_last_updated
314- );
315-
316- my $rowcount = $dbh->do( $sql, undef, @bind_variables );
317- if ( $rowcount >= 1 ) {
318- $update_count += $rowcount;
319- }
320- }
321- print "no. of programs updated: $update_count\n";
322-}
323-
324-sub print_sql {
325- my $dbh = shift or croak 'missing param: $dbh';
326-
327- my $sql = MVHub::Utils::DB::get_sql_select_statement(
328- 'PROGRAM_X_AGENCY_MAX_LAST_UPDATED');
329-
330- my $agencies_to_print_aref = $dbh->selectall_arrayref($sql);
331- print
332- "Agency id, Program id, Contact email, Last updated, Max last updated, Needs Update?\n";
333-
334- #$sql = MVHub::Utils::DB::get_sql_update_statement('PROGRAM_X_SYNC_DATES');
335- $sql
336- = "SELECT program_id, contact_email, last_updated FROM program WHERE agency_id = ? AND contact_email = ?";
337-
338- my $no_of_programs_to_update = 0;
339- foreach my $a_row (@$agencies_to_print_aref) {
340- my ( $agency_id, $contact_email, $max_last_updated ) = @$a_row;
341-
342- my @bind_variables = ( $agency_id, $contact_email );
343-
344- my $programs_to_print_aref
345- = $dbh->selectall_arrayref( $sql, undef, @bind_variables );
346-
347- foreach my $p_row (@$programs_to_print_aref) {
348- my ( $program_id, $contact_email, $last_updated ) = @$p_row;
349- my $needs_update;
350- if ( $last_updated lt $max_last_updated ) {
351- $needs_update = "Yes";
352- $no_of_programs_to_update++;
353- }
354- else { $needs_update = "No"; }
355-
356- print
357- "$agency_id, $program_id, $contact_email, $last_updated, $max_last_updated, $needs_update\n";
358-
359- }
360- }
361- print "programs need to be updated: $no_of_programs_to_update\n";
362+
363+ foreach my $program_rec_aref (@$programs_to_update_aref) {
364+ &$sub_ref( $dbh, $program_rec_aref );
365+ }
366 }
367
368 1;
369
370=== modified file 'lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t' (properties changed: -x to +x)
371--- lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t 2015-06-14 07:34:17 +0000
372+++ lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t 2015-07-26 16:05:28 +0000
373@@ -13,12 +13,12 @@
374 use Carp;
375 use DBI;
376
377-use Test::More tests => 4 + 1; # +1 for NoWarnings
378+use Test::More tests => 2 + 1; # +1 for NoWarnings
379 use Test::NoWarnings;
380 use Test::Exception;
381
382 use MVHub::Utils::SyncProgramUpdateDates
383- qw/ sync_program_dates print_sql do_sql /;
384+ qw/ sync_program_dates do_sync print_sync /;
385 use MVHub::Utils::DB qw / get_dbh /;
386 use MVHub::Wrap::ConfigSimple;
387
388@@ -28,35 +28,30 @@
389 my $config_file = $ARGV[0] || $ENV{MV_TEST_CONFIG_FILE};
390 my $cfg = MVHub::Wrap::ConfigSimple->new($config_file);
391
392-my $test_message;
393-
394-###
395-$test_message = 'die with missing parameters';
396-###
397-dies_ok { sync_program_dates() } $test_message;
398-
399 my $dbh = MVHub::Utils::DB::get_dbh($config_file);
400 TestHelper::create_sqlite_db($dbh) or croak "failed to setup test database\n";
401-
402 TestHelper::add_test_data($dbh);
403
404+my $test_message;
405+
406 ###
407 $test_message = 'prints program dates to be updated ok';
408 ###
409-lives_ok { sync_program_dates( $dbh, \&print_sql ) } $test_message;
410-
411-###
412-$test_message = 'updates program dates ok';
413-###
414-lives_ok { sync_program_dates( $dbh, \&do_sql ) } $test_message;
415+lives_ok { sync_program_dates( $dbh, \&print_sync ) } $test_message;
416
417 ###
418 $test_message = 'Program Test Data equals expected result after update';
419 ###
420-sync_program_dates( $dbh, \&do_sql );
421+
422+sync_program_dates( $dbh, \&do_sync );
423 is_deeply(
424- $dbh->selectcol_arrayref("SELECT last_updated FROM program"),
425- [ '2009-02-15', '2008-12-02', '2008-12-02', '2008-12-12', '2008-12-12' ],
426+ $dbh->selectall_arrayref("SELECT agency_id,last_updated FROM program"),
427+ [ [ 1001, '2009-02-15' ],
428+ [ 1002, '2008-12-02' ],
429+ [ 1003, '2008-12-02' ],
430+ [ 1004, '2008-12-12' ],
431+ [ 1004, '2008-12-12' ],
432+ ],
433 $test_message
434 );
435
436
437=== modified file 'lib-mvhub/t/conf/all.conf'
438--- lib-mvhub/t/conf/all.conf 2012-12-24 17:16:42 +0000
439+++ lib-mvhub/t/conf/all.conf 2015-07-26 16:05:28 +0000
440@@ -92,6 +92,7 @@
441 dir=/var/www/mvhub/
442
443 [RELATIVE_PATH]
444+bin_dir=link-to-live-code/app-mvhub/bin/
445 conf_dir=link-to-live-code/app-mvhub/conf/
446 document_root=link-to-live-code/app-mvhub/DocumentRoot/
447 template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
448
449=== modified file 'lib-mvhub/t/lib/TestData.pm'
450--- lib-mvhub/t/lib/TestData.pm 2015-05-14 23:56:55 +0000
451+++ lib-mvhub/t/lib/TestData.pm 2015-07-26 16:05:28 +0000
452@@ -147,6 +147,7 @@
453 last_updated => '2008-12-12',
454 reminders_sent => '2',
455 },
456+
457 );
458
459 our $notifications_href = {

Subscribers

People subscribed via source and target branches

to all changes: