From fd1be96894c0ad87332a5c72b55ab448d5010bd9 Mon Sep 17 00:00:00 2001 From: Ryan Anguiano Date: Wed, 7 Sep 2022 12:40:29 -0400 Subject: [PATCH 1/2] Add postgresql compatibility to seed_reports.sh --- .../resources/reports/calendar-postgresql.sql | 18 +++++++ ...eport_conversions_total_dollar_monthly.ddl | 2 +- src/main/resources/seed_reports.sh | 53 +++++++++++++++++-- .../system/system_report_payments_per_day.sql | 2 +- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/main/resources/reports/calendar-postgresql.sql mode change 100644 => 100755 src/main/resources/seed_reports.sh diff --git a/src/main/resources/reports/calendar-postgresql.sql b/src/main/resources/reports/calendar-postgresql.sql new file mode 100644 index 00000000..a4efe9e6 --- /dev/null +++ b/src/main/resources/reports/calendar-postgresql.sql @@ -0,0 +1,18 @@ +drop procedure if exists create_calendar; + +create procedure create_calendar(calendar_from date, calendar_to date) +language plpgsql as $$ + declare + d date; + begin + d := calendar_from; + drop table if exists calendar; + create table calendar(d date primary key); + while d <= calendar_to loop + insert into calendar(d) values (d); + d := d + interval '1 day'; + end loop; + end +$$; + +call create_calendar((now() - interval '5 years')::date, (now() + interval '10 years')::date); diff --git a/src/main/resources/reports/conversion/report_conversions_total_dollar_monthly.ddl b/src/main/resources/reports/conversion/report_conversions_total_dollar_monthly.ddl index ebb29301..0ac5493a 100644 --- a/src/main/resources/reports/conversion/report_conversions_total_dollar_monthly.ddl +++ b/src/main/resources/reports/conversion/report_conversions_total_dollar_monthly.ddl @@ -1,2 +1,2 @@ drop table if exists report_conversions_total_dollar_monthly; -create table report_conversions_total_dollar_monthly (tenant_record_id int(11), day date, term varchar(50), count int(10)); +create table report_conversions_total_dollar_monthly (tenant_record_id int, day date, term varchar(50), count int); diff --git a/src/main/resources/seed_reports.sh b/src/main/resources/seed_reports.sh old mode 100644 new mode 100755 index 1584501e..9e9e2aff --- a/src/main/resources/seed_reports.sh +++ b/src/main/resources/seed_reports.sh @@ -30,17 +30,32 @@ KILLBILL_PASSWORD=${KILLBILL_PASSWORD-"password"} KILLBILL_API_KEY=${KILLBILL_API_KEY-"bob"} KILLBILL_API_SECRET=${KILLBILL_API_SECRET-"lazar"} +DATABASE_TYPE=${DATABASE_TYPE-"mysql"} # can be mysql or postgresql + MYSQL_HOST=${MYSQL_HOST-"127.0.0.1"} MYSQL_USER=${MYSQL_USER-"root"} MYSQL_PASSWORD=${MYSQL_PASSWORD-"root"} MYSQL_DATABASE=${MYSQL_DATABASE-"killbill"} +POSTGRES_HOST=${POSTGRES_HOST-"127.0.0.1"} +POSTGRES_PORT=${POSTGRES_PORT-"5432"} +POSTGRES_USER=${POSTGRES_USER-"killbill"} +POSTGRES_PASSWORD=${POSTGRES_PASSWORD-"killbill"} +POSTGRES_DATABASE=${POSTGRES_DATABASE-"killbill"} + REPORTS=$HERE/reports SYSTEM=$HERE/system function install_ddl() { local ddl=$1 - mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "source $ddl" + if [[ $DATABASE_TYPE == "postgresql" ]]; then + # check if pg ddl file exists + local pg_ddl="${ddl%%.*}-postgresql.${ddl##*.}" + if [ -f $pg_ddl ]; then ddl=$pg_ddl; fi + PGPASSWORD=$POSTGRES_PASSWORD psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_DATABASE -f $ddl + else + mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "source $ddl" + fi } function create_report() { @@ -64,28 +79,60 @@ function create_report() { # Install the DDL - the calendar table needs to be first install_ddl $REPORTS/calendar.sql -for r in `find $REPORTS -type f -name '*.sql' -o -name '*.ddl' -maxdepth 1`; do install_ddl $r; done -for r in `find $SYSTEM -type f -name '*.sql' -o -name '*.ddl' -maxdepth 1`; do install_ddl $r; done # Dashboard views +install_ddl $REPORTS/accounts_summary/v_report_accounts_summary.ddl create_report 'accounts_summary' 'Account summary' 'COUNTERS' 'v_report_accounts_summary' + +install_ddl $REPORTS/active_by_product_term_monthly/v_report_active_by_product_term_monthly.ddl create_report 'active_by_product_term_monthly' 'Active subscriptions' 'TIMELINE' 'v_report_active_by_product_term_monthly' + +install_ddl $REPORTS/cancellations_daily/v_report_cancellations_daily.ddl create_report 'cancellations_count_daily' 'Cancellations' 'TIMELINE' 'v_report_cancellations_daily' + +install_ddl $REPORTS/chargebacks_daily/v_report_chargebacks_daily.ddl create_report 'chargebacks_daily' 'Chargebacks' 'TIMELINE' 'v_report_chargebacks_daily' + +install_ddl $REPORTS/v_report_conversions_daily.ddl create_report 'conversions_daily' 'Conversions' 'TIMELINE' 'v_report_conversions_daily' + +install_ddl $REPORTS/v_report_invoice_adjustments_daily.ddl create_report 'invoice_adjustments_daily' 'Invoice adjustments' 'TIMELINE' 'v_report_invoice_adjustments_daily' + +install_ddl $REPORTS/v_report_invoice_item_adjustments_daily.ddl create_report 'invoice_item_adjustments_daily' 'Invoice item adjustments' 'TIMELINE' 'v_report_invoice_item_adjustments_daily' + +install_ddl $REPORTS/v_report_invoice_item_credits_daily.ddl create_report 'invoice_item_credits_daily' 'Invoice credits' 'TIMELINE' 'v_report_invoice_item_credits_daily' + +install_ddl $REPORTS/invoices_balance_daily/v_report_invoices_balance_daily.ddl create_report 'invoices_balance_daily' 'Invoice balance' 'TIMELINE' 'v_report_invoices_balance_daily' + +install_ddl $REPORTS/invoices_daily/v_report_invoices_daily.ddl create_report 'invoices_daily' 'Invoices' 'TIMELINE' 'v_report_invoices_daily' + +install_ddl $REPORTS/mrr/v_report_mrr_daily.ddl create_report 'mrr_daily' 'MRR' 'TIMELINE' 'v_report_mrr_daily' + +install_ddl $REPORTS/new_accounts_daily/v_report_new_accounts_daily.ddl create_report 'new_accounts_daily' 'New accounts' 'TIMELINE' 'v_report_new_accounts_daily' + +install_ddl $REPORTS/v_report_overdue_states_count_daily.ddl create_report 'overdue_states_count_daily' 'Overdue states' 'TIMELINE' 'v_report_overdue_states_count_daily' + +install_ddl $REPORTS/payments_total_daily/v_report_payments_total_daily_sub1.ddl +install_ddl $REPORTS/payments_total_daily/v_report_payments_total_daily.ddl create_report 'payments_total_daily' 'Payment ($ amount)' 'TIMELINE' 'v_report_payments_total_daily' + +install_ddl $REPORTS/refunds_total_daily/v_report_refunds_total_daily.ddl create_report 'refunds_total_daily' 'Refunds' 'TIMELINE' 'v_report_refunds_total_daily' + +install_ddl $REPORTS/v_report_trial_starts_count_daily.ddl create_report 'trial_starts_count_daily' 'Trials' 'TIMELINE' 'v_report_trial_starts_count_daily' # System views +for r in `find $SYSTEM -type f -name '*.sql' -o -name '*.ddl' -maxdepth 1`; do install_ddl $r; done + create_report 'system_report_control_tag_no_test' 'Control tags' 'COUNTERS' 'v_system_report_control_tag_no_test' create_report 'system_report_notifications_per_queue_name' 'Notification queues' 'TIMELINE' 'v_system_report_notifications_per_queue_name' create_report 'system_report_notifications_per_queue_name_late' 'Late notifications' 'COUNTERS' 'v_system_report_notifications_per_queue_name_late' diff --git a/src/main/resources/system/system_report_payments_per_day.sql b/src/main/resources/system/system_report_payments_per_day.sql index e25f3adc..97621782 100644 --- a/src/main/resources/system/system_report_payments_per_day.sql +++ b/src/main/resources/system/system_report_payments_per_day.sql @@ -1,7 +1,7 @@ create or replace view v_system_report_payments_per_day as select tenant_record_id -, date_format(greatest(created_date, updated_date), '%Y-%m-%d') as day +, date_format((case when created_date > updated_date then created_date else updated_date end), '%Y-%m-%d') as day , case when state_name IN ('AUTH_ERRORED', 'CAPTURE_ERRORED', 'CHARGEBACK_ERRORED', 'CREDIT_ERRORED', 'PURCHASE_ERRORED', 'REFUND_ERRORED', 'VOID_ERRORED') then 'ERRORED' when state_name IN ('AUTH_FAILED', 'CAPTURE_FAILED', 'CHARGEBACK_FAILED', 'CREDIT_FAILED', 'PURCHASE_FAILED', 'REFUND_FAILED', 'VOID_FAILED') then 'FAILED' From 3311d5adc9c9dd3be4c5f2e577c96c5003949ec7 Mon Sep 17 00:00:00 2001 From: Ryan Anguiano Date: Wed, 7 Sep 2022 12:59:11 -0400 Subject: [PATCH 2/2] Fix bash variable --- src/main/resources/seed_reports.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/seed_reports.sh b/src/main/resources/seed_reports.sh index 9e9e2aff..a4d61169 100755 --- a/src/main/resources/seed_reports.sh +++ b/src/main/resources/seed_reports.sh @@ -50,7 +50,7 @@ function install_ddl() { local ddl=$1 if [[ $DATABASE_TYPE == "postgresql" ]]; then # check if pg ddl file exists - local pg_ddl="${ddl%%.*}-postgresql.${ddl##*.}" + local pg_ddl="${ddl%.*}-postgresql.${ddl##*.}" if [ -f $pg_ddl ]; then ddl=$pg_ddl; fi PGPASSWORD=$POSTGRES_PASSWORD psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_DATABASE -f $ddl else