Skip to content

Commit

Permalink
refactor: Also send along daily_usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jshearer committed Sep 5, 2023
1 parent cabb00d commit 873a0e5
Show file tree
Hide file tree
Showing 2 changed files with 592 additions and 15 deletions.
39 changes: 33 additions & 6 deletions supabase/migrations/26_free_trial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 873a0e5

Please sign in to comment.