mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
started adding test for shared group and created utility methods for
getting shared balances
This commit is contained in:
@@ -18,6 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
STRATEGY_LOWEST_FIRST = "*lowest_first"
|
||||
STRATEGY_HIGHEST_FIRST = "*highest_first"
|
||||
STRATEGY_RANDOM = "*random"
|
||||
)
|
||||
|
||||
type SharedGroup struct {
|
||||
Id string
|
||||
Strategy string
|
||||
@@ -26,3 +38,46 @@ type SharedGroup struct {
|
||||
Weight float64
|
||||
Members []string
|
||||
}
|
||||
|
||||
func (sg *SharedGroup) GetMembersExceptUser(ubId string) []string {
|
||||
for i, m := range sg.Members {
|
||||
if m == ubId {
|
||||
a := make([]string, len(sg.Members))
|
||||
copy(a, sg.Members)
|
||||
a[i], a = a[len(a)-1], a[:len(a)-1]
|
||||
return a
|
||||
}
|
||||
}
|
||||
return sg.Members
|
||||
}
|
||||
|
||||
func (sg *SharedGroup) PopBalanceByStrategy(balanceChain *BalanceChain) (bal *Balance) {
|
||||
bc := *balanceChain
|
||||
if len(bc) == 0 {
|
||||
return
|
||||
}
|
||||
index := 0
|
||||
switch sg.Strategy {
|
||||
case STRATEGY_RANDOM:
|
||||
rand.Seed(time.Now().Unix())
|
||||
index = rand.Intn(len(bc))
|
||||
case STRATEGY_LOWEST_FIRST:
|
||||
minVal := math.MaxFloat64
|
||||
for i, b := range bc {
|
||||
if b.Value < minVal {
|
||||
minVal = b.Value
|
||||
index = i
|
||||
}
|
||||
}
|
||||
case STRATEGY_HIGHEST_FIRST:
|
||||
maxVal := math.SmallestNonzeroFloat64
|
||||
for i, b := range bc {
|
||||
if b.Value > maxVal {
|
||||
maxVal = b.Value
|
||||
index = i
|
||||
}
|
||||
}
|
||||
}
|
||||
bal, bc[index], *balanceChain = bc[index], bc[len(bc)-1], bc[:len(bc)-1]
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user