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

Proposed by RPulaparti
Status: Merged
Merged at revision: 666
Proposed branch: lp:~rpulaparti/mvhub/cron_script_to_set_program_max_last_update_1277300
Merge into: lp:mvhub
Diff against target: 349 lines (+249/-6)
10 files modified
app-mvhub/bin/sync_program_update_dates (+47/-0)
app-mvhub/bin/sync_program_update_dates_and_send_notification_email (+46/-0)
app-mvhub/conf/sql_select.lib (+20/-0)
app-mvhub/conf/sql_update.lib (+9/-0)
app-mvhub/project-tools/templates/template.conf (+1/-0)
app-mvhub/setup/etc/cron.d/mvhub-cron (+3/-3)
lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm (+60/-0)
lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t (+58/-0)
lib-mvhub/t/conf/all.conf (+1/-0)
lib-mvhub/t/lib/TestData.pm (+4/-3)
To merge this branch: bzr merge lp:~rpulaparti/mvhub/cron_script_to_set_program_max_last_update_1277300
Reviewer Review Type Date Requested Status
Dan MacNeil Pending
Review via email: mp+255297@code.launchpad.net

Description of the change

Added a new Perl script to app-mvhub/bin directory with the set_program_max_last_update.pl to update the last
updated date to the latest record update date for a given agency.please refer to 1277300 for details.

To post a comment you must log in.
665. By Renuka Pulaparti <email address hidden>
666. By Renuka Pulaparti <email address hidden>

modified to do all updates with a single update statement

667. By Renuka Pulaparti <email address hidden>

added new files

668. By Renuka Pulaparti <email address hidden>

removed an old script

669. By Renuka Pulaparti <email address hidden>

refactored code

670. By Renuka Pulaparti <email address hidden>

added new cron script to the crontab

Revision history for this message
Dan MacNeil (omacneil) :
Revision history for this message
Dan MacNeil (omacneil) :
Revision history for this message
Dan MacNeil (omacneil) :
671. By Renuka Pulaparti <email address hidden>

updated the sync dates files

672. By Renuka Pulaparti <email address hidden>

removed comments

Revision history for this message
RPulaparti (rpulaparti) wrote :
Download full text (5.8 KiB)

I removed the comments and made all the changes we discussed. I didn't
understand, what you meant by

"Probably more efficient to put line 130 above line 126 (out side the loop)
probably not necesary to get it each time through the loop"

My logic was for every row of the select statement results do an update
 for the given agency.

On Sat, May 9, 2015 at 4:38 PM, Dan MacNeil <email address hidden> wrote:

>
>
> Diff comments:
>
> > === added file 'app-mvhub/bin/sync_program_update_dates.pl'
> > --- app-mvhub/bin/sync_program_update_dates.pl 1970-01-01
> 00:00:00 +0000
> > +++ app-mvhub/bin/sync_program_update_dates.pl 2015-05-03
> 16:14:55 +0000
> > @@ -0,0 +1,21 @@
> > +#!/usr/bin/perl
> > +
> > +# LICENSE: GNU Affero General Public License v3
> > +# COPYRIGHT: Community Software Lab
> > +# CONTACT: <email address hidden>
> > +# PROJECT: https://launchpad.net/mvhub/
> > +
> > +# PURPOSE: cron script for setting all programs last_update to the max
> last update for a given agency.
> > +
> > +use strict;
> > +use warnings;
> > +
> > +use CGI;
> > +use DBI;
> > +use SQL::Library;
> > +use MVHub::Utils::DB;
> > +use MVHub::Utils::SyncProgramUpdateDates;
> > +
> > +{
> > + MVHub::Utils::SyncProgramUpdateDates::sync_program_dates();
> > +}
> >
> > === modified file 'app-mvhub/conf/sql_select.lib'
> > --- app-mvhub/conf/sql_select.lib 2013-01-02 16:42:41 +0000
> > +++ app-mvhub/conf/sql_select.lib 2015-05-03 16:14:55 +0000
> > @@ -826,6 +826,16 @@
> > WHERE agency_id = ? AND hidden_reason IS NULL
> > ORDER BY program_name
> >
> > +[PROGRAM_X_AGENCY_MAX_LAST_UPDATED]
> > +SELECT
> > + agency_id, contact_email, MAX(last_updated)
> > +FROM
> > + program
> > +GROUP BY
> > + agency_id,contact_email
> > +ORDER BY
> > + agency_id,contact_email
> > +
> > [PROGRAM_X_AGENCY_PROGRAM_ID]
> > SELECT *
> > FROM program
> >
> > === modified file 'app-mvhub/conf/sql_update.lib'
> > --- app-mvhub/conf/sql_update.lib 2013-01-02 16:42:41 +0000
> > +++ app-mvhub/conf/sql_update.lib 2015-05-03 16:14:55 +0000
> > @@ -82,3 +82,13 @@
> > WHERE
> > agency_id = ?
> > AND last_updated < ?
> > +
> > +[PROGRAM_X_SYNC_DATES]
> > + UPDATE
> > + program
> > + SET
> > + last_updated = ?
> > + WHERE agency_id = ?
> > + AND contact_email = ?
> > + AND last_updated < ?
> > +
> > \ No newline at end of file
> >
> > === modified file 'app-mvhub/setup/etc/cron.d/mvhub-cron'
> > --- app-mvhub/setup/etc/cron.d/mvhub-cron 2013-01-06 00:18:39 +0000
> > +++ app-mvhub/setup/etc/cron.d/mvhub-cron 2015-05-03 16:14:55 +0000
> > @@ -34,6 +34,8 @@
> > 28 1 * * * production $BIN_DIR/
> dump.pl --config=$CONF_DIR/$NSP_CONF_FILE
> > 30 2 * * * production $BIN_DIR/
> sitemap_generate.pl --config=$CONF_DIR/$MVH_CONF_FILE
> > 35 2 * * * production $BIN_DIR/
> sitemap_generate.pl --config=$CONF_DIR/$NSP_CONF_FILE
> > +00 3 * * * production $BIN_DIR/
> sync_program_update_dates.pl --config=$CONF_DIR/$MVH_CONF_FILE
> > +05 3 * * * production ...

