This document is a scratchpad created quickly during the 2011 Evergreen Conference.
NOTE: Updates can be found at the permanent location - evergreen-reports:automated_sql_reports.
The following sample scripts are used to provide daily e-mailed reports for the previous day's circulation for a given library.
#!/bin/sh # TEST_daily_stats.sh # # Script for generating daily circ stats for TEST Library # Define our variables BASEDIR=/home/<user>/sql DATE=$(date --date='1 day ago' '+%F') FOLDER=TEST ORG=TEST RECIPIENT="test@library.org" # Run the SQL report to get csv output psql -U <db_user> -h <db_hostname> -f $BASEDIR/$FOLDER/$ORG'_daily_stats'.postgresql <db_name> # Insert Title at beginning and Delete the last line from the csv (number of rows) sed -e '1 i Daily Circ for '$DATE'|' -e '1 i \ |' -e '$d' $BASEDIR/$FOLDER/$ORG'_daily_stats2'.csv > $BASEDIR/$FOLDER/$ORG'_daily_stats'.csv rm $BASEDIR/$FOLDER/$ORG'_daily_stats2'.csv # Convert csv to xls $BASEDIR/csv2xls.pl -s '|' -v 0 -o $BASEDIR'/'$FOLDER'/'$ORG'_daily_circ_'$DATE $BASEDIR/$FOLDER/$ORG'_daily_stats'.csv # Create email text for the day $BASEDIR/$FOLDER/$ORG'_daily_text'.sh > $BASEDIR/$FOLDER/$ORG'_email_text_'$DATE.txt # E-mail out the report mutt -s "Daily Circ for $DATE" -a $BASEDIR/$FOLDER/$ORG'_daily_circ_'$DATE.xls -- $RECIPIENT < $BASEDIR/$FOLDER/$ORG'_email_text_'$DATE.txt # Remove leftovers rm $BASEDIR/$FOLDER/$ORG'_daily_stats'.csv rm $BASEDIR/$FOLDER/$ORG'_daily_circ_'$DATE.xls rm $BASEDIR/$FOLDER/$ORG'_email_text_'$DATE.txt
This file is called TEST_daily_circ.postgresql. It creates an output file used by the control script above. The path for the output is hardcoded into the first line of the SQL with the \o. The \f | indicates that | will be used as the delimiter between columns.
\o /home/<user>/SQL/TEST/TEST_daily_stats2.csv \a \f | SELECT acl.name AS "Shelving Location", COUNT(acirc.id) AS "# of Circs" FROM action.circulation acirc JOIN asset.copy acopy ON acopy.id = acirc.target_copy JOIN asset.copy_location acl ON acopy.location = acl.id WHERE acirc.circ_lib = '11' AND date_trunc('DAY'::text, acirc.create_time) = date_trunc('DAY'::text, now() - '1 day'::INTERVAL) GROUP BY acl.name UNION SELECT 'ZZZ Total' AS "Shelving Location", COUNT(acirc.id) AS "# of Circs" FROM action.circulation acirc JOIN asset.copy acopy ON acopy.id = acirc.target_copy JOIN asset.copy_location acl ON acopy.location = acl.id WHERE acirc.circ_lib = '11' AND date_trunc('DAY'::text, acirc.create_time) = date_trunc('DAY'::text, now() - '1 day'::INTERVAL) ORDER BY 1; \o
#!/bin/bash # TEST_daily_text.sh # # Script for generating daily email body text for TEST Library DATE=$(date --date='1 day ago' '+%A, %B %d, %Y') echo "Hi, Attached is your daily circ report for "$DATE". Please let us know if you have any questions. Thank you, Your Helpdesk"