mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-23 16:18:44 +05:00
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
/*
|
|
Rating system designed to be used in VoIP Carriers World
|
|
Copyright (C) 2012-2015 ITsysCOM
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package engine
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// Amount of a trafic of a certain type
|
|
type UnitsCounter struct {
|
|
BalanceType string // *monetary/*voice/*sms/etc
|
|
CounterType string // *event or *balance
|
|
Balances BalanceChain // first balance is the general one (no destination)
|
|
}
|
|
|
|
// clears balances for this counter
|
|
// makes sure there are balances for all action triggers
|
|
func (uc *UnitsCounter) initBalances(ats []*ActionTrigger) {
|
|
uc.Balances = BalanceChain{}
|
|
for _, at := range ats {
|
|
if !strings.Contains(at.ThresholdType, "counter") ||
|
|
at.BalanceType != uc.BalanceType {
|
|
// only get actions for counter type action triggers and with the same type
|
|
continue
|
|
}
|
|
uc.Balances = append(uc.Balances, at.CreateBalance())
|
|
}
|
|
}
|
|
|
|
type UnitCounters []*UnitsCounter
|
|
|
|
func (ucs UnitCounters) addUnits(amount float64, kind string, cc *CallCost, b *Balance) {
|
|
for _, uc := range ucs {
|
|
if uc.BalanceType != kind {
|
|
continue
|
|
}
|
|
if uc.CounterType == "" {
|
|
uc.CounterType = utils.COUNTER_EVENT
|
|
}
|
|
for _, bal := range uc.Balances {
|
|
if uc.CounterType == utils.COUNTER_EVENT && cc != nil && bal.MatchCCFilter(cc) {
|
|
log.Print("MATCHCC")
|
|
bal.AddValue(amount)
|
|
continue
|
|
}
|
|
if uc.CounterType == utils.COUNTER_BALANCE && b != nil && b.MatchFilter(bal) {
|
|
bal.AddValue(amount)
|
|
continue
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|