mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-19 22:28:45 +05:00
Updated GetCDRs filtering for internalDB
This commit is contained in:
committed by
Dan Christian Bogos
parent
44185aa657
commit
d85bb3633a
@@ -727,6 +727,38 @@ type CDRsFilter struct {
|
||||
Paginator
|
||||
}
|
||||
|
||||
// Prepare will sort all the slices in order to search more faster
|
||||
func (fltr *CDRsFilter) Prepare() {
|
||||
sort.Strings(fltr.CGRIDs)
|
||||
sort.Strings(fltr.NotCGRIDs)
|
||||
sort.Strings(fltr.RunIDs)
|
||||
sort.Strings(fltr.NotRunIDs)
|
||||
sort.Strings(fltr.OriginIDs)
|
||||
sort.Strings(fltr.NotOriginIDs)
|
||||
sort.Strings(fltr.OriginHosts)
|
||||
sort.Strings(fltr.NotOriginHosts)
|
||||
sort.Strings(fltr.Sources)
|
||||
sort.Strings(fltr.NotSources)
|
||||
sort.Strings(fltr.ToRs)
|
||||
sort.Strings(fltr.NotToRs)
|
||||
sort.Strings(fltr.RequestTypes)
|
||||
sort.Strings(fltr.NotRequestTypes)
|
||||
sort.Strings(fltr.Tenants)
|
||||
sort.Strings(fltr.NotTenants)
|
||||
sort.Strings(fltr.Categories)
|
||||
sort.Strings(fltr.NotCategories)
|
||||
sort.Strings(fltr.Accounts)
|
||||
sort.Strings(fltr.NotAccounts)
|
||||
sort.Strings(fltr.Subjects)
|
||||
sort.Strings(fltr.NotSubjects)
|
||||
// sort.Strings(fltr.DestinationPrefixes)
|
||||
// sort.Strings(fltr.NotDestinationPrefixes)
|
||||
|
||||
sort.Sort(sort.Float64Slice(fltr.Costs))
|
||||
sort.Sort(sort.Float64Slice(fltr.NotCosts))
|
||||
|
||||
}
|
||||
|
||||
// RPCCDRsFilter is a filter used in Rpc calls
|
||||
// RPCCDRsFilter is slightly different than CDRsFilter by using string instead of Time filters
|
||||
type RPCCDRsFilter struct {
|
||||
|
||||
@@ -1003,3 +1003,60 @@ func TestInitAttrReloadCache(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
|
||||
}
|
||||
}
|
||||
func TestCDRsFilterPrepare(t *testing.T) {
|
||||
fltr := &CDRsFilter{
|
||||
CGRIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotCGRIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
RunIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotRunIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
OriginIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotOriginIDs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
OriginHosts: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotOriginHosts: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Sources: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotSources: []string{"5", "6", "1", "3", "2", "4"},
|
||||
ToRs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotToRs: []string{"5", "6", "1", "3", "2", "4"},
|
||||
RequestTypes: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotRequestTypes: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Tenants: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotTenants: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Categories: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotCategories: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Accounts: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotAccounts: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Subjects: []string{"5", "6", "1", "3", "2", "4"},
|
||||
NotSubjects: []string{"5", "6", "1", "3", "2", "4"},
|
||||
Costs: []float64{5, 6, 1, 3, 2, 4},
|
||||
NotCosts: []float64{5, 6, 1, 3, 2, 4},
|
||||
}
|
||||
exp := &CDRsFilter{
|
||||
CGRIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotCGRIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
RunIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotRunIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
OriginIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotOriginIDs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
OriginHosts: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotOriginHosts: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Sources: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotSources: []string{"1", "2", "3", "4", "5", "6"},
|
||||
ToRs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotToRs: []string{"1", "2", "3", "4", "5", "6"},
|
||||
RequestTypes: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotRequestTypes: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Tenants: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotTenants: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Categories: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotCategories: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Accounts: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotAccounts: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Subjects: []string{"1", "2", "3", "4", "5", "6"},
|
||||
NotSubjects: []string{"1", "2", "3", "4", "5", "6"},
|
||||
Costs: []float64{1, 2, 3, 4, 5, 6},
|
||||
NotCosts: []float64{1, 2, 3, 4, 5, 6},
|
||||
}
|
||||
if fltr.Prepare(); !reflect.DeepEqual(exp, fltr) {
|
||||
t.Errorf("Expected %s,received %s", ToJSON(exp), ToJSON(fltr))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,19 +26,14 @@ import (
|
||||
// Binary string search in slice
|
||||
func IsSliceMember(ss []string, s string) bool {
|
||||
sort.Strings(ss)
|
||||
if i := sort.SearchStrings(ss, s); i < len(ss) && ss[i] == s {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return SliceHasMember(ss, s)
|
||||
}
|
||||
|
||||
// SliceHasMember searches within a *sorted* slice
|
||||
// useful to search in shared vars (no slice sort)
|
||||
func SliceHasMember(ss []string, s string) bool {
|
||||
if i := sort.SearchStrings(ss, s); i < len(ss) && ss[i] == s {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
i := sort.SearchStrings(ss, s)
|
||||
return i < len(ss) && ss[i] == s
|
||||
}
|
||||
|
||||
func SliceWithoutMember(ss []string, s string) []string {
|
||||
@@ -101,3 +96,20 @@ func SliceStringToIface(slc []string) (ifc []interface{}) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Float64SliceHasMember searches within a *sorted* slice
|
||||
// useful to search in shared vars (no slice sort)
|
||||
func Float64SliceHasMember(ss []float64, s float64) bool {
|
||||
i := sort.SearchFloat64s(ss, s)
|
||||
return i < len(ss) && ss[i] == s
|
||||
}
|
||||
|
||||
// HasPrefixSlice iterates over slice members and returns true if one the element has that prefix
|
||||
func HasPrefixSlice(prfxs []string, el string) bool {
|
||||
for _, prfx := range prfxs {
|
||||
if strings.HasPrefix(el, prfx) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -19,9 +19,69 @@ package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsSliceMember(t *testing.T) {
|
||||
if !IsSliceMember([]string{"1", "2", "3", "4", "5"}, "5") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if IsSliceMember([]string{"1", "2", "3", "4", "5"}, "6") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceHasMember(t *testing.T) {
|
||||
if !SliceHasMember([]string{"1", "2", "3", "4", "5"}, "5") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if SliceHasMember([]string{"1", "2", "3", "4", "5"}, "6") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlaot64SliceHasMember(t *testing.T) {
|
||||
if !Float64SliceHasMember([]float64{1, 2, 3, 4, 5}, 5) {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if Float64SliceHasMember([]float64{1, 2, 3, 4, 5}, 6) {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
func TestSliceWithoutMember(t *testing.T) {
|
||||
rcv := SliceWithoutMember([]string{"1", "2", "3", "4", "5"}, "5")
|
||||
sort.Strings(rcv)
|
||||
eOut := []string{"1", "2", "3", "4"}
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
rcv = SliceWithoutMember([]string{"1", "2", "3", "4", "5"}, "6")
|
||||
sort.Strings(rcv)
|
||||
eOut = []string{"1", "2", "3", "4", "5"}
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceMemberHasPrefix(t *testing.T) {
|
||||
if !SliceMemberHasPrefix([]string{"1", "*2", "3", "4", "5"}, "*") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if SliceMemberHasPrefix([]string{"1", "2", "3", "4", "5"}, "*") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasPrefixSlice(t *testing.T) {
|
||||
if !HasPrefixSlice([]string{"1", "2", "3", "4", "5"}, "123") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if HasPrefixSlice([]string{"1", "2", "3", "4", "5"}, "689") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvg(t *testing.T) {
|
||||
values := []float64{1, 2, 3}
|
||||
result := Avg(values)
|
||||
|
||||
Reference in New Issue
Block a user