Skip to content

Instantly share code, notes, and snippets.

@manuthu
Created December 14, 2021 14:21
Show Gist options
  • Save manuthu/4cbf520f79668d5a2b91233c15bc6833 to your computer and use it in GitHub Desktop.
Save manuthu/4cbf520f79668d5a2b91233c15bc6833 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
DB_USER=<db-user>
DB_HOST=<db-host>
DB_NAME=<db-name>
CSV_FILE_NAME=<Reports>
DATE=$(date "+%Y-%m-%d")
LOG_FILE=logs/Reports-$DATE.log
log() {
TS=$(date "+%Y-%m-%d-%H:%M:%S")
echo "[$TS][INFO] $1" | tee -a $LOG_FILE
}
log_and_die() {
TS=$(date "+%Y-%m-%d-%H:%M:%S")
echo "[TS]:[ERROR] $1" | tee -a $LOG_FILE
exit 1
}
validate_date() {
if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]
then log "Begin Generation of report for date $1."
else log_and_die "Date $1 is in an invalid format (not YYYY-MM-DD) e.g 2021-11-30"
fi
}
generate_csv_report() {
log "Generate CSV report $1 to $2"
psql -U $DB_USER -h $DB_HOST $DB_NAME -c "\copy ($1) to '$2' WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *)"
}
daily_all_downloads() {
log "Generate all user downloads for $1"
SQL="SELECT distinct phone_number FROM otp_token where date(created_at)=date('$1')"
CSV_FILE=reports/report_all_downloads_$1.csv
generate_csv_report "$SQL" "$CSV_FILE"
log "Done generating"
}
daily_activated_downloads() {
log "Generate activated users for $1"
SQL="SELECT distinct phone_number FROM otp_token where date(created_at)=date('$1') AND redeemed=TRUE"
CSV_FILE=reports/report_activated_users_$1.csv
generate_csv_report "$SQL" $CSV_FILE
log "Done generating"
}
show_help() {
log_and_die "$0 2021-12-14"
}
OPTIND=1
while getopts "h?d:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
d) DATE=$OPTARG
;;
*)
show_help
exit 1
esac
done
validate_date $DATE
daily_all_downloads "$DATE"
daily_activated_downloads "$DATE"
@manuthu
Copy link
Author

manuthu commented Dec 14, 2021

postgres@dfsdb1:~/reports$ ./daily-reports.sh -d 2021-06-01
[2021-12-14-16:23:40][INFO] Begin Generation of report for date 2021-06-01.
[2021-12-14-16:23:40][INFO] Generate all user downloads for 2021-06-01
[2021-12-14-16:23:40][INFO] Generate CSV report SELECT distinct phone_number FROM otp_token where date(created_at)=date('2021-06-01') to reports/T-kash_report_all_downloads_2021-06-01.csv
COPY 2
[2021-12-14-16:23:40][INFO] Done generating
[2021-12-14-16:23:40][INFO] Generate activated users for 2021-06-01
[2021-12-14-16:23:40][INFO] Generate CSV report SELECT distinct phone_number FROM otp_token where date(created_at)=date('2021-06-01') AND redeemed=TRUE to reports/T-kash_report_activated_users_2021-06-01.csv
COPY 1
[2021-12-14-16:23:40][INFO] Done generating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment