moved AcceptCDR method into CdrStats

This commit is contained in:
Radu Ioan Fericean
2014-07-31 15:00:56 +03:00
parent ebc972ed41
commit d29512de02
3 changed files with 103 additions and 98 deletions

View File

@@ -18,7 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import "time"
import (
"strings"
"time"
"github.com/cgrates/cgrates/utils"
)
type CdrStats struct {
Id string // Config id, unique per config instance
@@ -43,3 +48,79 @@ type CdrStats struct {
CostInterval []float64 // 2 or less items, (>=Cost, <Cost)
Triggers ActionTriggerPriotityList
}
func (cs *CdrStats) AcceptCDR(cdr *utils.StoredCdr) bool {
if len(cs.SetupInterval) > 0 {
if cdr.SetupTime.Before(cs.SetupInterval[0]) {
return false
}
if len(cs.SetupInterval) > 1 && (cdr.SetupTime.Equal(cs.SetupInterval[1]) || cdr.SetupTime.After(cs.SetupInterval[1])) {
return false
}
}
if len(cs.TOR) > 0 && !utils.IsSliceMember(cs.TOR, cdr.TOR) {
return false
}
if len(cs.CdrHost) > 0 && !utils.IsSliceMember(cs.CdrHost, cdr.CdrHost) {
return false
}
if len(cs.CdrSource) > 0 && !utils.IsSliceMember(cs.CdrSource, cdr.CdrSource) {
return false
}
if len(cs.ReqType) > 0 && !utils.IsSliceMember(cs.ReqType, cdr.ReqType) {
return false
}
if len(cs.Direction) > 0 && !utils.IsSliceMember(cs.Direction, cdr.Direction) {
return false
}
if len(cs.Tenant) > 0 && !utils.IsSliceMember(cs.Tenant, cdr.Tenant) {
return false
}
if len(cs.Category) > 0 && !utils.IsSliceMember(cs.Category, cdr.Category) {
return false
}
if len(cs.Account) > 0 && !utils.IsSliceMember(cs.Account, cdr.Account) {
return false
}
if len(cs.Subject) > 0 && !utils.IsSliceMember(cs.Subject, cdr.Subject) {
return false
}
if len(cs.DestinationPrefix) > 0 {
found := false
for _, prefix := range cs.DestinationPrefix {
if strings.HasPrefix(cdr.Destination, prefix) {
found = true
break
}
}
if !found {
return false
}
}
if len(cs.UsageInterval) > 0 {
if cdr.Usage < cs.UsageInterval[0] {
return false
}
if len(cs.UsageInterval) > 1 && cdr.Usage >= cs.UsageInterval[1] {
return false
}
}
if len(cs.MediationRunIds) > 0 && !utils.IsSliceMember(cs.MediationRunIds, cdr.MediationRunId) {
return false
}
if len(cs.CostInterval) > 0 {
if cdr.Cost < cs.CostInterval[0] {
return false
}
if len(cs.CostInterval) > 1 && cdr.Cost >= cs.CostInterval[1] {
return false
}
}
if len(cs.RatedAccount) > 0 && !utils.IsSliceMember(cs.RatedAccount, cdr.RatedAccount) {
return false
}
if len(cs.RatedSubject) > 0 && !utils.IsSliceMember(cs.RatedSubject, cdr.RatedSubject) {
return false
}
return true
}

View File

@@ -64,7 +64,7 @@ func (sq *StatsQueue) UpdateConf(conf *CdrStats) {
func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) {
sq.mux.Lock()
defer sq.mux.Unlock()
if sq.acceptCDR(cdr) {
if sq.conf.AcceptCDR(cdr) {
qcdr := sq.simplifyCDR(cdr)
sq.cdrs = append(sq.cdrs, qcdr)
sq.addToMetrics(qcdr)
@@ -153,79 +153,3 @@ func (sq *StatsQueue) getStats() map[string]float64 {
}
return stat
}
func (sq *StatsQueue) acceptCDR(cdr *utils.StoredCdr) bool {
if len(sq.conf.SetupInterval) > 0 {
if cdr.SetupTime.Before(sq.conf.SetupInterval[0]) {
return false
}
if len(sq.conf.SetupInterval) > 1 && (cdr.SetupTime.Equal(sq.conf.SetupInterval[1]) || cdr.SetupTime.After(sq.conf.SetupInterval[1])) {
return false
}
}
if len(sq.conf.TOR) > 0 && !utils.IsSliceMember(sq.conf.TOR, cdr.TOR) {
return false
}
if len(sq.conf.CdrHost) > 0 && !utils.IsSliceMember(sq.conf.CdrHost, cdr.CdrHost) {
return false
}
if len(sq.conf.CdrSource) > 0 && !utils.IsSliceMember(sq.conf.CdrSource, cdr.CdrSource) {
return false
}
if len(sq.conf.ReqType) > 0 && !utils.IsSliceMember(sq.conf.ReqType, cdr.ReqType) {
return false
}
if len(sq.conf.Direction) > 0 && !utils.IsSliceMember(sq.conf.Direction, cdr.Direction) {
return false
}
if len(sq.conf.Tenant) > 0 && !utils.IsSliceMember(sq.conf.Tenant, cdr.Tenant) {
return false
}
if len(sq.conf.Category) > 0 && !utils.IsSliceMember(sq.conf.Category, cdr.Category) {
return false
}
if len(sq.conf.Account) > 0 && !utils.IsSliceMember(sq.conf.Account, cdr.Account) {
return false
}
if len(sq.conf.Subject) > 0 && !utils.IsSliceMember(sq.conf.Subject, cdr.Subject) {
return false
}
if len(sq.conf.DestinationPrefix) > 0 {
found := false
for _, prefix := range sq.conf.DestinationPrefix {
if strings.HasPrefix(cdr.Destination, prefix) {
found = true
break
}
}
if !found {
return false
}
}
if len(sq.conf.UsageInterval) > 0 {
if cdr.Usage < sq.conf.UsageInterval[0] {
return false
}
if len(sq.conf.UsageInterval) > 1 && cdr.Usage >= sq.conf.UsageInterval[1] {
return false
}
}
if len(sq.conf.MediationRunIds) > 0 && !utils.IsSliceMember(sq.conf.MediationRunIds, cdr.MediationRunId) {
return false
}
if len(sq.conf.CostInterval) > 0 {
if cdr.Cost < sq.conf.CostInterval[0] {
return false
}
if len(sq.conf.CostInterval) > 1 && cdr.Cost >= sq.conf.CostInterval[1] {
return false
}
}
if len(sq.conf.RatedAccount) > 0 && !utils.IsSliceMember(sq.conf.RatedAccount, cdr.RatedAccount) {
return false
}
if len(sq.conf.RatedSubject) > 0 && !utils.IsSliceMember(sq.conf.RatedSubject, cdr.RatedSubject) {
return false
}
return true
}

View File

@@ -100,83 +100,83 @@ func TestAcceptCDR(t *testing.T) {
Cost: 10,
}
sq.conf = &CdrStats{}
if sq.acceptCDR(cdr) != true {
if sq.conf.AcceptCDR(cdr) != true {
t.Error("Should have accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{TOR: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{CdrHost: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{CdrSource: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{Direction: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{Tenant: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{Category: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{Account: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{Subject: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{RatedAccount: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{RatedSubject: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{DestinationPrefix: []string{"test"}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{DestinationPrefix: []string{"test", "123"}}
if sq.acceptCDR(cdr) != true {
if sq.conf.AcceptCDR(cdr) != true {
t.Error("Should have accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC)}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC)}}
if sq.acceptCDR(cdr) != true {
if sq.conf.AcceptCDR(cdr) != true {
t.Error("Should have accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}}
if sq.acceptCDR(cdr) != true {
if sq.conf.AcceptCDR(cdr) != true {
t.Error("Should have accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{UsageInterval: []time.Duration{11 * time.Second}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{UsageInterval: []time.Duration{1 * time.Second, 10 * time.Second}}
if sq.acceptCDR(cdr) == true {
if sq.conf.AcceptCDR(cdr) == true {
t.Error("Should have NOT accepted thif CDR: %+v", cdr)
}
sq.conf = &CdrStats{UsageInterval: []time.Duration{10 * time.Second, 11 * time.Second}}
if sq.acceptCDR(cdr) != true {
if sq.conf.AcceptCDR(cdr) != true {
t.Error("Should have accepted thif CDR: %+v", cdr)
}
}