~singpolyma/jmp-schemas

4508b807043f634154955377fb0908ebe5f9ef84 — Stephen Paul Weber 4 months ago 446d5d2
Customer with no plan gets highest matching rate

Since we don't know their plan, be safe and pick the highest rate.  This means
we can still get a sense of Transitional users' usage instead of not knowing at all.
A deploy/cdr_with_charge_rate_when_no_plan.sql => deploy/cdr_with_charge_rate_when_no_plan.sql +26 -0
@@ 0,0 1,26 @@
-- Deploy jmp:cdr_with_charge_faster to pg
-- requires: cdr_with_charge_faster

BEGIN;

DROP VIEW cdr_with_charge;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id, customer_id)
		cdr.*,
		rate,
		(CASE WHEN cdr.disposition = 'VOICEMAIL' THEN 0
		ELSE rate * ceiling(billsec / 60.0) END) AS charge
	FROM
		cdr
		LEFT JOIN customer_plans USING (customer_id)
		LEFT JOIN call_rates ON
			cdr.direction = call_rates.direction AND
			(
				customer_plans.plan_name IS NULL OR
				customer_plans.plan_name = call_rates.plan_name
			) AND
			cdr.tel LIKE call_rates.prefix || '%'
	ORDER BY cdr_id, customer_id, prefix DESC, rate DESC;

COMMIT;

A revert/cdr_with_charge_rate_when_no_plan.sql => revert/cdr_with_charge_rate_when_no_plan.sql +22 -0
@@ 0,0 1,22 @@
-- Revert jmp:cdr_with_charge_when_no_plan from pg

BEGIN;

DROP VIEW cdr_with_charge;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id, customer_id)
		cdr.*,
		rate,
		(CASE WHEN cdr.disposition = 'VOICEMAIL' THEN 0
		ELSE rate * ceiling(billsec / 60.0) END) AS charge
	FROM
		cdr
		INNER JOIN customer_plans USING (customer_id)
		LEFT JOIN call_rates ON
			cdr.direction = call_rates.direction AND
			customer_plans.plan_name = call_rates.plan_name AND
			cdr.tel LIKE call_rates.prefix || '%'
	ORDER BY cdr_id, customer_id, prefix DESC;

COMMIT;

M sqitch.plan => sqitch.plan +2 -0
@@ 26,3 26,5 @@ notify_low_balance [transactions balances] 2021-12-22T00:36:03Z Stephen Paul Web

cdr_with_charge_faster [cdr_with_charge] 2022-02-08T17:12:16Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Allow Postgres to push down WHERE over customer_id
@2022039.1 2022-02-08T17:16:11Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Fix for cdr_with_charge performance

cdr_with_charge_rate_when_no_plan [cdr_with_charge_faster] 2022-02-09T01:55:13Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Get a default rate when customer has no plan

A verify/cdr_with_charge_rate_when_no_plan.sql => verify/cdr_with_charge_rate_when_no_plan.sql +32 -0
@@ 0,0 1,32 @@
-- Verify jmp:cdr_with_charge_rate_when_no_plan on pg

BEGIN;

-- Check verify/cdr_with_charge.sql

INSERT INTO call_rates
	(prefix, direction, plan_name, rate)
	VALUES
	('+1', 'outbound', 'testplan', 1);

INSERT INTO call_rates
	(prefix, direction, plan_name, rate)
	VALUES
	('+1', 'outbound', 'testplan2', 2);

INSERT INTO cdr
	(cdr_id, customer_id, start, billsec, direction, disposition, tel)
	VALUES
	('test1', 'test', LOCALTIMESTAMP, 1, 'outbound', 'ANSWERED', '+15551234567');

DO $$
DECLARE
	r numeric;
BEGIN
	SELECT rate INTO r FROM cdr_with_charge WHERE cdr_id='test1';
	IF r IS NULL or r <> 2 THEN
		RAISE EXCEPTION 'Expected default rate, but got: %', r;
	END IF;
END $$;

ROLLBACK;