Changeset 1303


Ignore:
Timestamp:
01/16/07 16:50:25 (6 years ago)
Author:
mglb1
Message:

Rework radius traffic handling to be more efficient

  • Update the customer_state table using only completed session information from the radius_state table
  • Create a new view current_customer_state which will also pull in data from the radius_traffic table if up to the minute information is required.

Some care will need to be put into rollover logic. In particular, when a
rollover occurs, the usage of the currently active session for each user
needs to be inserted as a negative value into the used_mb field of the
customer_state table, so that when the session ends and the usage is recorded
the mb from the previous plan period are not erroneously counted again.

This change has not been made to any of the logic modules yet.

Location:
ccsd/trunk/dbschema
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ccsd/trunk/dbschema/ccs_billing.schema

    r1203 r1303  
    6666 
    6767-- Current state of the customer 
     68-- This table is updated regularly to track how much traffic the customer has 
     69-- used. Only completed sessions are added to the traffic used in this table. 
    6870CREATE TABLE customer_state ( 
    6971    contact_id      integer         NOT NULL REFERENCES contact (contact_id) 
     
    8587                                                ON DELETE RESTRICT 
    8688); 
     89 
     90-- The real current state of this customer  
     91-- This takes into accunt the used_mb's recorded in the above state table, plus 
     92-- the number of megabytes used in the currently active session 
     93CREATE VIEW current_customer_state AS SELECT  
     94    cs.contact_id, cs.plan_start, cs.used_mb,  
     95    cs.used_mb+COALESCE(t.acctinputoctets+t.acctoutputoctets,0)/1024/1024 AS 
     96    current_mb, ras.acctsessionid FROM customer_state cs, contact c LEFT JOIN 
     97    radius_active_sessions ras ON c.username=ras.username LEFT JOIN  
     98    radius_latest_traffic t ON ras.acctsessionid=t.acctsessionid WHERE  
     99    cs.contact_id=c.contact_id; 
    87100 
    88101-- Billing records for the customer 
  • ccsd/trunk/dbschema/radius.schema

    r1261 r1303  
    162162CREATE INDEX radacct_stop_user_idx ON radius_session (acctStopTime, UserName); 
    163163 
     164-- Helper views to return only active or completed sessions 
     165CREATE VIEW radius_active_sessions AS SELECT * FROM radius_session WHERE 
     166    acctstoptime IS NULL; 
     167CREATE VIEW radius_completed_sessions AS SELECT * FROM radius_session WHERE 
     168    acctstoptime IS NOT NULL; 
     169 
    164170-- Create a table that RADIUS will store interim accounting updates into 
    165171-- This table is used to determine how much traffic the user has done 
     
    175181); 
    176182CREATE INDEX radiustraffic_idx ON radius_traffic (AcctSessionId, UserName); 
     183CREATE INDEX radiustraffic_session_idx ON radius_traffic (acctsessionid, 
     184    accttime);  
     185CREATE INDEX radiustraffic_id_idx ON radius_traffic (acctsessionid); 
     186 
     187-- Views to filter the radius_traffic updates down to the latest update for 
     188-- each session 
     189CREATE VIEW radius_latest_updates AS SELECT acctsessionid,  
     190    max(accttime) AS latest FROM radius_traffic GROUP BY acctsessionid; 
     191CREATE VIEW radius_latest_traffic AS select t.acctsessionid, l.latest,  
     192    t.acctinputoctets, t.acctoutputoctets FROM radius_latest_updates l  
     193    LEFT JOIN radius_traffic t ON l.acctsessionid=t.acctsessionid AND 
     194    l.latest=t.accttime ORDER BY latest, acctinputoctets, acctoutputoctets; 
    177195 
    178196-- Create a function that can be called to update the customer state table 
     
    183201    rows integer; 
    184202BEGIN 
    185 UPDATE customer_state SET last_updated=NOW(), used_mb=COALESCE(( 
    186 SELECT (SUM(t1.acctinputoctets+t1.acctoutputoctets) - 
    187     SUM(COALESCE(t2.acctinputoctets+t2.acctoutputoctets,0))) /1024 /1024 
    188 FROM radius_traffic t1 LEFT JOIN radius_traffic t2  
    189 ON t1.acctsessionid = t2.acctsessionid AND t2.accttime=(  
    190     SELECT MAX(t4.accttime) FROM radius_traffic t4  
    191     WHERE t4.acctsessionid=t2.acctsessionid  
    192     AND t4.accttime<customer_state.plan_start), contact c 
    193 WHERE t1.accttime=(  
    194     SELECT MAX(t3.accttime) FROM radius_traffic t3  
    195     WHERE t3.acctsessionid=t1.acctsessionid  
    196     AND t3.accttime>customer_state.plan_start) 
    197 AND t1.username=c.username AND c.contact_id=customer_state.contact_id 
    198 GROUP BY t1.username),0); 
     203-- Update usage for sessions that have completed 
     204    UPDATE customer_state SET last_updated=NOW(), used_mb=used_mb+COALESCE(( 
     205        SELECT SUM(s.acctinputoctets+s.acctoutputoctets)/1024/1024 as mb FROM 
     206        radius_completed_sessions s, contact c WHERE s.username=c.username 
     207        AND c.contact_id=customer_state.contact_id  
     208        AND s.acctstoptime>customer_state.last_updated), 0); 
    199209    GET DIAGNOSTICS rows = ROW_COUNT; 
    200210    RETURN rows; 
Note: See TracChangeset for help on using the changeset viewer.