~singpolyma/jmp-schemas

583a6f7074d52cd90aef975fc28b33d831f4c279 — Stephen Paul Weber 1 year, 6 months ago 19f7d8f
Table for call_rates and view to augment CDRs with that data
A deploy/call_rates.sql => deploy/call_rates.sql +14 -0
@@ 0,0 1,14 @@
-- Deploy jmp:call_rates to pg
-- requires: cdr

BEGIN;

CREATE TABLE call_rates (
	prefix    TEXT           NOT NULL,
	direction call_direction NOT NULL,
	plan_name TEXT           NOT NULL,
	rate      NUMERIC        NOT NULL,
	PRIMARY KEY (prefix, direction, plan_name)
);

COMMIT;

A deploy/cdr_with_charge.sql => deploy/cdr_with_charge.sql +22 -0
@@ 0,0 1,22 @@
-- Deploy jmp:cdr_with_charge to pg
-- requires: cdr
-- requires: call_rates
-- requires: customer_plans

BEGIN;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id)
		cdr.*,
		rate,
		rate * ceiling(billsec / 60.0) 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, prefix DESC;

COMMIT;

A revert/call_rates.sql => revert/call_rates.sql +7 -0
@@ 0,0 1,7 @@
-- Revert jmp:call_rates from pg

BEGIN;

DROP TABLE call_rates;

COMMIT;

A revert/cdr_with_charge.sql => revert/cdr_with_charge.sql +7 -0
@@ 0,0 1,7 @@
-- Revert jmp:cdr_with_charge from pg

BEGIN;

DROP VIEW cdr_with_charge;

COMMIT;

M sqitch.plan => sqitch.plan +2 -0
@@ 15,3 15,5 @@ plan_log_with_range [plan_log customer_plans] 2021-05-03T19:30:36Z Stephen Paul 
invites 2021-05-17T20:31:00Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Table to store invite codes and their state
unused_invites 2021-05-17T20:40:20Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # View for invites not yet used
invite_per_15 [transactions invites] 2021-08-18T17:46:38Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # $15 gives you an invite code
call_rates 2021-11-17T01:29:07Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Table to store per-plan per-direction rates to different prefixes
cdr_with_charge [cdr call_rates customer_plans] 2021-11-17T01:44:14Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # View to get CDR records augmented with rate and charge amount

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

BEGIN;

SELECT prefix, direction, plan_name, rate FROM call_rates;

ROLLBACK;

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

BEGIN;

SELECT rate, charge FROM cdr_with_charge;

ROLLBACK;