Skip to content

Commit 5ec323a

Browse files
committed
Fix seeds-report.sh, add readme for it
1 parent 4f09f03 commit 5ec323a

6 files changed

Lines changed: 293 additions & 93 deletions

File tree

reports/README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# KillBill Analytics Reports Setup Script
2+
3+
This script installs all necessary database DDLs and creates KillBill analytics reports for your KillBill environment.
4+
5+
---
6+
7+
## Table of Contents
8+
9+
- [Overview](#overview)
10+
- [Prerequisites](#prerequisites)
11+
- [Usage](#usage)
12+
- [Environment Variables](#environment-variables)
13+
- [Script Behavior](#script-behavior)
14+
- [Examples](#examples)
15+
- [License](#license)
16+
17+
---
18+
19+
## Overview
20+
21+
This Bash script performs the following tasks:
22+
23+
1. Installs database DDL files (`.sql` or `.ddl`) into the configured MySQL database.
24+
2. Creates KillBill analytics reports via the KillBill Analytics plugin REST API.
25+
3. Supports optional dropping of existing reports before creation.
26+
27+
The script recursively installs DDL files, ensuring `v_report_*.ddl` files are installed before corresponding `report_*.ddl` files. If no `v_report_*.ddl` exists in a folder, all `.ddl` files in that folder are installed.
28+
29+
---
30+
31+
## Prerequisites
32+
33+
- **Bash shell** (Linux, macOS, or Windows Git Bash)
34+
- **MySQL client** installed and accessible in PATH
35+
- KillBill server running with the KillBill Analytics plugin installed
36+
- Appropriate permissions for the MySQL database and KillBill API
37+
38+
---
39+
40+
## Usage
41+
42+
Run the script from the directory containing your DDL files:
43+
44+
```bash
45+
./setup_reports.sh
46+
```
47+
48+
By default, the script installs DDLs and creates all reports.
49+
50+
---
51+
52+
## Environment Variables
53+
54+
The script uses environment variables to configure MySQL and KillBill settings. Defaults are provided if variables are not set:
55+
56+
| Variable | Default | Description |
57+
|--------------------------|--------------------------|---------------------------------------------------------------------------|
58+
| `KILLBILL_HTTP_PROTOCOL` | `http` | KillBill API protocol |
59+
| `KILLBILL_HOST` | `127.0.0.1` | KillBill host |
60+
| `KILLBILL_PORT` | `8080` | KillBill port |
61+
| `KILLBILL_USER` | `admin` | KillBill username |
62+
| `KILLBILL_PASSWORD` | `password` | KillBill password |
63+
| `KILLBILL_API_KEY` | `bob` | KillBill API key |
64+
| `KILLBILL_API_SECRET` | `lazar` | KillBill API secret |
65+
| `MYSQL_HOST` | `127.0.0.1` | MySQL host |
66+
| `MYSQL_USER` | `root` | MySQL user |
67+
| `MYSQL_PASSWORD` | `killbill` | MySQL password |
68+
| `MYSQL_DATABASE` | `killbill` | MySQL database name |
69+
| `INSTALL_DDL` | `true` | Whether to install DDL files (`true` or `false`) |
70+
| `DROP_EXISTING_REPORT` | `true` | Whether to drop existing reports before creating them (`true` or `false`) |
71+
72+
You can export environment variables before running the script to override defaults:
73+
74+
```bash
75+
export KILLBILL_HOST=192.168.1.10
76+
export MYSQL_PASSWORD=mysecret
77+
export INSTALL_DDL=false
78+
79+
./setup_reports.sh
80+
```
81+
82+
---
83+
84+
## Script Behavior
85+
86+
1. **DDL Installation**
87+
- Installs ddl from the `utils` directory first.
88+
- Recursively installs DDLs from the other subdirectories:
89+
- If `v_report_*.ddl` files exist, they are installed first, followed by `report_*.ddl`.
90+
- If no `v_report_*.ddl` exists, all `.ddl` files in the folder are installed.
91+
92+
2. **Report Creation**
93+
- All reports defined in the `create_all_reports` function are created.
94+
- If `DROP_EXISTING_REPORT=true`, existing reports are deleted before creation.
95+
- Reports are created via the KillBill Analytics plugin REST API.
96+
97+
---
98+
99+
## Examples
100+
101+
- **Run with default configuration:**
102+
103+
```bash
104+
./setup_reports.sh
105+
```
106+
107+
- **Skip DDL installation:**
108+
109+
```bash
110+
export INSTALL_DDL=false
111+
./setup_reports.sh
112+
```
113+
114+
- **Disable dropping existing reports:**
115+
116+
```bash
117+
export DROP_EXISTING_REPORT=false
118+
./setup_reports.sh
119+
```
120+
121+
- **Override KillBill host and MySQL password:**
122+
123+
```bash
124+
export KILLBILL_HOST=192.168.1.10
125+
export MYSQL_PASSWORD=mysecret
126+
./setup_reports.sh
127+
```
128+
129+
---
130+
131+
## License
132+
133+
This script is licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).

reports/accounts_summary/v_report_account_summary.ddl renamed to reports/accounts_summary/v_report_accounts_summary.ddl

File renamed without changes.
File renamed without changes.

reports/reports_setup.sh

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Copyright 2020-2025 Equinix, Inc
5+
# Copyright 2014-2025 The Billing Project, LLC
6+
#
7+
# The Billing Project licenses this file to you under the Apache License, version 2.0
8+
# (the "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at:
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
HERE=`cd \`dirname $0\`; pwd`
21+
22+
KILLBILL_HTTP_PROTOCOL=${KILLBILL_HTTP_PROTOCOL-"http"}
23+
KILLBILL_HOST=${KILLBILL_HOST-"127.0.0.1"}
24+
KILLBILL_PORT=${KILLBILL_PORT-"8080"}
25+
26+
KILLBILL_USER=${KILLBILL_USER-"admin"}
27+
KILLBILL_PASSWORD=${KILLBILL_PASSWORD-"password"}
28+
KILLBILL_API_KEY=${KILLBILL_API_KEY-"bob"}
29+
KILLBILL_API_SECRET=${KILLBILL_API_SECRET-"lazar"}
30+
31+
MYSQL_HOST=${MYSQL_HOST-"127.0.0.1"}
32+
MYSQL_USER=${MYSQL_USER-"root"}
33+
MYSQL_PASSWORD=${MYSQL_PASSWORD-"killbill"}
34+
MYSQL_DATABASE=${MYSQL_DATABASE-"killbill"}
35+
INSTALL_DDL=true
36+
DROP_EXISTING_REPORT=false
37+
38+
REPORTS=$HERE
39+
40+
function install_ddl() {
41+
local ddl=$1
42+
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "source $ddl"
43+
}
44+
45+
install_all_ddls() {
46+
echo "Installing DDLs from utils directory first..."
47+
(
48+
cd "$REPORTS" || exit 1
49+
50+
# Process utils first if it exists
51+
if [ -d "./utils" ]; then
52+
for ddl_pattern in "v_report_*.ddl" "report_*.ddl" "*.ddl" "*.sql"; do
53+
shopt -s nullglob
54+
for ddl in ./utils/$ddl_pattern; do
55+
[ -f "$ddl" ] && install_ddl "$ddl"
56+
done
57+
shopt -u nullglob
58+
done
59+
fi
60+
61+
echo "Installing other DDLs..."
62+
# Loop over all directories except utils
63+
find . -type d ! -path "./utils" -print0 | while IFS= read -r -d '' dir; do
64+
for ddl_pattern in "v_report_*.ddl" "report_*.ddl"; do
65+
shopt -s nullglob
66+
for ddl in "$dir"/$ddl_pattern; do
67+
[ -f "$ddl" ] && install_ddl "$ddl"
68+
done
69+
shopt -u nullglob
70+
done
71+
done
72+
)
73+
}
74+
75+
76+
77+
function create_report() {
78+
local report_name=$1
79+
local report_pretty_name=$2
80+
local report_type=$3
81+
local source_table_name=$4
82+
local refresh_procedure_name=$5
83+
84+
if [[ "$DROP_EXISTING_REPORT" == "true" ]]; then
85+
86+
curl -v \
87+
-X DELETE \
88+
-u "$KILLBILL_USER:$KILLBILL_PASSWORD" \
89+
-H "X-Killbill-ApiKey:$KILLBILL_API_KEY" \
90+
-H "X-Killbill-ApiSecret:$KILLBILL_API_SECRET" \
91+
"http://127.0.0.1:8080/plugins/killbill-analytics/reports/$report_name"
92+
fi
93+
94+
95+
curl -v \
96+
-X POST \
97+
-u $KILLBILL_USER:$KILLBILL_PASSWORD \
98+
-H "X-Killbill-ApiKey:$KILLBILL_API_KEY" \
99+
-H "X-Killbill-ApiSecret:$KILLBILL_API_SECRET" \
100+
-H 'Content-Type: application/json' \
101+
-d "{\"reportName\": \"$report_name\",
102+
\"reportPrettyName\": \"$report_pretty_name\",
103+
\"reportType\": \"$report_type\",
104+
\"sourceTableName\": \"$source_table_name\",
105+
\"refreshProcedureName\": \"$refresh_procedure_name\",
106+
\"refreshFrequency\": \"HOURLY\"
107+
}" \
108+
$KILLBILL_HTTP_PROTOCOL://$KILLBILL_HOST:$KILLBILL_PORT/plugins/killbill-analytics/reports
109+
}
110+
111+
# Create all Killbill reports
112+
create_all_reports() {
113+
declare -a reports=(
114+
"report_accounts_summary|Account Summary|TABLE|report_accounts_summary|refresh_report_accounts_summary"
115+
"active_by_product_term_monthly|Active Subscriptions|TIMELINE|report_active_by_product_term_monthly|refresh_report_active_by_product_term_monthly"
116+
"report_bundles_summary|Bundles Summary|TABLE|report_bundles_summary|refresh_report_bundles_summary"
117+
"cancellations_count_daily|Cancellations Daily|TIMELINE|report_cancellations_daily|refresh_report_cancellations_daily"
118+
"chargebacks_daily|Chargebacks Daily|TIMELINE|report_chargebacks_daily|refresh_report_chargebacks_daily"
119+
"conversions_daily|Conversions Daily|TIMELINE|report_conversions_daily|refresh_report_conversions_daily"
120+
"invoice_aging|Invoice Aging|TABLE|report_invoice_aging|refresh_report_invoice_aging"
121+
"invoice_aging_no_pmt|Invoice Aging No Payments|TABLE|report_invoice_aging_no_payment|refresh_report_invoice_aging_no_payment"
122+
"invoice_credits_daily|Invoice Credits Daily|TIMELINE|report_invoice_credits_daily|refresh_report_invoice_credits_daily"
123+
"invoice_credits_Monthly|Invoice Credits Monthly|TABLE|report_invoice_credits_monthly|refresh_report_invoice_credits_monthly"
124+
"invoice_item_adjustments_daily|Invoice Item Adjustments Daily|TIMELINE|report_invoice_item_adjustments_daily|refresh_report_invoice_item_adjustments_daily"
125+
"invoice_item_adjustments_monthly|Invoice Item Adjustments Monthly|TABLE|report_invoice_item_adjustments_monthly|refresh_report_invoice_item_adjustments_monthly"
126+
"invoice_items_monthly|Invoice Items Monthly|TABLE|report_invoice_items_monthly|refresh_report_invoice_items_monthly"
127+
"invoices_balance_daily|Invoice Balance|TIMELINE|report_invoices_balance_daily|refresh_report_invoices_balance_daily"
128+
"invoices_daily|Invoices Daily|TIMELINE|report_invoices_daily|refresh_report_invoices_daily"
129+
"invoice_monthly|Invoices Monthly|TABLE|report_invoices_monthly|refresh_report_invoices_monthly"
130+
"mrr_daily|MRR|TIMELINE|report_mrr_daily|refresh_report_mrr_daily"
131+
"new_accounts_daily|New Accounts Daily|TIMELINE|report_new_accounts_daily|refresh_report_new_accounts_daily"
132+
"overdue_states_count_daily|Overdue States Count|TIMELINE|report_overdue_states_count_daily|refresh_report_overdue_states_count_daily"
133+
"payments_monthly|Payments Monthly|TABLE|report_payments_monthly|refresh_report_payments_monthly"
134+
"payments_summary|Payments Summary|TABLE|report_payments_summary|refresh_report_payments_summary"
135+
"payments_total_daily|Payment Total Daily|TIMELINE|report_payments_total_daily|refresh_report_payments_total_daily"
136+
"refunds_monthly|Refunds Monthly|TABLE|report_refunds_summary|refresh_report_refunds_summary"
137+
"refunds_total_daily|Refunds Total Daily|TIMELINE|report_refunds_total_daily|refresh_report_refunds_total_daily"
138+
"subscribers_vs_non_subscribers|Subscribers v/s Non Subscribers|COUNTERS|report_subscribers_vs_non_subscribers|refresh_report_subscribers_vs_non_subscribers"
139+
"trial_starts_count_daily|Trials Start Count|TIMELINE|report_trial_starts_count_daily|refresh_report_trial_starts_count_daily"
140+
"trial_to_no_trial_conversions_daily|Trial to No Trial Conversions Daily|TIMELINE|report_trial_to_no_trial_conversions_daily|refresh_report_trial_to_no_trial_conversions_daily"
141+
)
142+
143+
for r in "${reports[@]}"; do
144+
IFS="|" read -r name pretty type source refresh <<< "$r"
145+
create_report "$name" "$pretty" "$type" "$source" "$refresh"
146+
done
147+
}
148+
149+
# ========================
150+
# Main Execution
151+
# ========================
152+
153+
if [[ "$INSTALL_DDL" == "true" ]]; then
154+
install_all_ddls
155+
else
156+
echo "INSTALL_DDL is not true. Skipping DDL installation."
157+
fi
158+
159+
create_all_reports
160+

src/main/resources/seed_reports.sh

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)