Convergent Rating¶
Rating is convergent in the strict sense: a single Rater class implements both prepaid and postpaid pricing, both paths read from the same REF_Price_plan reference data, and both apply the same per-month free-units allowance. A subscriber is flagged either prepaid or postpaid — the same plan id rates correctly for either.
Reference Data¶
The service catalogue is fully dynamic (managed from the CRM
Product Catalogue): the operator defines as many
services as the business sells.
The canonical service identifier is service.id, which equals the on-wire
svc value carried by every CDR/Charge and equals Utils::SVC_* for the seeded
starter set (1 = Voice, 2 = SMS, 3 = Data, 4 = MMS). Operators add, rename and
retire services freely; everything downstream keys off the live service rows.
A price plan carries per-service unit prices only — free units are not on
the plan. Prices live in the price_plan_service junction (keyed by plan
version) and the proto exposes the currently-active version's prices as a
map<service_id, price>, plus the full version history:
message PricePlanVersion {
int32 id = 1;
int32 plan_id = 2;
uint64 activation_date = 3; // 00:00:00 on the activation day
uint64 retirement_date = 4; // 0 = current; else next version's activation
float monthly_fee = 5;
int32 status = 6; // 1 = currently active, 2 = retired/scheduled
map<int32, float> svc_price = 7; // service_id -> unit price
}
message REF_Price_plan {
int32 id = 1;
int32 operator_id = 2;
string name = 3;
float monthly_fee = 12; // == current version's fee (display)
int32 status = 14; // 1 active, 2 retired
map<int32, float> svc_price = 15; // == current version's unit prices (display)
repeated PricePlanVersion versions = 16; // full dated history
}
Free units come exclusively from the subscriber's assigned bundle
(free-unit package). A bundle carries a map<service_id, units> where -1
means unlimited (every requested unit covered for free) and any non-negative
value is a hard monthly cap:
message Bundle {
int32 id = 1;
string name = 2;
int32 status = 3;
map<int32, int32> svc_units = 4; // service_id -> units (-1 = unlimited)
}
Consumed units are tracked per (subscriber_id, year, month, service) in
used_free_units and surfaced through Subscriber::getUsedFreeUnits().
Rating is resolved by event start time on two dimensions:
- Which plan the subscriber held — from
subscriber_plan_assignment(cutover): a CDR whosetstampprecedes a change keeps the previous plan/bundle; one at or after the change uses the new one. - Which version of that plan was active — from
price_plan_version: the version with the greatestactivation_date <= tstampsupplies the unit prices and monthly fee (REFDATA::instance()->getPP(plan_id, tstamp)).
Both cutovers are at 00:00:00. See
Product Catalogue for the versioning model.
Rater Surface¶
Rater (in usr/include/rater.hpp) provides three rating overloads, each operating on the matching protobuf type:
class Rater {
public:
// Postpaid
int rate(billing::CDR &);
int pre_rate(billing::CDR &);
int post_rate(billing::CDR &);
// Prepaid final charge (TERMINATION_REQUEST or EVENT_REQUEST)
int rate(billing::Charge &);
int pre_rate(billing::Charge &);
int post_rate(billing::Charge &);
// Prepaid intermediate balance reserve (INITIAL/UPDATE_REQUEST)
int rate(billing::Balance_reserve &);
};
The three overloads share the same algorithm but differ in which protobuf field they write back. The flow per rating call is:
- Resolve the effective plan and bundle for the event time via
Subscriber::effectiveAssignment(tstamp, …)(thesubscriber_plan_assignmentrow with the greatesteffective_from <= tstamp), then load the plan version active at the event date throughREFDATA::instance()->getPP(plan_id, tstamp)(the version with the greatestactivation_date <= tstamp) and validate the service throughREFDATA::instance()->getSV(svc). A null result fails the call with-1. - Look up the unit price from the resolved version's
svc_pricemap keyed bycdr.svc()(pp->svc_price().at(svc); absent → 0). No fixed slot switch. - If
app.free_units_functionalityis1, derive the calendar month and year from the event timestamp (CDR usescdr.tstamp(); the balance-reserve path falls back tostd::time(nullptr)). The allowance for the service is read from the assigned bundle (getBundle(bundle_id)->svc_units().at(svc);-1= unlimited). The atomicconsumeFreeUnits()then computes how many of the current event's units are covered; the covered count is written tofree_unitson the record and the subscriber's running total is incremented. - Compute
rated_price = unit_cost * (units - covered_free_units), rounded up to whole eurocents (Utils::ceilCents, two decimals) — every rated charge is expressed in eurocents. TheChargeandCDRpaths additionally subtract any explicit discount (discountfield) clamped at zero to producefinal_price(also cent-rounded). - Stamp the record's
status_idtoEVENT_STATUS_RATED(2). PostpaidCDRs additionally storediscount = unit_cost * free_unitsso the originally-rated cost and the applied discount are both retained on the row for invoicing.
pre_rate and post_rate are extension hooks. pre_rate is currently used in CDRLoader to resolve the subscriber's plan id from MSISDN before the actual rate() call (postpaid case). post_rate is a no-op placeholder kept for volume-based discount logic.
Prepaid Path¶
In PrepaidServer::svc() the same Rater instance is reused for the lifetime of a DIAMETER session. Mid-session updates rate against Balance_reserve; the closing CCR (TERMINATION_REQUEST or EVENT_REQUEST) rates against Charge. Because BalanceReserve::fromCCR() and Charge::fromCCR() produce protobuf objects whose plan id is overwritten with the current subscriber's plan_id after the database lookup, the rater is fed the live plan even if the network includes a stale identifier.
The Charge path also has a set_free_units step that decrements the subscriber's monthly allowance immediately — this is the behaviour that matches a real OCS where free-unit consumption must be observable in the next reservation request.
Postpaid Path¶
In CDRLoader, every record entering the system runs through pre_rate → rate → post_rate in sequence. The rated row, with final_price, discount, free_units, and status_id=EVENT_STATUS_RATED, is then inserted by DB_layer::insert(billing::CDR). At bill-cycle time the billing handler does not re-rate — it aggregates already-rated CDRs by service and month, sums their final_price per service into the svc_cost map of billing::Bill (one bill_service row per service), and inserts the bill. CDRs included in the bill are advanced to EVENT_STATUS_BILLED (4) inside DB_layer::billOneAtomic's per-subscriber transaction, atomically with the bill insert.
If the loader is configured without rating (the rater pointer is null in some test scenarios), CDRs are persisted as-is and rating happens later via the developer-only mass-rating role described below.
Mass Prepaid Rating (Test Mode)¶
prepaid_mass_rating is a developer mode that re-rates queued prepaid charges in bulk. It is only available when modules.prepaid_mass_rating = 1. The activity dispatch in BillingHandler::processActivity routes Utils::PREPAID_RATING activities to DB_layer::ratePrepaidCharges, which iterates pending charge rows and re-runs the rater. This path exists to load-test the rater against synthetic prepaid traffic and is explicitly documented as not for production use.
Currency¶
The platform's primary currency is the Euro. Every monetary value persisted by the rating engine — CDR.final_price, Charge.final_price, Balance_reserve.rated_price, and the running balance.balance — is in EUR, and the in-memory price plans (REF_Price_plan.svc_price, monthly_fee) are likewise EUR-denominated. The default Utils::formatCurrency(float) helper formats these internal amounts as €%.2f for log lines and dev tooling.
Display currency is a separate, per-subscriber concern. The ref_currency reference table holds an ISO 4217 code, a symbol, and a rate_from_eur multiplier for each supported currency; id=1 is the EUR anchor at rate 1.0. A subscriber's currency_id selects the row used to convert and format on-screen amounts at render time (PDF invoices, CRM dashboard, usage page). The conversion is one-way and lossless — the database never sees a non-EUR figure — and adding a new currency is a single INSERT INTO ref_currency with no application-code change.
Free-Unit Storage¶
Used free units are kept in a per-subscriber, per-month, per-service table accessed through Subscriber::getUsedFreeUnits(month, year) and Subscriber::useFreeUnits(month, year, service, units). The first time a subscriber consumes free units in a new month, Subscriber::initUsedFreeUnits(month, year) lazily seeds the row. Both prepaid and postpaid paths read and write through the same accessor — there is no duplicate accounting between online and offline domains.