-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Also send along
daily_usage
- Loading branch information
Showing
2 changed files
with
592 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,9 @@ declare | |
billed_range tstzrange; | ||
free_trial_overlap tstzrange; | ||
|
||
incremental_usage jsonb; | ||
daily_usage jsonb; | ||
|
||
free_trial_credit numeric; | ||
|
||
-- Temporary line items holders for free trial calculations | ||
|
@@ -208,7 +211,7 @@ begin | |
case | ||
when t.free_trial_start is null then 'empty'::tstzrange | ||
-- Inclusive start, exclusive end | ||
else tstzrange(date_trunc('day', t.free_trial_start), date_trunc('day', t.free_trial_start) + '1 month', '[)') | ||
else tstzrange(date_trunc('day', t.free_trial_start), date_trunc('day', t.free_trial_start) + '30 days', '[)') | ||
end | ||
from tenants t | ||
where billed_prefix ^@ t.tenant | ||
|
@@ -246,6 +249,10 @@ begin | |
(report->0->'task_usage_hours')::numeric | ||
from internal.incremental_usage_report('monthly', billed_prefix, billed_range) as report; | ||
|
||
select into incremental_usage | ||
report | ||
from internal.incremental_usage_report('daily', billed_prefix, billed_range) as report; | ||
|
||
-- Does the free trial range overlap the month in question? | ||
if not isempty(free_trial_range) and (free_trial_range && billed_range) then | ||
free_trial_overlap = billed_range * free_trial_range; | ||
|
@@ -255,12 +262,11 @@ begin | |
select into | ||
free_trial_credit coalesce(sum((line_item->>'subtotal_frac')::numeric), 0) | ||
from | ||
jsonb_array_elements( | ||
internal.incremental_usage_report('daily', billed_prefix, free_trial_overlap) | ||
) as line_item; | ||
jsonb_array_elements(incremental_usage) as line_item | ||
where free_trial_overlap @> (line_item->>'ts')::timestamptz; | ||
|
||
line_items = line_items || jsonb_build_object( | ||
'description', 'Free trial credit', | ||
'description', format('Free trial credit (%s to %s)', lower(free_trial_range)::date,upper(free_trial_range)::date), | ||
'count', 1, | ||
'rate', round(free_trial_credit) * -1, | ||
'subtotal', round(free_trial_credit) * -1 | ||
|
@@ -283,14 +289,35 @@ begin | |
select into subtotal_usd_cents sum((l->>'subtotal')::numeric) | ||
from jsonb_array_elements(line_items) l; | ||
|
||
-- Build up a list of days and their usage, with a default of 0 | ||
select into daily_usage json_agg( | ||
case | ||
when usage is null then jsonb_build_object( | ||
'line_items', '[]'::jsonb, | ||
'subtotal', 0, | ||
'processed_data_gb', 0, | ||
'task_usage_hours', 0, | ||
'ts', date_of_month | ||
) | ||
else (usage - 'subtotal_frac') || jsonb_build_object( | ||
'subtotal', round((usage->'subtotal_frac')::numeric) | ||
) | ||
end | ||
) | ||
-- Despite the range being exclusive on the upper bound, upper() still returns the upper bound | ||
-- See this thread https://www.postgresql.org/message-id/[email protected] | ||
from generate_series(lower(billed_range)::date, upper(billed_range)::date - interval '1 day', interval '1 day') as date_of_month | ||
left join jsonb_array_elements(incremental_usage) as usage on (usage->>'ts')::date = date_of_month::date; | ||
|
||
return jsonb_build_object( | ||
'billed_month', billed_month, | ||
'billed_prefix', billed_prefix, | ||
'line_items', line_items, | ||
'processed_data_gb', processed_data_gb, | ||
'recurring_fee', coalesce(recurring_usd_cents, 0), | ||
'subtotal', subtotal_usd_cents, | ||
'task_usage_hours', task_usage_hours | ||
'task_usage_hours', task_usage_hours, | ||
'daily_usage', daily_usage | ||
); | ||
|
||
end | ||
|
Oops, something went wrong.