RatingSubject in CGrateS

This is a topic I really struggled to understand.

When I was testing data balances I’d find CGrateS OOM killed by the OS when trying to charge for data usage:

{
    "method": "CDRsV2.ProcessExternalCDR",
    "params": [
        {
            "OriginID": "6a336fe5-29f3-11ef-81e4-3fbb47f8d4d0",
            "ToR": "*data",
            "RequestType": "*prepaid",
            "AnswerTime": "2024-06-14 12:11:30",
            "SetupTime": "2024-06-14 12:11:30",
            "Tenant": "cgrates.org",
            "Account": "Mobile_SIM_1c2a7fa5",
            "Subject": "",
            "Destination": "",
            "Usage": "107374182"
        }
    ]
}

So what was going on?

Well CGrateS deducts for each incriment in the usage, and in my case, a usage of 107374182 meant CGrateS was trying to deduct from the balance 107 million times.

But as you can imagine, we don’t actually charge our customers per byte but rather we round up our incriments.

This is where the RatingSubject comes into play. RatingSubject sets what “blocks” of balance get calculated.

For example rather than calculating usage of 107,374,182 bytes, with CGrateS deducting from the balance 107 million times, we could set the value *zero1048576ns which means we’d calculate per Megabyte, so we’re only calculating 102 times 1 megabyte.

Likewise we could set the RatingSubject value to *zero1024ns to get it in Kilobytes.

What’s with the ns suffix? Well CGrateS (actually Go) treats units as duration, and the smallest unit is nanoseconds, but we can ignore the meaning in this context, as just 1 integer unit.

The RatingSubject can be set when defining the Balance, or in the Rates, or in the JSON config as a default value.

Example of setting the default balance rating subject in the JSON config

Leave a Reply

Your email address will not be published. Required fields are marked *