Read more...

673. By Renuka Pulaparti <email address hidden>

broke down a single subroutine into three for easy testing

674. By Renuka Pulaparti <email address hidden>

moved the update statement out of the loop

675. By Renuka Pulaparti <email address hidden>

modified TestData

676. By Renuka Pulaparti <email address hidden>

Added new files

677. By Renuka Pulaparti <email address hidden>

ran perl tidy

678. By Renuka Pulaparti <email address hidden>

code refactoring

679. By Renuka Pulaparti <email address hidden>

minor change

680. By Renuka Pulaparti <email address hidden>

made the file executable

681. By Renuka Pulaparti <email address hidden>

added new script

682. By Renuka Pulaparti <email address hidden>

added parameters to the script

683. By Renuka Pulaparti <email address hidden>

code update

684. By Renuka Pulaparti <email address hidden>

perl tidy

685. By Renuka Pulaparti <email address hidden>

updated the cron script to call other scripts

686. By Renuka Pulaparti <email address hidden>

merged branch from Dan

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'app-mvhub/bin/sync_program_update_dates'
2--- app-mvhub/bin/sync_program_update_dates 1970-01-01 00:00:00 +0000
3+++ app-mvhub/bin/sync_program_update_dates 2015-07-28 01:03:21 +0000
4@@ -0,0 +1,47 @@
5+#!/usr/bin/perl
6+
7+# LICENSE: GNU Affero General Public License v3
8+# COPYRIGHT: Community Software Lab
9+# CONTACT: help@thecsl.org
10+# PROJECT: https://launchpad.net/mvhub/
11+
12+# PURPOSE: cron script for setting all programs last_update to the max last update for a given agency so the contact
13+# doesn't have to do updates more than once every 6 months.
14+
15+use strict;
16+use warnings;
17+
18+use Carp;
19+use CGI;
20+use DBI;
21+use Getopt::Long;
22+use SQL::Library;
23+
24+use MVHub::Utils::DB;
25+use MVHub::Utils::SyncProgramUpdateDates
26+ qw( print_sync do_sync sync_program_dates );
27+
28+{ # main
29+ # Command Line option handling
30+ my $CFG_FILENAME = $ENV{MV_CONFIG_FILE};
31+ my $EXECUTE = 0;
32+ Getopt::Long::GetOptions(
33+ 'config=s' => \$CFG_FILENAME,
34+ 'execute' => \$EXECUTE
35+ ) or die "GetOptions failed";
36+
37+ my $dbh = MVHub::Utils::DB::get_dbh($CFG_FILENAME);
38+
39+ if ($EXECUTE) {
40+ MVHub::Utils::SyncProgramUpdateDates::sync_program_dates( $dbh,
41+ \&do_sync );
42+ }
43+ else {
44+ printf "%-15s %-9s %-41s %-15s \n",
45+ qw/max_update_date agency_id contact_email update_date /;
46+ MVHub::Utils::SyncProgramUpdateDates::sync_program_dates( $dbh,
47+ \&print_sync );
48+ }
49+}
50+
51+exit 0 # 0 is UNIX for 'no error'
52
53=== added file 'app-mvhub/bin/sync_program_update_dates_and_send_notification_email'
54--- app-mvhub/bin/sync_program_update_dates_and_send_notification_email 1970-01-01 00:00:00 +0000
55+++ app-mvhub/bin/sync_program_update_dates_and_send_notification_email 2015-07-28 01:03:21 +0000
56@@ -0,0 +1,46 @@
57+#!/usr/bin/perl
58+
59+# LICENSE: GNU Affero General Public License v3
60+# COPYRIGHT: Community Software Lab
61+# CONTACT: help@thecsl.org
62+# PROJECT: https://launchpad.net/mvhub/
63+
64+# PURPOSE: Every two weeks, we send a notification email to the users who need to update their info.Before we send a
65+# notification, we need to sync the last updated dates for all programs for a given agency, so they don't receive too
66+# many emails to update each program separately.
67+
68+use strict;
69+use warnings;
70+
71+use Getopt::Long;
72+
73+use MVHub::Common;
74+use MVHub::Utils;
75+use MVHub::Wrap::ConfigSimple;
76+use MVHub::Utils::DB;
77+
78+# Command Line option handling
79+my $CFG_FILENAME = $ENV{MV_CONFIG_FILE};
80+my $EXECUTE = 0;
81+Getopt::Long::GetOptions(
82+ 'config=s' => \$CFG_FILENAME,
83+ 'execute' => \$EXECUTE
84+) or die "GetOptions failed";
85+
86+my $CFG = MVHub::Wrap::ConfigSimple->new($CFG_FILENAME);
87+my $BIN_DIR = $CFG->param('ABSOLUTE_PATH.bin_dir');
88+
89+my @update_dates_args
90+ = ( "$BIN_DIR/sync_program_update_dates.pl", '--config', $CFG_FILENAME );
91+my @notification_email_args
92+ = ( "$BIN_DIR/notification_email.pl", '--config', $CFG_FILENAME );
93+
94+if ($EXECUTE) {
95+ push( @update_dates_args, '--execute' );
96+ push( @notification_email_args, '--execute' );
97+}
98+
99+( system(@update_dates_args) == 0 ) or die "Sync program dates failed.\n";
100+( system(@notification_email_args) == 0 ) or die "Notification emails failed";
101+
102+exit 0 # zero is unix for 'no error'
103
104=== modified file 'app-mvhub/conf/sql_select.lib'
105--- app-mvhub/conf/sql_select.lib 2013-01-02 16:42:41 +0000
106+++ app-mvhub/conf/sql_select.lib 2015-07-28 01:03:21 +0000
107@@ -937,6 +937,26 @@
108 program
109 WHERE program_id = ?
110
111+[PROGRAM_X_LAST_UPDATE_MAX_LAST_UPDATE]
112+ SELECT
113+ pm.max_last_updated, p.agency_id,LOWER(p.contact_email) AS contact_email,p.last_updated AS update_need_sync
114+ FROM
115+ program p
116+ INNER JOIN
117+ (
118+ SELECT
119+ agency_id, LOWER(contact_email) AS contact_email, MAX(last_updated) AS max_last_updated
120+ FROM
121+ program p
122+ GROUP BY
123+ agency_id,contact_email
124+ ORDER BY
125+ agency_id,contact_email
126+ ) pm ON
127+ p.agency_id = pm.agency_id
128+ AND LOWER(p.contact_email) = pm.contact_email
129+ AND p.last_updated < pm.max_last_updated
130+
131 [PROGRAM_X_NAMES_THAT_REPEAT_AGENCY_NAME]
132 SELECT program_name, agency_name FROM program p, agency a
133 WHERE p.agency_id = a.agency_id
134
135=== modified file 'app-mvhub/conf/sql_update.lib'
136--- app-mvhub/conf/sql_update.lib 2013-01-02 16:42:41 +0000
137+++ app-mvhub/conf/sql_update.lib 2015-07-28 01:03:21 +0000
138@@ -82,3 +82,12 @@
139 WHERE
140 agency_id = ?
141 AND last_updated < ?
142+
143+[PROGRAM_X_SYNC_DATES]
144+ UPDATE
145+ program
146+ SET
147+ last_updated = ?
148+ WHERE agency_id = ?
149+ AND contact_email = ?
150+ AND last_updated = ?
151
152=== modified file 'app-mvhub/project-tools/templates/template.conf'
153--- app-mvhub/project-tools/templates/template.conf 2012-12-29 16:37:34 +0000
154+++ app-mvhub/project-tools/templates/template.conf 2015-07-28 01:03:21 +0000
155@@ -118,6 +118,7 @@
156
157 [RELATIVE_PATH]
158 conf_dir=link-to-live-code/app-mvhub/conf/
159+bin_dir=link-to-live-code/app-mvhub/bin/
160 document_root=link-to-live-code/app-mvhub/DocumentRoot/
161 template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
162 template_html_dir=link-to-live-code/app-mvhub/conf/templates/html/
163
164=== modified file 'app-mvhub/setup/etc/cron.d/mvhub-cron'
165--- app-mvhub/setup/etc/cron.d/mvhub-cron 2013-01-06 00:18:39 +0000
166+++ app-mvhub/setup/etc/cron.d/mvhub-cron 2015-07-28 01:03:21 +0000
167@@ -36,11 +36,11 @@
168 35 2 * * * production $BIN_DIR/sitemap_generate.pl --config=$CONF_DIR/$NSP_CONF_FILE
169
170 # restarting apache because we rotate/resolved logs and it is
171-# bad karma to keep writing to renamed file
172+# bad karma to keep writing to re-named file
173 30 0 * * * root /usr/sbin/apache2ctl graceful
174
175 45 0 * * * root $BIN_DIR/rm_session_files $CONF_DIR/$MVH_CONF_FILE
176 46 0 * * * root $BIN_DIR/rm_session_files $CONF_DIR/$NSP_CONF_FILE
177
178-51 8 1,15 * * production $BIN_DIR/notification_email.pl --config=$CONF_DIR/$MVH_CONF_FILE --execute
179-59 8 1,15 * * production $BIN_DIR/notification_email.pl --config=$CONF_DIR/$NSP_CONF_FILE --execute
180+51 8 1,15 * * production $BIN_DIR/sync_program_update_dates_and_send_notification_email --config=$CONF_DIR/$MVH_CONF_FILE --execute
181+59 8 1,15 * * production $BIN_DIR/sync_program_update_dates_and_send_notification_email --config=$CONF_DIR/$NSP_CONF_FILE --execute
182
183=== added file 'lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm'
184--- lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm 1970-01-01 00:00:00 +0000
185+++ lib-mvhub/lib/MVHub/Utils/SyncProgramUpdateDates.pm 2015-07-28 01:03:21 +0000
186@@ -0,0 +1,60 @@
187+
188+# LICENSE: GNU Affero General Public License v3
189+# COPYRIGHT: Community Software Lab
190+# CONTACT: help@thecsl.org
191+# PROJECT: https://launchpad.net/mvhub/
192+
193+# PURPOSE: sub to sync all the last updated dates of all the program to the latest updated date for a given agency so### the contact doesn't have to do updates more than once every 6 months.
194+
195+package MVHub::Utils::SyncProgramUpdateDates;
196+
197+use strict;
198+use warnings;
199+
200+use Carp;
201+use CGI;
202+use DBI;
203+use Params::Validate;
204+
205+use SQL::Library;
206+
207+use MVHub::Utils::DB;
208+
209+use base qw( Exporter );
210+our @EXPORT = qw(
211+ do_sync
212+ print_sync
213+ sync_program_dates
214+);
215+
216+sub do_sync {
217+ Params::Validate::validate_pos( @_, 1, 1 );
218+ my ( $dbh, $program_row_aref ) = @_;
219+
220+ my $sql
221+ = MVHub::Utils::DB::get_sql_update_statement('PROGRAM_X_SYNC_DATES');
222+ $dbh->do( $sql, undef, @$program_row_aref );
223+}
224+
225+sub print_sync {
226+ Params::Validate::validate_pos( @_, 1, 1 );
227+ my ( $dbh, $program_row_aref ) = @_;
228+
229+ printf "%-15s %-9s %-41s %-15s\n", @$program_row_aref;
230+}
231+
232+sub sync_program_dates {
233+ Params::Validate::validate_pos( @_, 1, 1 );
234+ my ( $dbh, $sub_ref ) = @_;
235+
236+ my $sql = MVHub::Utils::DB::get_sql_select_statement(
237+ 'PROGRAM_X_LAST_UPDATE_MAX_LAST_UPDATE');
238+
239+ my $programs_to_update_aref = $dbh->selectall_arrayref($sql);
240+
241+ foreach my $program_rec_aref (@$programs_to_update_aref) {
242+ &$sub_ref( $dbh, $program_rec_aref );
243+ }
244+}
245+
246+1;
247
248=== added directory 'lib-mvhub/t/Utils/SyncProgramUpdateDates'
249=== added file 'lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t'
250--- lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t 1970-01-01 00:00:00 +0000
251+++ lib-mvhub/t/Utils/SyncProgramUpdateDates/sync_program_dates.t 2015-07-28 01:03:21 +0000
252@@ -0,0 +1,58 @@
253+#!/usr/bin/perl
254+
255+# LICENSE: GNU Affero General Public License v3
256+# COPYRIGHT: Community Software Lab
257+# CONTACT: help@thecsl.org
258+# PROJECT: https://launchpad.net/mvhub/
259+
260+# PURPOSE: test sub that syncs the programs dates to the last updated date.
261+
262+use strict;
263+use warnings;
264+
265+use Carp;
266+use DBI;
267+
268+use Test::More tests => 2 + 1; # +1 for NoWarnings
269+use Test::NoWarnings;
270+use Test::Exception;
271+
272+use MVHub::Utils::SyncProgramUpdateDates
273+ qw/ sync_program_dates do_sync print_sync /;
274+use MVHub::Utils::DB qw / get_dbh /;
275+use MVHub::Wrap::ConfigSimple;
276+
277+use TestHelper qw/ add_test_data create_sqlite_db /;
278+use TestData qw/ @program /;
279+
280+my $config_file = $ARGV[0] || $ENV{MV_TEST_CONFIG_FILE};
281+my $cfg = MVHub::Wrap::ConfigSimple->new($config_file);
282+
283+my $dbh = MVHub::Utils::DB::get_dbh($config_file);
284+TestHelper::create_sqlite_db($dbh) or croak "failed to setup test database\n";
285+TestHelper::add_test_data($dbh);
286+
287+my $test_message;
288+
289+###
290+$test_message = 'prints program dates to be updated ok';
291+###
292+lives_ok { sync_program_dates( $dbh, \&print_sync ) } $test_message;
293+
294+###
295+$test_message = 'Program Test Data equals expected result after update';
296+###
297+
298+sync_program_dates( $dbh, \&do_sync );
299+is_deeply(
300+ $dbh->selectall_arrayref("SELECT agency_id,last_updated FROM program"),
301+ [ [ 1001, '2009-02-15' ],
302+ [ 1002, '2008-12-02' ],
303+ [ 1003, '2008-12-02' ],
304+ [ 1004, '2008-12-12' ],
305+ [ 1004, '2008-12-12' ],
306+ ],
307+ $test_message
308+);
309+
310+unlink( $cfg->param('DATABASE.database_name') );
311
312=== modified file 'lib-mvhub/t/conf/all.conf'
313--- lib-mvhub/t/conf/all.conf 2012-12-24 17:16:42 +0000
314+++ lib-mvhub/t/conf/all.conf 2015-07-28 01:03:21 +0000
315@@ -92,6 +92,7 @@
316 dir=/var/www/mvhub/
317
318 [RELATIVE_PATH]
319+bin_dir=link-to-live-code/app-mvhub/bin/
320 conf_dir=link-to-live-code/app-mvhub/conf/
321 document_root=link-to-live-code/app-mvhub/DocumentRoot/
322 template_conf_dir=link-to-live-code/app-mvhub/project-tools/templates/
323
324=== modified file 'lib-mvhub/t/lib/TestData.pm'
325--- lib-mvhub/t/lib/TestData.pm 2013-02-28 03:48:09 +0000
326+++ lib-mvhub/t/lib/TestData.pm 2015-07-28 01:03:21 +0000
327@@ -131,8 +131,8 @@
328 record_name => 'Test Program 4',
329 contact_first_name => 'Sue',
330 contact_last_name => 'Blue',
331- contact_email => 'sue_blue@example.com',
332- email => 'sue_public@example.com',
333+ contact_email => 'lou_blue@example.com',
334+ email => 'lou_public@example.com',
335 last_updated => '2008-12-02',
336 reminders_sent => '3',
337 },
338@@ -144,9 +144,10 @@
339 contact_last_name => 'Blue',
340 contact_email => 'lou_blue@example.com',
341 email => 'lou_public@example.com',
342- last_updated => '2008-12-02',
343+ last_updated => '2008-12-12',
344 reminders_sent => '2',
345 },
346+
347 );
348
349 our $notifications_href = {

Subscribers

People subscribed via source and target branches